- Timestamp:
- 2003-05-27T15:57:37+12:00 (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/checklist/CheckList.java
r4293 r4366 66 66 // #################################################################################### 67 67 public class CheckList 68 69 70 71 72 73 74 75 76 77 78 79 68 extends JList { 69 /** The border used when a list row is not in focus. */ 70 protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1); 71 /** Constructor. */ 72 public CheckList() { 73 super(); 74 setCellRenderer(new CellRenderer()); 75 setModel(new DefaultListModel()); 76 setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 77 addMouseListener(new CheckListListener()); 78 } 79 /** Constructor. 80 80 * @param list_data An <strong>ArrayList</strong> of entries for this checklist. 81 81 * @see org.greenstone.gatherer.checklist.CheckList.Entry 82 82 */ 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 83 public CheckList(ArrayList list_data) { 84 super(); 85 // Create a new model. 86 DefaultListModel model = new DefaultListModel(); 87 for(int i = 0; i < list_data.size(); i++) { 88 Entry entry = new Entry(list_data.get(i)); 89 String name = entry.toString(); 90 int index = 0; 91 boolean found = false; 92 while(index < model.size() && !found) { 93 Object sibling = model.getElementAt(index); 94 if(name.compareTo(sibling.toString()) <= 0) { 95 model.add(index, entry); 96 found = true; 97 } 98 index++; 99 } 100 if(!found) { 101 model.addElement(entry); 102 } 103 } 104 setModel(model); 105 setCellRenderer(new CellRenderer()); 106 addMouseListener(new CheckListListener()); 107 setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 108 } 109 110 /** Constructor. */ 111 public CheckList(ListModel data_model) { 112 super(data_model); 113 setCellRenderer(new CellRenderer()); 114 addMouseListener(new CheckListListener()); 115 setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 116 } 117 118 public void addEntry(Entry entry) { 119 DefaultListModel model = (DefaultListModel) getModel(); 120 String name = entry.toString(); 121 int index = 0; 122 boolean found = false; 123 while(index < model.size() && !found) { 124 Object sibling = model.getElementAt(index); 125 if(name.compareTo(sibling.toString()) <= 0) { 126 model.add(index, entry); 127 found = true; 128 } 129 index++; 130 sibling = null; 131 } 132 if(!found) { 133 model.addElement(entry); 134 } 135 name = null; 136 model = null; 137 } 138 139 public Entry get(int index) { 140 DefaultListModel model = (DefaultListModel) getModel(); 141 return (Entry) model.get(index); 142 } 143 144 /** Retrieve the currently ticked entries from this list. 145 145 * @return An <strong>ArrayList</strong> containing only those entries from the initial list that are checked. 146 146 * @see org.greenstone.gatherer.checklist.CheckList.Entry 147 147 */ 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 148 public ArrayList getSelected() { 149 ArrayList result = new ArrayList(); 150 DefaultListModel model = (DefaultListModel) getModel(); 151 int size = model.size(); 152 for(int i = 0; i < size; i++) { 153 Entry entry = (Entry) model.get(i); 154 if(entry.isSelected()) { 155 result.add(entry.getObject()); 156 } 157 } 158 return result; 159 } 160 161 public boolean isSelected(int index) { 162 DefaultListModel model = (DefaultListModel) getModel(); 163 Entry entry = (Entry) model.get(index); 164 return entry.isSelected(); 165 } 166 167 public int getEntryCount() { 168 return getModel().getSize(); 169 } 170 171 /** Checks the entries in the list whose name appear in the given array. 172 172 * @param names The name of entries to be checked as a <strong>String[]</strong>. 173 173 * @see org.greenstone.gatherer.checklist.CheckList.Entry 174 174 */ 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 175 public void setSelected(String names[]) { 176 DefaultListModel model = (DefaultListModel) getModel(); 177 int size = model.size(); 178 for(int i = 0; i < size; i++) { 179 Entry entry = (Entry) model.get(i); 180 for(int j = 0; names != null && j < names.length; j++) { 181 if(entry.toString().equals(names[j])) { 182 entry.setSelected(true); 183 } 184 } 185 } 186 } 187 /** A custom list cell renderer for producing rows which contain clickable check boxes. */ 188 protected class CellRenderer 189 implements ListCellRenderer { 190 /** 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. 191 * @param list The </strong>JList</strong> we're painting. 192 * @param value The value returned by list.getModel().getElementAt(index), as an <strong>Object</strong>. 193 * @param index The cells index as an <i>int</i>. 194 * @param is_selected <i>true</i> if the specified cell was selected, <i>false</i> otherwise. 195 * @param cell_has_focus <i>true</i> if and only if the specified cell has the focus. 196 * @return A <strong>Component</strong> whose paint() method will render the specified value. 197 197 */ 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 198 public Component getListCellRendererComponent(JList list, Object value, int index, boolean is_selected, boolean cell_has_focus) { 199 JCheckBox checkbox = (JCheckBox) value; 200 checkbox.setBackground(is_selected ? list.getSelectionBackground() : list.getBackground()); 201 checkbox.setForeground(is_selected ? list.getSelectionForeground() : list.getForeground()); 202 checkbox.setEnabled(list.isEnabled()); 203 checkbox.setFont(list.getFont()); 204 checkbox.setFocusPainted(false); 205 checkbox.setBorderPainted(true); 206 checkbox.setBorder((is_selected) ? UIManager.getBorder("List.focusCellHighlightBorder") : noFocusBorder); 207 return checkbox; 208 } 209 } 210 /** Listens for clicks apon the checks within the list, and updates as necessary. */ 211 private class CheckListListener 212 extends MouseAdapter { 213 private Entry previous_checkbox = null; 214 /** Called whenever the mouse is clicked over our list. We find the nearest checkbox and change its state. 215 * @param e A <strong>MouseEvent</strong> containing everything you ever wanted to know about the mouse event but were afraid to ask. 216 */ 217 public void mousePressed(MouseEvent e) { 218 JList list = (JList) e.getSource(); 219 int index = list.locationToIndex(e.getPoint()); 220 Entry checkbox = (Entry)list.getModel().getElementAt(index); 221 221 // If this is the same checkbox as was recently selected, change the tick. 222 223 224 225 226 227 222 if(list.isSelectedIndex(index) && checkbox == previous_checkbox) { 223 if(!checkbox.isFixed()) { 224 checkbox.setSelected(!checkbox.isSelected()); 225 } 226 checkbox.grabFocus(); 227 } 228 228 // Otherwise the selection has just changed, so select the new checkbox 229 230 231 232 233 234 235 236 229 else if(list.isSelectedIndex(index)) { 230 previous_checkbox = checkbox; 231 } 232 else { 233 previous_checkbox = null; 234 } 235 } 236 } 237 237 } 238 238
Note:
See TracChangeset
for help on using the changeset viewer.