1 19 24 package org.openide.explorer.propertysheet; 25 26 import org.openide.nodes.Node.Property; 27 import org.openide.nodes.Node.PropertySet; 28 import org.openide.util.NbBundle; 29 30 import java.beans.*; 31 32 import java.util.ArrayList ; 33 import java.util.List ; 34 import java.util.logging.Level ; 35 import java.util.logging.Logger ; 36 import javax.swing.event.*; 37 import javax.swing.table.*; 38 39 40 47 final class SheetTableModel implements TableModel, PropertyChangeListener, PropertySetModelListener { 48 49 private transient List <TableModelListener> tableModelListenerList; 50 51 52 PropertySetModel model = null; 53 54 55 public SheetTableModel() { 56 } 57 58 public SheetTableModel(PropertySetModel psm) { 59 setPropertySetModel(psm); 60 } 61 62 66 public void setPropertySetModel(PropertySetModel mod) { 67 if (this.model == mod) { 68 return; 69 } 70 71 if (model != null) { 72 model.removePropertySetModelListener(this); 73 } 74 75 model = mod; 76 77 if (model == null) { 78 throw new IllegalArgumentException ("Model cannot be null"); 79 } 80 81 mod.addPropertySetModelListener(this); 84 85 model = mod; 86 87 fireTableChanged(new TableModelEvent(this)); } 89 90 91 public PropertySetModel getPropertySetModel() { 92 return model; 93 } 94 95 97 public Class getColumnClass(int columnIndex) { 98 switch (columnIndex) { 99 case 0: 100 return String .class; 101 102 case 1: 103 return Object .class; 104 } 105 106 throw new IllegalArgumentException ("Column " + columnIndex + " does not exist."); } 108 109 110 public int getColumnCount() { 111 return 2; 112 } 113 114 117 public String getColumnName(int columnIndex) { 118 if (columnIndex == 0) { 119 return NbBundle.getMessage(SheetTableModel.class, "COLUMN_NAMES"); } 121 122 return NbBundle.getMessage(SheetTableModel.class, "COLUMN_VALUES"); } 124 125 public int getRowCount() { 126 if (model == null) { 129 return 0; 130 } 131 132 return model.getCount(); 134 } 135 136 public Object getValueAt(int rowIndex, int columnIndex) { 137 Object result; 138 139 if (rowIndex == -1) { 140 result = null; 141 } else { 142 result = model.getFeatureDescriptor(rowIndex); 143 } 144 145 return result; 146 } 147 148 public boolean isCellEditable(int rowIndex, int columnIndex) { 149 if (columnIndex == 0) { 151 return false; 152 } 153 154 if (columnIndex == 1) { 155 FeatureDescriptor fd = model.getFeatureDescriptor(rowIndex); 156 157 if (fd instanceof PropertySet) { 160 return false; 161 } 162 163 return ((Property) fd).canWrite(); 164 } 165 166 throw new IllegalArgumentException ( 167 "Illegal row/column: " + Integer.toString(rowIndex) + Integer.toString(columnIndex) 168 ); } 170 171 public void setValueAt(Object aValue, int rowIndex, int columnIndex) { 172 if (columnIndex == 0) { 173 throw new IllegalArgumentException ("Cannot set property names."); 174 } 175 176 try { 177 FeatureDescriptor fd = model.getFeatureDescriptor(rowIndex); 178 179 if (fd instanceof Property) { 180 Property p = (Property) fd; 181 p.setValue(aValue); 182 } else { 183 throw new IllegalArgumentException ( 184 "Index " + Integer.toString(rowIndex) + Integer.toString(columnIndex) + 185 " does not represent a property. " 186 ); } 188 } catch (IllegalAccessException iae) { 189 Logger.getLogger(SheetTableModel.class.getName()).log(Level.WARNING, null, iae); 190 } catch (java.lang.reflect.InvocationTargetException ite) { 191 Logger.getLogger(SheetTableModel.class.getName()).log(Level.WARNING, null, ite); 192 } 193 } 194 195 198 public String getDescriptionFor(int row, int column) { 199 if ((row == -1) || (column == -1)) { 200 return ""; } 202 203 FeatureDescriptor fd = model.getFeatureDescriptor(row); 204 Property p = (fd instanceof Property) ? (Property) fd : null; 205 String result = null; 206 207 if (p != null) { 208 try { 209 if (column == 0) { 211 result = p.getShortDescription(); 212 } else { 213 PropertyEditor ped = PropUtils.getPropertyEditor (p); 214 if (ped != null) { 215 result = ped.getAsText(); 216 } else { 217 if (p.getValueType() == String .class) { 220 String s = (String ) p.getValue(); 221 if (s != null && s.length() > 2048) { 222 return ""; 223 } else { 224 return s; 225 } 226 } 227 } 228 } 229 } catch (Exception e) { 230 result = (column == 0) ? p.getShortDescription() : e.toString(); 232 } 233 } else { 234 PropertySet ps = (PropertySet) fd; 235 result = ps.getShortDescription(); 236 } 237 238 if (result == null) { 239 result = ""; } 241 242 return result; 243 } 244 245 public synchronized void addTableModelListener(TableModelListener listener) { 247 if (tableModelListenerList == null) { 248 tableModelListenerList = new java.util.ArrayList <TableModelListener>(); 249 } 250 251 tableModelListenerList.add(listener); 252 } 253 254 public synchronized void removeTableModelListener(TableModelListener listener) { 255 if (tableModelListenerList != null) { 256 tableModelListenerList.remove(listener); 257 } 258 } 259 260 void fireTableChanged(javax.swing.event.TableModelEvent event) { 262 List list; 263 264 synchronized (this) { 265 if (tableModelListenerList == null) { 266 return; 267 } 268 269 list = (List ) ((ArrayList ) tableModelListenerList).clone(); 270 } 271 272 for (int i = 0; i < list.size(); i++) { 273 ((TableModelListener) list.get(i)).tableChanged(event); 274 } 275 } 276 277 279 281 public void propertyChange(PropertyChangeEvent evt) { 282 int index = getPropertySetModel().indexOf((FeatureDescriptor) evt.getSource()); 283 284 if (index == -1) { 285 fireTableChanged(new TableModelEvent(this)); 287 } else { 288 TableModelEvent tme = new TableModelEvent(this, index); 289 fireTableChanged(tme); 290 } 291 } 292 293 295 296 public void boundedChange(PropertySetModelEvent e) { 297 TableModelEvent tme = new TableModelEvent( 299 this, e.start, e.end, TableModelEvent.ALL_COLUMNS, 300 (e.type == e.TYPE_INSERT) ? TableModelEvent.INSERT : TableModelEvent.DELETE 301 ); 302 fireTableChanged(tme); 303 } 304 305 306 public void wholesaleChange(PropertySetModelEvent e) { 307 fireTableChanged(new TableModelEvent(this) ); 309 } 310 311 public void pendingChange(PropertySetModelEvent e) { 312 } 315 } 316 | Popular Tags |