Changeset 9139


Ignore:
Timestamp:
2005-02-22T16:14:16+13:00 (19 years ago)
Author:
kjdon
Message:

moved the ArgumentControl class out of OptionsPane. OptionsPane now has MyArgumentControl classs which extends ArgumentCOntrol, adding in the small amount of opitons pane specific functionality

Location:
trunk/gli/src/org/greenstone/gatherer/gui
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/gui/OptionsPane.java

    r9137 r9139  
    5252import org.greenstone.gatherer.collection.Collection;
    5353import org.greenstone.gatherer.collection.CollectionManager;
    54 import org.greenstone.gatherer.metadata.MetadataSetManager;
    5554import org.greenstone.gatherer.util.AppendLineOnlyFileDocument;
    5655import org.greenstone.gatherer.util.AppendLineOnlyFileDocumentOwner;
    57 import org.greenstone.gatherer.util.Utility;
    5856
    5957/** This class serves as the data holder for all subclasses of option panes, such as Import options or All options. It also contains methods for creating each of the option lines as they would appear in the subpane. Futhermore it has a method for considering all the arguments and generating a <strong>String[]</strong> to allow you to pass them to the <strong>GShell</strong>.
     
    7371    static private int IMPORT = 1;
    7472    static private int MINIMUM_ROWS = 11;
    75     static private Dimension LABEL_SIZE = new Dimension(180, 25);
    76     static private Dimension ROW_SIZE = new Dimension(610, 30);
    77     static private Dimension SPINNER_SIZE = new Dimension(100, 25);
    78     static private String DESCRIPTION_SEP = " + ";
    79 
     73 
    8074    /** All process messages are written to this log text area. */
    8175    public JTextArea log_textarea = null;
     
    120114     * @see org.greenstone.gatherer.collection.BuildOptions#getBuildValue
    121115     * @see org.greenstone.gatherer.collection.BuildOptions#getBuildValueEnabled
    122      * @see org.greenstone.gatherer.gui.OptionsPane.ArgumentControl
     116     * @see org.greenstone.gatherer.gui.OptionsPane.MyArgumentControl
    123117     */
    124118    public JPanel buildBuild(JPanel pane) {
     
    138132        boolean enabled = build_options.getValueEnabled(argument.getName());
    139133        String value = build_options.getValue(argument.getName());
    140         ArgumentControl argument_control = new ArgumentControl(BUILD, argument, enabled, value);
     134        MyArgumentControl argument_control = new MyArgumentControl(BUILD, argument, enabled, value);
    141135        build_arguments.add(argument_control);
    142136        }
     
    198192        boolean enabled = import_options.getValueEnabled(argument.getName());
    199193        String value = import_options.getValue(argument.getName());
    200         ArgumentControl argument_control = new ArgumentControl(IMPORT, argument, enabled, value);
     194        MyArgumentControl argument_control = new MyArgumentControl(IMPORT, argument, enabled, value);
    201195        import_arguments.add(argument_control);
    202196        }
     
    337331    for(int i = 0; i < panel.getComponentCount(); i++) {
    338332        Component component = panel.getComponent(i);
    339         if(component instanceof ArgumentControl) {
    340         ((ArgumentControl)component).update();
     333        if(component instanceof MyArgumentControl) {
     334        ((MyArgumentControl)component).update();
    341335        }
    342336    }
     
    359353    // Loop through the controls, and if the current control is a JSpinner, commit its current editing
    360354    for(int i = 0; i < current_controls.size(); i++) {
    361         ArgumentControl control = (ArgumentControl) current_controls.get(i);
     355        MyArgumentControl control = (MyArgumentControl) current_controls.get(i);
    362356        JComponent value_control = control.getValueControl();
    363357        if(value_control instanceof JSpinner) {
     
    385379    public void mouseReleased(MouseEvent e) {}
    386380
    387     private class ArgumentControl
    388     extends JPanel {
    389     private Argument argument;
     381    private class MyArgumentControl
     382    extends ArgumentControl {
    390383    private int type;
    391     private JComponent value_control;
    392     private JCheckBox enabled;
    393     public ArgumentControl(int type, Argument argument, boolean enable, String value) {
    394         super();
    395         this.argument = argument;
     384
     385    public MyArgumentControl(int type, Argument argument, boolean enable, String value) {
     386        super(argument, enable, value);
    396387        this.type = type;
    397         String tooltip = Utility.formatHTMLWidth("<html>" + argument.getDescription() + "</html>", 60);
    398         // Because of the dynamic order of component creation/connection/layout, we can't really follow that pattern here.
    399         setBackground(Configuration.getColor("coloring.collection_tree_background", false));
    400         setBorder(BorderFactory.createEmptyBorder(2,0,2,0));
    401         setLayout(new BorderLayout());
    402         setPreferredSize(ROW_SIZE);
    403         setMaximumSize(ROW_SIZE);
    404 
    405         // Try to determine what the value_controls value should be.
    406         if(value == null) {
    407         value = argument.getDefaultValue();
    408         }
    409         // Create a correct value control based on the argument provided.
    410         switch(argument.getType()) {
    411         case Argument.ENUM:
    412         case Argument.METADATUM:
    413         JComboBox combobox = new JComboBox();
    414         combobox.setEnabled(enable);
    415         combobox.setToolTipText(tooltip);
    416         combobox.setBackground(enable ? Color.white : Color.lightGray);
    417 
    418         // Build an option model, wrapping each entry of the list table
    419         if (argument.getType() == Argument.ENUM) {
    420             HashMap arg_list = argument.getOptions();
    421             Iterator it = arg_list.keySet().iterator();
    422             while (it.hasNext()) {
    423             combobox.addItem((String) it.next());
    424             }
    425 
    426             // Connect this so if a value is selected the tooltip updates accordingly
    427             combobox.addActionListener(new ToolTipUpdater(arg_list));
    428         }
    429         if (argument.getType() == Argument.METADATUM) {
    430             ArrayList every_metadata_set_element = MetadataSetManager.getEveryMetadataSetElement();
    431             for (int i = 0; i < every_metadata_set_element.size(); i++) {
    432             combobox.addItem(every_metadata_set_element.get(i));
    433             }
    434         }
    435 
    436         if (value != null) {
    437             // Set the selected string
    438             for (int i = 0; i < combobox.getItemCount(); i++) {
    439             if (combobox.getItemAt(i).toString().equals(value)) {
    440                 combobox.setSelectedIndex(i);
    441             }
    442             }
    443         }
    444         // Layout
    445         add(combobox, BorderLayout.CENTER);
    446         // And remember
    447         value_control = combobox;
    448         break;
    449         case Argument.FLAG:
    450         // Only need the check box.
    451         value_control = null;
    452         break;
    453         case Argument.INTEGER:
    454         // Build a spinner
    455         int initial_value=0;
    456         if (value != null) {
    457             try {
    458             initial_value = Integer.parseInt(value);
    459             } catch (Exception exception) {
    460             }
    461         }
    462         if (initial_value < argument.getMinimum()) {
    463             initial_value = argument.getMinimum();
    464         } else if (initial_value > argument.getMaximum()) {
    465             initial_value = argument.getMaximum();
    466         }
    467         JSpinner spinner = new JSpinner(new SpinnerNumberModel(initial_value, argument.getMinimum(), argument.getMaximum(), 1));       
    468         spinner.setEnabled(enable);
    469         spinner.setPreferredSize(SPINNER_SIZE);
    470         spinner.setToolTipText(tooltip);
    471         // Set enabled
    472         JComponent c = spinner.getEditor();
    473         if ( c instanceof JSpinner.DefaultEditor ) {
    474             JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c;
    475             JFormattedTextField field = editor.getTextField();
    476             field.setEditable(enable);
    477             if(enable) {
    478             field.setBackground(Color.white);
    479             }
    480             else {
    481             field.setBackground(Color.lightGray);
    482             }
    483         }
    484         // Layout
    485         add(new JLabel(), BorderLayout.CENTER);
    486         add(spinner, BorderLayout.EAST);
    487         // And remember it
    488         value_control = spinner;
    489         break;
    490         case Argument.STRING:
    491         // Use a standard text field
    492         JTextField textfield = new JTextField();
    493         textfield.setEnabled(enable);
    494         textfield.setToolTipText(tooltip);
    495         // Set enabled
    496         if(enable) {
    497             textfield.setBackground(Color.white);
    498         }
    499         else {
    500             textfield.setBackground(Color.lightGray);
    501         }
    502         // If there was an original value, set it.
    503         if(value != null) {
    504             textfield.setText(value);
    505         }
    506         // Layout
    507         add(textfield, BorderLayout.CENTER);
    508         // And remember it
    509         value_control = textfield;
    510         break;
    511         }
    512 
    513         // If the argument is required, then you don't get a choice of whether it is enabled.
    514         if(argument.isRequired()) {
    515         JLabel label = new JLabel(argument.getName());
    516         label.setOpaque(false);
    517         label.setPreferredSize(LABEL_SIZE);
    518         label.setToolTipText(tooltip);
    519         add(label, BorderLayout.WEST);
    520         }
    521         else {
    522         enabled = new JCheckBox(argument.getName(), enable);
    523         enabled.setOpaque(false);
    524         enabled.setPreferredSize(LABEL_SIZE);
    525         enabled.setToolTipText(tooltip);
    526         // Connect
    527         enabled.addActionListener(new EnabledListener(value_control));
    528         // Layout
    529         add(enabled, BorderLayout.WEST);
    530         }
    531     }
    532 
    533     /** Retrieve the control used for storing values.
    534      * @return JComponent
    535      */
    536     public JComponent getValueControl() {
    537         return value_control;
    538     }
    539 
    540     /** Method to ensure that a certain value is selected, if it exists within that combobox to begin with.
    541      * @param combobox the JComboBox whose selection we are trying to preset
    542      * @param target The desired value of the selection as a String
    543      * @return true if the item was found and selected, false otherwise
    544      */
    545     public boolean selectValue(JComboBox combobox, String target) {
    546         for(int i = 0; i < combobox.getItemCount(); i++) {
    547         if(combobox.getItemAt(i).toString().equals(target)) {
    548             combobox.setSelectedIndex(i);
    549             return true;
    550         }
    551         }
    552         return false;
    553388    }
    554389
     
    589424        else {
    590425            import_options.setValue(name, enable, value);
    591         }
    592         }
    593     }
    594     }
    595 
    596     /** Listens for actions apon the enable checkbox, and if detected enables or diables control appropriately. */
    597     private class EnabledListener
    598     implements ActionListener {
    599     /** An editor component, such as a JComboBox or JTextField, that might have its enabled state changed by this listener. */
    600     private JComponent target = null;
    601     /** Constructor. */
    602     public EnabledListener(JComponent target) {
    603         this.target = target;
    604     }
    605     /** 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.
    606      * @param event An <strong>ActionEvent</strong> containing information about the click.
    607      */
    608     public void actionPerformed(ActionEvent event) {
    609         JCheckBox source = (JCheckBox)event.getSource();
    610         if(target != null) {
    611         if(source.isSelected()) {
    612             target.setBackground(Color.white);
    613             target.setEnabled(true);
    614         }
    615         else {
    616             target.setBackground(Color.lightGray);
    617             target.setEnabled(false);
    618         }
    619         // Special case of stupid JSpinners who don't let their backgrounds change properly.
    620         if(target instanceof JSpinner) {
    621             JSpinner spinner = (JSpinner) target;
    622             JComponent c = spinner.getEditor();
    623             if ( c instanceof JSpinner.DefaultEditor ) {
    624             JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) c;
    625             JFormattedTextField field = editor.getTextField();
    626             field.setEditable(source.isSelected());
    627             if(source.isSelected()) {
    628                 field.setBackground(Color.white);
    629             }
    630             else {
    631                 field.setBackground(Color.lightGray);
    632             }
    633             }
    634426        }
    635427        }
     
    765557    }
    766558
    767     /** Listener that sets the tooltip associated to a combobox to the tooltip relevant to the selected item. */
    768     private class ToolTipUpdater
    769     implements ActionListener {
    770     private HashMap arg_list;
    771     public ToolTipUpdater(HashMap arg_list) {
    772         this.arg_list = arg_list;
    773     }
    774     /** 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.
    775      * @param event An <strong>ActionEvent</strong> containing information about the action that fired this call.
    776      */
    777     public void actionPerformed(ActionEvent event) {
    778         JComboBox source = (JComboBox)event.getSource();
    779         String key = (String) source.getSelectedItem();
    780         if(arg_list != null) {
    781         String description = (String) arg_list.get(key);
    782         if(description != null) {
    783             description = Utility.formatHTMLWidth(DESCRIPTION_SEP + description, 60);
    784             String original = source.getToolTipText();
    785             if(original == null) {
    786             original = "";
    787             }
    788             // Remove any existing extra description.
    789             if(original.indexOf(DESCRIPTION_SEP) != -1) {
    790             original = original.substring(0, original.indexOf(DESCRIPTION_SEP));
    791             }
    792             source.setToolTipText(original + description);
    793         }
    794         }
    795     }
    796     }
    797559}
Note: See TracChangeset for help on using the changeset viewer.