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

Last change on this file since 11365 was 11365, checked in by mdewsnip, 18 years ago

A few minor bug fixes and improvements for the new "exploded metadata set".

  • Property svn:keywords set to Author Date Id Revision
File size: 12.1 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.io.*;
42import java.util.*;
43import javax.swing.*;
44import javax.swing.event.*;
45import javax.swing.text.*;
46
47import org.greenstone.gatherer.Configuration;
48import org.greenstone.gatherer.Gatherer;
49import org.greenstone.gatherer.cdm.Argument;
50import org.greenstone.gatherer.util.StaticStrings;
51import org.greenstone.gatherer.util.Utility;
52import org.greenstone.gatherer.metadata.MetadataSet;
53import org.greenstone.gatherer.metadata.MetadataSetManager;
54
55public class ArgumentControl
56 extends JPanel {
57 static protected Dimension LABEL_SIZE = new Dimension(180, 25);
58 static protected Dimension ROW_SIZE = new Dimension(610, 30);
59 static protected Dimension SPINNER_SIZE = new Dimension(100, 25);
60 static protected String DESCRIPTION_SEP = " + ";
61
62 protected Argument argument;
63 protected JComponent value_control;
64 protected JCheckBox enabled;
65
66 public ArgumentControl(Argument argument, boolean enable, String value) {
67 super();
68 this.argument = argument;
69 String tooltip = Utility.formatHTMLWidth("<html>" + argument.getDescription() + "</html>", 60);
70 setBackground(Configuration.getColor("coloring.collection_tree_background", false));
71 setBorder(BorderFactory.createEmptyBorder(2,0,2,0));
72 setLayout(new BorderLayout());
73 setPreferredSize(ROW_SIZE);
74 setMaximumSize(ROW_SIZE);
75
76 // Try to determine what the value_controls value should be.
77 if(value == null) {
78 value = argument.getDefaultValue();
79 }
80 // Create a correct value control based on the argument provided.
81 switch(argument.getType()) {
82 case Argument.ENUM:
83 case Argument.METADATUM:
84 JComboBox combobox = new JComboBox();
85 combobox.setEnabled(enable);
86 combobox.setToolTipText(tooltip);
87 combobox.setBackground(enable ? Color.white : Color.lightGray);
88
89 // Build an option model, wrapping each entry of the list table
90 if (argument.getType() == Argument.ENUM) {
91 HashMap arg_list = argument.getOptions();
92 Iterator it = arg_list.keySet().iterator();
93 ArrayList options = new ArrayList();
94 while (it.hasNext()) {
95 options.add((String) it.next());
96 }
97 Collections.sort(options);
98 for (int i=0; i<options.size(); i++) {
99 combobox.addItem(options.get(i));
100 }
101 // Connect this so if a value is selected the tooltip updates accordingly
102 combobox.addActionListener(new ToolTipUpdater(arg_list));
103 }
104 if (argument.getType() == Argument.METADATUM) {
105 ArrayList every_metadata_set_element = MetadataSetManager.getEveryMetadataSetElement();
106 for (int i = 0; i < every_metadata_set_element.size(); i++) {
107 combobox.addItem(every_metadata_set_element.get(i));
108 }
109 }
110
111 if (value != null) {
112 // Set the selected string
113 for (int i = 0; i < combobox.getItemCount(); i++) {
114 if (combobox.getItemAt(i).toString().equals(value)) {
115 combobox.setSelectedIndex(i);
116 }
117 }
118 }
119 // Layout
120 add(combobox, BorderLayout.CENTER);
121 // And remember
122 value_control = combobox;
123 break;
124 case Argument.FLAG:
125 // Only need the check box.
126 value_control = null;
127 break;
128 case Argument.INTEGER:
129 // Build a spinner
130 int initial_value=0;
131 if (value != null) {
132 try {
133 initial_value = Integer.parseInt(value);
134 } catch (Exception exception) {
135 }
136 }
137 if (initial_value < argument.getMinimum()) {
138 initial_value = argument.getMinimum();
139 } else if (initial_value > argument.getMaximum()) {
140 initial_value = argument.getMaximum();
141 }
142 JSpinner spinner = new JSpinner(new SpinnerNumberModel(initial_value, argument.getMinimum(), argument.getMaximum(), 1));
143 spinner.setEnabled(enable);
144 spinner.setPreferredSize(SPINNER_SIZE);
145 spinner.setToolTipText(tooltip);
146 // Set enabled
147 JComponent c = spinner.getEditor();
148 if ( c instanceof JSpinner.DefaultEditor ) {
149 JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c;
150 JFormattedTextField field = editor.getTextField();
151 field.setEditable(enable);
152 if(enable) {
153 field.setBackground(Color.white);
154 }
155 else {
156 field.setBackground(Color.lightGray);
157 }
158 }
159 // Layout
160 add(new JLabel(), BorderLayout.CENTER);
161 add(spinner, BorderLayout.EAST);
162 // And remember it
163 value_control = spinner;
164 break;
165 case Argument.REGEXP:
166 case Argument.STRING:
167 // Use a standard text field
168 JTextField textfield = new JTextField();
169 textfield.setEnabled(enable);
170 textfield.setToolTipText(tooltip);
171 // Set enabled
172 if(enable) {
173 textfield.setBackground(Color.white);
174 }
175 else {
176 textfield.setBackground(Color.lightGray);
177 }
178 // If there was an original value, set it.
179 if(value != null) {
180 textfield.setText(value);
181 }
182 // Layout
183 add(textfield, BorderLayout.CENTER);
184 // And remember it
185 value_control = textfield;
186 break;
187 case Argument.METADATA_SET_NAMESPACE:
188 JComboBox metadata_sets_combobox = new JComboBox();
189 metadata_sets_combobox.setEnabled(enable);
190 metadata_sets_combobox.setToolTipText(tooltip);
191 metadata_sets_combobox.setBackground(enable ? Color.white : Color.lightGray);
192
193 // !! Hack for exploding metadata databases: add the (empty) exploded metadata set
194 File exploded_metadata_set_file = new File(Gatherer.getGLIMetadataDirectoryPath() + "exp" + StaticStrings.METADATA_SET_EXTENSION);
195 MetadataSet exploded_metadata_set = new MetadataSet(exploded_metadata_set_file);
196 Gatherer.c_man.importMetadataSet(exploded_metadata_set);
197
198 // All the loaded metadata sets except the extracted metadata set are applicable
199 ArrayList metadata_sets = MetadataSetManager.getMetadataSets();
200 for (int i = metadata_sets.size() - 1; i >= 0; i--) {
201 MetadataSet metadata_set = (MetadataSet) metadata_sets.get(i);
202 if (!(metadata_set.getNamespace().equals(MetadataSetManager.EXTRACTED_METADATA_NAMESPACE))) {
203 metadata_sets_combobox.addItem(metadata_set);
204 }
205 }
206
207 // Layout
208 add(metadata_sets_combobox, BorderLayout.CENTER);
209 // And remember
210 value_control = metadata_sets_combobox;
211 break;
212 }
213
214 // If the argument is required, then you don't get a choice of whether it is enabled.
215 if(argument.isRequired()) {
216 JLabel label = new JLabel(argument.getName());
217 label.setOpaque(false);
218 label.setPreferredSize(LABEL_SIZE);
219 label.setToolTipText(tooltip);
220 add(label, BorderLayout.WEST);
221 }
222 else {
223 enabled = new JCheckBox(argument.getName(), enable);
224 enabled.setOpaque(false);
225 enabled.setPreferredSize(LABEL_SIZE);
226 enabled.setToolTipText(tooltip);
227 // Connect
228 enabled.addActionListener(new EnabledListener(value_control));
229 // Layout
230 add(enabled, BorderLayout.WEST);
231 }
232 }
233
234 public String getArgumentName()
235 {
236 return argument.getName();
237 }
238
239 public String getValue() {
240 String value = null;
241 if(value_control == null) {
242 // Flag value, nothing to do.
243 }
244 else if(value_control instanceof JTextField) {
245 value = ((JTextField)value_control).getText();
246 }
247 else if(value_control instanceof JSpinner) {
248 value = ((JSpinner)value_control).getValue().toString();
249 }
250 else if(value_control instanceof JComboBox) {
251 Object selected_item = ((JComboBox)value_control).getSelectedItem();
252 if (selected_item != null) {
253 if (argument.getType() == Argument.METADATA_SET_NAMESPACE) {
254 value = ((MetadataSet) selected_item).getNamespace();
255 }
256 else {
257 value = selected_item.toString();
258 }
259 }
260 }
261 return value;
262 }
263 /** Retrieve the control used for storing values.
264 * @return JComponent
265 */
266 public JComponent getValueControl() {
267 return value_control;
268 }
269
270 public boolean isEnabled() {
271 if (enabled == null) {
272 return true; // always enabled
273 }
274 return enabled.isSelected();
275 }
276
277 /** Method to ensure that a certain value is selected, if it exists within that combobox to begin with.
278 * @param combobox the JComboBox whose selection we are trying to preset
279 * @param target The desired value of the selection as a String
280 * @return true if the item was found and selected, false otherwise
281 */
282 public boolean selectValue(JComboBox combobox, String target) {
283 for(int i = 0; i < combobox.getItemCount(); i++) {
284 if(combobox.getItemAt(i).toString().equals(target)) {
285 combobox.setSelectedIndex(i);
286 return true;
287 }
288 }
289 return false;
290 }
291
292 /** Listens for actions apon the enable checkbox, and if detected enables or diables control appropriately. */
293 private class EnabledListener
294 implements ActionListener {
295 /** An editor component, such as a JComboBox or JTextField, that might have its enabled state changed by this listener. */
296 private JComponent target = null;
297 /** Constructor. */
298 public EnabledListener(JComponent target) {
299 this.target = target;
300 }
301 /** 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.
302 * @param event An <strong>ActionEvent</strong> containing information about the click.
303 */
304 public void actionPerformed(ActionEvent event) {
305 JCheckBox source = (JCheckBox)event.getSource();
306 if(target != null) {
307 if(source.isSelected()) {
308 target.setBackground(Color.white);
309 target.setEnabled(true);
310 }
311 else {
312 target.setBackground(Color.lightGray);
313 target.setEnabled(false);
314 }
315 // Special case of stupid JSpinners who don't let their backgrounds change properly.
316 if(target instanceof JSpinner) {
317 JSpinner spinner = (JSpinner) target;
318 JComponent c = spinner.getEditor();
319 if ( c instanceof JSpinner.DefaultEditor ) {
320 JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c;
321 JFormattedTextField field = editor.getTextField();
322 field.setEditable(source.isSelected());
323 if(source.isSelected()) {
324 field.setBackground(Color.white);
325 }
326 else {
327 field.setBackground(Color.lightGray);
328 }
329 }
330 }
331 }
332 }
333 }
334
335 /** Listener that sets the tooltip associated to a combobox to the tooltip relevant to the selected item. */
336 private class ToolTipUpdater
337 implements ActionListener {
338 private HashMap arg_list;
339 public ToolTipUpdater(HashMap arg_list) {
340 this.arg_list = arg_list;
341 }
342 /** 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.
343 * @param event An <strong>ActionEvent</strong> containing information about the action that fired this call.
344 */
345 public void actionPerformed(ActionEvent event) {
346 JComboBox source = (JComboBox)event.getSource();
347 String key = (String) source.getSelectedItem();
348 if(arg_list != null) {
349 String description = (String) arg_list.get(key);
350 if(description != null) {
351 description = Utility.formatHTMLWidth(description, 60);
352 source.setToolTipText(description);
353 }
354 }
355 }
356 }
357}
Note: See TracBrowser for help on using the repository browser.