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

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

now sort the options in a drop down box

  • Property svn:keywords set to Author Date Id Revision
File size: 10.4 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.STRING:
162 // Use a standard text field
163 JTextField textfield = new JTextField();
164 textfield.setEnabled(enable);
165 textfield.setToolTipText(tooltip);
166 // Set enabled
167 if(enable) {
168 textfield.setBackground(Color.white);
169 }
170 else {
171 textfield.setBackground(Color.lightGray);
172 }
173 // If there was an original value, set it.
174 if(value != null) {
175 textfield.setText(value);
176 }
177 // Layout
178 add(textfield, BorderLayout.CENTER);
179 // And remember it
180 value_control = textfield;
181 break;
182 }
183
184 // If the argument is required, then you don't get a choice of whether it is enabled.
185 if(argument.isRequired()) {
186 JLabel label = new JLabel(argument.getName());
187 label.setOpaque(false);
188 label.setPreferredSize(LABEL_SIZE);
189 label.setToolTipText(tooltip);
190 add(label, BorderLayout.WEST);
191 }
192 else {
193 enabled = new JCheckBox(argument.getName(), enable);
194 enabled.setOpaque(false);
195 enabled.setPreferredSize(LABEL_SIZE);
196 enabled.setToolTipText(tooltip);
197 // Connect
198 enabled.addActionListener(new EnabledListener(value_control));
199 // Layout
200 add(enabled, BorderLayout.WEST);
201 }
202 }
203
204 public String getName() {
205 return argument.getName();
206 }
207
208 public String getValue() {
209 String value = null;
210 if(value_control == null) {
211 // Flag value, nothing to do.
212 }
213 else if(value_control instanceof JTextField) {
214 value = ((JTextField)value_control).getText();
215 }
216 else if(value_control instanceof JSpinner) {
217 value = ((JSpinner)value_control).getValue().toString();
218 }
219 else if(value_control instanceof JComboBox) {
220 value = (((JComboBox)value_control).getSelectedItem()).toString();
221 }
222 return value;
223 }
224 /** Retrieve the control used for storing values.
225 * @return JComponent
226 */
227 public JComponent getValueControl() {
228 return value_control;
229 }
230
231 public boolean isEnabled() {
232 if (enabled == null) {
233 return true; // always enabled
234 }
235 return enabled.isSelected();
236 }
237
238 /** Method to ensure that a certain value is selected, if it exists within that combobox to begin with.
239 * @param combobox the JComboBox whose selection we are trying to preset
240 * @param target The desired value of the selection as a String
241 * @return true if the item was found and selected, false otherwise
242 */
243 public boolean selectValue(JComboBox combobox, String target) {
244 for(int i = 0; i < combobox.getItemCount(); i++) {
245 if(combobox.getItemAt(i).toString().equals(target)) {
246 combobox.setSelectedIndex(i);
247 return true;
248 }
249 }
250 return false;
251 }
252
253 /** Listens for actions apon the enable checkbox, and if detected enables or diables control appropriately. */
254 private class EnabledListener
255 implements ActionListener {
256 /** An editor component, such as a JComboBox or JTextField, that might have its enabled state changed by this listener. */
257 private JComponent target = null;
258 /** Constructor. */
259 public EnabledListener(JComponent target) {
260 this.target = target;
261 }
262 /** 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.
263 * @param event An <strong>ActionEvent</strong> containing information about the click.
264 */
265 public void actionPerformed(ActionEvent event) {
266 JCheckBox source = (JCheckBox)event.getSource();
267 if(target != null) {
268 if(source.isSelected()) {
269 target.setBackground(Color.white);
270 target.setEnabled(true);
271 }
272 else {
273 target.setBackground(Color.lightGray);
274 target.setEnabled(false);
275 }
276 // Special case of stupid JSpinners who don't let their backgrounds change properly.
277 if(target instanceof JSpinner) {
278 JSpinner spinner = (JSpinner) target;
279 JComponent c = spinner.getEditor();
280 if ( c instanceof JSpinner.DefaultEditor ) {
281 JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c;
282 JFormattedTextField field = editor.getTextField();
283 field.setEditable(source.isSelected());
284 if(source.isSelected()) {
285 field.setBackground(Color.white);
286 }
287 else {
288 field.setBackground(Color.lightGray);
289 }
290 }
291 }
292 }
293 }
294 }
295
296 /** Listener that sets the tooltip associated to a combobox to the tooltip relevant to the selected item. */
297 private class ToolTipUpdater
298 implements ActionListener {
299 private HashMap arg_list;
300 public ToolTipUpdater(HashMap arg_list) {
301 this.arg_list = arg_list;
302 }
303 /** 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.
304 * @param event An <strong>ActionEvent</strong> containing information about the action that fired this call.
305 */
306 public void actionPerformed(ActionEvent event) {
307 JComboBox source = (JComboBox)event.getSource();
308 String key = (String) source.getSelectedItem();
309 if(arg_list != null) {
310 String description = (String) arg_list.get(key);
311 if(description != null) {
312 description = Utility.formatHTMLWidth(description, 60);
313 source.setToolTipText(description);
314 }
315 }
316 }
317 }
318}
Note: See TracBrowser for help on using the repository browser.