KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdnc > markup > elem > TableElement


1 /*
2  * $Id: TableElement.java,v 1.5 2005/01/27 13:31:59 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.elem;
9
10 import java.util.Hashtable JavaDoc;
11 import java.util.Map JavaDoc;
12 import java.util.Vector JavaDoc;
13
14 import javax.swing.Action JavaDoc;
15 import javax.swing.JComponent JavaDoc;
16 import javax.swing.table.*;
17 import javax.swing.table.TableColumnModel JavaDoc;
18
19 import org.jdesktop.swing.LabelProperties;
20 import org.jdesktop.swing.data.ConversionException;
21 import org.jdesktop.swing.data.Converter;
22 import org.jdesktop.swing.data.Converters;
23 import org.jdesktop.swing.data.MetaData;
24 import org.jdesktop.swing.data.MetaDataProvider;
25 import org.jdesktop.swing.data.DefaultTableModelExt;
26 import org.jdesktop.swing.decorator.Filter;
27 import org.jdesktop.swing.decorator.FilterPipeline;
28 import org.jdesktop.swing.decorator.Highlighter;
29 import org.jdesktop.swing.decorator.HighlighterPipeline;
30 import org.jdesktop.swing.table.TableColumnExt;
31 import org.jdesktop.swing.utils.LoadOnShowListener;
32 import org.jdesktop.jdnc.JNTable;
33 import org.jdesktop.jdnc.markup.Attributes;
34 import org.jdesktop.jdnc.markup.ElementTypes;
35 import org.jdesktop.jdnc.markup.Namespace;
36 import org.jdesktop.jdnc.markup.attr.ComponentAttributes;
37 import org.jdesktop.jdnc.markup.attr.TableAttributes;
38 import org.jdesktop.jdnc.markup.attr.NullAttribute;
39 import net.openmarkup.AttributeHandler;
40 import net.openmarkup.ElementAssimilator;
41 import net.openmarkup.ElementHandler;
42 import net.openmarkup.ElementType;
43 import net.openmarkup.Realizable;
44
45 import org.w3c.dom.Element JavaDoc;
46 import org.w3c.dom.NodeList JavaDoc;
47
48 /**
49  *
50  * @author Amy Fowler
51  * @author Ramesh Gupta
52  */

53 public class TableElement extends ComponentElement {
54     private static final Map JavaDoc attrMap = new Hashtable JavaDoc();
55     private static final Map JavaDoc elementMap = new Hashtable JavaDoc();
56
57     public TableElement(Element JavaDoc element, ElementType elementType) {
58         super(element, elementType);
59     }
60
61     protected Map JavaDoc getAttributeHandlerMap() {
62         return attrMap;
63     }
64
65     protected Map JavaDoc getElementHandlerMap() {
66         return elementMap;
67     }
68
69     protected void assimilateChildren() {
70         applyAttribute(Namespace.JDNC, Attributes.DATA);
71         super.assimilateChildren();
72     }
73
74     protected void applyAttributesBefore() {
75         /** @todo decide precedence of data and source attributes (when both are present) */
76         applyAttribute(Namespace.JDNC, Attributes.DATA);
77         applyAttribute(Namespace.JDNC, Attributes.DATA_SOURCE);
78         applyAttribute(Namespace.JDNC, Attributes.IS_FIRST_ROW_HEADER);
79     }
80
81     protected void applyAttributesAfter() {
82         super.applyAttributesAfter();
83         applyAttribute(Namespace.JDNC, Attributes.HAS_COLUMN_CONTROL);
84         applyAttribute(Namespace.JDNC, Attributes.COLUMN_MARGIN);
85         applyAttribute(Namespace.JDNC, Attributes.GRID_COLOR);
86         applyAttribute(Namespace.JDNC, Attributes.PREFERRED_SIZE);
87         applyAttribute(Namespace.JDNC, Attributes.ROW_HEIGHT);
88         applyAttribute(Namespace.JDNC, Attributes.ROW_MARGIN);
89         applyAttribute(Namespace.JDNC, Attributes.SELECTION);
90         applyAttribute(Namespace.JDNC, Attributes.SELECTION_MODE);
91         applyAttribute(Namespace.JDNC, Attributes.SHOWS_HORIZONTAL_LINES);
92         applyAttribute(Namespace.JDNC, Attributes.SHOWS_VERTICAL_LINES);
93         applyAttribute(Namespace.JDNC, Attributes.SORTABLE);
94         applyAttribute(Namespace.JDNC, Attributes.SIZE); // WIDTH and HEIGHT
95
applyAttribute(Namespace.JDNC, Attributes.IS_COLUMN_HEADER_LOCKED);
96         applyAttribute(Namespace.JDNC, Attributes.IS_ROW_HEADER_LOCKED);
97
98         // by now the model, columnModel, and columns have all been initialized
99
applySpecialProperties(this);
100     }
101
102     protected Map JavaDoc registerAttributeHandlers() {
103         Map JavaDoc handlerMap = super.registerAttributeHandlers();
104         if (handlerMap != null) {
105         // handlerMap.put(Namespace.JDNC + ":" + Attributes.SORTABLE,
106
// sortableHandler);
107
handlerMap.put(Namespace.JDNC + ":" +
108                            Attributes.HAS_COLUMN_CONTROL,
109                            hasColumnControlHandler);
110             handlerMap.put(Namespace.JDNC + ":" + Attributes.COLUMN_MARGIN,
111                            columnMarginHandler);
112             handlerMap.put(Namespace.JDNC + ":" + Attributes.DATA,
113                            dataHandler);
114             handlerMap.put(Namespace.JDNC + ":" + Attributes.DATA_SOURCE,
115                            dataSourceHandler);
116             handlerMap.put(Namespace.JDNC + ":" + Attributes.IS_FIRST_ROW_HEADER,
117                            firstRowHeaderHandler);
118             handlerMap.put(Namespace.JDNC + ":" + Attributes.GRID_COLOR,
119                            gridColorHandler);
120             handlerMap.put(Namespace.JDNC + ":" + Attributes.PREFERRED_SIZE,
121                            preferredSizeHandler);
122             handlerMap.put(Namespace.JDNC + ":" + Attributes.ROW_HEIGHT,
123                            rowHeightHandler);
124             handlerMap.put(Namespace.JDNC + ":" + Attributes.ROW_MARGIN,
125                            rowMarginHandler);
126             handlerMap.put(Namespace.JDNC + ":" + Attributes.SELECTION,
127                            selectionHandler);
128             handlerMap.put(Namespace.JDNC + ":" + Attributes.SELECTION_MODE,
129                            selectionModeHandler);
130             handlerMap.put(Namespace.JDNC + ":" + Attributes.SHOWS_HORIZONTAL_LINES,
131                            showsHorizontalLinesHandler);
132             handlerMap.put(Namespace.JDNC + ":" + Attributes.SHOWS_VERTICAL_LINES,
133                            showsVerticalLinesHandler);
134             handlerMap.put(Namespace.JDNC + ":" + Attributes.SIZE,
135                            sizeHandler); // WIDTH and HEIGHT
136
handlerMap.put(Namespace.JDNC + ":" + Attributes.IS_COLUMN_HEADER_LOCKED,
137                            isColumnHeaderLockedHandler);
138             handlerMap.put(Namespace.JDNC + ":" + Attributes.IS_ROW_HEADER_LOCKED,
139                            isRowHeaderLockedHandler);
140
141         // Register null appliers. These attributes are handled elsewhere
142
handlerMap.put(Namespace.JDNC + ":" + Attributes.ID,
143                            NullAttribute.idHandler);
144
145             handlerMap.put(Namespace.JDNC + ":" + Attributes.TITLE,
146                            titleHandler);
147
148         }
149         return handlerMap;
150     }
151
152     protected Map JavaDoc registerElementHandlers() {
153         Map JavaDoc handlerMap = super.registerElementHandlers();
154         if (handlerMap != null) {
155             //elementMap.put(Namespace.JDNC + ":" + ElementTypes.ACTIONS.getLocalName(), actionsElementHandler);
156
handlerMap.put(Namespace.JDNC + ":" +
157                            ElementTypes.FILTERS.getLocalName(),
158                            filtersElementHandler);
159             handlerMap.put(Namespace.JDNC + ":" +
160                            ElementTypes.TABLE_HEADER.getLocalName(),
161                            headerElementHandler);
162             handlerMap.put(Namespace.JDNC + ":" +
163                            ElementTypes.HIGHLIGHTERS.getLocalName(),
164                            highlightersElementHandler);
165             handlerMap.put(Namespace.JDNC + ":" +
166                            ElementTypes.COMPONENT_TOOLBAR.getLocalName(),
167                            toolBarElementHandler);
168             handlerMap.put(Namespace.JDNC + ":" +
169                            ElementTypes.TABLE_COLUMNS.getLocalName(),
170                            tableColumnsElementHandler);
171             /** @todo Move all of the above to AbstractTableElement */
172             handlerMap.put(Namespace.JDNC + ":" +
173                            ElementTypes.TABULAR_DATA.getLocalName(),
174                            tabularDataElementHandler);
175             handlerMap.put(Namespace.JDNC + ":" +
176                            ElementTypes.DATA.getLocalName(), dataElementHandler);
177         }
178         return handlerMap;
179     }
180
181     protected static void applySpecialProperties(Realizable target) {
182         JNTable table = (JNTable)target.getObject();
183         TableColumnModel JavaDoc columnModel = table.getTable().getColumnModel();
184         for(int i = 0; i < columnModel.getColumnCount(); i++) {
185             TableColumnExt column = (TableColumnExt)columnModel.getColumn(i);
186             applyColumnProperties(table, column);
187         }
188     }
189
190     protected static void applyColumnProperties(final JNTable table, final TableColumnExt column) {
191         // These table column properties are special, in that they are not
192
// direct properties on TableColumnExt; they can only be set once the
193
// table and the tableColumnModel have been initialized.
194
LabelProperties props = (LabelProperties) column.getClientProperty(
195             "LabelProperties");
196         if (props != null) {
197             String JavaDoc columnName = (String JavaDoc)column.getIdentifier();
198             if (props.isBackgroundSet()) {
199                 table.setColumnBackground(columnName, props.getBackground());
200             }
201             if (props.isForegroundSet()) {
202                 table.setColumnForeground(columnName, props.getForeground());
203             }
204             if (props.isHorizontalAlignmentSet()) {
205                 table.setColumnHorizontalAlignment(columnName, props.getHorizontalAlignment());
206             }
207             column.putClientProperty("LabelProperties", null); //clear temp stash
208
}
209
210         String JavaDoc prototypeValueString = (String JavaDoc)column.getClientProperty("prototypeValueString");
211         if (prototypeValueString != null) {
212             TableModel model = table.getModel();
213             Object JavaDoc prototypeValue = prototypeValueString; //fallback
214
Converter converter = null;
215             Object JavaDoc decodeFormat = null;
216             if (model instanceof MetaDataProvider) {
217                 MetaData metaData[] = ( (MetaDataProvider) model).getMetaData();
218                 MetaData columnMetaData = metaData[column.getModelIndex()];
219                 converter = columnMetaData.getConverter();
220                 decodeFormat = columnMetaData.getDecodeFormat();
221             } else { // no metadata
222
converter = Converters.get(model.getColumnClass(column.getModelIndex()));
223             }
224             if (converter != null) {
225                 try {
226                     prototypeValue = converter.decode(prototypeValueString,
227                         decodeFormat);
228                 } catch (ConversionException e) {
229                     System.err.println(e.getMessage());
230                 }
231             }
232             column.setPrototypeValue(prototypeValue);
233         }
234     }
235
236     protected static boolean hasColumnsElement(Realizable target) {
237         NodeList JavaDoc columnsNode = target.getElementsByTagNameNS(Namespace.JDNC,
238                                    ElementTypes.TABLE_COLUMNS.getLocalName());
239         return columnsNode.getLength() > 0;
240
241     }
242
243     // data can be set as table attribute OR child element, so factor common code here
244
public static void setModel(Realizable target, TableModel model) {
245         // Before setting model, check whether or not the columns should
246
// be automatically created or not (yes, only if no <columns> element is present)
247
JNTable table = (JNTable)target.getObject();
248         if (hasColumnsElement(target)) {
249             table.getTable().setAutoCreateColumnsFromModel(false);
250         }
251         table.setModel(model);
252     }
253
254     public static final ElementAssimilator dataAssimilator = new ElementAssimilator() {
255         public void assimilate(Realizable parent, Realizable child) {
256             JNTable table = (JNTable)parent.getObject();
257             DefaultTableModelExt data = (DefaultTableModelExt)child.getObject();
258             setModel(parent, data);
259             String JavaDoc actuate = child.getAttributeNSOptional(Namespace.JDNC, Attributes.ACTUATE);
260             if (actuate.equals("onRequest")) {
261                 table.addHierarchyListener(new LoadOnShowListener(data));
262             }
263         }
264     };
265
266     public static final ElementAssimilator filtersAssimilator = new ElementAssimilator() {
267         public void assimilate(Realizable parent, Realizable child) {
268             JNTable table = (JNTable)parent.getObject();
269             Vector JavaDoc filters = (Vector JavaDoc)child.getObject();
270             table.setFilters(new FilterPipeline((Filter[])
271                 filters.toArray(filterType)
272                 ));
273         }
274     };
275
276     public static final ElementAssimilator headerAssimilator = new ElementAssimilator() {
277         public void assimilate(Realizable parent, Realizable child) {
278             JNTable table = (JNTable)parent.getObject();
279             JTableHeader header = (JTableHeader)child.getObject();
280             table.getTable().setTableHeader(header);
281         }
282     };
283
284     public static final ElementAssimilator highlightersAssimilator = new ElementAssimilator() {
285         public void assimilate(Realizable parent, Realizable child) {
286             JNTable table = (JNTable)parent.getObject();
287             Vector JavaDoc highlighters = (Vector JavaDoc)child.getObject();
288             table.setHighlighters(new HighlighterPipeline((Highlighter[])
289                 highlighters.toArray(highlighterType)
290                 ));
291         }
292     };
293
294     public static final ElementAssimilator tableColumnsAssimilator = new
295         ElementAssimilator() {
296         public void assimilate(Realizable parent, Realizable child) {
297             JNTable table = (JNTable) parent.getObject();
298             TableColumnModel JavaDoc columnModel = (TableColumnModel JavaDoc) child.getObject();
299             table.getTable().setAutoCreateColumnsFromModel(false);
300             table.getTable().setColumnModel(columnModel);
301             for(int i = 0; i < columnModel.getColumnCount() ; i++) {
302                 TableColumn column = columnModel.getColumn(i);
303                 if (column.getModelIndex() == -1) {
304                     column.setModelIndex(i);
305                 }
306             }
307         }
308     };
309
310     public static final ElementAssimilator toolBarAssimilator = new ElementAssimilator() {
311         public void assimilate(Realizable parent, Realizable child) {
312             JNTable table = (JNTable)parent.getObject();
313             Vector JavaDoc toolBarEntries = (Vector JavaDoc)child.getObject();
314             int count = toolBarEntries.size();
315             for (int i = 0; i < count; i++) {
316                 Object JavaDoc entry = toolBarEntries.get(i);
317                 if (entry instanceof Action JavaDoc) {
318                     table.addAction((Action JavaDoc) entry);
319                 }
320                 else if (entry instanceof JComponent JavaDoc) {
321                     table.addToolBarComponent((JComponent JavaDoc) entry);
322                 }
323             }
324         }
325     };
326
327     // For typecasting entries in a Vector when converted to an array
328
private static final Filter[] filterType = new Filter[]{};
329     private static final Highlighter[] highlighterType = new Highlighter[]{};
330
331     protected static final AttributeHandler dataSourceHandler =
332         new AttributeHandler(Namespace.JDNC, Attributes.DATA_SOURCE, TableAttributes.dataSourceApplier);
333
334     protected static final AttributeHandler hasColumnControlHandler =
335         new AttributeHandler(Namespace.JDNC, Attributes.HAS_COLUMN_CONTROL, TableAttributes.hasColumnControlApplier);
336
337     protected static final AttributeHandler columnMarginHandler =
338         new AttributeHandler(Namespace.JDNC, Attributes.COLUMN_MARGIN, TableAttributes.columnMarginApplier);
339
340     protected static final AttributeHandler dataHandler =
341         new AttributeHandler(Namespace.JDNC, Attributes.DATA, TableAttributes.dataApplier);
342
343     protected static final AttributeHandler firstRowHeaderHandler =
344         new AttributeHandler(Namespace.JDNC, Attributes.IS_FIRST_ROW_HEADER, TableAttributes.firstRowHeaderApplier);
345
346     protected static final AttributeHandler gridColorHandler =
347         new AttributeHandler(Namespace.JDNC, Attributes.GRID_COLOR, TableAttributes.gridColorApplier);
348
349     protected static final AttributeHandler preferredSizeHandler =
350         new AttributeHandler(Namespace.JDNC, Attributes.PREFERRED_SIZE, TableAttributes.preferredSizeApplier);
351
352     protected static final AttributeHandler rowHeightHandler =
353         new AttributeHandler(Namespace.JDNC, Attributes.ROW_HEIGHT, TableAttributes.rowHeightApplier);
354
355     protected static final AttributeHandler rowMarginHandler =
356         new AttributeHandler(Namespace.JDNC, Attributes.ROW_MARGIN, TableAttributes.rowMarginApplier);
357
358     protected static final AttributeHandler selectionHandler =
359         new AttributeHandler(Namespace.JDNC, Attributes.SELECTION, TableAttributes.selectionApplier);
360
361     protected static final AttributeHandler selectionModeHandler =
362         new AttributeHandler(Namespace.JDNC, Attributes.SELECTION_MODE, TableAttributes.selectionModeApplier);
363
364     protected static final AttributeHandler showsHorizontalLinesHandler =
365         new AttributeHandler(Namespace.JDNC, Attributes.SHOWS_HORIZONTAL_LINES, TableAttributes.showsHorizontalLinesApplier);
366
367     protected static final AttributeHandler showsVerticalLinesHandler =
368         new AttributeHandler(Namespace.JDNC, Attributes.SHOWS_VERTICAL_LINES, TableAttributes.showsVerticalLinesApplier);
369
370     protected static final AttributeHandler sizeHandler =
371         new AttributeHandler(Namespace.JDNC, Attributes.SIZE, ComponentAttributes.sizeApplier);
372
373     protected static final AttributeHandler isColumnHeaderLockedHandler =
374         new AttributeHandler(Namespace.JDNC, Attributes.IS_COLUMN_HEADER_LOCKED, TableAttributes.isColumnHeaderLockedApplier);
375
376     protected static final AttributeHandler isRowHeaderLockedHandler =
377         new AttributeHandler(Namespace.JDNC, Attributes.IS_ROW_HEADER_LOCKED, TableAttributes.isRowHeaderLockedApplier);
378
379     protected static final AttributeHandler titleHandler =
380         new AttributeHandler(Namespace.JDNC, Attributes.TITLE, NullAttribute.nullApplier);
381
382 // protected static final AttributeHandler sortableHandler =
383
// new AttributeHandler(Namespace.JDNC, Attributes.SORTABLE, TableAttributes.sortableApplier);
384

385
386     protected static final ElementHandler filtersElementHandler =
387         new ElementHandler(ElementTypes.FILTERS, TableElement.filtersAssimilator);
388
389     protected static final ElementHandler headerElementHandler =
390         new ElementHandler(ElementTypes.TABLE_HEADER, TableElement.headerAssimilator);
391
392     protected static final ElementHandler highlightersElementHandler =
393         new ElementHandler(ElementTypes.HIGHLIGHTERS, TableElement.highlightersAssimilator);
394
395     protected static final ElementHandler dataElementHandler =
396         new ElementHandler(ElementTypes.DATA, TableElement.dataAssimilator);
397
398     protected static final ElementHandler tabularDataElementHandler =
399         new ElementHandler(ElementTypes.TABULAR_DATA, TableElement.dataAssimilator);
400
401     protected static final ElementHandler tableColumnsElementHandler =
402         new ElementHandler(ElementTypes.TABLE_COLUMNS, TableElement.tableColumnsAssimilator);
403
404     protected static final ElementHandler toolBarElementHandler =
405         new ElementHandler(ElementTypes.COMPONENT_TOOLBAR, TableElement.toolBarAssimilator);
406
407 }
408
Popular Tags