KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > core > filter > FilterRepositoryConvertor


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.filter;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import org.netbeans.spi.settings.Convertor;
28 import org.netbeans.spi.settings.DOMConvertor;
29 import org.netbeans.spi.settings.Saver;
30 import org.openide.ErrorManager;
31 import org.w3c.dom.Document JavaDoc;
32 import org.w3c.dom.Element JavaDoc;
33 import org.w3c.dom.Node JavaDoc;
34 import org.w3c.dom.NodeList JavaDoc;
35
36
37 /**
38  * Convertor for FilterRepository. Filters are converted using delegation to
39  * their convertors.
40  * Public ID: "-//NetBeans org.netbeans.modules.tasklist//DTD Filters 1.0//EN"
41  *
42  * @author or141057
43  */

44 public class FilterRepositoryConvertor extends DOMConvertor implements PropertyChangeListener JavaDoc {
45
46     private static final String JavaDoc ATTR_ACTIVE = "active";
47     private static final String JavaDoc ELEM_FILTER = "Filter";
48     private static final String JavaDoc ELEM_FILTERS = "Filters";
49     
50    /**
51      * Creates a converter for the specified FO
52      * @param fo an XML FO with Column Widths DTD
53      */

54     private static Object JavaDoc create(org.openide.filesystems.FileObject fo) {
55         return new FilterRepositoryConvertor();
56     }
57     
58     private Saver saver;
59     
60     /** Creates a new instance of FilerRepositoryConvertor */
61     public FilterRepositoryConvertor() {
62         super("-//NetBeans org.netbeans.modules.tasklist//DTD Filters 1.0//EN", // NOI18N
63
"http://tasklist.netbeans.org/dtd/filters-1_0.dtd", ELEM_FILTERS); // NOI18N
64

65     }
66     
67     protected Object JavaDoc readElement(org.w3c.dom.Element JavaDoc element) throws java.io.IOException JavaDoc, java.lang.ClassNotFoundException JavaDoc {
68         FilterRepository rep = new FilterRepository();
69         int activeIndex = Integer.parseInt(element.getAttribute(ATTR_ACTIVE));
70      
71         Node JavaDoc child = element.getFirstChild();
72         while (child != null) {
73             if ((child.getNodeType() == Node.ELEMENT_NODE)) {
74                 Filter f = (Filter)this.delegateRead((Element JavaDoc)child);
75                 rep.add(f);
76             }
77             child = child.getNextSibling();
78         }
79         
80         if (activeIndex != -1) {
81             rep.setActive((Filter)rep.get(activeIndex));
82         }
83         
84         return rep;
85     }
86
87     protected void writeElement(org.w3c.dom.Document JavaDoc document, org.w3c.dom.Element JavaDoc element, Object JavaDoc obj) throws java.io.IOException JavaDoc, org.w3c.dom.DOMException JavaDoc {
88         FilterRepository fr = (FilterRepository)obj;
89         element.setAttribute(ATTR_ACTIVE, Integer.toString(fr.indexOf(fr.getActive())));
90
91         Iterator JavaDoc fit = fr.iterator();
92         while (fit.hasNext()) {
93             Element JavaDoc childNode = this.delegateWrite(document, fit.next());
94             element.appendChild(childNode);
95         }
96     }
97     
98     public void registerSaver(Object JavaDoc obj, org.netbeans.spi.settings.Saver saver) {
99         this.saver = saver;
100         ((FilterRepository)obj).addPropertyChangeListener(this);
101     }
102     
103     public void unregisterSaver(Object JavaDoc obj, org.netbeans.spi.settings.Saver saver) {
104        if (saver == null || saver != this.saver) {
105             ErrorManager.getDefault().notify(ErrorManager.ERROR,
106                 new IllegalArgumentException JavaDoc(
107                     "Wrong argument for unregisterSaver(Object=" + obj + // NOI18N
108
", Saver=" + saver + ")")); // NOI18N
109
}
110         ((FilterRepository)obj).removePropertyChangeListener(this);
111         this.saver = null;
112     }
113     
114    
115     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
116         try {
117             saver.requestSave();
118         } catch (IOException JavaDoc e) {
119             ErrorManager.getDefault().notify(e);
120         }
121     }
122     
123 }
124
Popular Tags