source: trunk/gli/src/org/greenstone/gatherer/cdm/ArgumentConfiguration.java@ 8313

Last change on this file since 8313 was 8313, checked in by mdewsnip, 20 years ago

Finally committing the (many) changes to the GLI to use the new metadata code... I hope this doesn't have too many bugs in it and committing it now doesn't stuff anyone up! (Katherine said I could commit it, so blame her if anything goes wrong).

  • Property svn:keywords set to Author Date Id Revision
File size: 39.7 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 * Author: John Thompson, Greenstone Digital Library, University of Waikato
9 *
10 * Copyright (C) 1999 New Zealand Digital Library Project
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *########################################################################
26 */
27package org.greenstone.gatherer.cdm;
28
29import java.awt.*;
30import java.awt.event.*;
31import java.util.*;
32import javax.swing.*;
33import org.greenstone.gatherer.Configuration;
34import org.greenstone.gatherer.DebugStream;
35import org.greenstone.gatherer.Dictionary;
36import org.greenstone.gatherer.Gatherer;
37import org.greenstone.gatherer.gui.GComboBox;
38import org.greenstone.gatherer.gui.GLIButton;
39import org.greenstone.gatherer.gui.ModalDialog;
40import org.greenstone.gatherer.gui.SimpleMenuBar;
41import org.greenstone.gatherer.metadata.MetadataElement;
42import org.greenstone.gatherer.metadata.MetadataSetManager;
43import org.greenstone.gatherer.util.StaticStrings;
44import org.greenstone.gatherer.util.Utility;
45
46/** This class provides us with a dialog box which allows us to edit the arguments of either a PlugIn or a Classifier.
47 * @author John Thompson, Greenstone Digital Library, University of Waikato
48 * @version 2.3
49 * @see org.greenstone.gatherer.cdm.Classifier
50 * @see org.greenstone.gatherer.cdm.PlugIn
51 */
52public class ArgumentConfiguration
53 extends ModalDialog
54 implements ActionListener {
55 /** The data whose arguments we are editing. */
56 private ArgumentContainer data = null;
57 /** Argument these argument controls coloured or uncoloured (alternates to indicate inheritance). */
58 private boolean coloured = false;
59 /** Whether we have successfully edited the arguments associated with the ArgumentContainer or if we have failed to enter required arguments and have instead cancelled (which would cause argument additions to roll back). */
60 private boolean success = false;
61 /** A button to cancel this dialog. */
62 private JButton cancel = null;
63 /** A button to accept the changes and close the dialog. */
64 private JButton ok = null;
65 /** A reference to the ourselves so our inner classes can dispose us like a dialog. */
66 private ArgumentConfiguration self = null;
67 /** The central pane where a list of known arguments is displayed. */
68 private JPanel central_pane = null;
69 /** The field for entering custom arguments. */
70 private JTextField custom = null;
71 /** The panel for the custom arguments */
72 private JPanel custom_pane = null;
73 /** The name of the owner of the last argument control. */
74 private String previous_owner = null;
75 /** The size used for an argument label. */
76 static final private Dimension LABEL_SIZE = new Dimension(225, 25);
77 /** Size of a list. */
78 static final private Dimension LIST_SIZE = new Dimension(380, 50);
79 /** The size used for the dialog. */
80 static final private Dimension SIZE = new Dimension(800, 425);
81
82 /** Constructor.
83 * @param data The plugin or classifier whose arguments we are configuring, in the form of its supported <strong>ArgumentContainer</strong> interface.
84 * @see org.greenstone.gatherer.Configuration
85 */
86 public ArgumentConfiguration(ArgumentContainer data) {
87 super(Gatherer.g_man);
88 this.data = data;
89 this.self = this;
90
91 // Create
92 setModal(true);
93 setSize(SIZE);
94 setJMenuBar(new SimpleMenuBar("designingacollection")); // can we tell whether we are doing a classifier or plugin, to make the help more specific??
95 Dictionary.setText(this, "CDM.ArgumentConfiguration.Title");
96
97 central_pane = new JPanel();
98 JPanel content_pane = (JPanel) getContentPane();
99
100 custom_pane = new JPanel();
101 String custom_str = data.getCustom();
102 if (custom_str != null) {
103 custom = new JTextField(custom_str);
104 }
105 else {
106 custom = new JTextField();
107 }
108 JLabel custom_label = new JLabel();
109 custom_label.setPreferredSize(LABEL_SIZE);
110 Dictionary.setText(custom_label, "CDM.ArgumentConfiguration.Custom");
111
112 JPanel header_pane = new JPanel();
113 JLabel header = new JLabel();
114 header.setHorizontalAlignment(JLabel.CENTER);
115 header.setOpaque(true);
116 String args[] = new String[1];
117 args[0] = data.getName();
118 Dictionary.setText(header, "CDM.ArgumentConfiguration.Header", args);
119
120 JPanel button_pane = new JPanel();
121 cancel = new GLIButton();
122 cancel.setMnemonic(KeyEvent.VK_C);
123 Dictionary.setBoth(cancel, "General.Cancel", "General.Pure_Cancel_Tooltip");
124 ok = new GLIButton();
125 ok.setMnemonic(KeyEvent.VK_O);
126 Dictionary.setBoth(ok, "General.OK", "General.OK_Tooltip");
127
128 // Listeners
129 cancel.addActionListener(this);
130 ok.addActionListener(this);
131
132 // Layout
133 custom_pane.setLayout(new BorderLayout());
134 custom_pane.add(custom_label, BorderLayout.WEST);
135 custom_pane.add(custom, BorderLayout.CENTER);
136
137 button_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
138 button_pane.setLayout(new GridLayout(1,2));
139 button_pane.add(ok);
140 button_pane.add(cancel);
141
142 central_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
143 central_pane.setLayout(new BoxLayout(central_pane, BoxLayout.Y_AXIS));
144
145 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
146 content_pane.setLayout(new BorderLayout());
147 content_pane.add(header, BorderLayout.NORTH);
148 content_pane.add(new JScrollPane(central_pane), BorderLayout.CENTER);
149 content_pane.add(button_pane, BorderLayout.SOUTH);
150
151 // Now generate a set of controls for each of the arguments.
152 generateControls();
153
154 // Display on screen.
155 Dimension screen_size = Configuration.screen_size;
156 setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
157 screen_size = null;
158 }
159
160 /** Any implementation of ActionListener must include this method so that we can be informed when an action has occured on one of the controls we are listening to.
161 * @param event An <strong>ActionEvent</strong> containing pertinant information about the event that fired this call.
162 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl
163 * @see org.greenstone.gatherer.cdm.ArgumentContainer
164 */
165 public void actionPerformed(ActionEvent event) {
166 boolean cont = true;
167 if (event.getSource() == ok) {
168 // Clear the current focus to ensure components such as combobox have correctly updated
169 custom.requestFocus();
170 // Update the details stored in the data objects arguments.
171 data.setCustom(custom.getText());
172 // Loop through each of the controls in the central pane, updating the matching argument as necessary.
173 for(int i = 0; i < central_pane.getComponentCount(); i++) {
174 Component component = central_pane.getComponent(i);
175 if(component instanceof ArgumentControl) {
176 cont = cont && ((ArgumentControl)component).updateArgument();
177 }
178 }
179 if(cont) {
180 success = true;
181 }
182 }
183 if(cont) {
184 dispose();
185 }
186 }
187
188 /** Destructor. */
189 public void destroy() {
190 cancel = null;
191 central_pane = null;
192 custom_pane = null;
193 custom = null;
194 data = null;
195 ok = null;
196 self = null;
197 }
198
199 /** Method which actually forces the dialog to be shown on screen.
200 * @return <i>true</i> if the user completed configuration and pressed ok, <i>false</i> otherwise.
201 */
202 public boolean display() {
203 setVisible(true);
204 return success;
205 }
206
207 private void addHeader(String name, Color color) {
208 JPanel header = new JPanel();
209 header.setBackground(color);
210 JPanel inner_pane = new JPanel();
211 inner_pane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(5,5,5,5), BorderFactory.createRaisedBevelBorder()));
212 inner_pane.setBackground(color);
213 JLabel header_label = new JLabel("<html><strong>" + name + "</strong></html>");
214 header_label.setBackground(Configuration.getColor("coloring.collection_heading_background", false));
215 header_label.setHorizontalAlignment(JLabel.CENTER);
216 header_label.setOpaque(true);
217
218 // Layout
219 inner_pane.setLayout(new BorderLayout());
220 inner_pane.add(header_label, BorderLayout.CENTER);
221
222 header.setLayout(new BorderLayout());
223 header.add(inner_pane, BorderLayout.CENTER);
224 central_pane.add(header);
225 }
226
227 /** Method to iterate through the arguments associated with whatever argument container we are building an argument control view for, creating the appropriate controls for each.
228 * @see org.greenstone.gatherer.cdm.Argument
229 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl
230 */
231 private void generateControls() {
232 ArrayList arguments = data.getArguments(true, false);
233 int total_height = 250;
234 int mode = Configuration.getMode();
235 for(int i = 0; i < arguments.size(); i++) {
236 Argument argument = (Argument) arguments.get(i);
237 if(mode > Configuration.LIBRARIAN_MODE || !(argument.getType() == Argument.REGEXP)) {
238 ArgumentControl argument_control = new ArgumentControl(argument);
239 total_height = total_height - argument_control.getPreferredSize().height;
240 central_pane.add(argument_control);
241 }
242 }
243 // now add in the custom args bit
244 coloured = !coloured;
245 Color color = (coloured ? Configuration.getColor("coloring.collection_heading_background", false) : Configuration.getColor("coloring.collection_tree_background", false));
246 addHeader(Dictionary.get("CDM.ArgumentConfiguration.Custom_Header"), color);
247 custom_pane.setBackground(color);
248 central_pane.add(custom_pane);
249 if(total_height > 0) {
250 JPanel filler = new JPanel();
251 filler.setPreferredSize(new Dimension(100, total_height));
252 filler.setSize(new Dimension(100, total_height));
253 central_pane.add(filler);
254 }
255 }
256
257 /** This class encapsulates all the technical difficulty of creating a specific control based on an Argument. */
258 private class ArgumentControl
259 extends JPanel {
260 /** The Argument this control will be based on. */
261 private Argument argument = null;
262
263 private Color colour_one = Configuration.getColor("coloring.collection_heading_background", false);
264 private Color colour_two = Configuration.getColor("coloring.collection_tree_background", false);
265 /** One of a possible two buttons available for adding to this control. */
266 private JButton one = null;
267 /** The second of two buttons available for adding to this control. */
268 private JButton two = null;
269 /** A checkbox to allow enabling or diabling of this Argument. */
270 private JCheckBox enabled = null;
271 /** Some form of editor component, such as a JComboBox or JTextField, used to set parameters to an Argument. */
272 private JComponent value = null;
273 /** Can be used in place of the other editor components if a list is required. */
274 private JList list = null;
275 /** Constructor.
276 * @param argument The <strong>Argument</strong> this control will be built around.
277 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl.AddListener
278 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl.EnabledListener
279 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl.HierarchyListener
280 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl.ListOption
281 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl.RemoveListener
282 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl.ToolTipUpdater
283 */
284 public ArgumentControl(Argument argument) {
285 this.argument = argument;
286 ///ystem.err.println("generating controls for arg "+argument.getName());
287 String tip = "<html>" + argument.getDescription() + "</html>";
288 tip = Utility.formatHTMLWidth(tip, 60);
289
290 // If this is the first control, there is no history.
291 if(previous_owner == null) {
292 ///ystem.err.println("previous owner is null");
293 previous_owner = argument.getOwner();
294 addHeader(previous_owner, colour_two);
295 }
296 // Otherwise if the owner of the control has changed since the last argument, toggle the colouring of the control.
297 else if(previous_owner != argument.getOwner()) {
298 ///ystem.err.println("previous owner is different from current owner");
299 coloured = !coloured;
300 previous_owner = argument.getOwner();
301 addHeader(previous_owner, (coloured ? colour_one : colour_two));
302 }
303 // Create
304 if(coloured) {
305 setBackground(colour_one);
306 }
307 else {
308 setBackground(colour_two);
309 }
310 JLabel owner_label = new JLabel(argument.getOwner());
311 owner_label.setOpaque(false);
312 JLabel label = new JLabel(argument.getName());
313 label.setOpaque(false);
314 label.setPreferredSize(LABEL_SIZE);
315 label.setToolTipText(tip);
316 enabled = new JCheckBox(argument.getName());
317 enabled.setOpaque(false);
318 enabled.setPreferredSize(LABEL_SIZE);
319 enabled.setToolTipText(tip);
320 JPanel inner_pane = new JPanel();
321 inner_pane.setOpaque(false);
322 String existing_value = argument.getValue();
323 String default_value = argument.getDefaultValue();
324
325 switch(argument.getType()) {
326 case Argument.ENUM:
327 // Build an option model, wrapping each entry of the list table.
328 HashMap arg_list = argument.getOptions();
329 ArrayList options_model = new ArrayList();
330 Iterator it = arg_list.keySet().iterator();
331 while(it.hasNext()) {
332 String key = (String) it.next();
333 options_model.add(new ListOption(key, (String)arg_list.get(key)));
334 }
335 Collections.sort(options_model);
336 value = new GComboBox(options_model.toArray(), false);
337 ((JComboBox)value).addActionListener(new ToolTipUpdater());
338 if(existing_value != null && existing_value.length() > 0) {
339 // Select the correct value. Since they're all text strings we better iterate to be safe.
340 selectValue((JComboBox)value, existing_value);
341 }
342 else if(default_value != null) {
343 ///ystem.err.println("Default for argument: " + argument.getName());
344 // Same as above except for default value.
345 selectValue((JComboBox)value, default_value);
346 }
347 break;
348
349 case Argument.FLAG:
350 // Only need the check box.
351 break;
352
353 case Argument.HIERARCHY:
354 // I don't think these are used any more...
355 break;
356
357 case Argument.INTEGER:
358 // Build a spinner
359 int initial_value=0;
360 // If there was an original value, set it.
361 if(existing_value != null && !existing_value.equals("")) {
362 try {
363 initial_value = Integer.parseInt(existing_value);
364 //spinner.setValue(new Integer(existing_value));
365 }
366 catch (Exception error) {
367 DebugStream.println("ArgumentConfiguration Error: "+error);
368 }
369 } else if (default_value != null && !default_value.equals("")) {
370 try {
371 initial_value = Integer.parseInt(default_value);
372 //spinner.setValue(new Integer(default_value));
373 }
374 catch (Exception error) {
375 DebugStream.println("ArgumentConfiguration Error: "+error);
376 }
377 }
378 if (initial_value < argument.getMinimum()) {
379 initial_value = argument.getMinimum();
380 } else if (initial_value > argument.getMaximum()) {
381 initial_value = argument.getMaximum();
382 }
383
384 JSpinner spinner = new JSpinner(new SpinnerNumberModel(initial_value, argument.getMinimum(), argument.getMaximum(), 1));
385
386 // And remember it
387 value = spinner;
388 break;
389
390 case Argument.REGEXP:
391 case Argument.STRING:
392 // If there is already a value set for this argument, use it
393 if (existing_value != null && !existing_value.equals("")) {
394 value = new JTextField(existing_value);
395 break;
396 }
397
398 // Use the default value, if there is one
399 if (default_value != null && !default_value.equals("")) {
400 value = new JTextField(default_value);
401 break;
402 }
403
404// // Special test just for the hfile field.
405// if (argument.getName().equals("hfile")) {
406// // Work through previous controls looking for the metadata one.
407// for (int i = 0; i < central_pane.getComponentCount(); i++) {
408// Object object = central_pane.getComponent(i);
409// if (object instanceof ArgumentControl) {
410// ArgumentControl control = (ArgumentControl) object;
411// if (control.toString().equals("metadata")) {
412// Object temp = control.getValue();
413// if (temp != null) {
414// value = new JTextField(temp.toString() + ".txt");
415// break;
416// }
417// }
418// }
419// }
420// }
421
422 // Blank field
423 value = new JTextField();
424 break;
425
426 case Argument.LANGUAGE:
427 value = new GComboBox(CollectionDesignManager.language_manager.getLanguageCodes().toArray(), false);
428
429 // Now ensure we have the existing value or default value selected if either exist.
430 Language selected = null;
431 if(existing_value != null && existing_value.length() > 0) {
432 selected = CollectionDesignManager.language_manager.getLanguage(existing_value);
433 }
434 else if(default_value != null) {
435 selected = CollectionDesignManager.language_manager.getLanguage(default_value);
436 }
437 if(selected != null) {
438 ((JComboBox)value).setSelectedItem(selected.getCode());
439 }
440 break;
441
442 case Argument.METADATUM:
443 case Argument.METADATA:
444 value = new GComboBox(MetadataSetManager.getEveryMetadataSetElement(), false);
445
446 // Editable for advanced modes (allows things like dc.Title,ex.Title)
447 if (Configuration.getMode() > Configuration.ASSISTANT_MODE) {
448 ((JComboBox) value).setEditable(true);
449 }
450
451 // Now ensure we have the existing value or default value selected if either exist.
452 if (existing_value != null && existing_value.length() > 0) {
453 boolean found = selectValue((JComboBox) value, existing_value);
454 // It's possible that this is a custom value and so doesn't exist in the combobox
455 if (!found) {
456 // If so, add it then select it
457 ((JComboBox) value).addItem(existing_value);
458 ((JComboBox) value).setSelectedItem(existing_value);
459 }
460 }
461 else if (default_value != null) {
462 selectValue((JComboBox) value, default_value);
463 }
464 break;
465
466// ---- Special interface for adding and ordering multiple metadata items ----
467// Turned off at Ian's request!
468// case Argument.METADATA:
469// // Comma separated metadata values.
470// ArrayList values = argument.getValues();
471// value = new GComboBox(MetadataSetManager.getEveryMetadataSetElement(), false);
472// //((JComboBox)value).setEditable(false);
473// DefaultListModel model = new DefaultListModel();
474// list = new JList(model);
475// list.setVisibleRowCount(3);
476// for(int i = 0; i < values.size(); i++) {
477// model.addElement(values.get(i));
478// }
479
480// one = new GLIButton();
481// one.addActionListener(new AddListener((JComboBox)value, list));
482// one.setMnemonic(KeyEvent.VK_A);
483// Dictionary.setBoth(one, "CDM.ArgumentConfiguration.Add", "CDM.ArgumentConfiguration.Add_Tooltip");
484// two = new GLIButton();
485// two.addActionListener(new RemoveListener(list));
486// two.setMnemonic(KeyEvent.VK_R);
487// Dictionary.setBoth(two, "CDM.ArgumentConfiguration.Remove", "CDM.ArgumentConfiguration.Remove_Tooltip");
488
489// if(argument.getValues().size() > 0 || argument.isRequired()) {
490// enabled.setSelected(true);
491// list.setBackground(Color.white);
492// list.setEnabled(true);
493// one.setEnabled(true);
494// two.setEnabled(true);
495// value.setEnabled(true);
496// }
497// else {
498// enabled.setSelected(false);
499// list.setBackground(Color.lightGray);
500// list.setEnabled(false);
501// one.setEnabled(false);
502// two.setEnabled(false);
503// value.setEnabled(false);
504// }
505// break;
506 } // end of switch
507
508 // Enable or disable as necessary.
509 if(argument.isRequired() || argument.isAssigned()) {
510 enabled.setSelected(true);
511 if(value != null) {
512 value.setOpaque(true);
513 value.setBackground(Color.white);
514 value.setEnabled(true);
515 if(value instanceof JSpinner) {
516 // Set enabled
517 JComponent c = ((JSpinner)value).getEditor();
518 if ( c instanceof JSpinner.DefaultEditor ) {
519 JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c;
520 JFormattedTextField field = editor.getTextField();
521 field.setEditable(true);
522 field.setBackground(Color.white);
523 }
524 }
525 }
526 }
527 else {
528 enabled.setSelected(false);
529 if(value != null) {
530 value.setOpaque(true);
531 value.setBackground(Color.lightGray);
532 value.setEnabled(false);
533 if(value instanceof JSpinner) {
534 // Set enabled
535 JComponent c = ((JSpinner)value).getEditor();
536 if ( c instanceof JSpinner.DefaultEditor ) {
537 JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c;
538 JFormattedTextField field = editor.getTextField();
539 field.setEditable(false);
540 field.setBackground(Color.lightGray);
541 }
542 }
543 }
544 }
545 // Listener
546 if(value != null && !argument.isRequired()) {
547 enabled.addActionListener(new EnabledListener(one, two, list, value));
548 }
549
550 // Layout
551 inner_pane.setLayout(new BorderLayout());
552 if (argument.isRequired()) {
553 inner_pane.add(label, BorderLayout.WEST);
554 }
555 else {
556 inner_pane.add(enabled, BorderLayout.WEST);
557 }
558
559 if (list == null) {
560 enabled.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
561 if (value != null) {
562 inner_pane.add(value, BorderLayout.CENTER);
563 }
564 }
565 else {
566 JPanel left_pane = new JPanel(new BorderLayout());
567 left_pane.add(new JLabel(""), BorderLayout.NORTH);
568 left_pane.add(value, BorderLayout.CENTER);
569 left_pane.add(one, BorderLayout.SOUTH);
570 left_pane.setOpaque(false);
571
572 JPanel right_pane = new JPanel(new BorderLayout());
573 right_pane.add(new JScrollPane(list), BorderLayout.CENTER);
574 right_pane.add(two, BorderLayout.SOUTH);
575 right_pane.setOpaque(false);
576
577 JPanel control_pane = new JPanel(new GridLayout(1, 2));
578 control_pane.add(left_pane);
579 control_pane.add(right_pane);
580
581 inner_pane.add(control_pane, BorderLayout.CENTER);
582 }
583
584 setLayout(new BorderLayout());
585 add(inner_pane, BorderLayout.CENTER);
586 }
587
588 public Argument getArgument() {
589 return argument;
590 }
591
592 public Object getValue() {
593 if(value instanceof JComboBox) {
594 return ((JComboBox)value).getSelectedItem();
595 }
596 else if(value instanceof JTextField) {
597 return ((JTextField)value).getText();
598 }
599 return null;
600 }
601 /** Identifies this control by returning the name of the Argument it is based on.
602 * @return The name of the Argument as a <strong>String</strong>.
603 * @see org.greenstone.gatherer.cdm.Argument
604 */
605 public String toString() {
606 return argument.getName();
607 }
608 /** Updates the enwrapped Argument using the values provided by the controls.
609 * @return <i>true</i> if the update was successful, <i>false</i> otherwise.
610 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl.ListOption
611 * @see org.greenstone.gatherer.cdm.Language
612 */
613 public boolean updateArgument() {
614 if(enabled.isSelected() || argument.isRequired()) {
615 argument.setAssigned(false);
616 String result = null;
617 switch(argument.getType()) {
618 case Argument.ENUM:
619 ListOption option = (ListOption)((JComboBox)value).getSelectedItem();
620 if(option != null && option.getValue().length() > 0) {
621 argument.setValue(option.getValue());
622 }
623 else {
624 String args[] = new String[1];
625 args[0] = argument.getName();
626 if(argument.isRequired()) {
627 JOptionPane.showMessageDialog(self, Dictionary.get("CDM.ArgumentConfiguration.Required_Argument", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE);
628 }
629 // They've left the field blank
630 else {
631 JOptionPane.showMessageDialog(self, Dictionary.get("CDM.ArgumentConfiguration.No_Value", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE);
632 argument.setValue(null);
633 }
634 args = null;
635 return false;
636 }
637 argument.setAssigned(true);
638 return true;
639 case Argument.FLAG:
640 // Should have already been handled above.
641 argument.setAssigned(true);
642 return true;
643 case Argument.INTEGER:
644 result = ((JSpinner)value).getValue().toString();
645 if(result.length() > 0) {
646 // Test if the value entered is a valid int.
647 try {
648 int x = Integer.parseInt(result);
649 }
650 catch(NumberFormatException nfe) {
651 String args[] = new String[2];
652 args[0] = argument.getName();
653 args[1] = result;
654 JOptionPane.showMessageDialog(self, Dictionary.get("CDM.ArgumentConfiguration.Bad_Integer", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE);
655 args = null;
656 return false;
657 }
658 argument.setValue(result);
659 }
660 else {
661 String args[] = new String[1];
662 args[0] = argument.getName();
663 if(argument.isRequired()) {
664 JOptionPane.showMessageDialog(self, Dictionary.get("CDM.ArgumentConfiguration.Required_Argument", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE);
665 }
666 // They've left the field blank
667 else {
668 JOptionPane.showMessageDialog(self, Dictionary.get("CDM.ArgumentConfiguration.No_Value", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE);
669 argument.setValue(null);
670 }
671 args = null;
672 return false;
673 }
674 argument.setAssigned(true);
675 return true;
676 case Argument.LANGUAGE:
677 String language = (((JComboBox)value).getSelectedItem()).toString();
678 argument.setValue(language);
679 // Kinda lucked out here. Its impossible not to choose an entry from these comboboxes as they are restricted.
680 argument.setAssigned(true);
681 return true;
682 case Argument.METADATUM:
683 case Argument.METADATA:
684 Object new_value_raw = ((JComboBox) value).getSelectedItem();
685 if (new_value_raw instanceof MetadataElement) {
686 argument.setValue(((MetadataElement) new_value_raw).getFullName());
687 }
688 else {
689 // But we have to be careful as an arbitary string object could be zero length
690 String new_value = new_value_raw.toString();
691 ///ystem.err.println("The current value is: " + new_value);
692 if(new_value.length() > 0) {
693 argument.setValue(new_value);
694 }
695 else {
696 String args[] = new String[1];
697 args[0] = argument.getName();
698 if(argument.isRequired()) {
699 JOptionPane.showMessageDialog(self, Dictionary.get("CDM.ArgumentConfiguration.Required_Argument", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE);
700 }
701 // They've left the field blank
702 else {
703 JOptionPane.showMessageDialog(self, Dictionary.get("CDM.ArgumentConfiguration.No_Value", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE);
704 argument.setValue(null);
705 }
706 args = null;
707 return false;
708 }
709 }
710 argument.setAssigned(true);
711 return true;
712// case Argument.METADATA:
713// DefaultListModel model = (DefaultListModel)list.getModel();
714// ArrayList values = new ArrayList();
715// for(int i = 0; i < model.size(); i++) {
716// values.add(model.get(i));
717// }
718// argument.setValues(values);
719// argument.setAssigned(true);
720// return true;
721 case Argument.HIERARCHY:
722// argument.setValue(((JComboBox)value).getSelectedItem().toString());
723// // Kinda lucked out here. Its impossible not to choose an entry from these comboboxes as they are restricted.
724// argument.setAssigned(true);
725 return true;
726 case Argument.REGEXP:
727 case Argument.STRING:
728 result = ((JTextField)value).getText();
729 if(result.length() > 0) {
730 argument.setValue(result);
731 }
732 else {
733 String args[] = new String[1];
734 args[0] = argument.getName();
735 if(argument.isRequired()) {
736 JOptionPane.showMessageDialog(self, Dictionary.get("CDM.ArgumentConfiguration.Required_Argument", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE);
737 }
738 // They've left the field blank
739 else {
740 JOptionPane.showMessageDialog(self, Dictionary.get("CDM.ArgumentConfiguration.No_Value", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE);
741 argument.setValue(null);
742 }
743 args = null;
744 return false;
745 }
746 argument.setAssigned(true);
747 return true;
748 }
749 return false;
750 }
751 else {
752 argument.setAssigned(false);
753 return true;
754 }
755 }
756 /** Method to ensure that a certain value is selected, if it exists within that combobox to begin with.
757 * @param combobox The <strong>JComboBox</strong> whose selection we are trying to preset.
758 * @param target The desired value of the selection as a <strong>String</strong>.
759 * @return true if the item was found and selected, false otherwise
760 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl.ListOption
761 */
762 public boolean selectValue(JComboBox combobox, String target) {
763 ///ystem.err.println("Searching for the target string: " + target);
764 for(int i = 0; i < combobox.getItemCount(); i++) {
765 Object object = combobox.getItemAt(i);
766 if(object instanceof ListOption) {
767 ListOption lo = (ListOption) object;
768 ///ystem.err.print("/tChecking: " + lo.getValue() + "... ");
769 if(lo.getValue().startsWith(target)) {
770 ///ystem.err.println("Match!");
771 combobox.setSelectedIndex(i);
772 return true;
773 }
774 /*
775 else {
776 System.err.println("No Match.");
777 }
778 */
779 }
780 else if (object instanceof MetadataElement) {
781 if(object.toString().equals(target)) {
782 combobox.setSelectedIndex(i);
783 return true;
784 }
785 }
786 }
787 return false;
788 }
789 /** Forces the control into an 'enabled' mode. */
790 public void setEnabled() {
791 enabled.setSelected(true);
792 }
793 /** Explicitly sets the value of a JTextField type control to the given String.
794 * @param value_str The new value of the control as a <strong>String</strong>.
795 */
796 public void setValue(String value_str) {
797 ((JTextField)value).setText(value_str);
798 }
799// /** Listener which adds entries to a list from a combobox when fired. */
800// private class AddListener
801// implements ActionListener {
802// /** The model behind the target list. */
803// private DefaultListModel model = null;
804// /** The source for data to be added to the list. */
805// private JComboBox source = null;
806// /** The list to add data to. */
807// private JList target = null;
808// /** Constructor.
809// * @param source A <strong>JComboBox</strong> which serves as the source for data.
810// * @param target A <strong>JList</strong> which serves as the target for data.
811// */
812// public AddListener(JComboBox source, JList target) {
813// this.model = (DefaultListModel) target.getModel();
814// this.source = source;
815// this.target = target;
816// }
817// /** When the add button is clicked, we attempt to add the selected metadata from the source into the target.
818// * @param event An <strong>ActionEvent</strong> containing information about the event.
819// */
820// public void actionPerformed(ActionEvent event) {
821// ElementWrapper element = (ElementWrapper) source.getSelectedItem();
822// String name = element.toString();
823// if (!model.contains(name)) {
824// model.addElement(name);
825// }
826// }
827// }
828 /** Listens for actions apon the enable checkbox, and if detected enables or diables control appropriately. */
829 private class EnabledListener
830 implements ActionListener {
831 /** One of two possible buttons that might have their enabled state changed by this listener. */
832 private JButton one = null;
833 /** One of two possible buttons that might have their enabled state changed by this listener. */
834 private JButton two = null;
835 /** An editor component, such as a JComboBox or JTextField, that might have its enabled state changed by this listener. */
836 private JComponent target = null;
837 /** A list which might have its enabled state changed by this listener. */
838 private JList list = null;
839 /** Constructor.
840 * @param one A <strong>JButton</strong> whose enabled state is determined by the listener, or <i>null</i> if no button.
841 * @param two A <strong>JButton</strong> whose enabled state is determined by the listener, or <i>null</i> if no button.
842 * @param list A <strong>JList</strong> whose enabled state is determined by the listener, or <i>null</i> if no list.
843 * @param target A <strong>JComponent</strong> whose enabled state is determined by the listener, or <i>null</i> if no component.
844 */
845 public EnabledListener(JButton one, JButton two, JList list, JComponent target) {
846 this.list = list;
847 this.one = one;
848 this.target = target;
849 this.two = two;
850 }
851 /** 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.
852 * @param event An <strong>ActionEvent</strong> containing information about the click.
853 */
854 public void actionPerformed(ActionEvent event) {
855 JCheckBox source = (JCheckBox)event.getSource();
856 if(source.isSelected()) {
857 target.setBackground(Color.white);
858 target.setEnabled(true);
859 if(target instanceof JSpinner) {
860 // Set enabled
861 JComponent c = ((JSpinner)target).getEditor();
862 if ( c instanceof JSpinner.DefaultEditor ) {
863 JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c;
864 JFormattedTextField field = editor.getTextField();
865 field.setEditable(true);
866 field.setBackground(Color.white);
867 }
868 }
869 if(one != null && two != null && list != null) {
870 one.setEnabled(true);
871 two.setEnabled(true);
872 list.setBackground(Color.white);
873 list.setEnabled(true);
874 }
875 }
876 else {
877 target.setBackground(Color.lightGray);
878 target.setEnabled(false);
879if(target instanceof JSpinner) {
880 // Set enabled
881 JComponent c = ((JSpinner)target).getEditor();
882 if ( c instanceof JSpinner.DefaultEditor ) {
883 JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c;
884 JFormattedTextField field = editor.getTextField();
885 field.setEditable(false);
886 field.setBackground(Color.lightGray);
887 }
888 }
889 if(one != null && two != null && list != null) {
890 one.setEnabled(false);
891 two.setEnabled(false);
892 list.setBackground(Color.lightGray);
893 list.setEnabled(false);
894 }
895 }
896 }
897 }
898 /** If a metadata element is selected that requires an hfile, then this listener defaults that hfile. */
899// private class HierarchyListener
900// implements ItemListener {
901// /** Any implementation of ItemListener must include this method so that we can be informed when an item from the list is selected, and generate a predetermined hfile for that selection.
902// * @param event An <strong>ItemEvent</strong> containing information about the selection.
903// * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl
904// */
905// public void itemStateChanged(ItemEvent event) {
906// // Determine if the selected element represents a hierarchy.
907// Object temp = ((JComboBox)value).getSelectedItem();
908// String filename = temp.toString();
909// // Search for a argument control called hfile and enable and set value.
910// for(int i = 0; i < central_pane.getComponentCount(); i++) {
911// Object object = central_pane.getComponent(i);
912// if(object instanceof ArgumentControl) {
913// ArgumentControl control = (ArgumentControl) object;
914// if(control.toString().equals("hfile")) {
915// control.setValue(filename + ".txt");
916// control.setEnabled(true);
917// }
918// }
919// }
920// }
921// }
922 /** A ListOption is a compound item which is constructed from several Strings. That magic part is that the length of screen real-estate used by the text version of this item is limited. */
923 private class ListOption
924 implements Comparable {
925 /** The maximum length of this String version of this item. */
926 private int MAX_DESC = 65;
927 /** The description of the value for this item. */
928 private String description = null;
929 /** A cached value for the text value of this option, as it never changes after the first call to toString(). */
930 private String text = null;
931 /** The value for this item. */
932 private String value = null;
933 /** Constructor.
934 * @param value The value for this item as a <strong>String</strong>.
935 * @param description The description of the value as a <strong>String</strong>.
936 */
937 public ListOption(String value, String description) {
938 this.description = description;
939 this.value = value;
940 }
941 /** Compare two possible ListOption objects for ordering.
942 * @param object The <strong>Object</strong> to compare to.
943 * @return An <i>int</i> indicating order as explained in String.
944 * @see java.lang.String#compareTo
945 */
946 public int compareTo(Object object) {
947 return toString().compareTo(object.toString());
948 }
949 /** Tests two possible ListOption objects for equality. Uses the result from compareTo().
950 * @param object The <strong>Object</strong> which may be equal.
951 * @return <i>true</i> if the objects are equal, <i>false</i> otherwise.
952 */
953 public boolean equals(Object object) {
954 return (compareTo(object) == 0);
955 }
956 /** Retrieve the description of this list item.
957 * @return The description as a <strong>String</strong>.
958 */
959 public String getDesc() {
960 return description;
961 }
962 /** Retrieve the value of this list item.
963 * @return The value as a <strong>String</strong>.
964 */
965 public String getValue() {
966 return value;
967 }
968 /** Convert this object into a nice readable String.
969 * @return A <strong>String</strong> representing this object.
970 */
971 public String toString() {
972 if(text == null) {
973 if(description.length() >= MAX_DESC) {
974 text = value + StaticStrings.MINUS_CHARACTER + description.substring(0, MAX_DESC) + StaticStrings.TRUNCATED_STRING;
975 }
976 else {
977 text = value + StaticStrings.MINUS_CHARACTER + description;
978 }
979 }
980 return text;
981 }
982 }
983 /** Listener which removes entries from a list from a combobox when fired. */
984 private class RemoveListener
985 implements ActionListener {
986 /** The model behind the target list. */
987 private DefaultListModel model = null;
988 /** The list to remove data from. */
989 private JList target = null;
990 /** Constructor.
991 * @param target A <strong>JList</strong>.
992 */
993 public RemoveListener(JList target) {
994 this.model = (DefaultListModel) target.getModel();
995 this.target = target;
996 }
997 /** When the remove button is clicked, we attempt to remove the selected metadata from the target.
998 * @param event An <strong>ActionEvent</strong> containing information about the event.
999 */
1000 public void actionPerformed(ActionEvent event) {
1001 if(!target.isSelectionEmpty()) {
1002 int index = target.getSelectedIndex();
1003 model.remove(index);
1004 }
1005 }
1006 }
1007 /** Listener that sets the tooltip associated to a combobox to the tooltip relevant to the selected item. */
1008 private class ToolTipUpdater
1009 implements ActionListener {
1010 /** 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.
1011 * @param event An <strong>ActionEvent</strong> containing information about the action that fired this call.
1012 */
1013 public void actionPerformed(ActionEvent event) {
1014 JComboBox source = (JComboBox)event.getSource();
1015 Object object = source.getSelectedItem();
1016 if(object instanceof ListOption) {
1017 ListOption lo = (ListOption)object;
1018 if(lo != null) {
1019 String description = Utility.formatHTMLWidth(lo.getDesc(), 60);
1020 source.setToolTipText(description);
1021 }
1022 else {
1023 source.setToolTipText(StaticStrings.EMPTY_STR);
1024 }
1025 }
1026 }
1027 }
1028 }
1029}
Note: See TracBrowser for help on using the repository browser.