1 22 23 package org.gjt.sp.jedit.gui; 24 25 import java.awt.Component ; 27 import java.awt.Font ; 28 import java.util.Vector ; 29 import javax.swing.*; 30 import javax.swing.table.*; 31 33 37 public class JCheckBoxList extends JTable 38 { 39 45 public JCheckBoxList(Object [] items) 46 { 47 setModel(items); 48 } 50 56 public JCheckBoxList(Vector items) 57 { 58 setModel(items); 59 } 61 67 public void setModel(Object [] items) 68 { 69 setModel(new CheckBoxListModel(items)); 70 init(); 71 } 73 79 public void setModel(Vector items) 80 { 81 setModel(new CheckBoxListModel(items)); 82 init(); 83 } 85 public Object [] getCheckedValues() 87 { 88 Vector values = new Vector (); 89 CheckBoxListModel model = (CheckBoxListModel)getModel(); 90 for(int i = 0; i < model.items.size(); i++) 91 { 92 Entry entry = (Entry)model.items.elementAt(i); 93 if(entry.checked && !entry.caption) 94 values.addElement(entry.value); 95 } 96 97 Object [] retVal = new Object [values.size()]; 98 values.copyInto(retVal); 99 return retVal; 100 } 102 public void selectAll() 104 { 105 CheckBoxListModel model = (CheckBoxListModel)getModel(); 106 for(int i = 0; i < model.items.size(); i++) 107 { 108 Entry entry = (Entry)model.items.elementAt(i); 109 if(!entry.caption) 110 entry.checked = true; 111 } 112 113 model.fireTableRowsUpdated(0,model.getRowCount()); 114 } 116 public Entry[] getValues() 118 { 119 CheckBoxListModel model = (CheckBoxListModel)getModel(); 120 Entry[] retVal = new Entry[model.items.size()]; 121 model.items.copyInto(retVal); 122 return retVal; 123 } 125 public Object getSelectedValue() 127 { 128 int row = getSelectedRow(); 129 if(row == -1) 130 return null; 131 else 132 return getModel().getValueAt(row,1); 133 } 135 public TableCellRenderer getCellRenderer(int row, int column) 137 { 138 if(column == 0) 139 { 140 Entry entry = (Entry)((CheckBoxListModel)getModel()).items.get(row); 141 if(entry.caption) 142 return dummy; 143 } 144 145 return super.getCellRenderer(row,column); 146 } 148 private TableCellRenderer dummy; 150 151 private void init() 153 { 154 dummy = new DummyRenderer(); 155 getSelectionModel().setSelectionMode(ListSelectionModel 156 .SINGLE_SELECTION); 157 setShowGrid(false); 158 setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); 159 TableColumn column = getColumnModel().getColumn(0); 160 int checkBoxWidth = new JCheckBox().getPreferredSize().width; 161 column.setPreferredWidth(checkBoxWidth); 162 column.setMinWidth(checkBoxWidth); 163 column.setWidth(checkBoxWidth); 164 column.setMaxWidth(checkBoxWidth); 165 column.setResizable(false); 166 167 column = getColumnModel().getColumn(1); 168 column.setCellRenderer(new LabelRenderer()); 169 } 171 173 177 public static class Entry 178 { 179 boolean checked; 180 boolean caption; 181 Object value; 182 183 public Entry(Object value) 184 { 185 this.caption = true; 186 this.value = value; 187 } 188 189 public Entry(boolean checked, Object value) 190 { 191 this.checked = checked; 192 this.value = value; 193 } 194 195 public boolean isChecked() 196 { 197 return checked; 198 } 199 200 public Object getValue() 201 { 202 return value; 203 } 204 } 206 private class DummyRenderer extends DefaultTableCellRenderer 208 { 209 public Component getTableCellRendererComponent(JTable table, Object value, 210 boolean isSelected, boolean hasFocus, int row, int column) 211 { 212 return super.getTableCellRendererComponent(table,null , 213 isSelected,false ,row,column); 214 } 215 } 217 private class LabelRenderer extends DefaultTableCellRenderer 219 { 220 Font plainFont, boldFont; 221 222 LabelRenderer() 223 { 224 plainFont = UIManager.getFont("Tree.font"); 225 boldFont = plainFont.deriveFont(Font.BOLD); 226 } 227 228 public Component getTableCellRendererComponent(JTable table, Object value, 229 boolean isSelected, boolean hasFocus, int row, int column) 230 { 231 super.getTableCellRendererComponent(table,value,isSelected, 232 hasFocus,row,column); 233 234 Entry entry = (Entry)((CheckBoxListModel)getModel()).items.get(row); 235 if(entry.caption) 236 setFont(boldFont); 237 else 238 setFont(plainFont); 239 return this; 240 } 241 } } 243 244 class CheckBoxListModel extends AbstractTableModel 245 { 246 Vector items; 247 248 CheckBoxListModel(Vector _items) 249 { 250 items = new Vector (_items.size()); 251 for(int i = 0; i < _items.size(); i++) 252 { 253 items.addElement(createEntry(_items.elementAt(i))); 254 } 255 } 256 257 CheckBoxListModel(Object [] _items) 258 { 259 items = new Vector (_items.length); 260 for(int i = 0; i < _items.length; i++) 261 { 262 items.addElement(createEntry(_items[i])); 263 } 264 } 265 266 private JCheckBoxList.Entry createEntry(Object obj) 267 { 268 if(obj instanceof JCheckBoxList.Entry) 269 return (JCheckBoxList.Entry)obj; 270 else 271 return new JCheckBoxList.Entry(false,obj); 272 } 273 274 public int getRowCount() 275 { 276 return items.size(); 277 } 278 279 public int getColumnCount() 280 { 281 return 2; 282 } 283 284 public String getColumnName(int col) 285 { 286 return null; 287 } 288 289 public Object getValueAt(int row, int col) 290 { 291 JCheckBoxList.Entry entry = (JCheckBoxList.Entry)items.elementAt(row); 292 switch(col) 293 { 294 case 0: 295 return new Boolean (entry.checked); 296 case 1: 297 return entry.value; 298 default: 299 throw new InternalError (); 300 } 301 } 302 303 public Class getColumnClass(int col) 304 { 305 switch(col) 306 { 307 case 0: 308 return Boolean .class; 309 case 1: 310 return String .class; 311 default: 312 throw new InternalError (); 313 } 314 } 315 316 public boolean isCellEditable(int row, int col) 317 { 318 JCheckBoxList.Entry entry = (JCheckBoxList.Entry)items.elementAt(row); 319 return col == 0 && !entry.caption; 320 } 321 322 public void setValueAt(Object value, int row, int col) 323 { 324 if(col == 0) 325 { 326 JCheckBoxList.Entry entry = (JCheckBoxList.Entry)items.elementAt(row); 327 if(!entry.caption) 328 { 329 entry.checked = (value.equals(Boolean.TRUE)); 330 fireTableRowsUpdated(row,row); 331 } 332 } 333 } 334 } 335 | Popular Tags |