KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: TableColumnElement.java,v 1.4 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 javax.swing.table.TableCellRenderer JavaDoc;
11
12 import org.jdesktop.swing.data.MetaData;
13 import org.jdesktop.swing.data.DefaultTableModelExt;
14
15 import org.jdesktop.swing.LabelProperties;
16 import org.jdesktop.swing.table.ColumnHeaderRenderer;
17 import org.jdesktop.swing.table.TableColumnExt;
18
19 import org.jdesktop.jdnc.JNTable;
20
21 import java.util.Hashtable JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import net.openmarkup.AttributeHandler;
25 import net.openmarkup.ElementAssimilator;
26 import net.openmarkup.ElementHandler;
27 import net.openmarkup.ElementType;
28 import net.openmarkup.Realizable;
29
30 import org.w3c.dom.Element JavaDoc;
31 import org.w3c.dom.Node JavaDoc;
32
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.TableColumnAttributes;
37
38 /**
39  *
40  * @author Amy Fowler
41  * @author Ramesh Gupta
42  */

43 public class TableColumnElement extends ElementProxy {
44     private static final Map JavaDoc attrMap = new Hashtable JavaDoc();
45     private static final Map JavaDoc elementMap = new Hashtable JavaDoc();
46
47     public TableColumnElement(Element JavaDoc element, ElementType elementType) {
48         super(element, elementType);
49     }
50
51     protected Map JavaDoc getAttributeHandlerMap() {
52         return attrMap;
53     }
54
55     protected Map JavaDoc getElementHandlerMap() {
56         return elementMap;
57     }
58
59     protected void applyAttributesAfter() {
60         super.applyAttributesAfter();
61         applyAttribute(Namespace.JDNC, Attributes.BINDING); // must come first!
62

63         applyAttribute(Namespace.JDNC, Attributes.IS_READ_ONLY);
64         applyAttribute(Namespace.JDNC, Attributes.ENUM_VALUES);
65         applyAttribute(Namespace.JDNC, Attributes.PROTOTYPE_VALUE);
66         //applyAttribute(Namespace.JDNC, Attributes.SORTABLE);
67
applyAttribute(Namespace.JDNC, Attributes.TITLE);
68         initializeHeaderTitle();
69
70         //visual attributes applied to a LabelProperties object
71
applyAttribute(Namespace.JDNC, Attributes.BACKGROUND);
72         applyAttribute(Namespace.JDNC, Attributes.FOREGROUND);
73         applyAttribute(Namespace.JDNC, Attributes.HORIZONTAL_ALIGNMENT);
74         applyAttribute(Namespace.JDNC, Attributes.VERTICAL_ALIGNMENT);
75         applyAttribute(Namespace.JDNC, Attributes.PREFERRED_WIDTH);
76
77     }
78
79     protected Map JavaDoc registerAttributeHandlers() {
80         Map JavaDoc handlerMap = super.registerAttributeHandlers();
81         if (handlerMap != null) {
82             //handlerMap.put(Namespace.JDNC + ":" + Attributes.EDITABLE, editableHandler);
83
handlerMap.put(Namespace.JDNC + ":" + Attributes.BINDING,
84                            bindingHandler);
85             handlerMap.put(Namespace.JDNC + ":" + Attributes.IS_READ_ONLY,
86                            isReadOnlyHandler);
87             handlerMap.put(Namespace.JDNC + ":" + Attributes.ENUM_VALUES,
88                            enumValuesHandler);
89             handlerMap.put(Namespace.JDNC + ":" + Attributes.PROTOTYPE_VALUE,
90                            prototypeValueHandler);
91             //handlerMap.put(Namespace.JDNC + ":" + Attributes.SORTABLE,
92
// sortableHandler);
93
handlerMap.put(Namespace.JDNC + ":" + Attributes.TITLE,
94                            titleHandler);
95             handlerMap.put(Namespace.JDNC + ":" + Attributes.BACKGROUND,
96                            backgroundHandler);
97             handlerMap.put(Namespace.JDNC + ":" + Attributes.FOREGROUND,
98                            foregroundHandler);
99             handlerMap.put(Namespace.JDNC + ":" + Attributes.HORIZONTAL_ALIGNMENT,
100                            horizontalAlignmentHandler);
101             /*
102             handlerMap.put(Namespace.JDNC + ":" + Attributes.VERTICAL_ALIGNMENT,
103                            verticalAlignmentHandler);
104              */

105             handlerMap.put(Namespace.JDNC + ":" + Attributes.PREFERRED_WIDTH,
106                            preferredWidthHandler);
107
108         }
109         return handlerMap;
110     }
111
112     protected void initializeHeaderTitle() {
113         TableColumnExt column = (TableColumnExt)getObject();
114         // initialize header from column metadata if no title was set
115
if (column.getHeaderValue() == null) {
116             Realizable tableTarget = ElementProxy.getRealizable((Element JavaDoc)
117                 getParentNode().getParentNode());
118             JNTable table = (JNTable) tableTarget.getObject();
119             DefaultTableModelExt data = (DefaultTableModelExt)table.getModel();
120             int columnIndex = column.getModelIndex();
121             String JavaDoc label = "Column" + columnIndex;
122             if (data != null) {
123                 MetaData metaData = data.getColumnMetaData(columnIndex);
124                 String JavaDoc columnLabel = metaData.getLabel();
125                 if (columnLabel != null) {
126                     label = columnLabel;
127                 }
128             }
129             column.setHeaderValue(label);
130         }
131     }
132
133     protected Map JavaDoc registerElementHandlers() {
134         Map JavaDoc handlerMap = super.registerElementHandlers();
135         if (handlerMap != null) {
136             handlerMap.put(Namespace.JDNC + ":" +
137                            ElementTypes.TABLE_COLUMN_HEADER.getLocalName(),
138                            tableColumnHeaderElementHandler);
139         }
140         return handlerMap;
141     }
142
143     public static final ElementAssimilator tableColumnHeaderAssimilator = new
144         ElementAssimilator() {
145         public void assimilate(Realizable parent, Realizable child) {
146             TableColumnExt column = (TableColumnExt) parent.getObject();
147             LabelProperties headerProperties = (LabelProperties) child.getObject();
148             TableCellRenderer JavaDoc headerRenderer = new ColumnHeaderRenderer();
149             headerProperties.applyPropertiesTo(headerRenderer);
150             column.setHeaderRenderer(headerRenderer);
151         }
152     };
153
154     private static final ElementHandler tableColumnHeaderElementHandler =
155      new ElementHandler(ElementTypes.TABLE_COLUMN_HEADER, tableColumnHeaderAssimilator);
156
157
158     private static final AttributeHandler backgroundHandler =
159         new AttributeHandler(Namespace.JDNC, Attributes.BACKGROUND,
160                              TableColumnAttributes.backgroundApplier);
161
162     private static final AttributeHandler bindingHandler =
163         new AttributeHandler(Namespace.JDNC, Attributes.BINDING,
164                              TableColumnAttributes.bindingApplier);
165
166     private static final AttributeHandler foregroundHandler =
167         new AttributeHandler(Namespace.JDNC, Attributes.FOREGROUND,
168                              TableColumnAttributes.foregroundApplier);
169
170     private static final AttributeHandler isReadOnlyHandler =
171         new AttributeHandler(Namespace.JDNC, Attributes.IS_READ_ONLY,
172                              TableColumnAttributes.isReadOnlyApplier);
173
174     private static final AttributeHandler enumValuesHandler =
175         new AttributeHandler(Namespace.JDNC, Attributes.ENUM_VALUES,
176                              TableColumnAttributes.enumValuesApplier);
177
178     private static final AttributeHandler horizontalAlignmentHandler =
179         new AttributeHandler(Namespace.JDNC, Attributes.HORIZONTAL_ALIGNMENT,
180                              TableColumnAttributes.horizontalAlignmentApplier);
181
182     private static final AttributeHandler prototypeValueHandler =
183         new AttributeHandler(Namespace.JDNC, Attributes.PROTOTYPE_VALUE,
184                              TableColumnAttributes.prototypeValueApplier);
185 /*
186     private static final AttributeHandler sortableHandler =
187         new AttributeHandler(Namespace.JDNC, Attributes.SORTABLE,
188                              TableColumnAttributes.sortableApplier);
189 */

190     private static final AttributeHandler titleHandler =
191         new AttributeHandler(Namespace.JDNC, Attributes.TITLE,
192                              TableColumnAttributes.titleApplier);
193 /*
194     private static final AttributeHandler verticalAlignmentHandler =
195         new AttributeHandler(Namespace.JDNC, Attributes.VERTICAL_ALIGNMENT,
196                              TableColumnAttributes.verticalAlignmentApplier);
197         */

198
199     private static final AttributeHandler preferredWidthHandler =
200         new AttributeHandler(Namespace.JDNC, Attributes.PREFERRED_WIDTH,
201                              TableColumnAttributes.preferredWidthApplier);
202
203
204 }
205
Popular Tags