1 19 20 package org.netbeans.modules.tasklist.core.columns; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import javax.swing.event.ChangeEvent ; 25 import javax.swing.event.ChangeListener ; 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 ; 30 import org.w3c.dom.Element ; 31 import org.w3c.dom.Node ; 32 import org.w3c.dom.NodeList ; 33 34 38 public final class ColumnsConfigurationConvertor extends DOMConvertor 39 implements ChangeListener { 40 45 private static Object create(org.openide.filesystems.FileObject fo) { 46 ColumnsConfigurationConvertor conv = new ColumnsConfigurationConvertor(); 47 return conv; 48 } 49 50 private Saver saver; 51 52 55 public ColumnsConfigurationConvertor() { 56 super("-//NetBeans org.netbeans.modules.tasklist//DTD Columns 1.0//EN", "http://tasklist.netbeans.org/dtd/columns-1_0.dtd", "columns"); } 59 60 protected Object readElement(Element element) { 61 NodeList nl = element.getChildNodes(); 62 63 ArrayList w = new ArrayList (); 64 ArrayList n = new ArrayList (); 65 boolean ascending = true; 66 String 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 node = (Element ) nl.item(i); 73 74 n.add(node.getAttribute("property")); w.add(node.getAttribute("width")); 77 if (sortingColumn == null) { 78 String sort = node.getAttribute("sort"); if (sort.equals("ascending")) { sortingColumn = node.getAttribute("property"); ascending = true; 82 } else if (sort.equals("descending")) { sortingColumn = node.getAttribute("property"); 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 ) w.get(i)); 93 } catch (NumberFormatException e) { 94 ErrorManager.getDefault().notify(e); 95 } 96 } 97 String [] nn = (String []) n.toArray(new String [n.size()]); 98 99 return new ColumnsConfiguration(nn, ww, sortingColumn, ascending); 100 } 101 102 protected void writeElement(Document doc, Element element, Object obj) { 103 ColumnsConfiguration cw = (ColumnsConfiguration) obj; 104 int[] w = cw.getWidths(); 105 String [] n = cw.getProperties(); 106 String sortingColumn = cw.getSortingColumn(); 107 boolean ascending = cw.getSortingOrder(); 108 for (int i = 0; i < n.length; i++) { 109 Element col = doc.createElement("column"); col.setAttribute("property", n[i]); col.setAttribute("width", String.valueOf(w[i])); if (n[i].equals(sortingColumn)) { 113 col.setAttribute("sort", ascending ? "ascending" : "descending"); } 115 element.appendChild(col); 116 } 117 } 118 119 public void registerSaver(Object inst, Saver s) { 120 this.saver = s; 121 ((ColumnsConfiguration) inst).addChangeListener(this); 122 } 123 124 public void unregisterSaver(Object inst, Saver s) { 125 if (s == null || s != saver) { 126 ErrorManager.getDefault().notify(ErrorManager.ERROR, 127 new IllegalArgumentException ( 128 "Wrong argument for unregisterSaver(Object=" + inst + ", Saver=" + s + ")")); } 131 this.saver = null; 132 ((ColumnsConfiguration) inst).removeChangeListener(this); 133 } 134 135 public void stateChanged(ChangeEvent ev) { 136 try { 137 saver.requestSave(); 138 } catch (IOException e) { 139 ErrorManager.getDefault().notify(e); 140 } 141 } 142 } 143 144 | Popular Tags |