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

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

Fixed a problem with the CreatePane being blank with Java 1.5.0 and the GTK look and feel.

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