KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdnc > markup > attr > TableColumnAttributes


1 /*
2  * $Id: TableColumnAttributes.java,v 1.5 2005/01/27 13:32:01 kleopatra Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.jdnc.markup.attr;
9
10 import java.util.Map JavaDoc;
11
12 import java.awt.Component JavaDoc;
13 import javax.swing.DefaultCellEditor JavaDoc;
14 import javax.swing.DefaultListCellRenderer JavaDoc;
15 import javax.swing.JComboBox JavaDoc;
16 import javax.swing.JLabel JavaDoc;
17 import javax.swing.JList JavaDoc;
18 import javax.swing.JTable JavaDoc;
19 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
20
21 import org.w3c.dom.Element JavaDoc;
22 import org.w3c.dom.Node JavaDoc;
23
24 import org.jdesktop.swing.data.Converters;
25 import org.jdesktop.swing.data.DefaultTableModelExt;
26 import org.jdesktop.swing.LabelProperties;
27 import org.jdesktop.swing.table.TableColumnExt;
28 import org.jdesktop.swing.treetable.*;
29 import org.jdesktop.swing.data.*;
30 import org.jdesktop.swing.*;
31 import org.jdesktop.jdnc.JNTable;
32 import net.openmarkup.ApplierException;
33 import net.openmarkup.AttributeApplier;
34 import net.openmarkup.Attributes;
35 import net.openmarkup.Realizable;
36 import org.jdesktop.jdnc.markup.ElementTypes;
37 import org.jdesktop.jdnc.markup.Namespace;
38 import org.jdesktop.jdnc.markup.elem.ElementProxy;
39
40 /**
41  * @author Amy Fowler
42  * @author Ramesh Gupta
43  */

44 public class TableColumnAttributes {
45
46     /*
47          public static final AttributeApplier editableApplier = new AttributeApplier() {
48             public void apply(Realizable target, String namespaceURI,
49          String attributeName, String attributeValue) throws ApplierException {
50                 TableColumnExt column = (TableColumnExt)target.getObject();
51          column.setEditable(Boolean.valueOf(attributeValue).booleanValue());
52             }
53         };
54      */

55
56     public static final AttributeApplier backgroundApplier = new
57         AttributeApplier() {
58         public void apply(Realizable target, String JavaDoc namespaceURI,
59                           String JavaDoc attributeName, String JavaDoc attributeValue) throws
60             ApplierException {
61             TableColumnExt column = (TableColumnExt) target.getObject();
62             // Cannot set property on JNTable because columnModel has not yet
63
// been assimilated into the table.
64
// Store property to be applied later in call to applyColumnProperties
65
ComponentAttributes.applyBackground(getColumnProperties(column),
66                                                 attributeValue);
67         }
68     };
69
70     public static final AttributeApplier bindingApplier = new AttributeApplier() {
71         public void apply(Realizable target, String JavaDoc namespaceURI,
72                           String JavaDoc attributeName, String JavaDoc attributeValue) throws
73             ApplierException {
74             int columnIndex = TableColumnAttributes.getModelIndex(target,
75                 attributeValue);
76             if (columnIndex >= 0) {
77                 TableColumnExt column = (TableColumnExt) target.getObject();
78                 column.setIdentifier(attributeValue);
79                 column.setModelIndex(columnIndex);
80             }
81         }
82     };
83
84     public static int getModelIndex(Realizable target, String JavaDoc columnName) {
85         JNTable table = TableColumnAttributes.getTable(target);
86         if (table != null) {
87             Object JavaDoc model = table.getModel();
88             if (model != null) {
89                 if (model instanceof DefaultTableModelExt) {
90                     return ( (DefaultTableModelExt) model).getColumnIndex(
91                         columnName);
92                 }
93                 else if (model instanceof DOMAdapter) {
94                     return ( (DOMAdapter) model).getColumnIndex(columnName);
95                 }
96             }
97         }
98         return -1;
99     }
100
101     private static JNTable getTable(Realizable target) {
102         Node JavaDoc root = target.getOwnerDocument().getDocumentElement();
103         Node JavaDoc parent = target.getParentNode();
104         String JavaDoc nsURI = ElementTypes.TABLE.getNamespaceURI();
105         String JavaDoc tableName = ElementTypes.TABLE.getLocalName();
106         String JavaDoc treeTableName = ElementTypes.TREE_TABLE.getLocalName();
107         while (parent != root) {
108             String JavaDoc parentName = parent.getLocalName();
109             if ( (parentName.equals(tableName) ||
110                   parentName.equals(treeTableName)) &&
111                 parent.getNamespaceURI().equals(nsURI)) {
112                 Realizable tableElement = ElementProxy.getRealizable( (Element JavaDoc)
113                     parent);
114                 if (tableElement != null) {
115                     return (JNTable) tableElement.getObject();
116                 }
117                 return null;
118             }
119             parent = parent.getParentNode();
120         }
121         return null;
122     }
123
124     public static final AttributeApplier enumValuesApplier = new
125         AttributeApplier() {
126         public void apply(Realizable target, String JavaDoc namespaceURI,
127                           String JavaDoc attributeName, String JavaDoc attributeValue) throws
128             ApplierException {
129             Map JavaDoc map = (Map JavaDoc) BaseAttribute.getReferencedObject(target,
130                 attributeValue);
131             TableColumnExt column = (TableColumnExt) target.getObject();
132             /** @todo RG: Reconcile with LabelProperties */
133             TableColumnAttributes.setRenderer(column, map);
134             TableColumnAttributes.setEditor(column, map);
135         }
136     };
137
138     public static final AttributeApplier foregroundApplier = new
139         AttributeApplier() {
140         public void apply(Realizable target, String JavaDoc namespaceURI,
141                           String JavaDoc attributeName, String JavaDoc attributeValue) throws
142             ApplierException {
143             TableColumnExt column = (TableColumnExt) target.getObject();
144             // Cannot set column property on JNTable because columnModel has not yet
145
// been assimilated into the table.
146
// Store property to be applied later in call to applyColumnProperties
147
ComponentAttributes.applyForeground(getColumnProperties(column),
148                                                 attributeValue);
149         }
150     };
151
152     public static final AttributeApplier horizontalAlignmentApplier = new
153         AttributeApplier() {
154         public void apply(Realizable target, String JavaDoc namespaceURI,
155                           String JavaDoc attributeName, String JavaDoc attributeValue) throws
156             ApplierException {
157             TableColumnExt column = (TableColumnExt) target.getObject();
158             // Cannot set column property on JNTable because columnModel has not yet
159
// been assimilated into the table.
160
// Store property to be applied later in call to applyColumnProperties
161
LabelAttributes.applyHorizontalAlignment(getColumnProperties(column),
162                 attributeValue);
163         }
164     };
165
166     public static final AttributeApplier prototypeValueApplier = new
167         AttributeApplier() {
168         public void apply(Realizable target, String JavaDoc namespaceURI,
169                           String JavaDoc attributeName, String JavaDoc attributeValue) throws
170             ApplierException {
171             TableColumnExt column = (TableColumnExt) target.getObject();
172             // The attribute value must be converted to the type of the column
173
// which is not easily accessible at this point, so defer the
174
// conversion & property setting for later
175
column.putClientProperty("prototypeValueString", attributeValue);
176         }
177     };
178
179     public static final AttributeApplier isReadOnlyApplier = new
180         AttributeApplier() {
181         public void apply(Realizable target, String JavaDoc namespaceURI,
182                           String JavaDoc attributeName, String JavaDoc attributeValue) throws
183             ApplierException {
184             TableColumnExt column = (TableColumnExt) target.getObject();
185             // Note the negation! If readOnly="true", then editable=false;
186
column.setEditable(!Boolean.valueOf(attributeValue).booleanValue());
187         }
188     };
189
190     public static final AttributeApplier resizableApplier = new
191         AttributeApplier() {
192         public void apply(Realizable target, String JavaDoc namespaceURI,
193                           String JavaDoc attributeName, String JavaDoc attributeValue) throws
194             ApplierException {
195             TableColumnExt column = (TableColumnExt) target.getObject();
196             column.setResizable(Boolean.valueOf(attributeValue).booleanValue());
197         }
198     };
199
200     public static final AttributeApplier titleApplier = new AttributeApplier() {
201         public void apply(Realizable target, String JavaDoc namespaceURI,
202                           String JavaDoc attributeName, String JavaDoc attributeValue) throws
203             ApplierException {
204             TableColumnExt column = (TableColumnExt) target.getObject();
205             column.setTitle(attributeValue);
206         }
207     };
208
209 /*
210  how practical is it to be able to set vertical alignment per column?
211
212     public static final AttributeApplier verticalAlignmentApplier = new
213         AttributeApplier() {
214         public void apply(Realizable target, String namespaceURI,
215                           String attributeName, String attributeValue) throws
216             ApplierException {
217             TableColumnExt column = (TableColumnExt) target.getObject();
218             LabelAttributes.applyVerticalAlignment(getColumnProperties(column),
219                 attributeValue);
220         }
221     };
222  */

223
224     public static final AttributeApplier preferredWidthApplier = new
225         AttributeApplier() {
226         public void apply(Realizable target, String JavaDoc namespaceURI,
227                           String JavaDoc attributeName, String JavaDoc attributeValue) throws
228             ApplierException {
229             TableColumnExt column = (TableColumnExt) target.getObject();
230             /** @todo review attribute name: call it "preferredWidth"? */
231             column.setPreferredWidth(Integer.parseInt(attributeValue));
232         }
233     };
234
235     // convenience method for obtaining the temporarily stashed LabelProperties
236
// object for a given column
237
private static LabelProperties getColumnProperties(TableColumnExt column) {
238         LabelProperties props = (LabelProperties) column.getClientProperty(
239             "LabelProperties");
240         if (props == null) {
241             props = new LabelProperties();
242             column.putClientProperty("LabelProperties", props);
243         }
244         return props;
245     }
246
247     private static void setRenderer(final TableColumnExt tableColumn,
248                                     final Map JavaDoc map) {
249         tableColumn.setCellRenderer(new DefaultTableCellRenderer JavaDoc() {
250             public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table,
251                 Object JavaDoc value, boolean isSelected, boolean hasFocus, int row,
252                 int column) {
253                 JLabel JavaDoc label = (JLabel JavaDoc)super.getTableCellRendererComponent(
254                     table, value, isSelected, hasFocus, row, column);
255                 // RG: value could be null
256
JLabel JavaDoc fromLabel = value == null ? null :
257                     (JLabel JavaDoc) map.get(value.toString());
258                 return decorate(fromLabel, label, value);
259             }
260         });
261     }
262
263     private static void setEditor(final TableColumnExt tableColumn,
264                                   final Map JavaDoc map) {
265         JComboBox JavaDoc labelComboBox = new JComboBox JavaDoc(map.values().toArray());
266
267         labelComboBox.setRenderer(new DefaultListCellRenderer JavaDoc() {
268             public Component JavaDoc getListCellRendererComponent(JList JavaDoc list,
269                 Object JavaDoc value, int index, boolean isSelected, boolean hasFocus) {
270                 JLabel JavaDoc label = (JLabel JavaDoc)super.getListCellRendererComponent(list,
271                     value, index, isSelected, hasFocus);
272                 return decorate( (JLabel JavaDoc) value, label, value);
273             }
274         });
275
276         tableColumn.setCellEditor(new DefaultCellEditor JavaDoc(labelComboBox) {
277             public Component JavaDoc getTableCellEditorComponent(JTable JavaDoc table,
278                 Object JavaDoc value, boolean isSelected, int row, int column) {
279                 if ( (value != null) && tableColumn.isEditable()) {
280                     JComboBox JavaDoc comboBox =
281                         (JComboBox JavaDoc)super.getTableCellEditorComponent(
282                         table, value, isSelected, row, column);
283                     comboBox.setSelectedItem(map.get(value.toString()));
284                     return comboBox;
285                 }
286                 return null;
287             }
288
289             public Object JavaDoc getCellEditorValue() {
290                 JLabel JavaDoc label = (JLabel JavaDoc)super.getCellEditorValue();
291
292                 if (label == null) {
293                     return null;
294                 }
295                 else {
296                     Object JavaDoc value = label.getClientProperty(Namespace.JDNC + ":" +
297                         Attributes.VALUE);
298                     try {
299                         /**@todo (aim): temp breakage since we removed table backpointer from
300                           * TableColumnExt - need to figure out how to resolve this...
301                           */

302
303                         // M4 warning: Does not work with JXTreeTable
304
/*
305                         JXTable table = (JXTable) tableColumn.getTable();
306
307                         DefaultTableModelExt model = (DefaultTableModelExt) table.
308                             getModel();
309                         MetaData columnMetaData = model.getColumnMetaData(
310                             tableColumn.getModelIndex());
311                         Converter converter = columnMetaData.getConverter();
312                         return converter.decode( (String) value, null);
313                             */

314                            return value;
315                     }
316                     catch (Exception JavaDoc e) {
317                     }
318                     return value;
319                 }
320             }
321         });
322     }
323
324     private static Component JavaDoc decorate(JLabel JavaDoc fromLabel, JLabel JavaDoc toLabel,
325                                       Object JavaDoc value) {
326         if (fromLabel == null) {
327             toLabel.setHorizontalAlignment(JLabel.LEADING);
328             toLabel.setHorizontalTextPosition(JLabel.LEADING);
329             toLabel.setIcon(null);
330             toLabel.setText(value == null ? "" : value.toString()); // fallback value
331
}
332         else {
333             toLabel.setHorizontalAlignment(fromLabel.getHorizontalAlignment());
334             toLabel.setHorizontalTextPosition(fromLabel.
335                                               getHorizontalTextPosition());
336             toLabel.setIcon(fromLabel.getIcon());
337             toLabel.setText(fromLabel.getText());
338         }
339         return toLabel;
340     }
341 }
Popular Tags