regular expression based URL link generation
[jalview.git] / src / jalview / jbgui / GSequenceLink.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.jbgui;
20
21 import jalview.util.UrlLink;
22
23 import java.awt.*;
24 import java.awt.event.*;
25 import javax.swing.*;
26
27 public class GSequenceLink
28     extends Panel
29 {
30   public GSequenceLink()
31   {
32     try
33     {
34       jbInit();
35     }
36     catch (Exception ex)
37     {
38       ex.printStackTrace();
39     }
40   }
41
42   private void jbInit()
43       throws Exception
44   {
45     this.setLayout(gridBagLayout1);
46     nameTB.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
47     nameTB.setBounds(new Rectangle(77, 10, 310, 23));
48     nameTB.addKeyListener(new KeyAdapter()
49     {
50       public void keyTyped(KeyEvent e)
51       {
52         nameTB_keyTyped(e);
53       }
54     });
55     urlTB.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
56     urlTB.setText("http://www.");
57     urlTB.setBounds(new Rectangle(78, 40, 309, 23));
58     urlTB.addKeyListener(new KeyAdapter()
59     {
60       public void keyTyped(KeyEvent e)
61       {
62         urlTB_keyTyped(e);
63       }
64     });
65     jLabel1.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
66     jLabel1.setHorizontalAlignment(SwingConstants.TRAILING);
67     jLabel1.setText("Link Name");
68     jLabel1.setBounds(new Rectangle(4, 10, 71, 24));
69     jLabel2.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
70     jLabel2.setHorizontalAlignment(SwingConstants.TRAILING);
71     jLabel2.setText("URL");
72     jLabel2.setBounds(new Rectangle(17, 37, 54, 27));
73     jLabel3.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
74     jLabel3.setText("Use $SEQUENCE_ID$ to specify where sequence id is in URL");
75     jLabel3.setBounds(new Rectangle(21, 72, 351, 15));
76     jPanel1.setBorder(BorderFactory.createEtchedBorder());
77     jPanel1.setLayout(null);
78     jPanel1.add(jLabel1);
79     jPanel1.add(nameTB);
80     jPanel1.add(urlTB);
81     jPanel1.add(jLabel2);
82     jPanel1.add(jLabel3);
83     this.add(jPanel1, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0
84                                              , GridBagConstraints.CENTER,
85                                              GridBagConstraints.BOTH,
86                                              new Insets(5, 4, 6, 5), 390, 100));
87   }
88
89   public void setName(String name)
90   {
91     nameTB.setText(name);
92   }
93
94   public void setURL(String url)
95   {
96     urlTB.setText(url);
97   }
98
99   public String getName()
100   {
101     return nameTB.getText();
102   }
103
104   public String getURL()
105   {
106     return urlTB.getText();
107   }
108
109   public boolean checkValid()
110   {
111     UrlLink ul = new UrlLink("foo|"+urlTB.getText().trim());
112     if (ul.isValid()&&ul.isDynamic())
113     {
114       return true;
115     }
116     
117     JOptionPane.showInternalMessageDialog(jalview.gui.Desktop.desktop,
118             "Sequence URL must contain $SEQUENCE_ID$ or a regex $SEQUENCE_ID=/<regex>/=$",
119             "URL not valid",
120             JOptionPane.WARNING_MESSAGE);
121     return false;
122   }
123
124   JTextField nameTB = new JTextField();
125   JTextField urlTB = new JTextField();
126   JLabel jLabel1 = new JLabel();
127   JLabel jLabel2 = new JLabel();
128   JLabel jLabel3 = new JLabel();
129   JPanel jPanel1 = new JPanel();
130   GridBagLayout gridBagLayout1 = new GridBagLayout();
131   public void nameTB_keyTyped(KeyEvent e)
132   {
133     if (e.getKeyChar() == '|')
134     {
135       e.consume();
136     }
137   }
138
139   public void urlTB_keyTyped(KeyEvent e)
140   {
141     if (e.getKeyChar() == '|' || e.getKeyChar() == ' ')
142     {
143       e.consume();
144     }
145
146   }
147 }