1 19 20 package org.netbeans.modules.tasklist.core.filter; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.IOException ; 25 import java.util.ArrayList ; 26 import java.util.Iterator ; 27 import java.util.LinkedList ; 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 ; 33 import org.w3c.dom.Element ; 34 import org.w3c.dom.Node ; 35 import org.w3c.dom.NodeList ; 36 import org.netbeans.modules.tasklist.core.filter.SuggestionProperty; 37 import org.netbeans.modules.tasklist.core.filter.SuggestionProperties; 38 39 40 47 public abstract class FilterConvertor extends DOMConvertor implements PropertyChangeListener { 48 49 private static final String ATTR_ALLTRUE = "allTrue"; 50 private static final String ELEM_FILTER = "Filter"; 51 private static final String ELEM_CONDITION = "Condition"; 52 private static final String ATTR_NAME = "name"; 53 private static final String ATTR_PROPID = "propertyId"; 54 55 56 private Saver saver; 57 58 59 protected FilterConvertor(String rootElement) { 60 this("-//NetBeans org.netbeans.modules.tasklist//DTD " + rootElement + " 1.0//EN", "http://tasklist.netbeans.org/dtd/filter-1_0.dtd", rootElement); 62 } 63 64 protected FilterConvertor(String publicID, String systemID, String root) { 65 super(publicID, systemID, root); 66 } 67 68 protected abstract Filter createFilter(); 69 70 protected SuggestionProperty getProperty(String propid) { 71 return SuggestionProperties.getProperty(propid); 72 } 73 74 protected Object readElement(org.w3c.dom.Element element) throws java.io.IOException , java.lang.ClassNotFoundException { 75 Filter f = createFilter(); 76 readFilter(element, f); 77 return f; 78 } 79 80 protected void readFilter(org.w3c.dom.Element element, Filter filter) throws java.io.IOException , java.lang.ClassNotFoundException { 81 filter.setMatchAll(Boolean.valueOf(element.getAttribute(ATTR_ALLTRUE)).booleanValue()); 82 filter.setName(element.getAttribute(ATTR_NAME)); 83 84 LinkedList conditions = new LinkedList (); 85 Node child = element.getFirstChild(); 86 while (child != null) { 87 if (child.getNodeType() == Node.ELEMENT_NODE) { 88 FilterCondition fc = (FilterCondition)this.delegateRead((Element )child); 89 String propid = ((Element )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 document, org.w3c.dom.Element element, Object obj) throws java.io.IOException , org.w3c.dom.DOMException { 100 Filter filter = (Filter)obj; 101 writeFilter(document, element, filter); 102 } 103 104 protected void writeFilter(org.w3c.dom.Document document, org.w3c.dom.Element element, Filter filter) throws java.io.IOException , org.w3c.dom.DOMException { 105 element.setAttribute(ATTR_ALLTRUE, Boolean.toString(filter.matchAll())); 106 element.setAttribute(ATTR_NAME, filter.getName()); 107 108 Iterator it = filter.getConditions().iterator(); 109 while (it.hasNext()) { 110 AppliedFilterCondition afc = (AppliedFilterCondition)it.next(); 111 Element 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 obj, org.netbeans.spi.settings.Saver saver) { 118 this.saver = saver; 119 ((Filter)obj).addPropertyChangeListener(this); 120 } 121 122 public void unregisterSaver(Object obj, org.netbeans.spi.settings.Saver saver) { 123 if (saver == null || saver != this.saver) { 124 ErrorManager.getDefault().notify(ErrorManager.ERROR, 125 new IllegalArgumentException ( 126 "Wrong argument for unregisterSaver(Object=" + obj + ", Saver=" + saver + ")")); } 129 ((Filter)obj).removePropertyChangeListener(this); 130 this.saver = null; 131 } 132 133 134 public void propertyChange(PropertyChangeEvent evt) { 135 try { 136 saver.requestSave(); 137 } catch (IOException e) { 138 ErrorManager.getDefault().notify(e); 139 } 140 } 141 142 } 143 | Popular Tags |