KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > propertysheet > SheetTableModel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * SheetTableModel.java
21  *
22  * Created on December 13, 2002, 7:36 PM
23  */

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 JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.logging.Level JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36 import javax.swing.event.*;
37 import javax.swing.table.*;
38
39
40 /** Table model for property sheet. Note that sort order and management of
41  * expanded/unexpanded property sets is handled by the
42  * PropertySetModel attached to the SheetTableModel.
43  * This class is mainly a wrapper for the underlying
44  * PropertySetModel, which also handles expansion/closing of sets.
45  * @author Tim Boudreau
46  */

47 final class SheetTableModel implements TableModel, PropertyChangeListener, PropertySetModelListener {
48     /** Utility field holding list of TableModelListeners. */
49     private transient List JavaDoc<TableModelListener> tableModelListenerList;
50
51     /** Container variable for property set model. */
52     PropertySetModel model = null;
53
54     /** Creates a new instance of SheetTableModel */
55     public SheetTableModel() {
56     }
57
58     public SheetTableModel(PropertySetModel psm) {
59         setPropertySetModel(psm);
60     }
61
62     /** The property set model is a model-within-a-model which
63      * manages the expanded/unexpanded state of expandable
64      * property sets and handles the sorting of properties
65      * within a property set */

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 JavaDoc("Model cannot be null");
79         }
80
81         //set the node before adding listener so we don't get duplicate
82
//events
83
mod.addPropertySetModelListener(this);
84
85         model = mod;
86
87         fireTableChanged(new TableModelEvent(this)); //XXX optimize rows & stuff
88
}
89
90     /**Get the property set model this table is using*/
91     public PropertySetModel getPropertySetModel() {
92         return model;
93     }
94
95     /** Returns String for the names column, and Object for the
96      * values column. */

97     public Class JavaDoc getColumnClass(int columnIndex) {
98         switch (columnIndex) {
99         case 0:
100             return String JavaDoc.class;
101
102         case 1:
103             return Object JavaDoc.class;
104         }
105
106         throw new IllegalArgumentException JavaDoc("Column " + columnIndex + " does not exist."); //NOI18N
107
}
108
109     /** The column count will always be 2 - names and values. */
110     public int getColumnCount() {
111         return 2;
112     }
113
114     /** This is not really used for anything in property sheet, since
115      * column headings aren't displayed - but an alternative look and
116      * feel might have other ideas.*/

117     public String JavaDoc getColumnName(int columnIndex) {
118         if (columnIndex == 0) {
119             return NbBundle.getMessage(SheetTableModel.class, "COLUMN_NAMES"); //NOI18N
120
}
121
122         return NbBundle.getMessage(SheetTableModel.class, "COLUMN_VALUES"); //NOI18N
123
}
124
125     public int getRowCount() {
126         //JTable init will call this before the constructor is
127
//completed (!!), so handle this case
128
if (model == null) {
129             return 0;
130         }
131
132         //get the count from the model - will depend on what is expanded.
133
return model.getCount();
134     }
135
136     public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
137         Object JavaDoc 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 column is 0, it's the property name - can't edit that
150
if (columnIndex == 0) {
151             return false;
152         }
153
154         if (columnIndex == 1) {
155             FeatureDescriptor fd = model.getFeatureDescriptor(rowIndex);
156
157             //no worries, editCellAt() will expand it and return before
158
//this method is called
159
if (fd instanceof PropertySet) {
160                 return false;
161             }
162
163             return ((Property) fd).canWrite();
164         }
165
166         throw new IllegalArgumentException JavaDoc(
167             "Illegal row/column: " + Integer.toString(rowIndex) + Integer.toString(columnIndex)
168         ); //NOI18N
169
}
170
171     public void setValueAt(Object JavaDoc aValue, int rowIndex, int columnIndex) {
172         if (columnIndex == 0) {
173             throw new IllegalArgumentException JavaDoc("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 JavaDoc(
184                     "Index " + Integer.toString(rowIndex) + Integer.toString(columnIndex) +
185                     " does not represent a property. "
186                 ); //NOI18N
187
}
188         } catch (IllegalAccessException JavaDoc iae) {
189             Logger.getLogger(SheetTableModel.class.getName()).log(Level.WARNING, null, iae);
190         } catch (java.lang.reflect.InvocationTargetException JavaDoc ite) {
191             Logger.getLogger(SheetTableModel.class.getName()).log(Level.WARNING, null, ite);
192         }
193     }
194
195     /** Utility method that returns the short description
196      * of the property in question,
197      * used by the table to supply tooltips. */

198     public String JavaDoc getDescriptionFor(int row, int column) {
199         if ((row == -1) || (column == -1)) {
200             return ""; //NOI18N
201
}
202
203         FeatureDescriptor fd = model.getFeatureDescriptor(row);
204         Property p = (fd instanceof Property) ? (Property) fd : null;
205         String JavaDoc result = null;
206
207         if (p != null) {
208             try {
209                 //try to get the short description, fall back to the value
210
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                          //IZ 44152, Debugger can produce > 512K strings, so add
218
//some special handling for very long strings
219
if (p.getValueType() == String JavaDoc.class) {
220                              String JavaDoc s = (String JavaDoc) p.getValue();
221                              if (s != null && s.length() > 2048) {
222                                  return "";
223                              } else {
224                                  return s;
225                              }
226                          }
227                      }
228                 }
229             } catch (Exception JavaDoc e) {
230                 //Suppress the exception, this is a tooltip
231
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 = ""; //NOI18N
240
}
241
242         return result;
243     }
244
245     //**************Table model listener support *************************
246
public synchronized void addTableModelListener(TableModelListener listener) {
247         if (tableModelListenerList == null) {
248             tableModelListenerList = new java.util.ArrayList JavaDoc<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     //Setting to package access to hack a checkbox painting bug
261
void fireTableChanged(javax.swing.event.TableModelEvent JavaDoc event) {
262         List JavaDoc list;
263
264         synchronized (this) {
265             if (tableModelListenerList == null) {
266                 return;
267             }
268
269             list = (List JavaDoc) ((ArrayList JavaDoc) tableModelListenerList).clone();
270         }
271
272         for (int i = 0; i < list.size(); i++) {
273             ((TableModelListener) list.get(i)).tableChanged(event);
274         }
275     }
276
277     //*************PropertyChangeListener implementation***********
278

279     /** Called when a property value changes, in order to update
280      * the UI with the new value. */

281     public void propertyChange(PropertyChangeEvent evt) {
282         int index = getPropertySetModel().indexOf((FeatureDescriptor) evt.getSource());
283
284         if (index == -1) {
285             //We don't know what happened, do a generic change event
286
fireTableChanged(new TableModelEvent(this));
287         } else {
288             TableModelEvent tme = new TableModelEvent(this, index);
289             fireTableChanged(tme);
290         }
291     }
292
293     //*************PropertySetModelListener implementation***********
294

295     /**Implementation of PropertySetModelListener.boundedChange() */
296     public void boundedChange(PropertySetModelEvent e) {
297         //XXX should just have the set model fire a tablemodelevent
298
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     /**Implementation of PropertySetModelListener.wholesaleChange() */
306     public void wholesaleChange(PropertySetModelEvent e) {
307         fireTableChanged(new TableModelEvent(this) //XXX optimize rows & stuff
308
);
309     }
310
311     public void pendingChange(PropertySetModelEvent e) {
312         //Do nothing, the table is also listening in order to save
313
//its editing state if appropriate
314
}
315 }
316
Popular Tags