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

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

Chi had a NPE at this line (220), so I chnaged it to check whether the selected item form the combobox was not null before converting to a string. don't know if this was the problem cos we can't reproduce it

  • 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.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 Object selected_item = ((JComboBox)value_control).getSelectedItem();
221 if (selected_item != null) {
222 value = selected_item.toString();
223 }
224 }
225 return value;
226 }
227 /** Retrieve the control used for storing values.
228 * @return JComponent
229 */
230 public JComponent getValueControl() {
231 return value_control;
232 }
233
234 public boolean isEnabled() {
235 if (enabled == null) {
236 return true; // always enabled
237 }
238 return enabled.isSelected();
239 }
240
241 /** Method to ensure that a certain value is selected, if it exists within that combobox to begin with.
242 * @param combobox the JComboBox whose selection we are trying to preset
243 * @param target The desired value of the selection as a String
244 * @return true if the item was found and selected, false otherwise
245 */
246 public boolean selectValue(JComboBox combobox, String target) {
247 for(int i = 0; i < combobox.getItemCount(); i++) {
248 if(combobox.getItemAt(i).toString().equals(target)) {
249 combobox.setSelectedIndex(i);
250 return true;
251 }
252 }
253 return false;
254 }
255
256 /** Listens for actions apon the enable checkbox, and if detected enables or diables control appropriately. */
257 private class EnabledListener
258 implements ActionListener {
259 /** An editor component, such as a JComboBox or JTextField, that might have its enabled state changed by this listener. */
260 private JComponent target = null;
261 /** Constructor. */
262 public EnabledListener(JComponent target) {
263 this.target = target;
264 }
265 /** 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.
266 * @param event An <strong>ActionEvent</strong> containing information about the click.
267 */
268 public void actionPerformed(ActionEvent event) {
269 JCheckBox source = (JCheckBox)event.getSource();
270 if(target != null) {
271 if(source.isSelected()) {
272 target.setBackground(Color.white);
273 target.setEnabled(true);
274 }
275 else {
276 target.setBackground(Color.lightGray);
277 target.setEnabled(false);
278 }
279 // Special case of stupid JSpinners who don't let their backgrounds change properly.
280 if(target instanceof JSpinner) {
281 JSpinner spinner = (JSpinner) target;
282 JComponent c = spinner.getEditor();
283 if ( c instanceof JSpinner.DefaultEditor ) {
284 JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c;
285 JFormattedTextField field = editor.getTextField();
286 field.setEditable(source.isSelected());
287 if(source.isSelected()) {
288 field.setBackground(Color.white);
289 }
290 else {
291 field.setBackground(Color.lightGray);
292 }
293 }
294 }
295 }
296 }
297 }
298
299 /** Listener that sets the tooltip associated to a combobox to the tooltip relevant to the selected item. */
300 private class ToolTipUpdater
301 implements ActionListener {
302 private HashMap arg_list;
303 public ToolTipUpdater(HashMap arg_list) {
304 this.arg_list = arg_list;
305 }
306 /** 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.
307 * @param event An <strong>ActionEvent</strong> containing information about the action that fired this call.
308 */
309 public void actionPerformed(ActionEvent event) {
310 JComboBox source = (JComboBox)event.getSource();
311 String key = (String) source.getSelectedItem();
312 if(arg_list != null) {
313 String description = (String) arg_list.get(key);
314 if(description != null) {
315 description = Utility.formatHTMLWidth(description, 60);
316 source.setToolTipText(description);
317 }
318 }
319 }
320 }
321}
Note: See TracBrowser for help on using the repository browser.