1 | /* |
---|
2 | * This program is free software; you can redistribute it and/or modify |
---|
3 | * it under the terms of the GNU General Public License as published by |
---|
4 | * the Free Software Foundation; either version 2 of the License, or |
---|
5 | * (at your option) any later version. |
---|
6 | * |
---|
7 | * This program is distributed in the hope that it will be useful, |
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | * GNU General Public License for more details. |
---|
11 | * |
---|
12 | * You should have received a copy of the GNU General Public License |
---|
13 | * along with this program; if not, write to the Free Software |
---|
14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | * EnvironmentField.java |
---|
19 | * Copyright (C) 2009 University of Waikato, Hamilton, New Zealand |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | package weka.gui.beans; |
---|
24 | |
---|
25 | import java.awt.BorderLayout; |
---|
26 | import java.awt.event.ActionEvent; |
---|
27 | import java.awt.event.ActionListener; |
---|
28 | import java.awt.event.KeyAdapter; |
---|
29 | import java.awt.event.KeyEvent; |
---|
30 | import java.util.Vector; |
---|
31 | |
---|
32 | import javax.swing.BorderFactory; |
---|
33 | import javax.swing.DefaultComboBoxModel; |
---|
34 | import javax.swing.JComboBox; |
---|
35 | import javax.swing.JLabel; |
---|
36 | import javax.swing.JPanel; |
---|
37 | import javax.swing.JTextField; |
---|
38 | import javax.swing.event.CaretEvent; |
---|
39 | import javax.swing.event.CaretListener; |
---|
40 | |
---|
41 | import weka.core.Environment; |
---|
42 | |
---|
43 | /** |
---|
44 | * Widget that displays a label and a combo box for selecting |
---|
45 | * environment variables. The enter arbitrary text, select an |
---|
46 | * environment variable or a combination of both. Any variables |
---|
47 | * are resolved (if possible) and resolved values are displayed |
---|
48 | * in a tip-text. |
---|
49 | * |
---|
50 | * @author Mark Hall (mhall{[at]}pentaho{[dot]}com) |
---|
51 | * @version $Revision: 5563 $ |
---|
52 | */ |
---|
53 | public class EnvironmentField extends JPanel { |
---|
54 | |
---|
55 | /** For serialization */ |
---|
56 | private static final long serialVersionUID = -3125404573324734121L; |
---|
57 | |
---|
58 | /** The label for the widget */ |
---|
59 | protected JLabel m_label; |
---|
60 | |
---|
61 | /** The combo box */ |
---|
62 | protected JComboBox m_combo; |
---|
63 | |
---|
64 | /** The current environment variables */ |
---|
65 | protected Environment m_env; |
---|
66 | |
---|
67 | protected String m_currentContents = ""; |
---|
68 | protected int m_firstCaretPos = 0; |
---|
69 | protected int m_previousCaretPos = 0; |
---|
70 | protected int m_currentCaretPos = 0; |
---|
71 | |
---|
72 | /** |
---|
73 | * Construct an EnvironmentField with no label. |
---|
74 | */ |
---|
75 | public EnvironmentField() { |
---|
76 | this(""); |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * Construct an EnvironmentField with no label. |
---|
81 | * @param env the environment variables to display in |
---|
82 | * the drop-down box |
---|
83 | */ |
---|
84 | public EnvironmentField(Environment env) { |
---|
85 | this(""); |
---|
86 | setEnvironment(env); |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * Constructor. |
---|
91 | * |
---|
92 | * @param label the label to use |
---|
93 | */ |
---|
94 | public EnvironmentField(String label) { |
---|
95 | setLayout(new BorderLayout()); |
---|
96 | m_label = new JLabel(label); |
---|
97 | if (label.length() > 0) { |
---|
98 | m_label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5)); |
---|
99 | } |
---|
100 | add(m_label, BorderLayout.WEST); |
---|
101 | |
---|
102 | m_combo = new JComboBox(); |
---|
103 | m_combo.setEditable(true); |
---|
104 | //m_combo.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); |
---|
105 | |
---|
106 | java.awt.Component theEditor = m_combo.getEditor().getEditorComponent(); |
---|
107 | if (theEditor instanceof JTextField) { |
---|
108 | ((JTextField)m_combo.getEditor().getEditorComponent()).addCaretListener(new CaretListener() { |
---|
109 | |
---|
110 | public void caretUpdate(CaretEvent e) { |
---|
111 | m_firstCaretPos = m_previousCaretPos; |
---|
112 | m_previousCaretPos = m_currentCaretPos; |
---|
113 | m_currentCaretPos = e.getDot(); |
---|
114 | } |
---|
115 | }); |
---|
116 | } |
---|
117 | add(m_combo, BorderLayout.CENTER); |
---|
118 | setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * Constructor. |
---|
123 | * |
---|
124 | * @param label the label to use |
---|
125 | * @param env the environment variables to display in |
---|
126 | * the drop-down box |
---|
127 | */ |
---|
128 | public EnvironmentField(String label, Environment env) { |
---|
129 | this(label); |
---|
130 | setEnvironment(env); |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | * Set the label for this widget. |
---|
135 | * |
---|
136 | * @param label the label to use |
---|
137 | */ |
---|
138 | public void setLabel(String label) { |
---|
139 | m_label.setText(label); |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | * Set the text to display in the editable combo box. |
---|
144 | * |
---|
145 | * @param text the text to display |
---|
146 | */ |
---|
147 | public void setText(String text) { |
---|
148 | m_currentContents = text; |
---|
149 | m_combo.setSelectedItem(m_currentContents); |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | * Return the text from the combo box. |
---|
154 | * |
---|
155 | * @return the text from the combo box |
---|
156 | */ |
---|
157 | public String getText() { |
---|
158 | return (String)m_combo.getSelectedItem(); |
---|
159 | } |
---|
160 | |
---|
161 | private String processSelected(String selected) { |
---|
162 | if (selected.equals(m_currentContents)) { |
---|
163 | // don't do anything if the user has just pressed return |
---|
164 | // without adding anything new |
---|
165 | return selected; |
---|
166 | } |
---|
167 | if (m_firstCaretPos == 0) { |
---|
168 | m_currentContents = selected + m_currentContents; |
---|
169 | } else if (m_firstCaretPos >= m_currentContents.length()){ |
---|
170 | m_currentContents = m_currentContents + selected; |
---|
171 | } else { |
---|
172 | String left = m_currentContents.substring(0, m_firstCaretPos); |
---|
173 | String right = m_currentContents.substring(m_firstCaretPos, m_currentContents.length()); |
---|
174 | |
---|
175 | m_currentContents = left + selected + right; |
---|
176 | } |
---|
177 | |
---|
178 | /* java.awt.Component theEditor = m_combo.getEditor().getEditorComponent(); |
---|
179 | if (theEditor instanceof JTextField) { |
---|
180 | System.err.println("Setting current contents..." + m_currentContents); |
---|
181 | ((JTextField)theEditor).setText(m_currentContents); |
---|
182 | } */ |
---|
183 | m_combo.setSelectedItem(m_currentContents); |
---|
184 | |
---|
185 | return m_currentContents; |
---|
186 | } |
---|
187 | |
---|
188 | /** |
---|
189 | * Set the environment variables to display in the drop |
---|
190 | * down list. |
---|
191 | * |
---|
192 | * @param env the environment variables to display |
---|
193 | */ |
---|
194 | public void setEnvironment(final Environment env) { |
---|
195 | m_env = env; |
---|
196 | Vector<String> varKeys = new Vector<String>(env.getVariableNames()); |
---|
197 | |
---|
198 | DefaultComboBoxModel dm = new DefaultComboBoxModel(varKeys) { |
---|
199 | public Object getSelectedItem() { |
---|
200 | Object item = super.getSelectedItem(); |
---|
201 | if (item instanceof String) { |
---|
202 | if (env.getVariableValue((String)item) != null) { |
---|
203 | String newS = "${" + (String)item + "}"; |
---|
204 | item = newS; |
---|
205 | } |
---|
206 | } |
---|
207 | return item; |
---|
208 | } |
---|
209 | }; |
---|
210 | m_combo.setModel(dm); |
---|
211 | m_combo.setSelectedItem(""); |
---|
212 | m_combo.addActionListener(new ActionListener() { |
---|
213 | public void actionPerformed(ActionEvent e) { |
---|
214 | String selected = (String)m_combo.getSelectedItem(); |
---|
215 | try { |
---|
216 | selected = processSelected(selected); |
---|
217 | |
---|
218 | selected = m_env.substitute(selected); |
---|
219 | } catch (Exception ex) { |
---|
220 | // quietly ignore unresolved variables |
---|
221 | } |
---|
222 | m_combo.setToolTipText(selected); |
---|
223 | } |
---|
224 | }); |
---|
225 | |
---|
226 | m_combo.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() { |
---|
227 | public void keyReleased(KeyEvent e) { |
---|
228 | java.awt.Component theEditor = m_combo.getEditor().getEditorComponent(); |
---|
229 | if (theEditor instanceof JTextField) { |
---|
230 | String selected = ((JTextField)theEditor).getText(); |
---|
231 | m_currentContents = selected; |
---|
232 | if (m_env != null) { |
---|
233 | try { |
---|
234 | selected = m_env.substitute(selected); |
---|
235 | } catch (Exception ex) { |
---|
236 | // quietly ignore unresolved variables |
---|
237 | } |
---|
238 | } |
---|
239 | m_combo.setToolTipText(selected); |
---|
240 | } |
---|
241 | } |
---|
242 | }); |
---|
243 | } |
---|
244 | |
---|
245 | /** |
---|
246 | * Set the enabled status of the combo box. |
---|
247 | * |
---|
248 | * @param enabled true if the combo box is enabled |
---|
249 | */ |
---|
250 | public void setEnabled(boolean enabled) { |
---|
251 | m_combo.setEnabled(enabled); |
---|
252 | } |
---|
253 | |
---|
254 | /** |
---|
255 | * Set the editable status of the combo box. |
---|
256 | * |
---|
257 | * @param editable true if the combo box is editable |
---|
258 | */ |
---|
259 | public void setEditable(boolean editable) { |
---|
260 | m_combo.setEditable(editable); |
---|
261 | } |
---|
262 | |
---|
263 | /** |
---|
264 | * Main method for testing this class |
---|
265 | * |
---|
266 | * @param args command line args (ignored) |
---|
267 | */ |
---|
268 | public static void main(String[] args) { |
---|
269 | try { |
---|
270 | final javax.swing.JFrame jf = |
---|
271 | new javax.swing.JFrame("EnvironmentField"); |
---|
272 | jf.getContentPane().setLayout(new BorderLayout()); |
---|
273 | final EnvironmentField f = new EnvironmentField("A label here"); |
---|
274 | jf.getContentPane().add(f, BorderLayout.CENTER); |
---|
275 | Environment env = Environment.getSystemWide(); |
---|
276 | f.setEnvironment(env); |
---|
277 | jf.addWindowListener(new java.awt.event.WindowAdapter() { |
---|
278 | public void windowClosing(java.awt.event.WindowEvent e) { |
---|
279 | jf.dispose(); |
---|
280 | System.exit(0); |
---|
281 | } |
---|
282 | }); |
---|
283 | jf.pack(); |
---|
284 | jf.setVisible(true); |
---|
285 | } catch (Exception ex) { |
---|
286 | ex.printStackTrace(); |
---|
287 | } |
---|
288 | } |
---|
289 | } |
---|