source: trunk/gli/src/org/greenstone/gatherer/gui/ArgumentControl.java@ 10396

Last change on this file since 10396 was 9695, checked in by kjdon, 19 years ago

added the Argument.REGEXP case (same as STRING case) to the control generation - so now regexp options get a text box too :-)

  • Property svn:keywords set to Author Date Id Revision
File size: 10.5 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37package org.greenstone.gatherer.gui;
38
39import java.awt.*;
40import java.awt.event.*;
41import java.util.*;
42import javax.swing.*;
43import javax.swing.event.*;
44import javax.swing.text.*;
45
46import org.greenstone.gatherer.Configuration;
47import org.greenstone.gatherer.cdm.Argument;
48import org.greenstone.gatherer.util.Utility;
49import org.greenstone.gatherer.metadata.MetadataSetManager;
50
51public class ArgumentControl
52 extends JPanel {
53 static protected Dimension LABEL_SIZE = new Dimension(180, 25);
54 static protected Dimension ROW_SIZE = new Dimension(610, 30);
55 static protected Dimension SPINNER_SIZE = new Dimension(100, 25);
56 static protected String DESCRIPTION_SEP = " + ";
57
58 protected Argument argument;
59 protected JComponent value_control;
60 protected JCheckBox enabled;
61
62 public ArgumentControl(Argument argument, boolean enable, String value) {
63 super();
64 this.argument = argument;
65 String tooltip = Utility.formatHTMLWidth("<html>" + argument.getDescription() + "</html>", 60);
66 setBackground(Configuration.getColor("coloring.collection_tree_background", false));
67 setBorder(BorderFactory.createEmptyBorder(2,0,2,0));
68 setLayout(new BorderLayout());
69 setPreferredSize(ROW_SIZE);
70 setMaximumSize(ROW_SIZE);
71
72 // Try to determine what the value_controls value should be.
73 if(value == null) {
74 value = argument.getDefaultValue();
75 }
76 // Create a correct value control based on the argument provided.
77 switch(argument.getType()) {
78 case Argument.ENUM:
79 case Argument.METADATUM:
80 JComboBox combobox = new JComboBox();
81 combobox.setEnabled(enable);
82 combobox.setToolTipText(tooltip);
83 combobox.setBackground(enable ? Color.white : Color.lightGray);
84
85 // Build an option model, wrapping each entry of the list table
86 if (argument.getType() == Argument.ENUM) {
87 HashMap arg_list = argument.getOptions();
88 Iterator it = arg_list.keySet().iterator();
89 ArrayList options = new ArrayList();
90 while (it.hasNext()) {
91 options.add((String) it.next());
92 }
93 Collections.sort(options);
94 for (int i=0; i<options.size(); i++) {
95 combobox.addItem(options.get(i));
96 }
97 // Connect this so if a value is selected the tooltip updates accordingly
98 combobox.addActionListener(new ToolTipUpdater(arg_list));
99 }
100 if (argument.getType() == Argument.METADATUM) {
101 ArrayList every_metadata_set_element = MetadataSetManager.getEveryMetadataSetElement();
102 for (int i = 0; i < every_metadata_set_element.size(); i++) {
103 combobox.addItem(every_metadata_set_element.get(i));
104 }
105 }
106
107 if (value != null) {
108 // Set the selected string
109 for (int i = 0; i < combobox.getItemCount(); i++) {
110 if (combobox.getItemAt(i).toString().equals(value)) {
111 combobox.setSelectedIndex(i);
112 }
113 }
114 }
115 // Layout
116 add(combobox, BorderLayout.CENTER);
117 // And remember
118 value_control = combobox;
119 break;
120 case Argument.FLAG:
121 // Only need the check box.
122 value_control = null;
123 break;
124 case Argument.INTEGER:
125 // Build a spinner
126 int initial_value=0;
127 if (value != null) {
128 try {
129 initial_value = Integer.parseInt(value);
130 } catch (Exception exception) {
131 }
132 }
133 if (initial_value < argument.getMinimum()) {
134 initial_value = argument.getMinimum();
135 } else if (initial_value > argument.getMaximum()) {
136 initial_value = argument.getMaximum();
137 }
138 JSpinner spinner = new JSpinner(new SpinnerNumberModel(initial_value, argument.getMinimum(), argument.getMaximum(), 1));
139 spinner.setEnabled(enable);
140 spinner.setPreferredSize(SPINNER_SIZE);
141 spinner.setToolTipText(tooltip);
142 // Set enabled
143 JComponent c = spinner.getEditor();
144 if ( c instanceof JSpinner.DefaultEditor ) {
145 JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c;
146 JFormattedTextField field = editor.getTextField();
147 field.setEditable(enable);
148 if(enable) {
149 field.setBackground(Color.white);
150 }
151 else {
152 field.setBackground(Color.lightGray);
153 }
154 }
155 // Layout
156 add(new JLabel(), BorderLayout.CENTER);
157 add(spinner, BorderLayout.EAST);
158 // And remember it
159 value_control = spinner;
160 break;
161 case Argument.REGEXP:
162 case Argument.STRING:
163 // Use a standard text field
164 JTextField textfield = new JTextField();
165 textfield.setEnabled(enable);
166 textfield.setToolTipText(tooltip);
167 // Set enabled
168 if(enable) {
169 textfield.setBackground(Color.white);
170 }
171 else {
172 textfield.setBackground(Color.lightGray);
173 }
174 // If there was an original value, set it.
175 if(value != null) {
176 textfield.setText(value);
177 }
178 // Layout
179 add(textfield, BorderLayout.CENTER);
180 // And remember it
181 value_control = textfield;
182 break;
183 }
184
185 // If the argument is required, then you don't get a choice of whether it is enabled.
186 if(argument.isRequired()) {
187 JLabel label = new JLabel(argument.getName());
188 label.setOpaque(false);
189 label.setPreferredSize(LABEL_SIZE);
190 label.setToolTipText(tooltip);
191 add(label, BorderLayout.WEST);
192 }
193 else {
194 enabled = new JCheckBox(argument.getName(), enable);
195 enabled.setOpaque(false);
196 enabled.setPreferredSize(LABEL_SIZE);
197 enabled.setToolTipText(tooltip);
198 // Connect
199 enabled.addActionListener(new EnabledListener(value_control));
200 // Layout
201 add(enabled, BorderLayout.WEST);
202 }
203 }
204
205 public String getName() {
206 return argument.getName();
207 }
208
209 public String getValue() {
210 String value = null;
211 if(value_control == null) {
212 // Flag value, nothing to do.
213 }
214 else if(value_control instanceof JTextField) {
215 value = ((JTextField)value_control).getText();
216 }
217 else if(value_control instanceof JSpinner) {
218 value = ((JSpinner)value_control).getValue().toString();
219 }
220 else if(value_control instanceof JComboBox) {
221 Object selected_item = ((JComboBox)value_control).getSelectedItem();
222 if (selected_item != null) {
223 value = selected_item.toString();
224 }
225 }
226 return value;
227 }
228 /** Retrieve the control used for storing values.
229 * @return JComponent
230 */
231 public JComponent getValueControl() {
232 return value_control;
233 }
234
235 public boolean isEnabled() {
236 if (enabled == null) {
237 return true; // always enabled
238 }
239 return enabled.isSelected();
240 }
241
242 /** Method to ensure that a certain value is selected, if it exists within that combobox to begin with.
243 * @param combobox the JComboBox whose selection we are trying to preset
244 * @param target The desired value of the selection as a String
245 * @return true if the item was found and selected, false otherwise
246 */
247 public boolean selectValue(JComboBox combobox, String target) {
248 for(int i = 0; i < combobox.getItemCount(); i++) {
249 if(combobox.getItemAt(i).toString().equals(target)) {
250 combobox.setSelectedIndex(i);
251 return true;
252 }
253 }
254 return false;
255 }
256
257 /** Listens for actions apon the enable checkbox, and if detected enables or diables control appropriately. */
258 private class EnabledListener
259 implements ActionListener {
260 /** An editor component, such as a JComboBox or JTextField, that might have its enabled state changed by this listener. */
261 private JComponent target = null;
262 /** Constructor. */
263 public EnabledListener(JComponent target) {
264 this.target = target;
265 }
266 /** Any implementation of ActionListener must include this method so that we can be informed when an action has been performed on or registered check box, prompting us to change the state of the other controls as per the users request.
267 * @param event An <strong>ActionEvent</strong> containing information about the click.
268 */
269 public void actionPerformed(ActionEvent event) {
270 JCheckBox source = (JCheckBox)event.getSource();
271 if(target != null) {
272 if(source.isSelected()) {
273 target.setBackground(Color.white);
274 target.setEnabled(true);
275 }
276 else {
277 target.setBackground(Color.lightGray);
278 target.setEnabled(false);
279 }
280 // Special case of stupid JSpinners who don't let their backgrounds change properly.
281 if(target instanceof JSpinner) {
282 JSpinner spinner = (JSpinner) target;
283 JComponent c = spinner.getEditor();
284 if ( c instanceof JSpinner.DefaultEditor ) {
285 JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c;
286 JFormattedTextField field = editor.getTextField();
287 field.setEditable(source.isSelected());
288 if(source.isSelected()) {
289 field.setBackground(Color.white);
290 }
291 else {
292 field.setBackground(Color.lightGray);
293 }
294 }
295 }
296 }
297 }
298 }
299
300 /** Listener that sets the tooltip associated to a combobox to the tooltip relevant to the selected item. */
301 private class ToolTipUpdater
302 implements ActionListener {
303 private HashMap arg_list;
304 public ToolTipUpdater(HashMap arg_list) {
305 this.arg_list = arg_list;
306 }
307 /** Any implementation of an ActionListener must include this method so that we can be informed when the selection in a combobox has changed and update the tooltip accordingly.
308 * @param event An <strong>ActionEvent</strong> containing information about the action that fired this call.
309 */
310 public void actionPerformed(ActionEvent event) {
311 JComboBox source = (JComboBox)event.getSource();
312 String key = (String) source.getSelectedItem();
313 if(arg_list != null) {
314 String description = (String) arg_list.get(key);
315 if(description != null) {
316 description = Utility.formatHTMLWidth(description, 60);
317 source.setToolTipText(description);
318 }
319 }
320 }
321 }
322}
Note: See TracBrowser for help on using the repository browser.