allow '|' to be entered in URL text box and added regex extension form to instructions
[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$ or $SEQUENCE_ID=/<regex>/=$");
75     jLabel3.setBounds(new Rectangle(21, 72, 351, 15));
76     jLabel4.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
77     jLabel4.setText("\nto embed sequence id in URL");
78     jLabel4.setBounds(new Rectangle(21, 93, 351, 15));
79     jPanel1.setBorder(BorderFactory.createEtchedBorder());
80     jPanel1.setLayout(null);
81     jPanel1.add(jLabel1);
82     jPanel1.add(nameTB);
83     jPanel1.add(urlTB);
84     jPanel1.add(jLabel2);
85     jPanel1.add(jLabel3);
86     jPanel1.add(jLabel4);
87     this.add(jPanel1, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0
88                                              , GridBagConstraints.CENTER,
89                                              GridBagConstraints.BOTH,
90                                              new Insets(5, 4, 6, 5), 390, 130));
91   }
92
93   public void setName(String name)
94   {
95     nameTB.setText(name);
96   }
97
98   public void setURL(String url)
99   {
100     urlTB.setText(url);
101   }
102
103   public String getName()
104   {
105     return nameTB.getText();
106   }
107
108   public String getURL()
109   {
110     return urlTB.getText();
111   }
112
113   public boolean checkValid()
114   {
115     UrlLink ul = new UrlLink("foo|"+urlTB.getText().trim());
116     if (ul.isValid()&&ul.isDynamic())
117     {
118       return true;
119     }
120     
121     JOptionPane.showInternalMessageDialog(jalview.gui.Desktop.desktop,
122             "Sequence URL must contain $SEQUENCE_ID$ or a regex $SEQUENCE_ID=/<regex>/=$",
123             "URL not valid",
124             JOptionPane.WARNING_MESSAGE);
125     return false;
126   }
127
128   JTextField nameTB = new JTextField();
129   JTextField urlTB = new JTextField();
130   JLabel jLabel1 = new JLabel();
131   JLabel jLabel2 = new JLabel();
132   JLabel jLabel3 = new JLabel();
133   JLabel jLabel4 = new JLabel();
134   JPanel jPanel1 = new JPanel();
135   GridBagLayout gridBagLayout1 = new GridBagLayout();
136   public void nameTB_keyTyped(KeyEvent e)
137   {
138     if (e.getKeyChar() == '|')
139     {
140       e.consume();
141     }
142   }
143
144   public void urlTB_keyTyped(KeyEvent e)
145   {
146     // URLLink object validation takes care of incorrect regexes.
147     //if (e.getKeyChar() == '|' || e.getKeyChar() == ' ')
148     //{
149     //  e.consume();
150    // }
151
152   }
153 }