Changeset 4403


Ignore:
Timestamp:
2003-05-29T15:19:02+12:00 (21 years ago)
Author:
jmt12
Message:

2030082: Rewrote the text field enabler class to use a DocumentListener, and to perform appropriately.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/mem/MetadataEditorManager.java

    r4365 r4403  
    101101    private JScrollPane set_attributes_scroll = null;
    102102    private JPanel details_pane = null;
     103    private KeepTreeRootExpandedListener keep_root_expanded_listener = null;
    103104    private MEMModel model = null;
    104105    private MEMNode current_node = null;
     
    228229    element_values.setSelectionColor(Gatherer.config.getColor("coloring.collection_selection_background", false));
    229230    element_values.setSelectedTextColor(Gatherer.config.getColor("coloring.collection_selection_foreground", false));
    230          
     231       
     232    keep_root_expanded_listener = new KeepTreeRootExpandedListener();
     233
    231234    JPanel profile_details_pane = new JPanel();
    232235    profile_details_pane.setOpaque(false);
     
    12101213        this.setModal(true);
    12111214        this.setSize(ADD_OR_EDIT_VALUE_SIZE);
    1212                 // Create
     1215        // Create
    12131216        JPanel content_pane = (JPanel) getContentPane();
    12141217        content_pane.setBackground(Gatherer.config.getColor("coloring.collection_heading_background", false));
     
    12491252        ok_button = new JButton(get("General.OK"));
    12501253        ok_button.setEnabled(false);
     1254        TextFieldEnabler ok_button_enabler = new TextFieldEnabler(ok_button);
    12511255        cancel_button = new JButton(get("General.Cancel"));
    1252                 // Connect
    1253         TextFieldEnabler ok_button_enabler = new TextFieldEnabler(ok_button);
     1256        // Connect
     1257        ok_button_enabler.add(value);
    12541258        cancel_button.addActionListener(this);
    12551259        ok_button.addActionListener(this);
    12561260        subject_tree.addTreeSelectionListener(this);
    1257         ok_button_enabler.add(value);
    1258                 // Layout
     1261        // Layout
    12591262        subject_tree_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    12601263        subject_tree_pane.setLayout(new BorderLayout());
     
    12991302        GValueModel model = Gatherer.c_man.msm.getValueTree(current_element);
    13001303        if(add_type) {
     1304            ///ystem.err.println("Add type - insert node: " + value.getText() + " into " + subject_node);
    13011305            // Add this value to the tree.
    1302             GValueNode parent = null;
    13031306            model.addValue(value.getText(), subject_node, alias.getText());
    1304             parent = null;
    13051307        }
    13061308        else {
     
    13431345            }
    13441346            // Inform the tree model what two nodes structures have changed (origin and destination).
    1345             subject_node.unmap();
    1346             old_subject_node.unmap();
     1347            //subject_node.unmap();
     1348            //old_subject_node.unmap();
    13471349            model.nodeStructureChanged(old_subject_node);
    13481350            model.nodeStructureChanged(subject_node);
     
    14431445        self.dispose();
    14441446    }
     1447    }
     1448
     1449    /** This listener is responsible for keeping the root node of a value tree expanded where and when possible. */
     1450    private class KeepTreeRootExpandedListener
     1451    implements TreeModelListener {
     1452    /** Invoked after a node (or a set of siblings) has changed in some way. */
     1453    public void treeNodesChanged(TreeModelEvent e) {
     1454    }
     1455        /** Invoked after nodes have been inserted into the tree. */
     1456    public void treeNodesInserted(TreeModelEvent e) {
     1457        element_values.expandPath(new TreePath(element_values.getModel().getRoot()));
     1458    }
     1459    /** Invoked after nodes have been removed from the tree. */
     1460    public void treeNodesRemoved(TreeModelEvent e) {
     1461    }
     1462    /** Invoked after the tree has drastically changed structure from a given node down. */
     1463    public void treeStructureChanged(TreeModelEvent e) {
     1464        element_values.expandPath(new TreePath(element_values.getModel().getRoot()));
     1465    }
    14451466    }
    14461467
     
    15931614    /** Used to enable a certain target component if and only if all the registered text fields contain non-zero length strings. */
    15941615    private class TextFieldEnabler
    1595     extends KeyAdapter {
     1616    implements DocumentListener {
     1617    private boolean ignore = false;
    15961618    private Component target = null;
    15971619    private JTextComponent[] fields = null;
     
    16011623    }
    16021624    public void add(JTextComponent field) {
    1603         field.addKeyListener(this);
     1625        // Record this field as being one we depend upon
    16041626        if(fields == null) {
    16051627        fields = new JTextComponent[1];
     
    16111633        temp[fields.length] = field;
    16121634        fields = temp;
    1613         }
    1614     }
    1615     public void keyReleased(KeyEvent event) {
    1616         boolean enable = true;
    1617         for(int i = 0; i < fields.length; i++) {
    1618         enable = enable && fields[i].getText().length() > 0;
    1619         }
    1620         target.setEnabled(enable);
     1635        temp = null;
     1636        }
     1637        // Add the appropriate listener
     1638        field.getDocument().addDocumentListener(this);
     1639    }
     1640    /** Gives notification that an attribute or set of attributes changed. */
     1641    public void changedUpdate(DocumentEvent e) {
     1642        canEnable();
     1643    }
     1644    /** Gives notification that there was an insert into the document. */
     1645    public void insertUpdate(DocumentEvent e) {
     1646        canEnable();
     1647    }
     1648        /** Gives notification that a portion of the document has been removed. */
     1649    public void removeUpdate(DocumentEvent e) {
     1650        canEnable();
     1651    }
     1652    private void canEnable() {
     1653        if(!ignore) {
     1654        ignore = true;
     1655        boolean can_enable = true;
     1656        for(int i = 0; can_enable && i < fields.length; i++) {
     1657            can_enable = can_enable && (fields[i].getText().length() > 0);
     1658        }
     1659        target.setEnabled(can_enable);
     1660        ignore = false;
     1661        }
    16211662    }
    16221663    }
     
    17091750            atm.setTable(element_attributes);
    17101751
    1711             element_values.setModel(Gatherer.c_man.msm.getValueTree(current_element));
     1752            // Be careful not to add the keep_root_expanded_listener over and over!
     1753            GValueModel value_model = Gatherer.c_man.msm.getValueTree(current_element);
     1754            value_model.removeTreeModelListener(keep_root_expanded_listener); // Remove it if its already there
     1755            value_model.addTreeModelListener(keep_root_expanded_listener); // Then add again.
     1756            element_values.setModel(value_model);
    17121757            card_layout.show(details_pane, ELEMENT);
    17131758            // Meanwhile disable/enable controls depending on this Element selection.
Note: See TracChangeset for help on using the changeset viewer.