- Timestamp:
- 2004-11-09T13:54:52+13:00 (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/checklist/CheckList.java
r8379 r8496 37 37 package org.greenstone.gatherer.checklist; 38 38 39 /**************************************************************************************40 * Title: Gatherer41 * Description: The Gatherer: a tool for gathering and enriching a digital collection.42 * Company: The University of Waikato43 * Written: 14/08/0244 * Revised:45 **************************************************************************************/46 39 import java.awt.*; 47 40 import java.awt.event.*; … … 50 43 import javax.swing.border.*; 51 44 45 52 46 /** 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. 53 47 * @author John Thompson 54 48 * @version 2.3 55 49 */ 56 // ####################################################################################57 // Optimization Saving58 // ####################################################################################59 // Vector -> ArrayList + Processor60 // ####################################################################################61 50 public class CheckList 62 extends JList { 51 extends JList 52 { 63 53 /** The border used when a list row is not in focus. */ 64 protected staticBorder noFocusBorder = new EmptyBorder(1, 1, 1, 1);65 54 static private Border noFocusBorder = new EmptyBorder(1, 1, 1, 1); 55 66 56 private boolean show_selected_row = true; 57 58 67 59 /** Constructor. */ 68 public CheckList(boolean show_selected_row) { 60 public CheckList(boolean show_selected_row) 61 { 69 62 super(); 70 63 this.show_selected_row = show_selected_row; 64 71 65 setCellRenderer(new CellRenderer()); 72 66 setModel(new DefaultListModel()); … … 74 68 addMouseListener(new CheckListListener()); 75 69 } 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 127 77 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); 152 95 } 153 96 updateUI(); 154 97 } 155 98 156 public Entry get(int index) {157 DefaultListModel model = (DefaultListModel) getModel();158 return (Entry) model.get(index);159 }160 99 161 100 /** Retrieve the currently ticked entries from this list. … … 163 102 * @see org.greenstone.gatherer.checklist.Entry 164 103 */ 165 public ArrayList getSelected() { 104 public ArrayList getSelected() 105 { 166 106 ArrayList result = new ArrayList(); 167 107 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++) { 170 109 Entry entry = (Entry) model.get(i); 171 if (entry.isSelected()) {110 if (entry.isSelected()) { 172 111 result.add(entry.getObject()); 173 112 } … … 176 115 } 177 116 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++) { 188 159 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])) { 260 162 entry.setSelected(true); 261 163 } … … 263 165 } 264 166 updateUI(); 265 266 267 } 167 } 168 169 268 170 private void selectionChanged(int index) { 269 171 fireSelectionValueChanged(index, index, false); 270 172 } 173 271 174 272 175 /** A custom list cell renderer for producing rows which contain clickable check boxes. */ 273 176 protected class CellRenderer 274 177 implements ListCellRenderer { 178 275 179 /** 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. 276 180 * @param list The </strong>JList</strong> we're painting. … … 300 204 } 301 205 } 206 207 302 208 /** Listens for clicks apon the checks within the list, and updates as necessary. */ 303 209 private class CheckListListener 304 210 extends MouseAdapter { 305 211 306 //private Entry previous_checkbox = null;307 212 /** Called whenever the mouse is clicked over our list. We find the nearest checkbox and change its state. 308 213 * @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.