source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/cdm/ArgumentControl.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

File size: 21.0 KB
Line 
1package org.greenstone.gatherer.cdm;
2
3import java.awt.*;
4import java.awt.event.*;
5import javax.swing.*;
6import javax.swing.event.*;
7
8import java.io.File;
9import java.util.ArrayList;
10import java.util.Collections;
11import java.util.Map;
12import java.util.Iterator;
13
14import org.greenstone.gatherer.Configuration;
15import org.greenstone.gatherer.DebugStream;
16import org.greenstone.gatherer.Dictionary;
17import org.greenstone.gatherer.Gatherer;
18import org.greenstone.gatherer.gui.GComboBox;
19import org.greenstone.gatherer.util.StaticStrings;
20import org.greenstone.gatherer.util.Utility;
21import org.greenstone.gatherer.metadata.MetadataElement;
22import org.greenstone.gatherer.metadata.MetadataSet;
23import org.greenstone.gatherer.metadata.MetadataSetManager;
24import org.greenstone.gatherer.metadata.MetadataTools;
25
26/** This class encapsulates all the technical difficulty of creating a specific control based on an Argument. */
27public class ArgumentControl
28 extends JPanel {
29
30 static protected Dimension LABEL_SIZE = new Dimension(175, 25);
31 static protected Dimension ROW_SIZE = new Dimension(400, 30);
32 /** The Argument this control will be based on. */
33 private Argument argument = null;
34
35 /** A checkbox to allow enabling or diabling of this Argument. */
36 private JCheckBox enabled = null;
37 /** Some form of editor component, such as a JComboBox or JTextField, used to set parameters to an Argument. */
38 private JComponent value_control = null;
39
40 /** Constructor.
41 */
42 public ArgumentControl(Argument argument, boolean is_enabled, String preset_value) {
43 this.setComponentOrientation(Dictionary.getOrientation());
44 this.argument = argument;
45
46 String tip = "<html>" + argument.getName()+": "+argument.getDescription() + "</html>";
47 tip = Utility.formatHTMLWidth(tip, 80);
48
49 setBackground(Configuration.getColor("coloring.collection_tree_background", false));
50 setBorder(BorderFactory.createEmptyBorder(2,0,2,0));
51 setLayout(new BorderLayout());
52 setPreferredSize(ROW_SIZE);
53
54 // display the name set in the disp option if there is one
55 // otherwise display name option value instead
56 String dispName = argument.getDisplayName();
57 if(dispName.equals("")){
58 dispName = argument.getName();
59 }
60
61 if (argument.isRequired()) {
62 //JLabel label = new JLabel(argument.getName());
63 JLabel label = new JLabel(dispName);
64 label.setComponentOrientation(Dictionary.getOrientation());
65 label.setOpaque(false);
66 label.setPreferredSize(LABEL_SIZE);
67 label.setToolTipText(tip);
68 add(label, BorderLayout.LINE_START);
69 } else {
70 //enabled = new JCheckBox(argument.getName());
71 enabled = new JCheckBox(dispName);
72 enabled.setComponentOrientation(Dictionary.getOrientation());
73 enabled.setOpaque(false);
74 enabled.setPreferredSize(LABEL_SIZE);
75 enabled.setToolTipText(tip);
76 add(enabled, BorderLayout.LINE_START);
77 }
78
79 String initial_value;
80 if (preset_value != null && !preset_value.equals("")) {
81 initial_value = preset_value;
82 }
83 else {
84 initial_value = argument.getValue();
85 }
86 if (initial_value == null || initial_value.equals("")) {
87 initial_value = argument.getDefaultValue();
88 }
89 if (initial_value == null) {
90 initial_value = "";
91 }
92
93 switch(argument.getType()) {
94 case Argument.ENUM_STRING:
95 case Argument.ENUM:
96 ArrayList option_list = argument.getOptions();
97 boolean editable = false;
98 if (argument.getType() == Argument.ENUM_STRING) {
99 editable = true;
100 }
101 value_control = new GComboBox(option_list.toArray(), editable, false);
102 value_control.setComponentOrientation(Dictionary.getOrientation());
103
104 boolean found = selectValue((JComboBox)value_control, initial_value); // also sets the tooltip
105 if (!found && argument.getType() == Argument.ENUM_STRING) {
106 // Its a custom item
107 ((JComboBox) value_control).addItem(initial_value);
108 ((JComboBox) value_control).setSelectedItem(initial_value);
109 }
110
111 ((JComboBox)value_control).addActionListener(new ToolTipUpdater());
112 break;
113
114 case Argument.FLAG:
115 // Only need the check box.
116 break;
117
118 case Argument.INTEGER:
119 // Build a spinner
120 int initial_int=0;
121 // If there was an original value, set it.
122 try {
123 initial_int = Integer.parseInt(initial_value);
124 } catch (Exception error) {
125 DebugStream.println("ArgumentControl Error: "+error);
126 }
127 if (initial_int < argument.getMinimum()) {
128 initial_int = argument.getMinimum();
129 } else if (initial_int > argument.getMaximum()) {
130 initial_int = argument.getMaximum();
131 }
132
133 JSpinner spinner = new JSpinner(new SpinnerNumberModel(initial_int, argument.getMinimum(), argument.getMaximum(), 1));
134 spinner.setComponentOrientation(Dictionary.getOrientation());
135 // And remember it
136 value_control = spinner;
137 break;
138
139 case Argument.REGEXP:
140 case Argument.STRING:
141 value_control = new JTextField(initial_value);
142 value_control.setComponentOrientation(Dictionary.getOrientation());
143 break;
144
145 case Argument.LANGUAGE:
146 value_control = new GComboBox(CollectionDesignManager.language_manager.getLanguageCodes().toArray(), false);
147 value_control.setComponentOrientation(Dictionary.getOrientation());
148 // we want to display the language name not the code
149 ((JComboBox)value_control).setRenderer(new LanguageListCellRenderer());
150 // Now ensure we have the existing value or default value selected if either exist in our known languages
151 String lang_name = CollectionDesignManager.language_manager.getLanguageName(initial_value);
152 if (lang_name != null) {
153 ((JComboBox)value_control).setSelectedItem(initial_value);
154 }
155 break;
156
157 case Argument.METADATA:
158 value_control = new GComboBox(MetadataSetManager.getEveryMetadataSetElement(), false);
159 value_control.setComponentOrientation(Dictionary.getOrientation());
160 // Editable for advanced modes (allows things like dc.Title,ex.Title)
161 if (Configuration.getMode() > Configuration.ASSISTANT_MODE) {
162 ((JComboBox) value_control).setEditable(true);
163 }
164 // Now ensure we have the existing value or default value selected if either exist.
165 String existing_value = preset_value;
166 if (existing_value == null || existing_value.equals("")) {
167 existing_value = argument.getValue();
168 }
169 if (existing_value == null || existing_value.equals("")) {
170 // try default value
171 String default_value = argument.getDefaultValue();
172 if (default_value != null) {
173 // if no namespace for default value, add ex.
174 // won't work if we want to set a non-metadata value
175 if (MetadataTools.getMetadataSetNamespace(default_value).equals("")) {
176 default_value = StaticStrings.EXTRACTED_NAMESPACE+default_value;
177 }
178 existing_value = default_value;
179 }
180 }
181
182 if (existing_value != null && !existing_value.equals("")) {
183
184 found = selectValue((JComboBox) value_control, existing_value);
185 // It's possible that this is a custom value and so doesn't exist in the combobox
186 if (!found) {
187 // If so, add it then select it
188 ((JComboBox) value_control).addItem(existing_value);
189 ((JComboBox) value_control).setSelectedItem(existing_value);
190 }
191 }
192 break;
193
194 case Argument.METADATA_SET_NAMESPACE:
195 value_control = new JComboBox();
196 value_control.setComponentOrientation(Dictionary.getOrientation());
197 // !! Hack for exploding metadata databases: add the (empty) exploded metadata set
198 File exploded_metadata_set_file = new File(Gatherer.getGLIMetadataDirectoryPath() + "exp" + StaticStrings.METADATA_SET_EXTENSION);
199 MetadataSet exploded_metadata_set = new MetadataSet(exploded_metadata_set_file);
200 Gatherer.c_man.importMetadataSet(exploded_metadata_set);
201
202 // All the loaded metadata sets except the extracted metadata set are applicable
203 ArrayList metadata_sets = MetadataSetManager.getMetadataSets();
204 for (int i = metadata_sets.size() - 1; i >= 0; i--) {
205 MetadataSet metadata_set = (MetadataSet) metadata_sets.get(i);
206 if (!(metadata_set.getNamespace().equals(MetadataSetManager.EXTRACTED_METADATA_NAMESPACE))) {
207 ((JComboBox)value_control).addItem(metadata_set);
208 }
209 }
210
211 selectValue((JComboBox) value_control, initial_value);
212
213 } // end of switch
214
215 // Enable or disable as necessary.
216 if(argument.isRequired() || argument.isAssigned() || is_enabled) {
217 if (enabled != null) {
218 enabled.setSelected(true);
219 }
220 if(value_control != null) {
221 value_control.setOpaque(true);
222 value_control.setBackground(Color.white);
223 value_control.setEnabled(true);
224 if(value_control instanceof JSpinner) {
225 // Set enabled
226 JComponent c = ((JSpinner)value_control).getEditor();
227 if ( c instanceof JSpinner.DefaultEditor ) {
228 JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c;
229 JFormattedTextField field = editor.getTextField();
230 field.setEditable(true);
231 field.setBackground(Color.white);
232 }
233 }
234 }
235 }
236 else {
237 if (enabled != null) {
238 enabled.setSelected(false);
239 }
240 if(value_control != null) {
241 value_control.setOpaque(true);
242 value_control.setBackground(Color.lightGray);
243 value_control.setEnabled(false);
244 if(value_control instanceof JSpinner) {
245 // Set enabled
246 JComponent c = ((JSpinner)value_control).getEditor();
247 if ( c instanceof JSpinner.DefaultEditor ) {
248 JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c;
249 JFormattedTextField field = editor.getTextField();
250 field.setEditable(false);
251 field.setBackground(Color.lightGray);
252 }
253 }
254 }
255 }
256
257 // Listener
258 if(value_control != null) {
259 if (argument.getType() != Argument.ENUM && argument.getType() != Argument.ENUM_STRING) {
260 // enums have already set tooltips based on option value
261 value_control.setToolTipText(tip);
262 }
263 add(value_control, BorderLayout.CENTER);
264 if (!argument.isRequired()) {
265 enabled.addActionListener(new EnabledListener(value_control));
266 }
267 }
268 }
269
270 public Argument getArgument() {
271 return argument;
272 }
273
274 public String getArgumentName() {
275 return argument.getName();
276 }
277
278 public String getValue() {
279 if(value_control == null) {
280 return null;
281 }
282 if (value_control instanceof JSpinner) {
283 return ((JSpinner)value_control).getValue().toString();
284 }
285 if(value_control instanceof JComboBox) {
286 Object selected_item = ((JComboBox)value_control).getSelectedItem();
287 if (selected_item != null) {
288 if (argument.getType() == Argument.METADATA_SET_NAMESPACE) {
289 return ((MetadataSet) selected_item).getNamespace();
290 }
291 if (selected_item instanceof Argument.ArgumentOption) {
292 return ((Argument.ArgumentOption)selected_item).name;
293 }
294 if (selected_item instanceof MetadataElement) {
295 return ((MetadataElement) selected_item).getFullName();
296 }
297 return selected_item.toString();
298 }
299 return null;
300 }
301 if(value_control instanceof JTextField) {
302 return ((JTextField)value_control).getText();
303 }
304 return null;
305 }
306 /** Retrieve the control used for storing values.
307 * @return JComponent
308 */
309 public JComponent getValueControl() {
310 return value_control;
311 }
312
313 public boolean isEnabled() {
314 if (enabled == null) {
315 return true; // always enabled
316 }
317 return enabled.isSelected();
318 }
319
320
321 /** Identifies this control by returning the name of the Argument it is based on.
322 * @return The name of the Argument as a <strong>String</strong>.
323 * @see org.greenstone.gatherer.cdm.Argument
324 */
325 public String toString() {
326 return argument.getName();
327 }
328 /** Updates the enwrapped Argument using the values provided by the controls.
329 * @return <i>true</i> if the update was successful, <i>false</i> otherwise.
330 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.Argument.ArgumentOption
331 * @see org.greenstone.gatherer.cdm.Language
332 */
333 public boolean updateArgument() {
334 if(argument.isRequired() || enabled.isSelected() ) {
335 argument.setAssigned(false);
336 String result = null;
337 switch(argument.getType()) {
338 case Argument.ENUM:
339 Argument.ArgumentOption option = (Argument.ArgumentOption)((JComboBox)value_control).getSelectedItem();
340 // its impossible not to choose an entry
341 argument.setValue(option.name);
342 argument.setAssigned(true);
343 return true;
344 case Argument.ENUM_STRING:
345 Object new_value_raw = ((JComboBox)value_control).getSelectedItem();
346 if (new_value_raw instanceof Argument.ArgumentOption) {
347 argument.setValue(((Argument.ArgumentOption)new_value_raw).name);
348 } else {
349 // have entered a new string
350 String new_value = new_value_raw.toString();
351 if (new_value.length() > 0) {
352 argument.setValue(new_value);
353 }
354 else {
355 String args[] = new String[1];
356 args[0] = argument.getName();
357 if(argument.isRequired()) {
358 JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.Required_Argument", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE);
359 }
360 // They've left the field blank
361 else {
362 JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.No_Value", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE);
363 argument.setValue(null);
364 }
365 args = null;
366 return false;
367 }
368 }
369 argument.setAssigned(true);
370 return true;
371 case Argument.FLAG:
372 // Should have already been handled above.
373 argument.setAssigned(true);
374 return true;
375 case Argument.INTEGER:
376 result = ((JSpinner)value_control).getValue().toString();
377 if(result.length() > 0) {
378 // Test if the value entered is a valid int.
379 try {
380 int x = Integer.parseInt(result);
381 }
382 catch(NumberFormatException nfe) {
383 String args[] = new String[2];
384 args[0] = argument.getName();
385 args[1] = result;
386 JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.Bad_Integer", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE);
387 args = null;
388 return false;
389 }
390 argument.setValue(result);
391 }
392 else {
393 String args[] = new String[1];
394 args[0] = argument.getName();
395 if(argument.isRequired()) {
396 JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.Required_Argument", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE);
397 }
398 // They've left the field blank
399 else {
400 JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.No_Value", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE);
401 argument.setValue(null);
402 }
403 args = null;
404 return false;
405 }
406 argument.setAssigned(true);
407 return true;
408 case Argument.LANGUAGE:
409 String language = (((JComboBox)value_control).getSelectedItem()).toString();
410 argument.setValue(language);
411 // Kinda lucked out here. Its impossible not to choose an entry from these comboboxes as they are restricted.
412 argument.setAssigned(true);
413 return true;
414 case Argument.METADATA:
415 String meta_value = (((JComboBox)value_control).getSelectedItem()).toString();
416 if(meta_value.length() > 0) {
417
418 argument.setValue(meta_value);
419 }
420 else {
421 String args[] = new String[1];
422 args[0] = argument.getName();
423 if(argument.isRequired()) {
424 JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.Required_Argument", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE);
425 }
426 // They've left the field blank
427 else {
428 JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.No_Value", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE);
429 argument.setValue(null);
430 }
431 args = null;
432 return false;
433 }
434
435 argument.setAssigned(true);
436 return true;
437 case Argument.REGEXP:
438 case Argument.STRING:
439 result = ((JTextField)value_control).getText();
440 if(result.length() > 0) {
441 argument.setValue(result);
442 }
443 else {
444 String args[] = new String[1];
445 args[0] = argument.getName();
446 if(argument.isRequired()) {
447 JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.Required_Argument", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE);
448 }
449 // They've left the field blank
450 else {
451 JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.No_Value", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE);
452 argument.setValue(null);
453 }
454 args = null;
455 return false;
456 }
457 argument.setAssigned(true);
458 return true;
459 }
460 return false;
461 }
462 else {
463 argument.setAssigned(false);
464 return true;
465 }
466 }
467
468
469 public boolean updateArgument(boolean checkRequired) {
470
471
472 if (checkRequired){
473 return updateArgument();
474 }
475 else{
476 if (argument.getType() == Argument.STRING){
477 String result = ((JTextField)value_control).getText();
478
479 if(result.length() > 0) {
480 argument.setValue(result);
481 argument.setAssigned(true);
482 }
483 }
484 }
485
486 return true;
487
488 }
489
490
491
492 /** Method to ensure that a certain value is selected, if it exists within that combobox to begin with.
493 * @param combobox The <strong>JComboBox</strong> whose selection we are trying to preset.
494 * @param target The desired value of the selection as a <strong>String</strong>.
495 * @return true if the item was found and selected, false otherwise
496 * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.Argument.ArgumentOption
497 */
498 public static boolean selectValue(JComboBox combobox, String target)
499 {
500 for (int i = 0; i < combobox.getItemCount(); i++) {
501 Object object = combobox.getItemAt(i);
502
503 if (object instanceof Argument.ArgumentOption) {
504 Argument.ArgumentOption opt = (Argument.ArgumentOption) object;
505 if (opt.name.startsWith(target)) {
506 combobox.setSelectedIndex(i);
507 combobox.setToolTipText(opt.getToolTip());
508 return true;
509 }
510 }
511 else if (object instanceof MetadataElement) {
512 if(((MetadataElement)object).getFullName().equals(target)) {
513 combobox.setSelectedIndex(i);
514 return true;
515 }
516 }
517 else if (object.toString().equals(target)) {
518 combobox.setSelectedIndex(i);
519 return true;
520 }
521 }
522
523 return false;
524 }
525
526
527 /** Forces the control into an 'enabled' mode. */
528 public void setEnabled() {
529 enabled.setSelected(true);
530 }
531 /** Explicitly sets the value of a JTextField type control to the given String.
532 * @param value_str The new value of the control as a <strong>String</strong>.
533 */
534 public void setValue(String value_str) {
535 ((JTextField)value_control).setText(value_str);
536 }
537
538 /** Listens for actions apon the enable checkbox, and if detected enables or diables control appropriately. */
539 private class EnabledListener
540 implements ActionListener {
541 /** An editor component, such as a JComboBox or JTextField, that might have its enabled state changed by this listener. */
542 private JComponent target = null;
543
544 /** Constructor. */
545 public EnabledListener(JComponent target) {
546 this.target = target;
547 }
548
549 /** 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.
550 * @param event An <strong>ActionEvent</strong> containing information about the click.
551 */
552 public void actionPerformed(ActionEvent event) {
553 if (this.target == null) {
554 return;
555 }
556 JCheckBox source = (JCheckBox)event.getSource();
557 if(source.isSelected()) {
558 target.setBackground(Color.white);
559 target.setEnabled(true);
560 }
561 else {
562 target.setBackground(Color.lightGray);
563 target.setEnabled(false);
564 }
565 // Special case of stupid JSpinners who don't let their backgrounds change properly.
566 if(target instanceof JSpinner) {
567 JSpinner spinner = (JSpinner) target;
568 JComponent c = spinner.getEditor();
569 if ( c instanceof JSpinner.DefaultEditor ) {
570 JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c;
571 JFormattedTextField field = editor.getTextField();
572 field.setEditable(source.isSelected());
573 if(source.isSelected()) {
574 field.setBackground(Color.white);
575 }
576 else {
577 field.setBackground(Color.lightGray);
578 }
579 }
580 }
581 }
582 }
583
584
585 /** Listener that sets the tooltip associated to a combobox to the tooltip relevant to the selected item. */
586 private class ToolTipUpdater
587 implements ActionListener {
588 /** 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.
589 * @param event An <strong>ActionEvent</strong> containing information about the action that fired this call.
590 */
591 public void actionPerformed(ActionEvent event) {
592 JComboBox source = (JComboBox)event.getSource();
593 Object object = source.getSelectedItem();
594 if(object instanceof Argument.ArgumentOption) {
595 Argument.ArgumentOption opt = (Argument.ArgumentOption)object;
596 if(opt != null) {
597 source.setToolTipText(opt.getToolTip());
598 }
599 else {
600 source.setToolTipText(StaticStrings.EMPTY_STR);
601 }
602 }
603 }
604 }
605}
Note: See TracBrowser for help on using the repository browser.