/** *######################################################################### * * A component of the Gatherer application, part of the Greenstone digital * library suite from the New Zealand Digital Library Project at the * University of Waikato, New Zealand. * * Author: John Thompson, Greenstone Digital Library, University of Waikato * * Copyright (C) 1999 New Zealand Digital Library Project * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *######################################################################## */ package org.greenstone.gatherer.cdm; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import org.greenstone.gatherer.Configuration; import org.greenstone.gatherer.DebugStream; import org.greenstone.gatherer.Dictionary; import org.greenstone.gatherer.Gatherer; import org.greenstone.gatherer.gui.GComboBox; import org.greenstone.gatherer.gui.GLIButton; import org.greenstone.gatherer.gui.ModalDialog; import org.greenstone.gatherer.gui.SimpleMenuBar; import org.greenstone.gatherer.metadata.MetadataElement; import org.greenstone.gatherer.metadata.MetadataSetManager; import org.greenstone.gatherer.util.StaticStrings; import org.greenstone.gatherer.util.Utility; /** This class provides us with a dialog box which allows us to edit the arguments of either a Plugin or a Classifier. * @author John Thompson, Greenstone Digital Library, University of Waikato * @version 2.3 * @see org.greenstone.gatherer.cdm.Classifier * @see org.greenstone.gatherer.cdm.Plugin */ public class ArgumentConfiguration extends ModalDialog implements ActionListener { /** The data whose arguments we are editing. */ private ArgumentContainer data = null; /** Argument these argument controls coloured or uncoloured (alternates to indicate inheritance). */ private boolean coloured = false; /** 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). */ private boolean success = false; /** A button to cancel this dialog. */ private JButton cancel = null; /** A button to accept the changes and close the dialog. */ private JButton ok = null; /** A reference to the ourselves so our inner classes can dispose us like a dialog. */ private ArgumentConfiguration self = null; /** The central pane where a list of known arguments is displayed. */ private JPanel central_pane = null; /** The field for entering custom arguments. */ //private JTextField custom = null; /** The panel for the custom arguments */ //private JPanel custom_pane = null; /** The name of the owner of the last argument control. */ private String previous_owner = null; /** The size used for an argument label. */ static final private Dimension LABEL_SIZE = new Dimension(225, 25); /** Size of a list. */ static final private Dimension LIST_SIZE = new Dimension(380, 50); /** The size used for the dialog. */ static final private Dimension SIZE = new Dimension(800, 425); /** Constructor. * @param data The plugin or classifier whose arguments we are configuring, in the form of its supported ArgumentContainer interface. * @see org.greenstone.gatherer.Configuration */ public ArgumentConfiguration(ArgumentContainer data) { super(Gatherer.g_man); this.data = data; this.self = this; // Create setModal(true); setSize(SIZE); setJMenuBar(new SimpleMenuBar("designingacollection")); // can we tell whether we are doing a classifier or plugin, to make the help more specific?? Dictionary.setText(this, "CDM.ArgumentConfiguration.Title"); central_pane = new JPanel(); JPanel content_pane = (JPanel) getContentPane(); // custom_pane = new JPanel(); // String custom_str = data.getCustom(); // if (custom_str != null) { // custom = new JTextField(custom_str); // } // else { // custom = new JTextField(); // } // JLabel custom_label = new JLabel(); // custom_label.setPreferredSize(LABEL_SIZE); // Dictionary.setText(custom_label, "CDM.ArgumentConfiguration.Custom"); JPanel header_pane = new JPanel(); JLabel header = new JLabel(); header.setHorizontalAlignment(JLabel.CENTER); header.setOpaque(true); String args[] = new String[1]; args[0] = data.getName(); Dictionary.setText(header, "CDM.ArgumentConfiguration.Header", args); JPanel button_pane = new JPanel(); cancel = new GLIButton(); cancel.setMnemonic(KeyEvent.VK_C); Dictionary.setBoth(cancel, "General.Cancel", "General.Pure_Cancel_Tooltip"); ok = new GLIButton(); ok.setMnemonic(KeyEvent.VK_O); Dictionary.setBoth(ok, "General.OK", "General.OK_Tooltip"); // Listeners cancel.addActionListener(this); ok.addActionListener(this); ok.addActionListener(CollectionDesignManager.all_change_listener); // Layout // custom_pane.setLayout(new BorderLayout()); // custom_pane.add(custom_label, BorderLayout.WEST); // custom_pane.add(custom, BorderLayout.CENTER); button_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0)); button_pane.setLayout(new GridLayout(1,2)); button_pane.add(ok); button_pane.add(cancel); central_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); central_pane.setLayout(new BoxLayout(central_pane, BoxLayout.Y_AXIS)); content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); content_pane.setLayout(new BorderLayout()); content_pane.add(header, BorderLayout.NORTH); content_pane.add(new JScrollPane(central_pane), BorderLayout.CENTER); content_pane.add(button_pane, BorderLayout.SOUTH); // Now generate a set of controls for each of the arguments. generateControls(); // Display on screen. Dimension screen_size = Configuration.screen_size; setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2); screen_size = null; } /** 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. * @param event An ActionEvent containing pertinant information about the event that fired this call. * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl * @see org.greenstone.gatherer.cdm.ArgumentContainer */ public void actionPerformed(ActionEvent event) { boolean cont = true; if (event.getSource() == ok) { // Clear the current focus to ensure components such as combobox have correctly updated //custom.requestFocus(); // Update the details stored in the data objects arguments. //data.setCustom(custom.getText()); // Loop through each of the controls in the central pane, updating the matching argument as necessary. for(int i = 0; i < central_pane.getComponentCount(); i++) { Component component = central_pane.getComponent(i); if(component instanceof ArgumentControl) { cont = cont && ((ArgumentControl)component).updateArgument(); } } if(cont) { success = true; } } if(cont) { dispose(); } } /** Destructor. */ public void destroy() { cancel = null; central_pane = null; //custom_pane = null; //custom = null; data = null; ok = null; self = null; } /** Method which actually forces the dialog to be shown on screen. * @return true if the user completed configuration and pressed ok, false otherwise. */ public boolean display() { setVisible(true); return success; } private void addHeader(String name, Color color) { JPanel header = new JPanel(); header.setBackground(color); JPanel inner_pane = new JPanel(); inner_pane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(5,5,5,5), BorderFactory.createRaisedBevelBorder())); inner_pane.setBackground(color); JLabel header_label = new JLabel("" + name + ""); header_label.setBackground(Configuration.getColor("coloring.collection_heading_background", false)); header_label.setHorizontalAlignment(JLabel.CENTER); header_label.setOpaque(true); // Layout inner_pane.setLayout(new BorderLayout()); inner_pane.add(header_label, BorderLayout.CENTER); header.setLayout(new BorderLayout()); header.add(inner_pane, BorderLayout.CENTER); central_pane.add(header); } /** 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. * @see org.greenstone.gatherer.cdm.Argument * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl */ private void generateControls() { ArrayList arguments = data.getArguments(true, false); int total_height = 250; int mode = Configuration.getMode(); for(int i = 0; i < arguments.size(); i++) { Argument argument = (Argument) arguments.get(i); if(mode > Configuration.LIBRARIAN_MODE || !(argument.getType() == Argument.REGEXP)) { ArgumentControl argument_control = new ArgumentControl(argument); total_height = total_height - argument_control.getPreferredSize().height; central_pane.add(argument_control); } } // now add in the custom args bit // coloured = !coloured; // Color color = (coloured ? Configuration.getColor("coloring.collection_heading_background", false) : Configuration.getColor("coloring.collection_tree_background", false)); // addHeader(Dictionary.get("CDM.ArgumentConfiguration.Custom_Header"), color); // custom_pane.setBackground(color); // central_pane.add(custom_pane); if(total_height > 0) { JPanel filler = new JPanel(); filler.setPreferredSize(new Dimension(100, total_height)); filler.setSize(new Dimension(100, total_height)); central_pane.add(filler); } } /** This class encapsulates all the technical difficulty of creating a specific control based on an Argument. */ private class ArgumentControl extends JPanel { /** The Argument this control will be based on. */ private Argument argument = null; private Color colour_one = Configuration.getColor("coloring.collection_heading_background", false); private Color colour_two = Configuration.getColor("coloring.collection_tree_background", false); /** One of a possible two buttons available for adding to this control. */ private JButton one = null; /** The second of two buttons available for adding to this control. */ private JButton two = null; /** A checkbox to allow enabling or diabling of this Argument. */ private JCheckBox enabled = null; /** Some form of editor component, such as a JComboBox or JTextField, used to set parameters to an Argument. */ private JComponent value = null; /** Can be used in place of the other editor components if a list is required. */ private JList list = null; /** Constructor. * @param argument The Argument this control will be built around. * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl.AddListener * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl.EnabledListener * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl.HierarchyListener * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl.ListOption * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl.RemoveListener * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl.ToolTipUpdater */ public ArgumentControl(Argument argument) { this.argument = argument; ///ystem.err.println("generating controls for arg "+argument.getName()); String tip = "" + argument.getDescription() + ""; tip = Utility.formatHTMLWidth(tip, 60); // If this is the first control, there is no history. if(previous_owner == null) { ///ystem.err.println("previous owner is null"); previous_owner = argument.getOwner(); addHeader(previous_owner, colour_two); } // Otherwise if the owner of the control has changed since the last argument, toggle the colouring of the control. else if(previous_owner != argument.getOwner()) { ///ystem.err.println("previous owner is different from current owner"); coloured = !coloured; previous_owner = argument.getOwner(); addHeader(previous_owner, (coloured ? colour_one : colour_two)); } // Create if(coloured) { setBackground(colour_one); } else { setBackground(colour_two); } JLabel owner_label = new JLabel(argument.getOwner()); owner_label.setOpaque(false); JLabel label = new JLabel(argument.getName()); label.setOpaque(false); label.setPreferredSize(LABEL_SIZE); label.setToolTipText(tip); enabled = new JCheckBox(argument.getName()); enabled.setOpaque(false); enabled.setPreferredSize(LABEL_SIZE); enabled.setToolTipText(tip); JPanel inner_pane = new JPanel(); inner_pane.setOpaque(false); String existing_value = argument.getValue(); String default_value = argument.getDefaultValue(); switch(argument.getType()) { case Argument.ENUM: // Build an option model, wrapping each entry of the list table. HashMap arg_list = argument.getOptions(); ArrayList options_model = new ArrayList(); Iterator it = arg_list.keySet().iterator(); while(it.hasNext()) { String key = (String) it.next(); options_model.add(new ListOption(key, (String)arg_list.get(key))); } Collections.sort(options_model); value = new GComboBox(options_model.toArray(), false); ((JComboBox)value).addActionListener(new ToolTipUpdater()); if(existing_value != null && existing_value.length() > 0) { // Select the correct value. Since they're all text strings we better iterate to be safe. selectValue((JComboBox)value, existing_value); } else if(default_value != null) { ///ystem.err.println("Default for argument: " + argument.getName()); // Same as above except for default value. selectValue((JComboBox)value, default_value); } break; case Argument.FLAG: // Only need the check box. break; case Argument.HIERARCHY: // I don't think these are used any more... break; case Argument.INTEGER: // Build a spinner int initial_value=0; // If there was an original value, set it. if(existing_value != null && !existing_value.equals("")) { try { initial_value = Integer.parseInt(existing_value); //spinner.setValue(new Integer(existing_value)); } catch (Exception error) { DebugStream.println("ArgumentConfiguration Error: "+error); } } else if (default_value != null && !default_value.equals("")) { try { initial_value = Integer.parseInt(default_value); //spinner.setValue(new Integer(default_value)); } catch (Exception error) { DebugStream.println("ArgumentConfiguration Error: "+error); } } if (initial_value < argument.getMinimum()) { initial_value = argument.getMinimum(); } else if (initial_value > argument.getMaximum()) { initial_value = argument.getMaximum(); } JSpinner spinner = new JSpinner(new SpinnerNumberModel(initial_value, argument.getMinimum(), argument.getMaximum(), 1)); // And remember it value = spinner; break; case Argument.REGEXP: case Argument.STRING: // If there is already a value set for this argument, use it if (existing_value != null && !existing_value.equals("")) { value = new JTextField(existing_value); break; } // Use the default value, if there is one if (default_value != null && !default_value.equals("")) { value = new JTextField(default_value); break; } // // Special test just for the hfile field. // if (argument.getName().equals("hfile")) { // // Work through previous controls looking for the metadata one. // for (int i = 0; i < central_pane.getComponentCount(); i++) { // Object object = central_pane.getComponent(i); // if (object instanceof ArgumentControl) { // ArgumentControl control = (ArgumentControl) object; // if (control.toString().equals("metadata")) { // Object temp = control.getValue(); // if (temp != null) { // value = new JTextField(temp.toString() + ".txt"); // break; // } // } // } // } // } // Blank field value = new JTextField(); break; case Argument.LANGUAGE: value = new GComboBox(CollectionDesignManager.language_manager.getLanguageCodes().toArray(), false); // we want to display the language name not the code ((JComboBox)value).setRenderer(new LanguageListCellRenderer()); // Now ensure we have the existing value or default value selected if either exist in our known languages String lang_name = null; String selected_code = existing_value; if(existing_value != null && !existing_value.equals("")) { lang_name = CollectionDesignManager.language_manager.getLanguageName(existing_value); } if(lang_name == null && default_value != null) { lang_name = CollectionDesignManager.language_manager.getLanguageName(default_value); selected_code = default_value; } if (lang_name != null) { ((JComboBox)value).setSelectedItem(selected_code); } break; case Argument.METADATUM: case Argument.METADATA: value = new GComboBox(MetadataSetManager.getEveryMetadataSetElement(), false); // Editable for advanced modes (allows things like dc.Title,ex.Title) if (Configuration.getMode() > Configuration.ASSISTANT_MODE) { ((JComboBox) value).setEditable(true); } // Now ensure we have the existing value or default value selected if either exist. if (existing_value != null && existing_value.length() > 0) { boolean found = selectValue((JComboBox) value, existing_value); // It's possible that this is a custom value and so doesn't exist in the combobox if (!found) { // If so, add it then select it ((JComboBox) value).addItem(existing_value); ((JComboBox) value).setSelectedItem(existing_value); } } else if (default_value != null) { selectValue((JComboBox) value, default_value); } break; // ---- Special interface for adding and ordering multiple metadata items ---- // Turned off at Ian's request! // case Argument.METADATA: // // Comma separated metadata values. // ArrayList values = argument.getValues(); // value = new GComboBox(MetadataSetManager.getEveryMetadataSetElement(), false); // //((JComboBox)value).setEditable(false); // DefaultListModel model = new DefaultListModel(); // list = new JList(model); // list.setVisibleRowCount(3); // for(int i = 0; i < values.size(); i++) { // model.addElement(values.get(i)); // } // one = new GLIButton(); // one.addActionListener(new AddListener((JComboBox)value, list)); // one.setMnemonic(KeyEvent.VK_A); // Dictionary.setBoth(one, "CDM.ArgumentConfiguration.Add", "CDM.ArgumentConfiguration.Add_Tooltip"); // two = new GLIButton(); // two.addActionListener(new RemoveListener(list)); // two.setMnemonic(KeyEvent.VK_R); // Dictionary.setBoth(two, "CDM.ArgumentConfiguration.Remove", "CDM.ArgumentConfiguration.Remove_Tooltip"); // if(argument.getValues().size() > 0 || argument.isRequired()) { // enabled.setSelected(true); // list.setBackground(Color.white); // list.setEnabled(true); // one.setEnabled(true); // two.setEnabled(true); // value.setEnabled(true); // } // else { // enabled.setSelected(false); // list.setBackground(Color.lightGray); // list.setEnabled(false); // one.setEnabled(false); // two.setEnabled(false); // value.setEnabled(false); // } // break; } // end of switch // Enable or disable as necessary. if(argument.isRequired() || argument.isAssigned()) { enabled.setSelected(true); if(value != null) { value.setOpaque(true); value.setBackground(Color.white); value.setEnabled(true); if(value instanceof JSpinner) { // Set enabled JComponent c = ((JSpinner)value).getEditor(); if ( c instanceof JSpinner.DefaultEditor ) { JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c; JFormattedTextField field = editor.getTextField(); field.setEditable(true); field.setBackground(Color.white); } } } } else { enabled.setSelected(false); if(value != null) { value.setOpaque(true); value.setBackground(Color.lightGray); value.setEnabled(false); if(value instanceof JSpinner) { // Set enabled JComponent c = ((JSpinner)value).getEditor(); if ( c instanceof JSpinner.DefaultEditor ) { JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c; JFormattedTextField field = editor.getTextField(); field.setEditable(false); field.setBackground(Color.lightGray); } } } } // Listener if(value != null && !argument.isRequired()) { enabled.addActionListener(new EnabledListener(one, two, list, value)); } // Layout inner_pane.setLayout(new BorderLayout()); if (argument.isRequired()) { inner_pane.add(label, BorderLayout.WEST); } else { inner_pane.add(enabled, BorderLayout.WEST); } if (list == null) { enabled.setBorder(BorderFactory.createEmptyBorder(0,0,5,0)); if (value != null) { inner_pane.add(value, BorderLayout.CENTER); } } else { JPanel left_pane = new JPanel(new BorderLayout()); left_pane.add(new JLabel(""), BorderLayout.NORTH); left_pane.add(value, BorderLayout.CENTER); left_pane.add(one, BorderLayout.SOUTH); left_pane.setOpaque(false); JPanel right_pane = new JPanel(new BorderLayout()); right_pane.add(new JScrollPane(list), BorderLayout.CENTER); right_pane.add(two, BorderLayout.SOUTH); right_pane.setOpaque(false); JPanel control_pane = new JPanel(new GridLayout(1, 2)); control_pane.add(left_pane); control_pane.add(right_pane); inner_pane.add(control_pane, BorderLayout.CENTER); } setLayout(new BorderLayout()); add(inner_pane, BorderLayout.CENTER); } public Argument getArgument() { return argument; } public Object getValue() { if(value instanceof JComboBox) { return ((JComboBox)value).getSelectedItem(); } else if(value instanceof JTextField) { return ((JTextField)value).getText(); } return null; } /** Identifies this control by returning the name of the Argument it is based on. * @return The name of the Argument as a String. * @see org.greenstone.gatherer.cdm.Argument */ public String toString() { return argument.getName(); } /** Updates the enwrapped Argument using the values provided by the controls. * @return true if the update was successful, false otherwise. * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl.ListOption * @see org.greenstone.gatherer.cdm.Language */ public boolean updateArgument() { if(enabled.isSelected() || argument.isRequired()) { argument.setAssigned(false); String result = null; switch(argument.getType()) { case Argument.ENUM: ListOption option = (ListOption)((JComboBox)value).getSelectedItem(); if(option != null && option.getValue().length() > 0) { argument.setValue(option.getValue()); } else { String args[] = new String[1]; args[0] = argument.getName(); if(argument.isRequired()) { JOptionPane.showMessageDialog(self, Dictionary.get("CDM.ArgumentConfiguration.Required_Argument", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE); } // They've left the field blank else { JOptionPane.showMessageDialog(self, Dictionary.get("CDM.ArgumentConfiguration.No_Value", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE); argument.setValue(null); } args = null; return false; } argument.setAssigned(true); return true; case Argument.FLAG: // Should have already been handled above. argument.setAssigned(true); return true; case Argument.INTEGER: result = ((JSpinner)value).getValue().toString(); if(result.length() > 0) { // Test if the value entered is a valid int. try { int x = Integer.parseInt(result); } catch(NumberFormatException nfe) { String args[] = new String[2]; args[0] = argument.getName(); args[1] = result; JOptionPane.showMessageDialog(self, Dictionary.get("CDM.ArgumentConfiguration.Bad_Integer", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE); args = null; return false; } argument.setValue(result); } else { String args[] = new String[1]; args[0] = argument.getName(); if(argument.isRequired()) { JOptionPane.showMessageDialog(self, Dictionary.get("CDM.ArgumentConfiguration.Required_Argument", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE); } // They've left the field blank else { JOptionPane.showMessageDialog(self, Dictionary.get("CDM.ArgumentConfiguration.No_Value", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE); argument.setValue(null); } args = null; return false; } argument.setAssigned(true); return true; case Argument.LANGUAGE: String language = (((JComboBox)value).getSelectedItem()).toString(); argument.setValue(language); // Kinda lucked out here. Its impossible not to choose an entry from these comboboxes as they are restricted. argument.setAssigned(true); return true; case Argument.METADATUM: case Argument.METADATA: Object new_value_raw = ((JComboBox) value).getSelectedItem(); if (new_value_raw instanceof MetadataElement) { argument.setValue(((MetadataElement) new_value_raw).getFullName()); } else { // But we have to be careful as an arbitary string object could be zero length String new_value = new_value_raw.toString(); ///ystem.err.println("The current value is: " + new_value); if(new_value.length() > 0) { argument.setValue(new_value); } else { String args[] = new String[1]; args[0] = argument.getName(); if(argument.isRequired()) { JOptionPane.showMessageDialog(self, Dictionary.get("CDM.ArgumentConfiguration.Required_Argument", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE); } // They've left the field blank else { JOptionPane.showMessageDialog(self, Dictionary.get("CDM.ArgumentConfiguration.No_Value", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE); argument.setValue(null); } args = null; return false; } } argument.setAssigned(true); return true; // case Argument.METADATA: // DefaultListModel model = (DefaultListModel)list.getModel(); // ArrayList values = new ArrayList(); // for(int i = 0; i < model.size(); i++) { // values.add(model.get(i)); // } // argument.setValues(values); // argument.setAssigned(true); // return true; case Argument.HIERARCHY: // argument.setValue(((JComboBox)value).getSelectedItem().toString()); // // Kinda lucked out here. Its impossible not to choose an entry from these comboboxes as they are restricted. // argument.setAssigned(true); return true; case Argument.REGEXP: case Argument.STRING: result = ((JTextField)value).getText(); if(result.length() > 0) { argument.setValue(result); } else { String args[] = new String[1]; args[0] = argument.getName(); if(argument.isRequired()) { JOptionPane.showMessageDialog(self, Dictionary.get("CDM.ArgumentConfiguration.Required_Argument", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE); } // They've left the field blank else { JOptionPane.showMessageDialog(self, Dictionary.get("CDM.ArgumentConfiguration.No_Value", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE); argument.setValue(null); } args = null; return false; } argument.setAssigned(true); return true; } return false; } else { argument.setAssigned(false); return true; } } /** Method to ensure that a certain value is selected, if it exists within that combobox to begin with. * @param combobox The JComboBox whose selection we are trying to preset. * @param target The desired value of the selection as a String. * @return true if the item was found and selected, false otherwise * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl.ListOption */ public boolean selectValue(JComboBox combobox, String target) { ///ystem.err.println("Searching for the target string: " + target); for(int i = 0; i < combobox.getItemCount(); i++) { Object object = combobox.getItemAt(i); if(object instanceof ListOption) { ListOption lo = (ListOption) object; ///ystem.err.print("/tChecking: " + lo.getValue() + "... "); if(lo.getValue().startsWith(target)) { ///ystem.err.println("Match!"); combobox.setSelectedIndex(i); return true; } /* else { System.err.println("No Match."); } */ } else if (object instanceof MetadataElement) { if(object.toString().equals(target)) { combobox.setSelectedIndex(i); return true; } } } return false; } /** Forces the control into an 'enabled' mode. */ public void setEnabled() { enabled.setSelected(true); } /** Explicitly sets the value of a JTextField type control to the given String. * @param value_str The new value of the control as a String. */ public void setValue(String value_str) { ((JTextField)value).setText(value_str); } // /** Listener which adds entries to a list from a combobox when fired. */ // private class AddListener // implements ActionListener { // /** The model behind the target list. */ // private DefaultListModel model = null; // /** The source for data to be added to the list. */ // private JComboBox source = null; // /** The list to add data to. */ // private JList target = null; // /** Constructor. // * @param source A JComboBox which serves as the source for data. // * @param target A JList which serves as the target for data. // */ // public AddListener(JComboBox source, JList target) { // this.model = (DefaultListModel) target.getModel(); // this.source = source; // this.target = target; // } // /** When the add button is clicked, we attempt to add the selected metadata from the source into the target. // * @param event An ActionEvent containing information about the event. // */ // public void actionPerformed(ActionEvent event) { // ElementWrapper element = (ElementWrapper) source.getSelectedItem(); // String name = element.toString(); // if (!model.contains(name)) { // model.addElement(name); // } // } // } /** Listens for actions apon the enable checkbox, and if detected enables or diables control appropriately. */ private class EnabledListener implements ActionListener { /** One of two possible buttons that might have their enabled state changed by this listener. */ private JButton one = null; /** One of two possible buttons that might have their enabled state changed by this listener. */ private JButton two = null; /** An editor component, such as a JComboBox or JTextField, that might have its enabled state changed by this listener. */ private JComponent target = null; /** A list which might have its enabled state changed by this listener. */ private JList list = null; /** Constructor. * @param one A JButton whose enabled state is determined by the listener, or null if no button. * @param two A JButton whose enabled state is determined by the listener, or null if no button. * @param list A JList whose enabled state is determined by the listener, or null if no list. * @param target A JComponent whose enabled state is determined by the listener, or null if no component. */ public EnabledListener(JButton one, JButton two, JList list, JComponent target) { this.list = list; this.one = one; this.target = target; this.two = two; } /** 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. * @param event An ActionEvent containing information about the click. */ public void actionPerformed(ActionEvent event) { JCheckBox source = (JCheckBox)event.getSource(); if(source.isSelected()) { target.setBackground(Color.white); target.setEnabled(true); if(target instanceof JSpinner) { // Set enabled JComponent c = ((JSpinner)target).getEditor(); if ( c instanceof JSpinner.DefaultEditor ) { JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c; JFormattedTextField field = editor.getTextField(); field.setEditable(true); field.setBackground(Color.white); } } if(one != null && two != null && list != null) { one.setEnabled(true); two.setEnabled(true); list.setBackground(Color.white); list.setEnabled(true); } } else { target.setBackground(Color.lightGray); target.setEnabled(false); if(target instanceof JSpinner) { // Set enabled JComponent c = ((JSpinner)target).getEditor(); if ( c instanceof JSpinner.DefaultEditor ) { JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c; JFormattedTextField field = editor.getTextField(); field.setEditable(false); field.setBackground(Color.lightGray); } } if(one != null && two != null && list != null) { one.setEnabled(false); two.setEnabled(false); list.setBackground(Color.lightGray); list.setEnabled(false); } } } } /** If a metadata element is selected that requires an hfile, then this listener defaults that hfile. */ // private class HierarchyListener // implements ItemListener { // /** 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. // * @param event An ItemEvent containing information about the selection. // * @see org.greenstone.gatherer.cdm.ArgumentConfiguration.ArgumentControl // */ // public void itemStateChanged(ItemEvent event) { // // Determine if the selected element represents a hierarchy. // Object temp = ((JComboBox)value).getSelectedItem(); // String filename = temp.toString(); // // Search for a argument control called hfile and enable and set value. // for(int i = 0; i < central_pane.getComponentCount(); i++) { // Object object = central_pane.getComponent(i); // if(object instanceof ArgumentControl) { // ArgumentControl control = (ArgumentControl) object; // if(control.toString().equals("hfile")) { // control.setValue(filename + ".txt"); // control.setEnabled(true); // } // } // } // } // } /** 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. */ private class ListOption implements Comparable { /** The maximum length of this String version of this item. */ private int MAX_DESC = 65; /** The description of the value for this item. */ private String description = null; /** A cached value for the text value of this option, as it never changes after the first call to toString(). */ private String text = null; /** The value for this item. */ private String value = null; /** Constructor. * @param value The value for this item as a String. * @param description The description of the value as a String. */ public ListOption(String value, String description) { this.description = description; this.value = value; } /** Compare two possible ListOption objects for ordering. * @param object The Object to compare to. * @return An int indicating order as explained in String. * @see java.lang.String#compareTo */ public int compareTo(Object object) { return toString().compareTo(object.toString()); } /** Tests two possible ListOption objects for equality. Uses the result from compareTo(). * @param object The Object which may be equal. * @return true if the objects are equal, false otherwise. */ public boolean equals(Object object) { return (compareTo(object) == 0); } /** Retrieve the description of this list item. * @return The description as a String. */ public String getDesc() { return description; } /** Retrieve the value of this list item. * @return The value as a String. */ public String getValue() { return value; } /** Convert this object into a nice readable String. * @return A String representing this object. */ public String toString() { if(text == null) { if(description.length() >= MAX_DESC) { text = value + StaticStrings.MINUS_CHARACTER + description.substring(0, MAX_DESC) + StaticStrings.TRUNCATED_STRING; } else { text = value + StaticStrings.MINUS_CHARACTER + description; } } return text; } } /** Listener which removes entries from a list from a combobox when fired. */ private class RemoveListener implements ActionListener { /** The model behind the target list. */ private DefaultListModel model = null; /** The list to remove data from. */ private JList target = null; /** Constructor. * @param target A JList. */ public RemoveListener(JList target) { this.model = (DefaultListModel) target.getModel(); this.target = target; } /** When the remove button is clicked, we attempt to remove the selected metadata from the target. * @param event An ActionEvent containing information about the event. */ public void actionPerformed(ActionEvent event) { if(!target.isSelectionEmpty()) { int index = target.getSelectedIndex(); model.remove(index); } } } /** Listener that sets the tooltip associated to a combobox to the tooltip relevant to the selected item. */ private class ToolTipUpdater implements ActionListener { /** 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. * @param event An ActionEvent containing information about the action that fired this call. */ public void actionPerformed(ActionEvent event) { JComboBox source = (JComboBox)event.getSource(); Object object = source.getSelectedItem(); if(object instanceof ListOption) { ListOption lo = (ListOption)object; if(lo != null) { String description = Utility.formatHTMLWidth(lo.getDesc(), 60); source.setToolTipText(description); } else { source.setToolTipText(StaticStrings.EMPTY_STR); } } } } } }