/** *######################################################################### * * 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.gui; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; import org.greenstone.gatherer.Configuration; import org.greenstone.gatherer.cdm.Argument; import org.greenstone.gatherer.util.Utility; import org.greenstone.gatherer.metadata.MetadataSetManager; public class ArgumentControl extends JPanel { static protected Dimension LABEL_SIZE = new Dimension(180, 25); static protected Dimension ROW_SIZE = new Dimension(610, 30); static protected Dimension SPINNER_SIZE = new Dimension(100, 25); static protected String DESCRIPTION_SEP = " + "; protected Argument argument; protected JComponent value_control; protected JCheckBox enabled; public ArgumentControl(Argument argument, boolean enable, String value) { super(); this.argument = argument; String tooltip = Utility.formatHTMLWidth("" + argument.getDescription() + "", 60); setBackground(Configuration.getColor("coloring.collection_tree_background", false)); setBorder(BorderFactory.createEmptyBorder(2,0,2,0)); setLayout(new BorderLayout()); setPreferredSize(ROW_SIZE); setMaximumSize(ROW_SIZE); // Try to determine what the value_controls value should be. if(value == null) { value = argument.getDefaultValue(); } // Create a correct value control based on the argument provided. switch(argument.getType()) { case Argument.ENUM: case Argument.METADATUM: JComboBox combobox = new JComboBox(); combobox.setEnabled(enable); combobox.setToolTipText(tooltip); combobox.setBackground(enable ? Color.white : Color.lightGray); // Build an option model, wrapping each entry of the list table if (argument.getType() == Argument.ENUM) { HashMap arg_list = argument.getOptions(); Iterator it = arg_list.keySet().iterator(); ArrayList options = new ArrayList(); while (it.hasNext()) { options.add((String) it.next()); } Collections.sort(options); for (int i=0; i argument.getMaximum()) { initial_value = argument.getMaximum(); } JSpinner spinner = new JSpinner(new SpinnerNumberModel(initial_value, argument.getMinimum(), argument.getMaximum(), 1)); spinner.setEnabled(enable); spinner.setPreferredSize(SPINNER_SIZE); spinner.setToolTipText(tooltip); // Set enabled JComponent c = spinner.getEditor(); if ( c instanceof JSpinner.DefaultEditor ) { JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c; JFormattedTextField field = editor.getTextField(); field.setEditable(enable); if(enable) { field.setBackground(Color.white); } else { field.setBackground(Color.lightGray); } } // Layout add(new JLabel(), BorderLayout.CENTER); add(spinner, BorderLayout.EAST); // And remember it value_control = spinner; break; case Argument.STRING: // Use a standard text field JTextField textfield = new JTextField(); textfield.setEnabled(enable); textfield.setToolTipText(tooltip); // Set enabled if(enable) { textfield.setBackground(Color.white); } else { textfield.setBackground(Color.lightGray); } // If there was an original value, set it. if(value != null) { textfield.setText(value); } // Layout add(textfield, BorderLayout.CENTER); // And remember it value_control = textfield; break; } // If the argument is required, then you don't get a choice of whether it is enabled. if(argument.isRequired()) { JLabel label = new JLabel(argument.getName()); label.setOpaque(false); label.setPreferredSize(LABEL_SIZE); label.setToolTipText(tooltip); add(label, BorderLayout.WEST); } else { enabled = new JCheckBox(argument.getName(), enable); enabled.setOpaque(false); enabled.setPreferredSize(LABEL_SIZE); enabled.setToolTipText(tooltip); // Connect enabled.addActionListener(new EnabledListener(value_control)); // Layout add(enabled, BorderLayout.WEST); } } public String getName() { return argument.getName(); } public String getValue() { String value = null; if(value_control == null) { // Flag value, nothing to do. } else if(value_control instanceof JTextField) { value = ((JTextField)value_control).getText(); } else if(value_control instanceof JSpinner) { value = ((JSpinner)value_control).getValue().toString(); } else if(value_control instanceof JComboBox) { value = (((JComboBox)value_control).getSelectedItem()).toString(); } return value; } /** Retrieve the control used for storing values. * @return JComponent */ public JComponent getValueControl() { return value_control; } public boolean isEnabled() { if (enabled == null) { return true; // always enabled } return enabled.isSelected(); } /** 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 */ public boolean selectValue(JComboBox combobox, String target) { for(int i = 0; i < combobox.getItemCount(); i++) { if(combobox.getItemAt(i).toString().equals(target)) { combobox.setSelectedIndex(i); return true; } } return false; } /** Listens for actions apon the enable checkbox, and if detected enables or diables control appropriately. */ private class EnabledListener implements ActionListener { /** An editor component, such as a JComboBox or JTextField, that might have its enabled state changed by this listener. */ private JComponent target = null; /** Constructor. */ public EnabledListener(JComponent target) { this.target = target; } /** 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(target != null) { if(source.isSelected()) { target.setBackground(Color.white); target.setEnabled(true); } else { target.setBackground(Color.lightGray); target.setEnabled(false); } // Special case of stupid JSpinners who don't let their backgrounds change properly. if(target instanceof JSpinner) { JSpinner spinner = (JSpinner) target; JComponent c = spinner.getEditor(); if ( c instanceof JSpinner.DefaultEditor ) { JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c; JFormattedTextField field = editor.getTextField(); field.setEditable(source.isSelected()); if(source.isSelected()) { field.setBackground(Color.white); } else { field.setBackground(Color.lightGray); } } } } } } /** Listener that sets the tooltip associated to a combobox to the tooltip relevant to the selected item. */ private class ToolTipUpdater implements ActionListener { private HashMap arg_list; public ToolTipUpdater(HashMap arg_list) { this.arg_list = arg_list; } /** 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(); String key = (String) source.getSelectedItem(); if(arg_list != null) { String description = (String) arg_list.get(key); if(description != null) { description = Utility.formatHTMLWidth(description, 60); source.setToolTipText(description); } } } } }