KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > data > DOMAdapter


1 /*
2  * $Id: DOMAdapter.java,v 1.1.1.1 2004/06/16 01:43:39 davidson1 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.swing.data;
9
10 import java.io.File JavaDoc;
11
12 import org.w3c.dom.Document JavaDoc;
13 import org.w3c.dom.Element JavaDoc;
14 import org.w3c.dom.*;
15 import org.w3c.dom.NodeList JavaDoc;
16 import org.jdesktop.swing.treetable.DefaultTreeTableModel;
17 import java.beans.Expression JavaDoc;
18
19 import org.jdesktop.swing.data.Converter;
20
21 public class DOMAdapter extends DefaultTreeTableModel {
22     protected Document JavaDoc dom = null;
23 // private Element columns = null;
24
private HierarchicalDataMetaData metaData;
25 /*
26     private int userDataSupportType = 0; // 1 == getUserData; 2 == getUserObject
27     private static final int GET_USER_DATA = 1;
28     private static final int GET_USER_OBJECT = 2;
29 */

30     public DOMAdapter() {
31     }
32
33     public DOMAdapter(Document JavaDoc dom) {
34         bind(dom);
35     }
36
37     public final void bind(Document JavaDoc dom) {
38         if (dom == null) {
39             throw new IllegalArgumentException JavaDoc("null document object model");
40         }
41
42         if (this.dom == null) {
43             this.dom = dom;
44             /** @todo Handle case where no metaData is present! */
45             Element JavaDoc metaDataElem = (Element JavaDoc) dom.getDocumentElement().getElementsByTagNameNS(
46                 "http://www.jdesktop.org/2004/05/jdnc", "metaData").item(0);
47             setMetaData(new HierarchicalDataMetaData(metaDataElem));
48 /*
49             Object[] cells = null;
50             try {
51                 cells = (Object[]) (new Expression(columns, "getUserData",
52                                                   new Object[0])).getValue();
53                 userDataSupportType = GET_USER_DATA;
54             }
55             catch (Exception ex) {
56                 try {
57                     cells = (Object[]) (new Expression(columns,
58                         "getUserObject", new Object[0])).getValue();
59                     userDataSupportType = GET_USER_OBJECT;
60                 }
61                 catch (Exception nex) {
62
63                 }
64             }
65 */

66         }
67         else {
68             throw new IllegalArgumentException JavaDoc("dom already bound");
69         }
70     }
71
72     public String JavaDoc convertValueToText(Object JavaDoc value) {
73         // This method requires support from JXTree. Will NOT work with JTree!
74
if(value != null) {
75             Object JavaDoc realValue = getValueAt(value, 0);
76             if (realValue != null) {
77                 return realValue.toString();
78             }
79         }
80         return "";
81     }
82
83     public Object JavaDoc getRoot() {
84         return dom.getDocumentElement().getElementsByTagNameNS(
85                 "http://www.jdesktop.org/2004/05/jdnc", "rows").item(0);
86     }
87
88     public Class JavaDoc getColumnClass(int column) {
89         return column == 0 ? super.getColumnClass(0) :
90             getMetaData().getColumnClass(column+1);
91     }
92
93     public Object JavaDoc getChild(Object JavaDoc parent, int index) {
94         Element JavaDoc parentElement = (Element JavaDoc) parent;
95         NodeList JavaDoc list = ((Element JavaDoc) parentElement).getChildNodes();
96         int i = 0, k = index, max = list.getLength();
97         Node node;
98         Element JavaDoc elem;
99         while (i < max) {
100             node = list.item(i++);
101             if (node instanceof Element JavaDoc) {
102                 elem = (Element JavaDoc) node;
103                 if (elem.getLocalName().equals("row")) {
104                     if (k-- == 0) {
105                         /*
106                         System.out.print("got child:");
107                         NamedNodeMap attributes = elem.getAttributes();
108                         for (int a = 0; a < attributes.getLength(); a++) {
109                             System.out.print(" " +
110                                              attributes.item(a).getNamespaceURI() + ":" +
111                                              attributes.item(a).getLocalName() + "=" +
112                                              attributes.item(a).getNodeValue());
113                         }
114                         System.out.println(";");
115 */

116                         return elem;
117                     }
118                 }
119             }
120         }
121 /*
122         if (true) {
123             // We should never get here!
124             System.out.print(parentElement.getLocalName());
125             NamedNodeMap attributes = parentElement.getAttributes();
126             for (int a = 0; a < attributes.getLength(); a++) {
127                 System.out.print(" " + attributes.item(a).getNamespaceURI() + ":" +
128                                  attributes.item(a).getLocalName() + "=" +
129                                  attributes.item(a).getNodeValue());
130             }
131             System.out.println(" has no child at index " + k);
132         }
133  */

134         return null;
135     }
136
137     public int getChildCount(Object JavaDoc parent) {
138         Element JavaDoc parentElement = (Element JavaDoc) parent;
139         NodeList JavaDoc list = ((Element JavaDoc) parentElement).getChildNodes();
140         int i = 0, k = 0, max = list.getLength();
141         Node node;
142         Element JavaDoc elem = null;
143         while (i < max) {
144             node = list.item(i++);
145             if (node instanceof Element JavaDoc) {
146                 elem = (Element JavaDoc) node;
147                 if (elem.getLocalName().equals("row")) {
148                     k++;
149                 }
150             }
151         }
152 /*
153         if (true) {
154             System.out.print(parentElement.getLocalName());
155             NamedNodeMap attributes = parentElement.getAttributes();
156             for (int a = 0; a < attributes.getLength(); a++) {
157                 System.out.print(" " + attributes.item(a).getNamespaceURI() + ":" +
158                                  attributes.item(a).getLocalName() + "=" +
159                                  attributes.item(a).getNodeValue());
160             }
161             System.out.println(" has " + k + " children");
162         }
163  */

164         return k;
165     }
166
167     public void setMetaData(HierarchicalDataMetaData metaData) {
168         this.metaData = metaData;
169     }
170
171     public HierarchicalDataMetaData getMetaData() {
172         return metaData;
173     }
174
175     public int getColumnCount() {
176         return getMetaData().getColumnCount();
177     }
178
179     /**
180      * @throws IllegalArgumentException if the column name does not exist in
181      * this tabular data model
182      * @param columnName String containing the name of the column
183      * @return integer index of column in the data model which corresponds
184      * to the specified column name
185      */

186     public int getColumnIndex(String JavaDoc columnName) {
187         return getMetaData().getColumnIndex(columnName);
188     }
189
190     public String JavaDoc getColumnName(int column) {
191         return getMetaData().getColumnName(column+1);
192     }
193
194     public Object JavaDoc getValueAt(Object JavaDoc node, int column) {
195         if (node == null)
196             throw new IllegalArgumentException JavaDoc("Node is null; " + column);
197         Element JavaDoc parentElement = (Element JavaDoc) node;
198         NodeList JavaDoc list = ((Element JavaDoc) parentElement).getChildNodes();
199         int i = 0, k = column, max = list.getLength();
200         Node n;
201         Element JavaDoc elem;
202         while (i < max) {
203             n = list.item(i++);
204             if (n instanceof Element JavaDoc) {
205                 elem = (Element JavaDoc) n;
206                 if (elem.getLocalName().equals("cell")) {
207                     if (k-- == 0) {
208                         // System.out.println("Value of " + node + " at column " +
209
// column + "=" + elem + ";" + elem.getFirstChild());
210
Node cellData = elem.getFirstChild();
211                         String JavaDoc rawValue = cellData == null ?
212                                 "" : cellData.getNodeValue();
213                         Converter converter =
214                             getMetaData().getColumnConverter(column + 1);
215                         if (converter == null) {
216                             return rawValue;
217                         }
218                         else {
219                             try {
220                                 /** @todo cache converted value */
221                                 return converter.decode(rawValue, null);
222                             }
223                             catch (Exception JavaDoc ex) {
224                                 return rawValue;
225                             }
226                         }
227                     }
228                 }
229             }
230         }
231         return null;
232     }
233 /*
234     private Object[] getCells(Element element) {
235         Object[] cells = null;
236         switch (userDataSupportType) {
237             case GET_USER_DATA: {
238                 try {
239                     cells = (Object[]) (new Expression(element, "getUserData",
240                         new Object[0])).getValue();
241                 }
242                 catch (Exception ex) {
243                 }
244                 break;
245             }
246             case GET_USER_OBJECT: {
247                 try {
248                     cells = (Object[]) (new Expression(element, "getUserObject",
249                         new Object[0])).getValue();
250                 }
251                 catch (Exception ex) {
252                 }
253                 break;
254             }
255             default: {
256                 break;
257             }
258         }
259         return cells;
260     }
261 */

262 }
263
Popular Tags