JAL-2316 GUI updates to Connections tab in Preferences dialog
[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.JvSwingUtils;
24 import jalview.util.MessageManager;
25 import jalview.util.UrlLink;
26
27 import java.awt.Font;
28 import java.awt.GridBagConstraints;
29 import java.awt.GridBagLayout;
30 import java.awt.Insets;
31 import java.awt.Rectangle;
32 import java.awt.event.KeyAdapter;
33 import java.awt.event.KeyEvent;
34
35 import javax.swing.BorderFactory;
36 import javax.swing.JLabel;
37 import javax.swing.JOptionPane;
38 import javax.swing.JPanel;
39 import javax.swing.JTextField;
40 import javax.swing.SwingConstants;
41
42 public class GSequenceLink extends JPanel
43 {
44   public GSequenceLink()
45   {
46     try
47     {
48       jbInit();
49     } catch (Exception ex)
50     {
51       ex.printStackTrace();
52     }
53   }
54
55   private void jbInit() throws Exception
56   {
57     this.setLayout(gridBagLayout1);
58     nameTB.setFont(JvSwingUtils.getLabelFont());
59     nameTB.setBounds(new Rectangle(77, 10, 310, 23));
60     nameTB.addKeyListener(new KeyAdapter()
61     {
62       @Override
63       public void keyTyped(KeyEvent e)
64       {
65         nameTB_keyTyped(e);
66       }
67     });
68     urlTB.setFont(JvSwingUtils.getLabelFont());
69     urlTB.setText("http://");
70     urlTB.setBounds(new Rectangle(78, 40, 309, 23));
71     urlTB.addKeyListener(new KeyAdapter()
72     {
73       @Override
74       public void keyTyped(KeyEvent e)
75       {
76         urlTB_keyTyped(e);
77       }
78     });
79     jLabel1.setFont(JvSwingUtils.getLabelFont());
80     jLabel1.setHorizontalAlignment(SwingConstants.TRAILING);
81     jLabel1.setText(MessageManager.getString("label.link_name"));
82     jLabel1.setBounds(new Rectangle(4, 10, 71, 24));
83     jLabel2.setFont(JvSwingUtils.getLabelFont());
84     jLabel2.setHorizontalAlignment(SwingConstants.TRAILING);
85     jLabel2.setText(MessageManager.getString("label.url"));
86     jLabel2.setBounds(new Rectangle(17, 37, 54, 27));
87     jLabel3.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
88     jLabel3.setText(MessageManager.getString("label.use_sequence_id_1"));
89     jLabel3.setBounds(new Rectangle(21, 72, 351, 15));
90     jLabel4.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
91     jLabel4.setText(MessageManager.getString("label.use_sequence_id_2"));
92     jLabel4.setBounds(new Rectangle(21, 88, 351, 15));
93     jLabel5.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
94     jLabel5.setText(MessageManager.getString("label.use_sequence_id_3"));
95     jLabel5.setBounds(new Rectangle(21, 106, 351, 15));
96
97     String lastLabel = MessageManager.getString("label.use_sequence_id_4");
98     if (lastLabel.length() > 0)
99     {
100       // e.g. Spanish version has longer text
101       jLabel6.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
102       jLabel6.setText(lastLabel);
103       jLabel6.setBounds(new Rectangle(21, 122, 351, 15));
104     }
105
106     jPanel1.setBorder(BorderFactory.createEtchedBorder());
107     jPanel1.setLayout(null);
108     jPanel1.add(jLabel1);
109     jPanel1.add(nameTB);
110     jPanel1.add(urlTB);
111     jPanel1.add(jLabel2);
112     jPanel1.add(jLabel3);
113     jPanel1.add(jLabel4);
114     jPanel1.add(jLabel5);
115
116     int height = 130;
117     if (lastLabel.length() > 0)
118     {
119       jPanel1.add(jLabel6);
120       height = 146;
121     }
122
123     this.add(jPanel1, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
124             GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
125                     5, 4, 6, 5), 390, height));
126   }
127
128   @Override
129   public void setName(String name)
130   {
131     nameTB.setText(name);
132   }
133
134   public void setURL(String url)
135   {
136     urlTB.setText(url);
137   }
138
139   @Override
140   public String getName()
141   {
142     return nameTB.getText();
143   }
144
145   public String getURL()
146   {
147     return urlTB.getText();
148   }
149
150   public boolean checkValid()
151   {
152     UrlLink ul = new UrlLink("foo|" + urlTB.getText().trim());
153     if (ul.isValid() && ul.isDynamic())
154     {
155       return true;
156     }
157
158     JOptionPane.showInternalMessageDialog(jalview.gui.Desktop.desktop,
159             MessageManager.getString("warn.url_must_contain"),
160             MessageManager.getString("label.invalid_url"),
161             JOptionPane.WARNING_MESSAGE);
162     return false;
163   }
164
165   JTextField nameTB = new JTextField();
166
167   JTextField urlTB = new JTextField();
168
169   JLabel jLabel1 = new JLabel();
170
171   JLabel jLabel2 = new JLabel();
172
173   JLabel jLabel3 = new JLabel();
174
175   JLabel jLabel4 = new JLabel();
176
177   JLabel jLabel5 = new JLabel();
178
179   JLabel jLabel6 = new JLabel();
180
181   JPanel jPanel1 = new JPanel();
182
183   GridBagLayout gridBagLayout1 = new GridBagLayout();
184
185   public void nameTB_keyTyped(KeyEvent e)
186   {
187     if (e.getKeyChar() == '|')
188     {
189       e.consume();
190     }
191   }
192
193   public void urlTB_keyTyped(KeyEvent e)
194   {
195     // URLLink object validation takes care of incorrect regexes.
196     // if (e.getKeyChar() == '|' || e.getKeyChar() == ' ')
197     // {
198     // e.consume();
199     // }
200
201   }
202 }