Ignore:
Timestamp:
2004-11-09T13:54:52+13:00 (19 years ago)
Author:
mdewsnip
Message:

Started off fixing a bug where the loaded collection wasn't being ticked on in the cross-collection searching page. Ended up removing a ton of stuff from the CheckList class, some of which was duplicated code and buggy.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/checklist/CheckList.java

    r8379 r8496  
    3737package org.greenstone.gatherer.checklist;
    3838
    39 /**************************************************************************************
    40  * Title:        Gatherer
    41  * Description:  The Gatherer: a tool for gathering and enriching a digital collection.
    42  * Company:      The University of Waikato
    43  * Written:      14/08/02
    44  * Revised:     
    45  **************************************************************************************/
    4639import java.awt.*;
    4740import java.awt.event.*;
     
    5043import javax.swing.border.*;
    5144
     45
    5246/** This class provides a visual component that has the form of a list, as provided by JList but uses JCheckBox for data selection. Thus several elements can be 'ticked' in the list, and this selection returned using the method getSelected().<BR>Parts of this code modified from Trevor Harmon's posting on www.dejanews.com.
    5347 * @author John Thompson
    5448 * @version 2.3
    5549 */
    56 // ####################################################################################
    57 // Optimization                          Saving
    58 // ####################################################################################
    59 // Vector -> ArrayList                   + Processor
    60 // ####################################################################################
    6150public class CheckList
    62     extends JList {
     51    extends JList
     52{
    6353    /** The border used when a list row is not in focus. */
    64     protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
    65    
     54    static private Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
     55
    6656    private boolean show_selected_row = true;
     57
     58
    6759    /** Constructor. */
    68     public CheckList(boolean show_selected_row) {
     60    public CheckList(boolean show_selected_row)
     61    {
    6962    super();
    7063    this.show_selected_row = show_selected_row;
     64
    7165    setCellRenderer(new CellRenderer());
    7266    setModel(new DefaultListModel());
     
    7468    addMouseListener(new CheckListListener());
    7569    }
    76     /** Constructor.
    77      * @param list_data An ArrayList of entries for this checklist.
    78      * @see org.greenstone.gatherer.checklist.Entry
    79      */
    80     public CheckList(ArrayList list_data, boolean show_selected_row) {
    81     super();
    82     this.show_selected_row = show_selected_row;
    83     // Create a new model.
    84     DefaultListModel model = new DefaultListModel();
    85     for(int i = 0; i < list_data.size(); i++) {
    86         Entry entry = null;
    87         Object temp = list_data.get(i);
    88         if(temp instanceof Entry) {
    89         entry = (Entry) temp;
    90         }
    91         else {
    92         entry = new Entry(list_data.get(i));
    93         }
    94         temp = null;
    95         String name = entry.toString();
    96         int index = 0;
    97         boolean found = false;
    98         while(index < model.size() && !found) {
    99         Object sibling = model.getElementAt(index);
    100         if(name.compareTo(sibling.toString()) <= 0) {
    101             model.add(index, entry);
    102             found = true;
    103         }
    104         index++;
    105         }
    106         if(!found) {
    107         model.addElement(entry);
    108         }
    109     }
    110     setModel(model);
    111     setCellRenderer(new CellRenderer());
    112     addMouseListener(new CheckListListener());
    113     setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    114     }
    115 
    116     /** Constructor. */
    117     public CheckList(ListModel data_model, boolean show_selected_row) {
    118     super(data_model);
    119     this.show_selected_row = show_selected_row;
    120     setCellRenderer(new CellRenderer());
    121     addMouseListener(new CheckListListener());
    122     setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    123     }
    124 
    125     public void addEntry(Entry entry) {
    126     DefaultListModel model = (DefaultListModel) getModel();
     70
     71
     72    public void addEntry(Entry entry)
     73    {
     74    DefaultListModel model = (DefaultListModel) getModel();
     75
     76    // Add the entry in alpabetical order
    12777    String name = entry.toString();
    128     int index = 0;
    129     boolean found = false;
    130     while(index < model.size() && !found) {
    131         Object sibling = model.getElementAt(index);
    132         if(name.compareTo(sibling.toString()) <= 0) {
    133         model.add(index, entry);
    134         found = true;
    135         }
    136         index++;
    137         sibling = null;
    138     }
    139     if(!found) {
    140         model.addElement(entry);
    141     }         
    142     name = null;
    143     model = null;
    144     }
    145 
    146     public void clearSelection() {
    147     DefaultListModel model = (DefaultListModel) getModel();
    148     int size = model.size();
    149     for(int i = 0; i < size; i++) {
    150         Entry entry = (Entry) model.get(i);
    151         entry.setSelected(false);
     78    for (int i = 0; i < model.size(); i++) {
     79        Object sibling = model.getElementAt(i);
     80        if (name.compareTo(sibling.toString()) <= 0) {
     81        model.add(i, entry);
     82        return;
     83        }
     84    }
     85
     86    model.addElement(entry);
     87    }
     88
     89
     90    public void clearSelection()
     91    {
     92    DefaultListModel model = (DefaultListModel) getModel();
     93    for (int i = 0; i < model.size(); i++) {
     94        ((Entry) model.get(i)).setSelected(false);
    15295    }
    15396    updateUI();
    15497    }
    15598
    156     public Entry get(int index) {
    157     DefaultListModel model = (DefaultListModel) getModel();
    158     return (Entry) model.get(index);
    159     }
    16099
    161100    /** Retrieve the currently ticked entries from this list.
     
    163102     * @see org.greenstone.gatherer.checklist.Entry
    164103     */
    165     public ArrayList getSelected() {
     104    public ArrayList getSelected()
     105    {
    166106    ArrayList result = new ArrayList();
    167107    DefaultListModel model = (DefaultListModel) getModel();
    168     int size = model.size();
    169     for(int i = 0; i < size; i++) {
     108    for (int i = 0; i < model.size(); i++) {
    170109        Entry entry = (Entry) model.get(i);
    171         if(entry.isSelected()) {
     110        if (entry.isSelected()) {
    172111        result.add(entry.getObject());
    173112        }
     
    176115    }
    177116
    178     public boolean isSelected(int index) {
    179     DefaultListModel model = (DefaultListModel) getModel();
    180     Entry entry = (Entry) model.get(index);
    181     return entry.isSelected();
    182     }
    183 
    184     public boolean isSelectionEmpty() {
    185     DefaultListModel model = (DefaultListModel) getModel();
    186     int size = model.size();
    187     for(int i = 0; i < size; i++) {
     117
     118//     public boolean isSelectionEmpty()
     119//     {
     120//  System.err.println("In CheckList.isSelectionEmpty()...");
     121//  DefaultListModel model = (DefaultListModel) getModel();
     122//  for (int i = 0; i < model.size(); i++) {
     123//      Entry entry = (Entry) model.get(i);
     124//      if (entry.isSelected()) {
     125//      return false;
     126//      }
     127//  }
     128
     129//  return true;
     130//     }
     131
     132
     133    public void setListData(ArrayList list_data)
     134    {
     135    // Create a new model.
     136    setModel(new DefaultListModel());
     137
     138    // Add the items from the list to the model
     139    for (int i = 0; i < list_data.size(); i++) {
     140        Object list_object = list_data.get(i);
     141        if (list_object instanceof Entry) {
     142        addEntry((Entry) list_object);
     143        }
     144        else {
     145        addEntry(new Entry(list_object));
     146        }
     147    }
     148    }
     149
     150
     151    public void setSelectedObjects(Object objects[])
     152    {
     153    if (objects == null) {
     154        return;
     155    }
     156
     157    DefaultListModel model = (DefaultListModel) getModel();
     158    for (int i = 0; i < model.size(); i++) {
    188159        Entry entry = (Entry) model.get(i);
    189         if(entry.isSelected()) {
    190         entry = null;
    191         model = null;
    192         return false;
    193         }
    194         entry = null;
    195     }
    196     model = null;
    197     return true;
    198     }
    199 
    200     public int getEntryCount() {
    201     return getModel().getSize();
    202     }
    203 
    204     public void setListData(Object[] list_data) {
    205     // Create a new model.
    206     DefaultListModel model = new DefaultListModel();
    207     for(int i = 0; i < list_data.length; i++) {
    208         Entry entry = null;
    209         Object temp = list_data[i];
    210         if(temp instanceof Entry) {
    211         entry = (Entry) temp;
    212         }
    213         else {
    214         entry = new Entry(temp);
    215         }
    216         temp = null;
    217         String name = entry.toString();
    218         int index = 0;
    219         boolean found = false;
    220         while(index < model.size() && !found) {
    221         Object sibling = model.getElementAt(index);
    222         if(name.compareTo(sibling.toString()) <= 0) {
    223             model.add(index, entry);
    224             found = true;
    225         }
    226         index++;
    227         }
    228         if(!found) {
    229         model.addElement(entry);
    230         }
    231     }
    232     setModel(model);
    233     }
    234 
    235     /** Checks the entries in the list whose name appear in the given array.
    236      * @param names The name of entries to be checked as a <strong>String[]</strong>.
    237      * @see org.greenstone.gatherer.checklist.Entry
    238      */
    239     public void setSelected(String names[]) {
    240     DefaultListModel model = (DefaultListModel) getModel();
    241     int size = model.size();
    242     for(int i = 0; i < size; i++) {
    243         Entry entry = (Entry) model.get(i);
    244         for(int j = 0; names != null && j < names.length; j++) {
    245         if(entry.toString().equals(names[j])) {
    246             entry.setSelected(true);
    247            
    248         }
    249         }
    250     }         
    251     }
    252 
    253     public void setSelectedObjects(Object objects[]) {
    254     DefaultListModel model = (DefaultListModel) getModel();
    255     int size = model.size();
    256     for(int i = 0; i < size; i++) {
    257         Entry entry = (Entry) model.get(i);
    258         for(int j = 0; objects != null && j < objects.length; j++) {
    259         if(entry.getObject().equals(objects[j])) {
     160        for (int j = 0; j < objects.length; j++) {
     161        if (entry.getObject().equals(objects[j])) {
    260162            entry.setSelected(true);
    261163        }
     
    263165    }
    264166    updateUI();
    265 
    266 
    267     }
     167    }
     168
     169
    268170    private void selectionChanged(int index) {
    269171    fireSelectionValueChanged(index, index, false);
    270172    }
     173
    271174
    272175    /** A custom list cell renderer for producing rows which contain clickable check boxes. */
    273176    protected class CellRenderer
    274177    implements ListCellRenderer {
     178
    275179    /** Return a component that has been configured to display the specified value. That component's paint method is then called to "render" the cell. If it is necessary to compute the dimensions of a list because the list cells do not have a fixed size, this method is called to generate a component on which getPreferredSize  can be invoked.
    276180     * @param list The </strong>JList</strong> we're painting.
     
    300204    }
    301205    }
     206
     207
    302208    /** Listens for clicks apon the checks within the list, and updates as necessary. */
    303209    private class CheckListListener
    304210    extends MouseAdapter {
    305211
    306     //private Entry previous_checkbox = null;
    307212    /** Called whenever the mouse is clicked over our list. We find the nearest checkbox and change its state.
    308213     * @param e A <strong>MouseEvent</strong> containing everything you ever wanted to know about the mouse event but were afraid to ask.
Note: See TracChangeset for help on using the changeset viewer.