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

Last change on this file since 4293 was 4293, checked in by jmt12, 21 years ago

Initial revision

  • Property svn:keywords set to Author Date Id Revision
File size: 36.9 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 */
37
38
39
40
41
42
43/* GPL_HEADER */
44package org.greenstone.gatherer.cdm;
45/**************************************************************************************
46 * Title: Gatherer
47 * Description: The Gatherer: a tool for gathering and enriching a digital collection.
48 * Company: The University of Waikato
49 * Written: 01/05/02
50 * Revised: 16/08/02 Optimized and Commented. Added destructor.
51 **************************************************************************************/
52import java.awt.BorderLayout;
53import java.awt.Color;
54import java.awt.Component;
55import java.awt.Dimension;
56import java.awt.GridLayout;
57import java.awt.Toolkit;
58import java.awt.event.ActionEvent;
59import java.awt.event.ActionListener;
60import java.awt.event.ItemEvent;
61import java.awt.event.ItemListener;
62import java.awt.event.KeyEvent;
63import java.util.ArrayList;
64import java.util.Collections;
65import java.util.HashMap;
66import java.util.Iterator;
67import javax.swing.BorderFactory;
68import javax.swing.BoxLayout;
69import javax.swing.DefaultListModel;
70import javax.swing.JButton;
71import javax.swing.JCheckBox;
72import javax.swing.JComboBox;
73import javax.swing.JComponent;
74import javax.swing.JDialog;
75import javax.swing.JLabel;
76import javax.swing.JList;
77import javax.swing.JOptionPane;
78import javax.swing.JPanel;
79import javax.swing.JScrollPane;
80import javax.swing.JTextField;
81import org.greenstone.gatherer.Gatherer;
82import org.greenstone.gatherer.cdm.Argument;
83import org.greenstone.gatherer.cdm.ArgumentContainer;
84import org.greenstone.gatherer.cdm.Classifier;
85import org.greenstone.gatherer.cdm.CollectionDesignManager;
86import org.greenstone.gatherer.cdm.PlugIn;
87import org.greenstone.gatherer.msm.ElementWrapper;
88import org.greenstone.gatherer.util.Utility;
89import org.greenstone.gatherer.valuetree.GValueModel;
90/** This class provides us with a dialog box which allows us to edit the arguments of either a PlugIn or a Classifier.
91 * @author John Thompson, Greenstone Digital Library, University of Waikato
92 * @version 2.3
93 * @see org.greenstone.gatherer.cdm.Classifier
94 * @see org.greenstone.gatherer.cdm.PlugIn
95 */
96// ####################################################################################
97// Optimization Saving
98// ####################################################################################
99// Vector -> ArrayList + Processor
100// Hashtable -> HashMap + Processor
101// Remove several global references + Memory (1Kb+)
102// ####################################################################################
103
104public class ArgumentConfiguration
105 extends JDialog
106 implements ActionListener {
107 /** The data whose arguments we are editing. */
108 private ArgumentContainer data = null;
109 /** Argument these argument controls coloured or uncoloured (alternates to indicate inheritance). */
110 private boolean coloured = false;
111 /** 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). */
112 private boolean success = false;
113 /** A reference to the main CollectionDesignManager class for access to other managers. */
114 private CollectionDesignManager manager = null;
115 /** A reference to the Gatherer. */
116 private Gatherer gatherer = null;
117 /** A button to cancel this dialog. */
118 private JButton cancel = null;
119 /** A button to accept the changes and close the dialog. */
120 private JButton ok = null;
121 /** A reference to the ourselves so our inner classes can dispose us like a dialog. */
122 private ArgumentConfiguration self = null;
123 /** The central pane where a list of known arguments is displayed. */
124 private JPanel central_pane = null;
125 /** The field for entering custom arguments. */
126 private JTextField custom = null;
127 /** The name of the owner of the last argument control. */
128 private String previous_owner = null;
129 /** The size used for an argument label. */
130 static final private Dimension LABEL_SIZE = new Dimension(225, 25);
131 /** Size of a list. */
132 static final private Dimension LIST_SIZE = new Dimension(380, 50);
133 /** The size used for the dialog. */
134 static final private Dimension SIZE = new Dimension(800, 400);
135 /** Constructor.
136 * @param gatherer A reference to the <strong>Gatherer</strong>.
137 * @param manager The <strong>CollectionDesignManager</strong> for access to other configuration managers.
138 * @param data The plugin or classifier whose arguments we are configuring, in the form of its supported <strong>ArgumentContainer</strong> interface.
139 * @see org.greenstone.gatherer.Configuration
140 */
141 public ArgumentConfiguration(Gatherer gatherer, CollectionDesignManager manager, ArgumentContainer data) {
142 super(gatherer.g_man);
143 this.data = data;
144 this.gatherer = gatherer;
145 this.manager = manager;
146 this.self = this;
147 String custom_str = data.getCustom();
148 // Create
149 setModal(true);
150 setSize(SIZE);
151 setTitle(get("Title"));
152 JPanel button_pane = new JPanel();
153 cancel = new JButton(get("General.Cancel"));
154 cancel.setMnemonic(KeyEvent.VK_C);
155 central_pane = new JPanel();
156 JPanel content_pane = (JPanel) getContentPane();
157 if(custom_str != null) {
158 custom = new JTextField(custom_str);
159 }
160 else {
161 custom = new JTextField();
162 }
163 JLabel custom_label = new JLabel(get("Custom"));
164 custom_label.setPreferredSize(LABEL_SIZE);
165 JPanel custom_pane = new JPanel();
166 String args[] = new String[1];
167 args[0] = data.getName();
168 JLabel header = new JLabel(get("Header",args));
169 args = null;
170 header.setHorizontalAlignment(JLabel.CENTER);
171 header.setOpaque(true);
172 JPanel header_pane = new JPanel();
173 ok = new JButton(get("General.OK"));
174 ok.setMnemonic(KeyEvent.VK_O);
175 // Listeners
176 cancel.addActionListener(this);
177 ok.addActionListener(this);
178 // Layout
179 custom_pane.setLayout(new BorderLayout());
180 custom_pane.add(custom_label, BorderLayout.WEST);
181 custom_pane.add(custom, BorderLayout.CENTER);
182
183 header_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
184 header_pane.setLayout(new BorderLayout());
185 header_pane.add(header, BorderLayout.NORTH);
186 header_pane.add(custom_pane, BorderLayout.CENTER);
187
188 button_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
189 button_pane.setLayout(new GridLayout(1,2));
190 button_pane.add(ok);
191 button_pane.add(cancel);
192
193 central_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
194 central_pane.setLayout(new BoxLayout(central_pane, BoxLayout.Y_AXIS));
195
196 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
197 content_pane.setLayout(new BorderLayout());
198 content_pane.add(header_pane, BorderLayout.NORTH);
199 content_pane.add(new JScrollPane(central_pane), BorderLayout.CENTER);
200 content_pane.add(button_pane, BorderLayout.SOUTH);
201
202 // Now generate a set of controls for each of the arguments.
203 generateControls();
204
205 // Display on screen.
206 Dimension screen_size = gatherer.config.screen_size;
207 setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
208 screen_size = null;
209 }
210 /** 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.
211 * @param event An <strong>ActionEvent</strong> containing pertinant information about the event that fired this call.
212 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl
213 * @see org.greenstone.gatherer.cdm.ArgumentContainer
214 */
215 public void actionPerformed(ActionEvent event) {
216 boolean cont = true;
217 if(event.getSource() == ok) {
218 // Update the details stored in the data objects arguments.
219 if(custom.getText().length() > 0) {
220 data.setCustom(custom.getText());
221 }
222 // Loop through each of the controls in the central pane, updating the matching argument as necessary.
223 for(int i = 0; i < central_pane.getComponentCount(); i++) {
224 Component component = central_pane.getComponent(i);
225 if(component instanceof ArgumentControl) {
226 // Once cont goes false it stays false
227 cont = cont && ((ArgumentControl)component).updateArgument();
228 }
229 }
230 if(cont) {
231 success = true;
232 }
233 }
234 if(cont) {
235 dispose();
236 }
237 }
238 /** Destructor. */
239 public void destroy() {
240 cancel = null;
241 central_pane = null;
242 custom = null;
243 data = null;
244 gatherer = null;
245 manager = null;
246 ok = null;
247 self = null;
248 }
249 /** Method which actually forces the dialog to be shown on screen.
250 * @return <i>true</i> if the user completed configuration and pressed ok, <i>false</i> otherwise.
251 */
252 public boolean display() {
253 show();
254 return success;
255 }
256
257 private void addHeader(String name, Color color) {
258 JPanel header = new JPanel();
259 header.setBackground(color);
260 JPanel inner_pane = new JPanel();
261 inner_pane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(5,5,5,5), BorderFactory.createRaisedBevelBorder()));
262 inner_pane.setBackground(color);
263 JLabel header_label = new JLabel("<html><strong>" + name + "</strong></html>");
264 header_label.setBackground(Gatherer.config.getColor("coloring.collection_heading_background", false));
265 header_label.setHorizontalAlignment(JLabel.CENTER);
266 header_label.setOpaque(true);
267 // Layout.
268 inner_pane.setLayout(new BorderLayout());
269 inner_pane.add(header_label, BorderLayout.CENTER);
270
271 header.setLayout(new BorderLayout());
272 header.add(inner_pane, BorderLayout.CENTER);
273 central_pane.add(header);
274 }
275
276 /** 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.
277 * @see org.greenstone.gatherer.cdm.Argument
278 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl
279 */
280 private void generateControls() {
281 ArrayList arguments = data.getArguments();
282 int total_height = 250;
283 int size = arguments.size();
284 for(int i = 0; i < size; i++) {
285 Argument argument = (Argument) arguments.get(i);
286 ArgumentControl argument_control = new ArgumentControl(argument);
287 total_height = total_height - argument_control.getPreferredSize().height;
288 central_pane.add(argument_control);
289 }
290 if(total_height > 0) {
291 JPanel filler = new JPanel();
292 filler.setPreferredSize(new Dimension(100, total_height));
293 filler.setSize(new Dimension(100, total_height));
294 central_pane.add(filler);
295 }
296 }
297 /** Method to retrieve a phrase from the dictionary based on a key.
298 * @param key A <strong>String</strong> used to find the correct phrase.
299 * @param args A <strong>String[]</strong> of arguments used in formatting and filling out the phrase.
300 * @return A <strong>String</strong> containing the correct phrase with the correct formatting.
301 */
302 private String get(String key) {
303 return get(key, null);
304 }
305 /** Method to retrieve a phrase from the dictionary based on a key.
306 * @param key A <strong>String</strong> used to find the correct phrase.
307 * @return A <strong>String</strong> containing the correct phrase with the correct formatting.
308 */
309 private String get(String key, String args[]) {
310 if(key.indexOf(".") == -1) {
311 key = "CDM.ArgumentConfiguration." + key;
312 }
313 return gatherer.dictionary.get(key, args);
314 }
315 /** This class encapsulates all the technical difficulty of creating a specific control based on an Argument. */
316 private class ArgumentControl
317 extends JPanel {
318 /** The Argument this control will be based on. */
319 private Argument argument = null;
320 /** One of a possible two buttons available for adding to this control. */
321 private JButton one = null;
322 /** The second of two buttons available for adding to this control. */
323 private JButton two = null;
324 /** A checkbox to allow enabling or diabling of this Argument. */
325 private JCheckBox enabled = null;
326 /** Some form of editor component, such as a JComboBox or JTextField, used to set parameters to an Argument. */
327 private JComponent value = null;
328 /** Can be used in place of the other editor components if a list is required. */
329 private JList list = null;
330 /** Constructor.
331 * @param argument The <strong>Argument</strong> this control will be built around.
332 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl.AddListener
333 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl.EnabledListener
334 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl.HierarchyListener
335 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl.ListOption
336 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl.RemoveListener
337 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl.ToolTipUpdater
338 */
339 public ArgumentControl(Argument argument) {
340 this.argument = argument;
341 String tip = "<html>" + argument.getDesc() + "</html>";
342 tip = Utility.formatHTMLWidth(tip, 60);
343 // If this is the first control, there is no history.
344 if(previous_owner == null) {
345 previous_owner = argument.getOwner();
346 addHeader(previous_owner, Color.white);
347 }
348 // Otherwise if the owner of the control has changed since the last argument, toggle the colouring of the control.
349 else if(previous_owner != argument.getOwner()) {
350 coloured = !coloured;
351 previous_owner = argument.getOwner();
352 addHeader(previous_owner, (coloured ? Gatherer.config.getColor("coloring.collection_tree_background", false) : Color.white));
353 }
354 // Create
355 if(coloured) {
356 setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false));
357 }
358 else {
359 setBackground(Color.white);
360 }
361 JLabel owner_label = new JLabel(argument.getOwner());
362 owner_label.setOpaque(false);
363 JLabel label = new JLabel(argument.getName());
364 label.setOpaque(false);
365 label.setPreferredSize(LABEL_SIZE);
366 label.setToolTipText(tip);
367 enabled = new JCheckBox(argument.getName());
368 enabled.setOpaque(false);
369 enabled.setPreferredSize(LABEL_SIZE);
370 enabled.setToolTipText(tip);
371 JPanel inner_pane = new JPanel();
372 inner_pane.setOpaque(false);
373 String existing_value = argument.getValue();
374 String default_value = argument.getDefaultValue();
375 switch(argument.getType()) {
376 case Argument.ENUM:
377 // Build an option model, wrapping each entry of the list table.
378 HashMap arg_list = argument.getList();
379 ArrayList options_model = new ArrayList();
380 Iterator it = arg_list.keySet().iterator();
381 while(it.hasNext()) {
382 String key = (String) it.next();
383 options_model.add(new ListOption(key, (String)arg_list.get(key)));
384 }
385 Collections.sort(options_model);
386 value = new JComboBox(options_model.toArray());
387 ((JComboBox)value).addActionListener(new ToolTipUpdater());
388 if(existing_value != null) {
389 // Select the correct value. Since they're all text strings we better iterate to be safe.
390 selectValue((JComboBox)value, existing_value);
391 }
392 else if(default_value != null) {
393 // Same as above except for default value.
394 selectValue((JComboBox)value, default_value);
395 }
396 break;
397 case Argument.FLAG:
398 // Only need the check box.
399 break;
400 case Argument.HIERARCHY:
401 value = new JComboBox(gatherer.c_man.msm.getAssignedElements(true));
402 /** @TODO - figure out a smarter way of allowing Greenstone extracted metadata to be selected. */
403 ((JComboBox)value).setEditable(true);
404 ((JComboBox)value).addItemListener(new HierarchyListener());
405 // Now ensure we have the existing value or default value selected if either exist.
406 if(existing_value != null) {
407 selectValue((JComboBox)value, existing_value);
408 }
409 else if(default_value != null) {
410 selectValue((JComboBox)value, default_value);
411 }
412 break;
413 case Argument.INTEGER:
414 case Argument.STRING:
415 // Use a standard text field
416 if(existing_value != null) {
417 value = new JTextField(existing_value);
418 }
419 else {
420 if(default_value != null) {
421 value = new JTextField(default_value);
422 }
423 // Special test just for the hfile field.
424 else if(argument.getName().equals("hfile")) {
425 // Work through previous controls looking for the metadata one.
426 for(int i = 0; i < central_pane.getComponentCount(); i++) {
427 Object object = central_pane.getComponent(i);
428 if(object instanceof ArgumentControl) {
429 ArgumentControl control = (ArgumentControl) object;
430 if(control.toString().equals("metadata")) {
431 Object temp = control.getValue();
432 if(temp != null) {
433 value = new JTextField(temp.toString() + ".txt");
434 }
435 }
436
437 }
438 }
439 }
440 else {
441 value = new JTextField();
442 }
443 }
444 break;
445 case Argument.LANGUAGE:
446 value = new JComboBox(manager.languages.getLanguageCodes().toArray());
447 // Now ensure we have the existing value or default value selected if either exist.
448 Language selected = null;
449 if(existing_value != null) {
450 selected = manager.languages.getLanguage(existing_value, false);
451 }
452 else if(default_value != null) {
453 selected = manager.languages.getLanguage(default_value, false);
454 }
455 if(selected != null) {
456 ((JComboBox)value).setSelectedItem(selected);
457 }
458 break;
459 case Argument.METADATA:
460 value = new JComboBox(gatherer.c_man.msm.getAssignedElements());
461 /** @TODO - figure out a smarter way of allowing Greenstone extracted metadata to be selected. */
462 ((JComboBox)value).setEditable(true);
463 // Now ensure we have the existing value or default value selected if either exist.
464 if(existing_value != null) {
465 selectValue((JComboBox)value, existing_value);
466 }
467 else if(default_value != null) {
468 selectValue((JComboBox)value, default_value);
469 }
470 break;
471 case Argument.METADATUM:
472 // Comma separated metadata values.
473 ArrayList values = argument.getValues();
474 value = new JComboBox(gatherer.c_man.msm.getAssignedElements());
475 DefaultListModel model = new DefaultListModel();
476 list = new JList(model);
477 list.setVisibleRowCount(3);
478 for(int i = 0; i < values.size(); i++) {
479 model.addElement(values.get(i));
480 }
481 one = new JButton(get("Add"));
482 one.addActionListener(new AddListener((JComboBox)value, list));
483 two = new JButton(get("Remove"));
484 two.addActionListener(new RemoveListener(list));
485 if(argument.getValues().size() > 0 || argument.isRequired()) {
486 enabled.setSelected(true);
487 list.setBackground(Color.white);
488 list.setEnabled(true);
489 one.setEnabled(true);
490 two.setEnabled(true);
491 value.setEnabled(true);
492 }
493 else {
494 enabled.setSelected(false);
495 list.setBackground(Color.lightGray);
496 list.setEnabled(false);
497 one.setEnabled(false);
498 two.setEnabled(false);
499 value.setEnabled(false);
500 }
501 break;
502 }
503 // Enable or disable as necessary.
504 if(argument.isRequired() || argument.isAssigned()) {
505 enabled.setSelected(true);
506 if(value != null) {
507 value.setOpaque(true);
508 value.setBackground(Color.white);
509 value.setEnabled(true);
510 }
511 }
512 else {
513 enabled.setSelected(false);
514 if(value != null) {
515 value.setOpaque(true);
516 value.setBackground(Color.lightGray);
517 value.setEnabled(false);
518 }
519 }
520 // Listener
521 if(value != null && !argument.isRequired()) {
522 enabled.addActionListener(new EnabledListener(one, two, list, value));
523 }
524 // Layout
525 if(list == null) {
526 enabled.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
527 inner_pane.setLayout(new BorderLayout());
528 if(argument.isRequired()) {
529 inner_pane.add(label, BorderLayout.WEST);
530 }
531 else {
532 inner_pane.add(enabled, BorderLayout.WEST);
533 }
534 if(value != null) {
535 inner_pane.add(value, BorderLayout.CENTER);
536 }
537 }
538 else {
539 JPanel control_pane = new JPanel(new GridLayout(2,1));
540 control_pane.add(enabled);
541 control_pane.add(value);
542 control_pane.setOpaque(false);
543
544 JPanel left_pane = new JPanel(new BorderLayout());
545 left_pane.add(control_pane, BorderLayout.CENTER);
546 left_pane.add(one, BorderLayout.SOUTH);
547 left_pane.setOpaque(false);
548
549 JPanel right_pane = new JPanel(new BorderLayout());
550 right_pane.add(new JScrollPane(list), BorderLayout.CENTER);
551 right_pane.add(two, BorderLayout.SOUTH);
552 right_pane.setOpaque(false);
553
554 inner_pane.setLayout(new GridLayout(1,2));
555 inner_pane.add(left_pane);
556 inner_pane.add(right_pane);
557 }
558 setLayout(new BorderLayout());
559 //add(owner_label, BorderLayout.NORTH);
560 add(inner_pane, BorderLayout.CENTER);
561 }
562 public Object getValue() {
563 if(value instanceof JComboBox) {
564 return ((JComboBox)value).getSelectedItem();
565 }
566 else if(value instanceof JTextField) {
567 return ((JTextField)value).getText();
568 }
569 return null;
570 }
571 /** Identifies this control by returning the name of the Argument it is based on.
572 * @return The name of the Argument as a <strong>String</strong>.
573 * @see org.greenstone.gatherer.cdm.Argument
574 */
575 public String toString() {
576 return argument.getName();
577 }
578 /** Updates the enwrapped Argument using the values provided by the controls.
579 * @return <i>true</i> if the update was successful, <i>false</i> otherwise.
580 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl.ListOption
581 * @see org.greenstone.gatherer.cdm.Language
582 * @see org.greenstone.gatherer.msm.ElementWrapper
583 */
584 public boolean updateArgument() {
585 if(enabled.isSelected() || argument.isRequired()) {
586 argument.setAssigned(true);
587 String result = null;
588 switch(argument.getType()) {
589 case Argument.ENUM:
590 ListOption option = (ListOption)((JComboBox)value).getSelectedItem();
591 result = option.getValue();
592 if(result.length() > 0) {
593 argument.setValue(result);
594 }
595 else {
596 if(argument.isRequired()) {
597 String args[] = new String[1];
598 args[0] = argument.getName();
599 JOptionPane.showMessageDialog(self, get("Required_Argument", args), get("Error_Title"), JOptionPane.ERROR_MESSAGE);
600 args = null;
601 return false;
602 }
603 else {
604 argument.setValue(null);
605 }
606 }
607 return true;
608 case Argument.FLAG:
609 // Should have already been handled above.
610 return true;
611 case Argument.INTEGER:
612 result = ((JTextField)value).getText();
613 if(result.length() > 0) {
614 // Test if the value entered is a valid int.
615 try {
616 int x = Integer.parseInt(result);
617 }
618 catch(NumberFormatException nfe) {
619 String args[] = new String[2];
620 args[0] = argument.getName();
621 args[1] = result;
622 JOptionPane.showMessageDialog(self, get("Bad_Integer", args), get("Error_Title"), JOptionPane.ERROR_MESSAGE);
623 args = null;
624 return false;
625 }
626 argument.setValue(result);
627 }
628 else {
629 if(argument.isRequired()) {
630 String args[] = new String[1];
631 args[0] = argument.getName();
632 JOptionPane.showMessageDialog(self, get("Required_Argument", args), get("Error_Title"), JOptionPane.ERROR_MESSAGE);
633 args = null;
634 return false;
635 }
636 else {
637 argument.setValue(null);
638 }
639 }
640 return true;
641 case Argument.LANGUAGE:
642 Language language = (Language) ((JComboBox)value).getSelectedItem();
643 argument.setValue(language.getCode());
644 // Kinda lucked out here. Its impossible not to choose an entry from these comboboxes as they are restricted.
645 return true;
646 case Argument.METADATA:
647 case Argument.HIERARCHY:
648 argument.setValue(((JComboBox)value).getSelectedItem().toString());
649 // Kinda lucked out here. Its impossible not to choose an entry from these comboboxes as they are restricted.
650 return true;
651 case Argument.METADATUM:
652 DefaultListModel model = (DefaultListModel)list.getModel();
653 ArrayList values = new ArrayList();
654 for(int i = 0; i < model.size(); i++) {
655 values.add(model.get(i));
656 }
657 argument.setValues(values);
658 return true;
659 case Argument.STRING:
660 result = ((JTextField)value).getText();
661 if(result.length() > 0) {
662 argument.setValue(result);
663 }
664 else {
665 if(argument.isRequired()) {
666 String args[] = new String[1];
667 args[0] = argument.getName();
668 JOptionPane.showMessageDialog(self, get("Required_Argument", args), get("Error_Title"), JOptionPane.ERROR_MESSAGE);
669 return false;
670 }
671 else {
672 argument.setValue(null);
673 }
674 }
675 return true;
676 }
677 return false;
678 }
679 else {
680 argument.setAssigned(false);
681 return true;
682 }
683 }
684 /** Method to ensure that a certain value is selected, if it exists within that combobox to begin with.
685 * @param combobox The <strong>JComboBox</strong> whose selection we are trying to preset.
686 * @param target The desired value of the selection as a <strong>String</strong>.
687 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl.ListOption
688 * @see org.greenstone.gatherer.msm.ElementWrapper
689 */
690 public void selectValue(JComboBox combobox, String target) {
691 for(int i = 0; i < combobox.getItemCount(); i++) {
692 Object object = combobox.getItemAt(i);
693 if(object instanceof ListOption) {
694 ListOption lo = (ListOption) object;
695 if(lo.getValue().equals(target)) {
696 combobox.setSelectedIndex(i);
697 return;
698 }
699 }
700 else if(object instanceof ElementWrapper) {
701 if(object.toString().equals(target)) {
702 combobox.setSelectedIndex(i);
703 return;
704 }
705 }
706 }
707 }
708 /** Forces the control into an 'enabled' mode. */
709 public void setEnabled() {
710 enabled.setSelected(true);
711 }
712 /** Explicitly sets the value of a JTextField type control to the given String.
713 * @param value_str The new value of the control as a <strong>String</strong>.
714 */
715 public void setValue(String value_str) {
716 ((JTextField)value).setText(value_str);
717 }
718 /** Listener which adds entries to a list from a combobox when fired. */
719 private class AddListener
720 implements ActionListener {
721 /** The model behind the target list. */
722 private DefaultListModel model = null;
723 /** The source for data to be added to the list. */
724 private JComboBox source = null;
725 /** The list to add data to. */
726 private JList target = null;
727 /** Constructor.
728 * @param source A <strong>JComboBox</strong> which serves as the source for data.
729 * @param target A <strong>JList</strong> which serves as the target for data.
730 */
731 public AddListener(JComboBox source, JList target) {
732 this.model = (DefaultListModel) target.getModel();
733 this.source = source;
734 this.target = target;
735 }
736 /** When the add button is clicked, we attempt to add the selected metadata from the source into the target.
737 * @param event An <strong>ActionEvent</strong> containing information about the event.
738 * @see org.greenstone.gatherer.msm.ElementWrapper
739 */
740 public void actionPerformed(ActionEvent event) {
741 ElementWrapper element = (ElementWrapper) source.getSelectedItem();
742 String name = element.toString();
743 if(!model.contains(name)) {
744 boolean found = false;
745 int index = 0;
746 while(!found && index < model.size()) {
747 String sibling = (String) model.get(index);
748 if(name.compareTo(sibling) < 0) {
749 model.add(index, name);
750 found = true;
751 }
752 else {
753 index++;
754 }
755 }
756 if(!found) {
757 model.addElement(name);
758 }
759 }
760 }
761 }
762 /** Listens for actions apon the enable checkbox, and if detected enables or diables control appropriately. */
763 private class EnabledListener
764 implements ActionListener {
765 /** One of two possible buttons that might have their enabled state changed by this listener. */
766 private JButton one = null;
767 /** One of two possible buttons that might have their enabled state changed by this listener. */
768 private JButton two = null;
769 /** An editor component, such as a JComboBox or JTextField, that might have its enabled state changed by this listener. */
770 private JComponent target = null;
771 /** A list which might have its enabled state changed by this listener. */
772 private JList list = null;
773 /** Constructor.
774 * @param one A <strong>JButton</strong> whose enabled state is determined by the listener, or <i>null</i> if no button.
775 * @param two A <strong>JButton</strong> whose enabled state is determined by the listener, or <i>null</i> if no button.
776 * @param list A <strong>JList</strong> whose enabled state is determined by the listener, or <i>null</i> if no list.
777 * @param list A <strong>JComponent</strong> whose enabled state is determined by the listener, or <i>null</i> if no component.
778 */
779 public EnabledListener(JButton one, JButton two, JList list, JComponent target) {
780 this.list = list;
781 this.one = one;
782 this.target = target;
783 this.two = two;
784 }
785 /** 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.
786 * @param event An <strong>ActionEvent</strong> containing information about the click.
787 */
788 public void actionPerformed(ActionEvent event) {
789 JCheckBox source = (JCheckBox)event.getSource();
790 if(source.isSelected()) {
791 target.setBackground(Color.white);
792 target.setEnabled(true);
793 if(one != null && two != null && list != null) {
794 one.setEnabled(true);
795 two.setEnabled(true);
796 list.setBackground(Color.white);
797 list.setEnabled(true);
798 }
799 }
800 else {
801 target.setBackground(Color.lightGray);
802 target.setEnabled(false);
803 if(one != null && two != null && list != null) {
804 one.setEnabled(false);
805 two.setEnabled(false);
806 list.setBackground(Color.lightGray);
807 list.setEnabled(false);
808 }
809 }
810 }
811 }
812 /** If a metadata element is selected that requires an hfile, then this listener defaults that hfile. */
813 private class HierarchyListener
814 implements ItemListener {
815 /** 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.
816 * @param event An <strong>ItemEvent</strong> containing information about the selection.
817 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl
818 * @see org.greenstone.gatherer.valuetree.GValueModel
819 */
820 public void itemStateChanged(ItemEvent event) {
821 // Determine if the selected element represents a hierarchy.
822 Object temp = ((JComboBox)value).getSelectedItem();
823 String filename = temp.toString();
824 // Search for a argument control called hfile and enable and set value.
825 for(int i = 0; i < central_pane.getComponentCount(); i++) {
826 Object object = central_pane.getComponent(i);
827 if(object instanceof ArgumentControl) {
828 ArgumentControl control = (ArgumentControl) object;
829 if(control.toString().equals("hfile")) {
830 control.setValue(filename + ".txt");
831 control.setEnabled(true);
832 }
833 }
834 }
835 }
836 }
837 /** 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. */
838 private class ListOption
839 implements Comparable {
840 /** The maximum length of this String version of this item. */
841 private int MAX_DESC = 35;
842 /** The description of the value for this item. */
843 private String description = null;
844 /** A cached value for the text value of this option, as it never changes after the first call to toString(). */
845 private String text = null;
846 /** The value for this item. */
847 private String value = null;
848 /** Constructor.
849 * @param value The value for this item as a <strong>String</strong>.
850 * @param description The description of the value as a <strong>String</strong>.
851 */
852 public ListOption(String value, String description) {
853 this.description = description;
854 this.value = value;
855 }
856 /** Compare two possible ListOption objects for ordering.
857 * @param object The <strong>Object</strong> to compare to.
858 * @return An <i>int</i> indicating order as explained in String.
859 * @see java.lang.String#compareTo
860 */
861 public int compareTo(Object object) {
862 return toString().compareTo(object.toString());
863 }
864 /** Tests two possible ListOption objects for equality. Uses the result from compareTo().
865 * @param The <strong>Object</strong> which may be equal.
866 * @return <i>true</i> if the objects are equal, <i>false</i> otherwise.
867 */
868 public boolean equals(Object object) {
869 return (compareTo(object) == 0);
870 }
871 /** Retrieve the description of this list item.
872 * @return The description as a <strong>String</strong>.
873 */
874 public String getDesc() {
875 return description;
876 }
877 /** Retrieve the value of this list item.
878 * @return The value as a <strong>String</strong>.
879 */
880 public String getValue() {
881 return value;
882 }
883 /** Convert this object into a nice readable String.
884 * @return A <strong>String</strong> representing this object.
885 */
886 public String toString() {
887 if(text == null) {
888 if(description.length() >= MAX_DESC) {
889 text = value + " - " + description.substring(0, MAX_DESC);
890 }
891 else {
892 text = value + " - " + description;
893 }
894 }
895 return text;
896 }
897 }
898 /** Listener which removes entries from a list from a combobox when fired. */
899 private class RemoveListener
900 implements ActionListener {
901 /** The model behind the target list. */
902 private DefaultListModel model = null;
903 /** The list to remove data from. */
904 private JList target = null;
905 /** Constructor.
906 * @param target A <strong>JList</strong>.
907 */
908 public RemoveListener(JList target) {
909 this.model = (DefaultListModel) target.getModel();
910 this.target = target;
911 }
912 /** When the remove button is clicked, we attempt to remove the selected metadata from the target.
913 * @param event An <strong>ActionEvent</strong> containing information about the event.
914 */
915 public void actionPerformed(ActionEvent event) {
916 if(!target.isSelectionEmpty()) {
917 int index = target.getSelectedIndex();
918 model.remove(index);
919 }
920 }
921 }
922 /** Listener that sets the tooltip associated to a combobox to the tooltip relevant to the selected item. */
923 private class ToolTipUpdater
924 implements ActionListener {
925 /** 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.
926 * @param event An <strong>ActionEvent</strong> containing information about the action that fired this call.
927 */
928 public void actionPerformed(ActionEvent event) {
929 JComboBox source = (JComboBox)event.getSource();
930 ListOption lo = (ListOption)source.getSelectedItem();
931 String description = Utility.formatHTMLWidth(lo.getDesc(), 60);
932 source.setToolTipText(description);
933 }
934 }
935 }
936}
Note: See TracBrowser for help on using the repository browser.