Changeset 4592 for trunk


Ignore:
Timestamp:
2003-06-12T11:36:04+12:00 (21 years ago)
Author:
kjdon
Message:

changed the names of a lot of listeners so that its more clear which ones are for subcolls, which for subindexes. changed teh key listener to a document listener, and fiddled with validateAdd - only enable teh add button when there is a valid name

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/cdm/SubcollectionManager.java

    r4583 r4592  
    417417     */
    418418    public Control() {
    419                 // Create
     419        // Create
    420420        JPanel border_pane = new JPanel();
    421421        JPanel header_pane = new JPanel();
     
    429429        JLabel title = new JLabel(get("Title"));
    430430        title.setHorizontalAlignment(JLabel.CENTER);
    431         createSubcollection();
    432         createSubindex();
     431        createSubCollection();
     432        createSubIndex();
    433433        // Add listeners
    434434        // Layout
     
    449449    }
    450450    /** Create the subcollection controls.
    451             * @see org.greenstone.gatherer.Gatherer
    452             * @see org.greenstone.gatherer.cdm.SubcollectionManager.Control.AddListener
    453             * @see org.greenstone.gatherer.cdm.SubcollectionManager.Control.ListListener
    454             * @see org.greenstone.gatherer.cdm.SubcollectionManager.Control.RemoveListener
    455             * @see org.greenstone.gatherer.collection.CollectionManager
    456             * @see org.greenstone.gatherer.msm.MetadataSetManager
    457             */
    458     public void createSubcollection() {
     451     * @see org.greenstone.gatherer.Gatherer
     452     * @see org.greenstone.gatherer.cdm.SubcollectionManager.Control.AddListener
     453     * @see org.greenstone.gatherer.cdm.SubcollectionManager.Control.ListListener
     454     * @see org.greenstone.gatherer.cdm.SubcollectionManager.Control.RemoveListener
     455     * @see org.greenstone.gatherer.collection.CollectionManager
     456     * @see org.greenstone.gatherer.msm.MetadataSetManager
     457     */
     458    public void createSubCollection() {
    459459        // Create
    460460        add = new JButton(get("Add"));
    461461        add.setMnemonic(KeyEvent.VK_A);
     462        add.setEnabled(false);
    462463        JPanel button_pane = new JPanel();
    463464        JPanel button_pane_1 = new JPanel();
     
    477478        remove = new JButton(get("Remove"));
    478479        remove.setMnemonic(KeyEvent.VK_R);
     480        remove.setEnabled(false);
    479481        Vector source_model = gatherer.c_man.msm.getAssignedElements();
    480482        source_model.add(0, "Filename");
     
    492494
    493495        // Add listeners
    494         add.addActionListener(new AddListener());
    495         remove.addActionListener(new RemoveListener());
    496         subcollection_list.addListSelectionListener(new ListListener());
     496        SubCollectionChangeListener cl = new SubCollectionChangeListener();
     497        add.addActionListener(new AddSubCollectionListener());
     498        remove.addActionListener(new RemoveSubCollectionListener());
     499        subcollection_list.addListSelectionListener(new SubCollectionListListener());
     500        //exclude.addActionListener(cl);
     501        //include.addActionListener(cl);
     502        //source.addActionListener(cl);
     503        //flags.addDocumentListener(cl);
     504        match.getDocument().addDocumentListener(cl);
     505        name.getDocument().addDocumentListener(cl);
    497506
    498507        // Layout
     
    535544     * @see org.greenstone.gatherer.util.ExclusiveListListener
    536545     */
    537     public void createSubindex() {
     546    public void createSubIndex() {
    538547        // Creation
    539548        JPanel subindex_name_panel = new JPanel();
     
    577586
    578587        // Add listeners
    579         ChangeListener cl = new ChangeListener();
    580588        ExclusiveListSelectionListener ell = new ExclusiveListSelectionListener();
    581589        ell.add(subcollection_list_2);
    582590        ell.add(subindexes_list);
    583591        add_index.addActionListener(new AddSubIndexListener());
    584         clear_default.addActionListener(new ClearDefaultListener());
    585         exclude.addActionListener(cl);
    586         include.addActionListener(cl);
     592        clear_default.addActionListener(new ClearDefaultSubIndexListener());
    587593        remove_index.addActionListener(new RemoveSubIndexListener());
    588         set_default.addActionListener(new SetDefaultListener());
    589         source.addActionListener(cl);
    590         flags.addKeyListener(cl);
    591         match.addKeyListener(cl);
    592         name.addKeyListener(cl);
     594        set_default.addActionListener(new SetDefaultSubIndexListener());
    593595
    594596        subindex_name.getDocument().addDocumentListener(new SubIndexNameDocumentListener());
    595         subcollection_list_2.addListSelectionListener(new SubcollectionListSelectionListener());
     597        subcollection_list_2.addListSelectionListener(new SubCollectionList2Listener());
    596598
    597599        // Layout
     
    634636        subindex_pane.add(button_pane_2, BorderLayout.SOUTH);
    635637    }
    636 
    637     private class SubIndexListCellRenderer
    638         extends DefaultListCellRenderer {
    639        
    640         public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    641         StringBuffer text = new StringBuffer(value.toString());
    642         // Retrieve the indexes name if any.
    643         CollectionMeta metadata = manager.collectionmetadatum.getMetadata(value, manager.languages.getDefaultLanguage(), true);
    644         if(metadata != null) {
    645             text.append(" \"");
    646             text.append(metadata.getValue());
    647             text.append("\"");
    648         }
    649         return super.getListCellRendererComponent(list, text.toString(), index, isSelected, cellHasFocus);
    650         }
    651 
    652     }
    653 
    654     private class SubcollectionListSelectionListener
    655         implements ListSelectionListener {
    656 
    657         public void valueChanged(ListSelectionEvent event) {
    658         if(!event.getValueIsAdjusting()) {
    659             add_index.setEnabled(!subcollection_list_2.isSelectionEmpty() && subindex_name.getText().length() > 0);
    660         }
    661         }
    662     }
    663638   
    664     private class SubIndexNameDocumentListener
    665         implements DocumentListener {
    666         /** Gives notification that an attribute or set of attributes changed. */
    667         public void changedUpdate(DocumentEvent e) {
    668         update();
    669         }
    670         /** Gives notification that there was an insert into the document. */
    671         public void insertUpdate(DocumentEvent e) {
    672         update();
    673         }
    674         /** Gives notification that a portion of the document has been removed. */
    675         public void removeUpdate(DocumentEvent e) {
    676         update();
    677         }
    678         /** The text area has changed in some way. Given that this can only happed when we are editing or adding a text fragment we better respond appropriately. */
    679         private void update() {
    680         add_index.setEnabled(!subcollection_list_2.isSelectionEmpty() && subindex_name.getText().length() > 0);
    681         }
    682     }
    683 
     639   
    684640    /** Method to unregister any listeners to avoid memory leaks.
    685641     */
     
    687643    }
    688644    /** We have overriden this method to provide completely different functionality, given that our JPanel will never actually have focus. We call this method in GUI on the view we are about to replace, and this method checks if a change has occured and if so updates the current object.
    689             * @return A <i>boolean</i> which is always <i>false</i>.
    690             * @see org.greenstone.gatherer.msm.ElementWrapper
    691             */
     645     * @return A <i>boolean</i> which is always <i>false</i>.
     646     * @see org.greenstone.gatherer.msm.ElementWrapper
     647     */
    692648    public boolean hasFocus() {
    693                 // If we have a current metadata open, and something has changed then save the change.
     649        // If we have a current metadata open, and something has changed then save the change.
    694650        if(changed && current != null) {
    695651        String n = name.getText();
     
    716672    }
    717673    /** Overriden to ensure the instructions are scrolled to top.
    718           */
     674    */
    719675    public void updateUI() {
    720676        if(instructions != null) {
     
    723679        super.updateUI();
    724680    }
     681
     682    /** Method to validate the current subcollection editor values, and enable or disable controls (add button) based on said values.
     683     * we want to disable add until a new name is present
     684     */
     685    protected void validateAdd() {
     686        if(changed && name.getText().length() > 0 && match.getText().length() > 0) {
     687        System.out.println("in validate, name="+name.getText());
     688        if (subcollections.containsKey(name.getText())) {
     689            add.setEnabled(false);
     690        } else {
     691            add.setEnabled(true);
     692        }
     693        }
     694        else {
     695        add.setEnabled(false);
     696        }
     697    }
    725698    /** Listens for actions apon the 'add' button in the SubcollectionManager controls, and if detected calls the addSubcollection method of the manager with a newly created subcollection. */
    726     private class AddListener
     699    private class AddSubCollectionListener
    727700        implements ActionListener {
    728701        /** Any implementation of ActionListener must include this method so we can be informed when an action has been performed on one of our target controls. In this case we wish to retrieve information from the various edit controls, and if we have sufficient data to build a new subcollection do so.
     
    750723        }
    751724        changed = false;
     725        validateAdd();
    752726        }
    753727    }
     
    775749    }
    776750    /** This class listens for any key entry in a text field, selection change in a combobox or button click, and when detected sets the changed flag to true. Its also convenient to use this class to test if the add button should be active yet. */
    777     public class ChangeListener
    778         extends KeyAdapter
    779         implements ActionListener {
     751    private class SubCollectionChangeListener
     752        implements DocumentListener, ActionListener {
    780753        /** Any implementation of ActionListener must include this method so we can be informed when an action has been performed on one of our target controls. In this case we want to record that somethings changed, then validate the controls.
    781754         * @param event An <strong>ActionEvent</strong> containing information about the event.
     
    783756        public void actionPerformed(ActionEvent event) {
    784757        changed = true;
    785         validate();
     758        validateAdd();
     759        }
     760
     761        public void changedUpdate(DocumentEvent event) {
     762        System.out.println("changed");
     763        changed = true;
     764        validateAdd();
     765        }
     766        public void insertUpdate(DocumentEvent event) {
     767        System.out.println("inserted");
     768        changed = true;
     769        validateAdd();
     770
     771        }
     772
     773        public void removeUpdate(DocumentEvent event) {
     774        System.out.println("removed");
     775        changed = true;
     776        validateAdd();
    786777        }
    787778        /** Any inheritor of KeyAdapter can override this method so we can be informed when an key has been type in one of our target controls. In this case we want to record that somethings changed, then validate the controls.
    788779         * @param event An <strong>KeyEvent</strong> containing information about the event.
    789780         */
    790         public void keyPressed(KeyEvent event) {
    791         changed = true;
    792         validate();
    793         }
    794         /** Method to validate the current editor values, and enable or disable controls based on said values. */
    795         private void validate() {
    796         if(changed && name.getText().length() > 0 && match.getText().length() > 0) {
    797             add.setEnabled(true);
    798         }
    799         else {
    800             add.setEnabled(false);
    801         }
    802         }
     781        // public void keyPressed(KeyEvent event) {
     782        //changed = true;
     783        //validate();
     784        // }
     785
    803786    }
    804787    /** Listens for actions apon the 'clear default subindex' button in the SubcollectionManager controls, and if detected calls the setDefaultSubIndex() method of the manager with <i>null</i>. */
    805     private class ClearDefaultListener
     788    private class ClearDefaultSubIndexListener
    806789        implements ActionListener {
    807790        /** Any implementation of ActionListener must include this method so we can be informed when an action has been performed on one of our target controls. In this case we want to clear the default subindex.
     
    814797        }
    815798    }
     799    /** Listens for actions apon the 'remove' button in the SubcollectionManager controls, and if detected calls the remove method of the manager with the SubIndex selected for removal. */
     800    private class RemoveSubCollectionListener
     801        implements ActionListener {
     802        /** Any implementation of ActionListener must include this method so we can be informed when an action has been performed on one of our target controls. In this case we want to check if they have a subcolleciton selected, and if so remove both it and any subindexes based on it.
     803         * @param event An <strong>ActionEvent</strong> containing information about the event.
     804         * @see org.greenstone.gatherer.cdm.Subcollection
     805         */
     806        public void actionPerformed(ActionEvent event) {
     807        if(!subcollection_list.isSelectionEmpty()) {
     808            Subcollection sub_col = (Subcollection)subcollection_list.getSelectedValue();
     809            removeSubIndexes(sub_col);
     810            removeSubcollection(sub_col);
     811        }
     812        }
     813    }
     814    /** Listens for actions apon the 'remove subindex' button in the SubcollectionManager controls, and if detected calls the removeSubIndex method of the manager with the SubIndex selected for removal. */
     815    private class RemoveSubIndexListener
     816        implements ActionListener {
     817        /** Any implementation of ActionListener must include this method so we can be informed when an action has been performed on one of our target controls. In this case we want to check if they have a subindex selected, and if so remove it.
     818         * @param event An <strong>ActionEvent</strong> containing information about the event.
     819         * @see org.greenstone.gatherer.cdm.SubIndex
     820         */
     821        public void actionPerformed(ActionEvent event) {
     822        if(!subindexes_list.isSelectionEmpty()) {
     823            removeSubIndex((SubIndex)subindexes_list.getSelectedValue());
     824        }
     825        }
     826    }
     827    /** Listens for actions apon the 'set default subindex' button in the SubcollectionManager controls, and if detected calls the setDefaultSubIndex method of the manager with the SubIndex selected for default. */
     828    private class SetDefaultSubIndexListener
     829        implements ActionListener {
     830        /** Any implementation of ActionListener must include this method so we can be informed when an action has been performed on one of our target controls. In this case we want to check if they have a subindex selected, and if so set it as default.
     831         * @param event An <strong>ActionEvent</strong> containing information about the event.
     832         * @see org.greenstone.gatherer.cdm.DefaultSubIndex
     833         * @see org.greenstone.gatherer.cdm.SubIndex
     834         */
     835        public void actionPerformed(ActionEvent event) {
     836        if(!subindexes_list.isSelectionEmpty()) {
     837            setDefaultSubIndex(new DefaultSubIndex((SubIndex)subindexes_list.getSelectedValue()));
     838            clear_default.setEnabled(true);
     839            default_value.setText(default_index.getSubIndex().toString());
     840        }
     841        }
     842    }
     843
     844
    816845    /** This class listens for selections in the list on the subcollections pane of the SubcollectionManager, and updates the controls as necessary to reflect selection. */
    817     private class ListListener
     846    private class SubCollectionListListener
    818847        implements ListSelectionListener {
    819848        /** Any implementation of ListSelectionListener must include this method so we can be informed when the selection changes. In this case we want to execute any changes the users made to the entry, then update the controls with details of the new selection.
     
    891920        // Have to do this after so we don't get called when nothings actually changed.
    892921        changed = false;
    893         }
    894     }
    895     /** Listens for actions apon the 'remove' button in the SubcollectionManager controls, and if detected calls the remove method of the manager with the SubIndex selected for removal. */
    896     private class RemoveListener
    897         implements ActionListener {
    898         /** Any implementation of ActionListener must include this method so we can be informed when an action has been performed on one of our target controls. In this case we want to check if they have a subcolleciton selected, and if so remove both it and any subindexes based on it.
    899          * @param event An <strong>ActionEvent</strong> containing information about the event.
    900          * @see org.greenstone.gatherer.cdm.Subcollection
    901          */
    902         public void actionPerformed(ActionEvent event) {
    903         if(!subcollection_list.isSelectionEmpty()) {
    904             Subcollection sub_col = (Subcollection)subcollection_list.getSelectedValue();
    905             removeSubIndexes(sub_col);
    906             removeSubcollection(sub_col);
    907         }
    908         }
    909     }
    910     /** Listens for actions apon the 'remove subindex' button in the SubcollectionManager controls, and if detected calls the removeSubIndex method of the manager with the SubIndex selected for removal. */
    911     private class RemoveSubIndexListener
    912         implements ActionListener {
    913         /** Any implementation of ActionListener must include this method so we can be informed when an action has been performed on one of our target controls. In this case we want to check if they have a subindex selected, and if so remove it.
    914          * @param event An <strong>ActionEvent</strong> containing information about the event.
    915          * @see org.greenstone.gatherer.cdm.SubIndex
    916          */
    917         public void actionPerformed(ActionEvent event) {
    918         if(!subindexes_list.isSelectionEmpty()) {
    919             removeSubIndex((SubIndex)subindexes_list.getSelectedValue());
    920         }
    921         }
    922     }
    923     /** Listens for actions apon the 'set default subindex' button in the SubcollectionManager controls, and if detected calls the setDefaultSubIndex method of the manager with the SubIndex selected for default. */
    924     private class SetDefaultListener
    925         implements ActionListener {
    926         /** Any implementation of ActionListener must include this method so we can be informed when an action has been performed on one of our target controls. In this case we want to check if they have a subindex selected, and if so set it as default.
    927          * @param event An <strong>ActionEvent</strong> containing information about the event.
    928          * @see org.greenstone.gatherer.cdm.DefaultSubIndex
    929          * @see org.greenstone.gatherer.cdm.SubIndex
    930          */
    931         public void actionPerformed(ActionEvent event) {
    932         if(!subindexes_list.isSelectionEmpty()) {
    933             setDefaultSubIndex(new DefaultSubIndex((SubIndex)subindexes_list.getSelectedValue()));
    934             clear_default.setEnabled(true);
    935             default_value.setText(default_index.getSubIndex().toString());
    936         }
    937         }
    938     }
     922       
     923        }
     924    }
     925
     926    private class SubCollectionList2Listener
     927        implements ListSelectionListener {
     928
     929        public void valueChanged(ListSelectionEvent event) {
     930        if(!event.getValueIsAdjusting()) {
     931            add_index.setEnabled(!subcollection_list_2.isSelectionEmpty() && subindex_name.getText().length() > 0);
     932        }
     933        }
     934    }
     935   
     936    private class SubIndexListCellRenderer
     937        extends DefaultListCellRenderer {
     938       
     939        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
     940        StringBuffer text = new StringBuffer(value.toString());
     941        // Retrieve the indexes name if any.
     942        CollectionMeta metadata = manager.collectionmetadatum.getMetadata(value, manager.languages.getDefaultLanguage(), true);
     943        if(metadata != null) {
     944            text.append(" \"");
     945            text.append(metadata.getValue());
     946            text.append("\"");
     947        }
     948        return super.getListCellRendererComponent(list, text.toString(), index, isSelected, cellHasFocus);
     949        }
     950
     951    }
     952   
     953    private class SubIndexNameDocumentListener
     954        implements DocumentListener {
     955        /** Gives notification that an attribute or set of attributes changed. */
     956        public void changedUpdate(DocumentEvent e) {
     957        update();
     958        }
     959        /** Gives notification that there was an insert into the document. */
     960        public void insertUpdate(DocumentEvent e) {
     961        update();
     962        }
     963        /** Gives notification that a portion of the document has been removed. */
     964        public void removeUpdate(DocumentEvent e) {
     965        update();
     966        }
     967        /** The text area has changed in some way. Given that this can only happed when we are editing or adding a text fragment we better respond appropriately. */
     968        private void update() {
     969        add_index.setEnabled(!subcollection_list_2.isSelectionEmpty() && subindex_name.getText().length() > 0);
     970        }
     971    }
     972
    939973    }
    940974}
Note: See TracChangeset for help on using the changeset viewer.