KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > core > columns > ColumnsConfigurationConvertor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.core.columns;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import javax.swing.event.ChangeEvent JavaDoc;
25 import javax.swing.event.ChangeListener JavaDoc;
26 import org.netbeans.spi.settings.DOMConvertor;
27 import org.netbeans.spi.settings.Saver;
28 import org.openide.ErrorManager;
29 import org.w3c.dom.Document JavaDoc;
30 import org.w3c.dom.Element JavaDoc;
31 import org.w3c.dom.Node JavaDoc;
32 import org.w3c.dom.NodeList JavaDoc;
33
34 /**
35  * Convertor for column widths in XML format.
36  * Public ID: "-//NetBeans org.netbeans.modules.tasklist//DTD Column Widths 1.0//EN"
37  */

38 public final class ColumnsConfigurationConvertor extends DOMConvertor
39 implements ChangeListener JavaDoc {
40     /**
41      * Creates a converter for the specified FO
42      *
43      * @param fo an XML FO with Column Widths DTD
44      */

45     private static Object JavaDoc create(org.openide.filesystems.FileObject fo) {
46         ColumnsConfigurationConvertor conv = new ColumnsConfigurationConvertor();
47         return conv;
48     }
49     
50     private Saver saver;
51     
52     /**
53      * Creates a new instance
54      */

55     public ColumnsConfigurationConvertor() {
56         super("-//NetBeans org.netbeans.modules.tasklist//DTD Columns 1.0//EN", // NOI18N
57
"http://tasklist.netbeans.org/dtd/columns-1_0.dtd", "columns"); // NOI18N
58
}
59     
60     protected Object JavaDoc readElement(Element JavaDoc element) {
61         NodeList JavaDoc nl = element.getChildNodes();
62         
63         ArrayList JavaDoc w = new ArrayList JavaDoc();
64         ArrayList JavaDoc n = new ArrayList JavaDoc();
65         boolean ascending = true;
66         String JavaDoc sortingColumn = null;
67         
68         for (int i = 0; i < nl.getLength(); i++) {
69             if (nl.item(i).getNodeType() != Node.ELEMENT_NODE)
70                 continue;
71
72             Element JavaDoc node = (Element JavaDoc) nl.item(i);
73             
74             n.add(node.getAttribute("property")); // NOI18N
75
w.add(node.getAttribute("width")); // NOI18N
76

77             if (sortingColumn == null) {
78                 String JavaDoc sort = node.getAttribute("sort"); // NOI18N
79
if (sort.equals("ascending")) { // NOI18N
80
sortingColumn = node.getAttribute("property"); // NOI18N
81
ascending = true;
82                 } else if (sort.equals("descending")) { // NOI18N
83
sortingColumn = node.getAttribute("property"); // NOI18N
84
ascending = false;
85                 }
86             }
87         }
88         
89         int[] ww = new int[w.size()];
90         for (int i = 0; i < w.size(); i++) {
91             try {
92                 ww[i] = Integer.parseInt((String JavaDoc) w.get(i));
93             } catch (NumberFormatException JavaDoc e) {
94                 ErrorManager.getDefault().notify(e);
95             }
96         }
97         String JavaDoc[] nn = (String JavaDoc[]) n.toArray(new String JavaDoc[n.size()]);
98         
99         return new ColumnsConfiguration(nn, ww, sortingColumn, ascending);
100     }
101
102     protected void writeElement(Document JavaDoc doc, Element JavaDoc element, Object JavaDoc obj) {
103         ColumnsConfiguration cw = (ColumnsConfiguration) obj;
104         int[] w = cw.getWidths();
105         String JavaDoc[] n = cw.getProperties();
106         String JavaDoc sortingColumn = cw.getSortingColumn();
107         boolean ascending = cw.getSortingOrder();
108         for (int i = 0; i < n.length; i++) {
109             Element JavaDoc col = doc.createElement("column"); // NOI18N
110
col.setAttribute("property", n[i]); // NOI18N
111
col.setAttribute("width", String.valueOf(w[i])); // NOI18N
112
if (n[i].equals(sortingColumn)) {
113                 col.setAttribute("sort", ascending ? "ascending" : "descending"); // NOI18N
114
}
115             element.appendChild(col);
116         }
117     }
118
119     public void registerSaver(Object JavaDoc inst, Saver s) {
120         this.saver = s;
121         ((ColumnsConfiguration) inst).addChangeListener(this);
122     }
123
124     public void unregisterSaver(Object JavaDoc inst, Saver s) {
125         if (s == null || s != saver) {
126             ErrorManager.getDefault().notify(ErrorManager.ERROR,
127                 new IllegalArgumentException JavaDoc(
128                     "Wrong argument for unregisterSaver(Object=" + inst + // NOI18N
129
", Saver=" + s + ")")); // NOI18N
130
}
131         this.saver = null;
132         ((ColumnsConfiguration) inst).removeChangeListener(this);
133     }
134
135     public void stateChanged(ChangeEvent JavaDoc ev) {
136         try {
137             saver.requestSave();
138         } catch (IOException JavaDoc e) {
139             ErrorManager.getDefault().notify(e);
140         }
141     }
142 }
143
144
Popular Tags