KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.LinkedList JavaDoc;
28 import org.netbeans.spi.settings.Convertor;
29 import org.netbeans.spi.settings.DOMConvertor;
30 import org.netbeans.spi.settings.Saver;
31 import org.openide.ErrorManager;
32 import org.w3c.dom.Document JavaDoc;
33 import org.w3c.dom.Element JavaDoc;
34 import org.w3c.dom.Node JavaDoc;
35 import org.w3c.dom.NodeList JavaDoc;
36 import org.netbeans.modules.tasklist.core.filter.SuggestionProperty;
37 import org.netbeans.modules.tasklist.core.filter.SuggestionProperties;
38
39
40 /**
41  * A common base for all Filter Convertors. Filters are converted using delegation to
42  * their convertors.
43  * Public ID: "-//NetBeans org.netbeans.modules.tasklist//DTD Filters 1.0//EN"
44  *
45  * @author or141057
46  */

47 public abstract class FilterConvertor extends DOMConvertor implements PropertyChangeListener JavaDoc {
48
49     private static final String JavaDoc ATTR_ALLTRUE = "allTrue";
50     private static final String JavaDoc ELEM_FILTER = "Filter";
51     private static final String JavaDoc ELEM_CONDITION = "Condition";
52     private static final String JavaDoc ATTR_NAME = "name";
53     private static final String JavaDoc ATTR_PROPID = "propertyId";
54     
55   
56     private Saver saver;
57     
58     /** Creates a new instance of FilerRepositoryConvertor */
59     protected FilterConvertor(String JavaDoc rootElement) {
60       this("-//NetBeans org.netbeans.modules.tasklist//DTD " + rootElement + " 1.0//EN", // NOI18N
61
"http://tasklist.netbeans.org/dtd/filter-1_0.dtd", rootElement);
62     }
63
64     protected FilterConvertor(String JavaDoc publicID, String JavaDoc systemID, String JavaDoc root) {
65       super(publicID, systemID, root);
66     }
67     
68     protected abstract Filter createFilter();
69
70     protected SuggestionProperty getProperty(String JavaDoc propid) {
71       return SuggestionProperties.getProperty(propid);
72     }
73     
74     protected Object JavaDoc readElement(org.w3c.dom.Element JavaDoc element) throws java.io.IOException JavaDoc, java.lang.ClassNotFoundException JavaDoc {
75         Filter f = createFilter();
76         readFilter(element, f);
77         return f;
78     }
79     
80     protected void readFilter(org.w3c.dom.Element JavaDoc element, Filter filter) throws java.io.IOException JavaDoc, java.lang.ClassNotFoundException JavaDoc {
81         filter.setMatchAll(Boolean.valueOf(element.getAttribute(ATTR_ALLTRUE)).booleanValue());
82         filter.setName(element.getAttribute(ATTR_NAME));
83         
84         LinkedList JavaDoc conditions = new LinkedList JavaDoc();
85         Node JavaDoc child = element.getFirstChild();
86         while (child != null) {
87             if (child.getNodeType() == Node.ELEMENT_NODE) {
88                 FilterCondition fc = (FilterCondition)this.delegateRead((Element JavaDoc)child);
89         String JavaDoc propid = ((Element JavaDoc)child).getAttribute(ATTR_PROPID);
90         AppliedFilterCondition afc = new AppliedFilterCondition(getProperty(propid), fc);
91         conditions.add(afc);
92             }
93             child = child.getNextSibling();
94         }
95
96     filter.setConditions(conditions);
97     }
98         
99     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 {
100         Filter filter = (Filter)obj;
101         writeFilter(document, element, filter);
102     }
103      
104     protected void writeFilter(org.w3c.dom.Document JavaDoc document, org.w3c.dom.Element JavaDoc element, Filter filter) throws java.io.IOException JavaDoc, org.w3c.dom.DOMException JavaDoc {
105         element.setAttribute(ATTR_ALLTRUE, Boolean.toString(filter.matchAll()));
106         element.setAttribute(ATTR_NAME, filter.getName());
107         
108         Iterator JavaDoc it = filter.getConditions().iterator();
109         while (it.hasNext()) {
110       AppliedFilterCondition afc = (AppliedFilterCondition)it.next();
111       Element JavaDoc e = this.delegateWrite(document, afc.getCondition());
112       e.setAttribute(ATTR_PROPID, afc.getProperty().getID());
113       element.appendChild(e);
114         }
115     }
116     
117     public void registerSaver(Object JavaDoc obj, org.netbeans.spi.settings.Saver saver) {
118         this.saver = saver;
119         ((Filter)obj).addPropertyChangeListener(this);
120     }
121     
122     public void unregisterSaver(Object JavaDoc obj, org.netbeans.spi.settings.Saver saver) {
123        if (saver == null || saver != this.saver) {
124             ErrorManager.getDefault().notify(ErrorManager.ERROR,
125                 new IllegalArgumentException JavaDoc(
126                     "Wrong argument for unregisterSaver(Object=" + obj + // NOI18N
127
", Saver=" + saver + ")")); // NOI18N
128
}
129         ((Filter)obj).removePropertyChangeListener(this);
130         this.saver = null;
131     }
132     
133    
134     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
135         try {
136             saver.requestSave();
137         } catch (IOException JavaDoc e) {
138             ErrorManager.getDefault().notify(e);
139         }
140     }
141     
142 }
143
Popular Tags