Merge branch 'releases/Release_2_11_3_Branch'
[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,
187             new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
188                     GridBagConstraints.CENTER, GridBagConstraints.BOTH,
189                     new Insets(5, 4, 6, 5), 390, height));
190   }
191
192   @Override
193   public void setName(String name)
194   {
195     nameTB.setText(name);
196   }
197
198   public void setURL(String url)
199   {
200     urlTB.setText(url);
201   }
202
203   @Override
204   public String getName()
205   {
206     return nameTB.getText();
207   }
208
209   public String getURL()
210   {
211     return urlTB.getText();
212   }
213
214   public boolean checkValid()
215   {
216     UrlLink ul = new UrlLink("foo|" + urlTB.getText().trim());
217     if (ul.isValid() && ul.isDynamic())
218     {
219       return true;
220     }
221
222     JvOptionPane.showInternalMessageDialog(jalview.gui.Desktop.desktop,
223             MessageManager.getString("warn.url_must_contain"),
224             MessageManager.getString("label.invalid_url"),
225             JvOptionPane.WARNING_MESSAGE);
226     return false;
227   }
228
229   public void notifyDuplicate()
230   {
231     JvOptionPane.showInternalMessageDialog(jalview.gui.Desktop.desktop,
232             MessageManager.getString("warn.name_cannot_be_duplicate"),
233             MessageManager.getString("label.invalid_name"),
234             JvOptionPane.WARNING_MESSAGE);
235   }
236
237   public void nameTB_keyTyped(KeyEvent e)
238   {
239     if (e.getKeyChar() == '|')
240     {
241       e.consume();
242     }
243   }
244
245   public void urlTB_keyTyped(KeyEvent e)
246   {
247     // URLLink object validation takes care of incorrect regexes.
248     // if (e.getKeyChar() == '|' || e.getKeyChar() == ' ')
249     // {
250     // e.consume();
251     // }
252
253   }
254
255   public void insertSeq_action(ActionEvent e)
256   {
257     insertIntoUrl(insertSeq.getText());
258   }
259
260   public void insertDBAcc_action(ActionEvent e)
261   {
262     insertIntoUrl(insertDBAcc.getText());
263   }
264
265   private void insertIntoUrl(String insertion)
266   {
267     int pos = urlTB.getCaretPosition();
268     String text = urlTB.getText();
269     String newText = text.substring(0, pos) + insertion
270             + text.substring(pos);
271     urlTB.setText(newText);
272   }
273 }