KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: TableAttributes.java,v 1.5 2005/01/27 14:40:30 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.net.URL JavaDoc;
11
12 import java.awt.Color JavaDoc;
13 import java.awt.Dimension JavaDoc;
14
15 import javax.swing.JTable JavaDoc;
16 import javax.swing.event.TableModelEvent JavaDoc;
17 import javax.swing.event.TableModelListener JavaDoc;
18 import javax.swing.ListSelectionModel JavaDoc;
19 import javax.swing.table.TableModel JavaDoc;
20
21 import org.jdesktop.swing.data.DataLoader;
22 import org.jdesktop.swing.data.DefaultTableModelExt;
23 import org.jdesktop.swing.data.TableModelExtTextLoader;
24 import org.jdesktop.swing.utils.LoadOnShowListener;
25 import org.jdesktop.jdnc.JNTable;
26 import org.jdesktop.jdnc.markup.RealizationUtils;
27 import org.jdesktop.jdnc.markup.elem.TableElement;
28
29 import net.openmarkup.ApplierException;
30 import net.openmarkup.AttributeApplier;
31 import net.openmarkup.Realizable;
32
33 /**
34  * @author Ramesh Gupta
35  * @author Amy Fowler
36  */

37 public class TableAttributes {
38     public static final AttributeApplier hasColumnControlApplier = new AttributeApplier() {
39         public void apply(Realizable target, String JavaDoc namespaceURI,
40                           String JavaDoc attributeName, String JavaDoc attributeValue) throws ApplierException {
41             JNTable table = (JNTable) target.getObject();
42             boolean enabled = Boolean.valueOf(attributeValue).booleanValue();
43             table.setHasColumnControl(enabled);
44         }
45     };
46
47     public static final AttributeApplier columnMarginApplier = new AttributeApplier() {
48         public void apply(Realizable target, String JavaDoc namespaceURI,
49                           String JavaDoc attributeName, String JavaDoc attributeValue) throws ApplierException {
50             JNTable table = (JNTable) target.getObject();
51             try {
52                 int columnMargin = Integer.parseInt(attributeValue);
53                 table.setColumnMargin(columnMargin);
54             }
55             catch (NumberFormatException JavaDoc ex) {
56                 throw new ApplierException("Bad format: " + attributeName + "=" + attributeValue, ex);
57             }
58         }
59     };
60
61     public static final AttributeApplier dataApplier = new AttributeApplier() {
62         public void apply(Realizable target, String JavaDoc namespaceURI,
63                           String JavaDoc attributeName, String JavaDoc attributeValue)
64             throws ApplierException {
65             DefaultTableModelExt data = (DefaultTableModelExt)BaseAttribute.getReferencedObject(target,
66                 attributeValue);
67             TableElement.setModel(target, data);
68         }
69     };
70
71     public static final AttributeApplier dataSourceApplier = new AttributeApplier() {
72         public void apply(Realizable target, String JavaDoc namespaceURI,
73                           String JavaDoc attributeName, String JavaDoc attributeValue) throws ApplierException {
74
75             // Validate the URL before we do anything else.
76
URL JavaDoc url = target.getResolvedURL(attributeValue);
77             RealizationUtils.validateURL(url);
78
79             try {
80                 DefaultTableModelExt model = new DefaultTableModelExt();
81                 model.setSource(url);
82                 JNTable table = (JNTable) target.getObject();
83                 table.setModel(model);
84                 table.addHierarchyListener(new LoadOnShowListener(model));
85             }
86             catch (Exception JavaDoc ex) {
87                 throw new ApplierException("Couldn't set data source " +
88                     attributeName + "=" + attributeValue, ex);
89             }
90         }
91     };
92
93     public static final AttributeApplier firstRowHeaderApplier = new AttributeApplier() {
94         public void apply(Realizable target, String JavaDoc namespaceURI,
95                           String JavaDoc attributeName, String JavaDoc attributeValue) throws ApplierException {
96             try {
97                 JNTable table = (JNTable) target.getObject();
98                 TableModel JavaDoc model = table.getModel();
99                 if (model instanceof DefaultTableModelExt) {
100                     TableModelExtTextLoader loader = (TableModelExtTextLoader)((DefaultTableModelExt)model).getLoader();
101                     loader.setFirstRowHeader(Boolean.valueOf(attributeValue).booleanValue());
102                 }
103             }
104             catch (Exception JavaDoc ex) {
105                 throw new ApplierException("Couldn't set firstRowHeader " +
106                     attributeName + "=" + attributeValue, ex);
107             }
108         }
109     };
110
111     public static final AttributeApplier gridColorApplier = new AttributeApplier() {
112         public void apply(Realizable target, String JavaDoc namespaceURI,
113                           String JavaDoc attributeName, String JavaDoc attributeValue) {
114             JNTable table = (JNTable) target.getObject();
115             Color JavaDoc color = Decoder.decodeColor(attributeValue);
116             table.setGridColor(color);
117         }
118     };
119
120     public static final AttributeApplier isColumnHeaderLockedApplier = new AttributeApplier() {
121         public void apply(Realizable target, String JavaDoc namespaceURI,
122                           String JavaDoc attributeName, String JavaDoc attributeValue) {
123             JNTable table = (JNTable) target.getObject();
124             boolean isLocked = Boolean.valueOf(attributeValue).booleanValue();
125             table.getTable().getTableHeader().setEnabled(!isLocked);
126             /** @todo add error checking: getTable() or getTableHeader() could return null? */
127         }
128     };
129
130     public static final AttributeApplier isRowHeaderLockedApplier = new AttributeApplier() {
131         public void apply(Realizable target, String JavaDoc namespaceURI,
132                           String JavaDoc attributeName, String JavaDoc attributeValue) {
133             JNTable table = (JNTable) target.getObject();
134             boolean isLocked = Boolean.valueOf(attributeValue).booleanValue();
135             table.setRowHeaderLocked(isLocked);
136         }
137     };
138
139     public static final AttributeApplier preferredSizeApplier = new AttributeApplier() {
140         public void apply(Realizable target, String JavaDoc namespaceURI,
141                           String JavaDoc attributeName, String JavaDoc attributeValue) {
142             String JavaDoc[] args = attributeValue.split("\\s");
143             if (args.length == 2) {
144                 Dimension JavaDoc size = new Dimension JavaDoc(
145                     Integer.parseInt(args[0]), Integer.parseInt(args[1]));
146                 JNTable table = (JNTable) target.getObject();
147                 table.setPreferredSize(size);
148             }
149         }
150     };
151
152     public static final AttributeApplier rowHeightApplier = new AttributeApplier() {
153         public void apply(Realizable target, String JavaDoc namespaceURI,
154                           String JavaDoc attributeName, String JavaDoc attributeValue) {
155             JNTable table = (JNTable) target.getObject();
156             int rowHeight = Integer.parseInt(attributeValue);
157             table.setRowHeight(rowHeight);
158         }
159     };
160
161     public static final AttributeApplier rowMarginApplier = new AttributeApplier() {
162         public void apply(Realizable target, String JavaDoc namespaceURI,
163                           String JavaDoc attributeName, String JavaDoc attributeValue) throws ApplierException {
164             JNTable table = (JNTable) target.getObject();
165             try {
166                 int rowMargin = Integer.parseInt(attributeValue);
167                 table.setRowMargin(rowMargin);
168             }
169             catch (NumberFormatException JavaDoc ex) {
170                 throw new ApplierException("Bad format: " + attributeName + "=" + attributeValue, ex);
171             }
172         }
173     };
174
175     public static final AttributeApplier selectionApplier = new AttributeApplier() {
176         public void apply(Realizable target, String JavaDoc namespaceURI,
177                           String JavaDoc attributeName, String JavaDoc attributeValue) throws ApplierException {
178             int index = Integer.parseInt(attributeValue);
179             final JTable JavaDoc table = ((JNTable) target.getObject()).getTable();
180
181             // no data yet, so we watch for data to set initial selection
182
// to ensure that DataModel has a non -1 current index
183
table.getModel().addTableModelListener(new TableModelListener JavaDoc() {
184                 public void tableChanged(TableModelEvent JavaDoc e) {
185                     if (e.getType() == TableModelEvent.INSERT) {
186                         table.getSelectionModel().setLeadSelectionIndex(0);
187                         table.getSelectionModel().setAnchorSelectionIndex(0);
188                         table.getModel().removeTableModelListener(this);
189                     }
190                 }
191             });
192         }
193     };
194
195     public static final AttributeApplier selectionModeApplier = new AttributeApplier() {
196          public void apply(Realizable target, String JavaDoc namespaceURI,
197                            String JavaDoc attributeName, String JavaDoc attributeValue) throws ApplierException {
198              JNTable table = (JNTable) target.getObject();
199              if (attributeValue.equals("single")) {
200                  table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
201              } else if (attributeValue.equals("contiguous")) {
202                  table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
203              } else if (attributeValue.equals("discontiguous")) {
204                  table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
205              } else {
206                  /**@todo aim: log warning? */
207              }
208          }
209      };
210
211      public static final AttributeApplier showsHorizontalLinesApplier = new AttributeApplier() {
212          public void apply(Realizable target, String JavaDoc namespaceURI,
213                            String JavaDoc attributeName, String JavaDoc attributeValue) throws ApplierException {
214              JNTable table = (JNTable) target.getObject();
215              boolean show = Boolean.valueOf(attributeValue).booleanValue();
216              table.setShowHorizontalLines(show);
217          }
218      };
219
220      public static final AttributeApplier showsVerticalLinesApplier = new AttributeApplier() {
221          public void apply(Realizable target, String JavaDoc namespaceURI,
222                            String JavaDoc attributeName, String JavaDoc attributeValue) throws ApplierException {
223              JNTable table = (JNTable) target.getObject();
224              boolean show = Boolean.valueOf(attributeValue).booleanValue();
225              table.setShowVerticalLines(show);
226          }
227      };
228 /*
229      public static final AttributeApplier sortableApplier = new AttributeApplier() {
230         public void apply(Realizable target, String namespaceURI,
231                           String attributeName, String attributeValue) throws ApplierException {
232             JNTable table = (JNTable) target.getObject();
233             boolean sortable = Boolean.valueOf(attributeValue).booleanValue();
234             table.setSortable(sortable);
235         }
236     };
237 */

238
239      // ...
240
}
241
Popular Tags