1 7 8 package org.jdesktop.swing.table; 9 10 import java.beans.Expression ; 11 import java.beans.PropertyChangeEvent ; 12 import java.beans.PropertyChangeListener ; 13 import java.beans.PropertyChangeSupport ; 14 15 import java.awt.Component ; 16 17 import java.lang.reflect.Constructor ; 18 19 import java.util.Hashtable ; 20 21 import javax.swing.JTable ; 22 import javax.swing.table.TableCellEditor ; 23 import javax.swing.table.TableCellRenderer ; 24 25 import org.jdesktop.swing.LabelProperties; 26 import org.jdesktop.swing.data.Converters; 27 import org.jdesktop.swing.data.Converter; 28 import org.jdesktop.swing.decorator.Sorter; 29 30 37 public class TableColumnExt extends javax.swing.table.TableColumn 38 implements Cloneable { 39 40 protected boolean editable = true; 41 protected boolean visible = true; 42 protected Object prototypeValue = null; 43 44 private Hashtable clientProperties = null; 45 46 protected Sorter sorter = null; 47 private Constructor sorterConstructor = null; 48 private final static Constructor defaultSorterConstructor; 49 private final static Class [] sorterConstructorSignature = 50 new Class []{int.class, boolean.class}; 51 52 static { 53 Constructor constructor = null; 54 try { 55 Class sorterClass = Class.forName("org.jdesktop.swing.decorator.ShuttleSorter", true, 56 TableColumnExt.class.getClassLoader()); 57 constructor = sorterClass.getConstructor(sorterConstructorSignature); 58 } 59 catch (Exception ex) { 60 } 61 defaultSorterConstructor = constructor; 62 } 63 64 67 public TableColumnExt() { 68 this(0); 69 } 70 71 76 public TableColumnExt(int modelIndex) { 77 this(modelIndex, 75); } 79 80 86 public TableColumnExt(int modelIndex, int width) { 87 this(modelIndex, width, null, null); 88 } 89 90 100 public TableColumnExt(int modelIndex, int width, 101 TableCellRenderer cellRenderer, TableCellEditor cellEditor) { 102 super(modelIndex, width, cellRenderer, cellEditor); 103 this.sorterConstructor = defaultSorterConstructor; 104 } 105 106 121 public void setEditable(boolean editable) { 122 boolean oldEditable = this.editable; 123 this.editable = editable; 124 firePropertyChange("editable", 125 Boolean.valueOf(oldEditable), 126 Boolean.valueOf(editable)); 127 } 128 129 134 public boolean isEditable() { 135 return editable; 136 } 137 138 150 public void setPrototypeValue(Object value) { 151 Object oldPrototypeValue = this.prototypeValue; 152 this.prototypeValue = value; 153 firePropertyChange("prototypeValue", 154 oldPrototypeValue, 155 value); 156 157 } 158 159 164 public Object getPrototypeValue() { 165 return prototypeValue; 166 } 167 168 173 public void setSorterClass(String sorterClassName) { 174 if ((sorterClassName == null) || (sorterClassName.length() == 0)){ 175 sorterConstructor = null; 176 } 177 else { 178 try { 179 Class sorterClass = Class.forName(sorterClassName, true, 180 getClass().getClassLoader()); 181 sorterConstructor = sorterClass.getConstructor(sorterConstructorSignature); 182 } 183 catch (Exception ex) { 184 sorterConstructor = null; 185 } 186 } 187 } 188 189 194 public String getSorterClass() { 195 return sorterConstructor == null ? null : 196 sorterConstructor.getDeclaringClass().getName(); 197 } 198 199 203 public Sorter getSorter() { 204 if (sorter == null) { 205 if (sorterConstructor != null) { 206 try { 207 sorter = (Sorter) sorterConstructor.newInstance( 208 new Object [] { 209 new Integer (getModelIndex()), 210 new Boolean (true)}); 211 } 212 catch (Exception ex) { 213 } 214 } 215 } 216 return sorter; 217 } 218 219 223 public boolean isSortable() { 224 return sorterConstructor != null; 225 } 226 227 232 public void setTitle(String title) { 233 setHeaderValue(title); } 235 236 241 public String getTitle() { 242 return getHeaderValue().toString(); } 244 245 252 public void setVisible(boolean visible) { 253 boolean oldVisible = this.visible; 254 this.visible = visible; 255 firePropertyChange("visible", 256 Boolean.valueOf(oldVisible), 257 Boolean.valueOf(visible)); 258 259 } 260 261 266 public boolean isVisible() { 267 return visible; 268 } 269 270 276 public void putClientProperty(Object key, Object value) { 277 if (key == null) 278 throw new IllegalArgumentException ("null key"); 279 280 if ((value == null) && (getClientProperty(key) == null)) { 281 return; 282 } 283 284 if (value == null) { 285 getClientProperties().remove(key); 286 } 287 else { 288 getClientProperties().put(key, value); 289 } 290 291 293 } 294 295 301 public Object getClientProperty(Object key) { 302 return ((key == null) || (clientProperties == null)) ? 303 null : clientProperties.get(key); 304 } 305 306 private Hashtable getClientProperties() { 307 if (clientProperties == null) { 308 clientProperties = new Hashtable (); 309 } 310 return clientProperties; 311 } 312 313 322 public Object clone() { 323 final TableColumnExt copy = new TableColumnExt( 324 this.getModelIndex(), this.getWidth(), 325 this.getCellRenderer(), this.getCellEditor()); 326 327 copy.setEditable(this.isEditable()); 328 copy.setHeaderValue(this.getHeaderValue()); copy.setIdentifier(this.getIdentifier()); 330 copy.setMaxWidth(this.getMaxWidth()); 331 copy.setMinWidth(this.getMinWidth()); 332 copy.setPreferredWidth(this.getPreferredWidth()); 333 copy.setPrototypeValue(this.getPrototypeValue()); 334 copy.setResizable(this.getResizable()); 335 copy.setVisible(this.isVisible()); 336 copy.setSorterClass(this.getSorterClass()); 337 copy.sorterConstructor = sorterConstructor; 338 return copy; 339 } 340 341 protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { 342 if ((oldValue != null && !oldValue.equals(newValue)) || 343 oldValue == null && newValue != null) { 344 PropertyChangeListener pcl[] = getPropertyChangeListeners(); 345 if (pcl != null && pcl.length != 0) { 346 PropertyChangeEvent pce = new PropertyChangeEvent (this, 347 propertyName, 348 oldValue, newValue); 349 350 for (int i = 0; i < pcl.length; i++) { 351 pcl[i].propertyChange(pce); 352 } 353 } 354 } 355 } 356 } 357 | Popular Tags |