JAL-2664 Updates following review
[jalview.git] / src / jalview / jbgui / GSequenceLink.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.jbgui;
22
23 import jalview.gui.JvOptionPane;
24 import jalview.gui.JvSwingUtils;
25 import jalview.util.MessageManager;
26 import jalview.util.UrlLink;
27
28 import java.awt.Font;
29 import java.awt.GridBagConstraints;
30 import java.awt.GridBagLayout;
31 import java.awt.Insets;
32 import java.awt.Rectangle;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.awt.event.KeyAdapter;
36 import java.awt.event.KeyEvent;
37
38 import javax.swing.BorderFactory;
39 import javax.swing.JButton;
40 import javax.swing.JLabel;
41 import javax.swing.JPanel;
42 import javax.swing.JTextField;
43 import javax.swing.SwingConstants;
44
45 public class GSequenceLink extends JPanel
46 {
47
48   JTextField nameTB = new JTextField();
49
50   JTextField urlTB = new JTextField();
51
52   JButton insertSeq = new JButton();
53
54   JButton insertDBAcc = new JButton();
55
56   JLabel insert = new JLabel();
57
58   JLabel jLabel1 = new JLabel();
59
60   JLabel jLabel2 = new JLabel();
61
62   JLabel jLabel3 = new JLabel();
63
64   JLabel jLabel4 = new JLabel();
65
66   JLabel jLabel5 = new JLabel();
67
68   JLabel jLabel6 = new JLabel();
69
70   JPanel jPanel1 = new JPanel();
71
72   GridBagLayout gridBagLayout1 = new GridBagLayout();
73
74   public GSequenceLink()
75   {
76     try
77     {
78       jbInit();
79     } catch (Exception ex)
80     {
81       ex.printStackTrace();
82     }
83   }
84
85   private void jbInit() throws Exception
86   {
87     this.setLayout(gridBagLayout1);
88     nameTB.setFont(JvSwingUtils.getLabelFont());
89     nameTB.setBounds(new Rectangle(77, 10, 310, 23));
90     nameTB.addKeyListener(new KeyAdapter()
91     {
92       @Override
93       public void keyTyped(KeyEvent e)
94       {
95         nameTB_keyTyped(e);
96       }
97     });
98     urlTB.setFont(JvSwingUtils.getLabelFont());
99     urlTB.setText("http://");
100     urlTB.setBounds(new Rectangle(78, 40, 309, 23));
101     urlTB.addKeyListener(new KeyAdapter()
102     {
103       @Override
104       public void keyTyped(KeyEvent e)
105       {
106         urlTB_keyTyped(e);
107       }
108     });
109
110     insertSeq.setLocation(77, 75);
111     insertSeq.setSize(141, 24);
112     insertSeq.setText(MessageManager.getString("action.seq_id"));
113     insertSeq.addActionListener(new ActionListener()
114     {
115       @Override
116       public void actionPerformed(ActionEvent e)
117       {
118         insertSeq_action(e);
119       }
120     });
121
122     insertDBAcc.setLocation(210, 75);
123     insertDBAcc.setSize(141, 24);
124     insertDBAcc.setText(MessageManager.getString("action.db_acc"));
125     insertDBAcc.addActionListener(new ActionListener()
126     {
127       @Override
128       public void actionPerformed(ActionEvent e)
129       {
130         insertDBAcc_action(e);
131       }
132     });
133
134     insert.setText(MessageManager.getString("label.insert"));
135     insert.setFont(JvSwingUtils.getLabelFont());
136     insert.setHorizontalAlignment(SwingConstants.RIGHT);
137     insert.setBounds(17, 78, 58, 16);
138
139     jLabel1.setFont(JvSwingUtils.getLabelFont());
140     jLabel1.setHorizontalAlignment(SwingConstants.TRAILING);
141     jLabel1.setText(MessageManager.getString("label.link_name"));
142     jLabel1.setBounds(new Rectangle(4, 10, 71, 24));
143     jLabel2.setFont(JvSwingUtils.getLabelFont());
144     jLabel2.setHorizontalAlignment(SwingConstants.TRAILING);
145     jLabel2.setText(MessageManager.getString("label.url:"));
146     jLabel2.setBounds(new Rectangle(17, 37, 54, 27));
147     jLabel3.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
148     jLabel3.setText(MessageManager.getString("label.use_sequence_id_1"));
149     jLabel3.setBounds(new Rectangle(21, 102, 351, 15));
150     jLabel4.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
151     jLabel4.setText(MessageManager.getString("label.use_sequence_id_2"));
152     jLabel4.setBounds(new Rectangle(21, 118, 351, 15));
153     jLabel5.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
154     jLabel5.setText(MessageManager.getString("label.use_sequence_id_3"));
155     jLabel5.setBounds(new Rectangle(21, 136, 351, 15));
156
157     String lastLabel = MessageManager.getString("label.use_sequence_id_4");
158     if (lastLabel.length() > 0)
159     {
160       // e.g. Spanish version has longer text
161       jLabel6.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
162       jLabel6.setText(lastLabel);
163       jLabel6.setBounds(new Rectangle(21, 152, 351, 15));
164     }
165
166     jPanel1.setBorder(BorderFactory.createEtchedBorder());
167     jPanel1.setLayout(null);
168     jPanel1.add(jLabel1);
169     jPanel1.add(nameTB);
170     jPanel1.add(urlTB);
171     jPanel1.add(insertSeq);
172     jPanel1.add(insertDBAcc);
173     jPanel1.add(insert);
174     jPanel1.add(jLabel2);
175     jPanel1.add(jLabel3);
176     jPanel1.add(jLabel4);
177     jPanel1.add(jLabel5);
178
179     int height = 160;
180     if (lastLabel.length() > 0)
181     {
182       jPanel1.add(jLabel6);
183       height = 176;
184     }
185
186     this.add(jPanel1, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
187             GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
188                     5, 4, 6, 5), 390, height));
189   }
190
191   @Override
192   public void setName(String name)
193   {
194     nameTB.setText(name);
195   }
196
197   public void setURL(String url)
198   {
199     urlTB.setText(url);
200   }
201
202   @Override
203   public String getName()
204   {
205     return nameTB.getText();
206   }
207
208   public String getURL()
209   {
210     return urlTB.getText();
211   }
212
213   public boolean checkValid()
214   {
215     UrlLink ul = new UrlLink("foo|" + urlTB.getText().trim());
216     if (ul.isValid() && ul.isDynamic())
217     {
218       return true;
219     }
220
221     JvOptionPane.showInternalMessageDialog(jalview.gui.Desktop.desktop,
222             MessageManager.getString("warn.url_must_contain"),
223             MessageManager.getString("label.invalid_url"),
224             JvOptionPane.WARNING_MESSAGE);
225     return false;
226   }
227
228   public void notifyDuplicate()
229   {
230     JvOptionPane.showInternalMessageDialog(jalview.gui.Desktop.desktop,
231             MessageManager.getString("warn.name_cannot_be_duplicate"),
232             MessageManager.getString("label.invalid_name"),
233             JvOptionPane.WARNING_MESSAGE);
234   }
235
236   public void nameTB_keyTyped(KeyEvent e)
237   {
238     if (e.getKeyChar() == '|')
239     {
240       e.consume();
241     }
242   }
243
244   public void urlTB_keyTyped(KeyEvent e)
245   {
246     // URLLink object validation takes care of incorrect regexes.
247     // if (e.getKeyChar() == '|' || e.getKeyChar() == ' ')
248     // {
249     // e.consume();
250     // }
251
252   }
253
254   public void insertSeq_action(ActionEvent e)
255   {
256     insertIntoUrl(insertSeq.getText());
257   }
258
259   public void insertDBAcc_action(ActionEvent e)
260   {
261     insertIntoUrl(insertDBAcc.getText());
262   }
263
264   private void insertIntoUrl(String insertion)
265   {
266     int pos = urlTB.getCaretPosition();
267     String text = urlTB.getText();
268     String newText = text.substring(0, pos) + insertion
269             + text.substring(pos);
270     urlTB.setText(newText);
271   }
272 }