1 11 package org.eclipse.ui.internal.intro.universal; 12 13 import java.io.PrintWriter ; 14 import java.util.ArrayList ; 15 import java.util.List ; 16 17 import org.eclipse.ui.intro.config.IntroElement; 18 import org.w3c.dom.Element ; 19 import org.w3c.dom.Node ; 20 import org.w3c.dom.NodeList ; 21 22 23 public class GroupData { 24 boolean fDefault=false; 25 private String path; 26 private ArrayList children = new ArrayList (); 27 28 public GroupData(String path, boolean defaultGroup) { 29 fDefault = defaultGroup; 30 this.path = path; 31 } 32 33 public GroupData(Element element) { 34 if (element.getNodeName().equals("hidden")) path = IUniversalIntroConstants.HIDDEN; 36 else 37 path = element.getAttribute("path"); NodeList children = element.getChildNodes(); 39 for (int i = 0; i < children.getLength(); i++) { 40 Node child = children.item(i); 41 if (child.getNodeType() == Node.ELEMENT_NODE) { 42 if (child.getNodeName().equals("extension")) { loadExtension((Element) child); 44 } 45 else if (child.getNodeName().equals("separator")) { loadSeparator((Element)child); 47 } 48 } 49 } 50 String df = element.getAttribute("default"); if (df!=null && df.equalsIgnoreCase("true")) fDefault = true; 53 } 54 55 public boolean isHidden() { 56 return (path.equals(IUniversalIntroConstants.HIDDEN)); 57 } 58 59 public boolean isDefault() { 60 return fDefault; 61 } 62 63 public void addAnchors(List result) { 64 for (int i = 0; i < children.size(); i++) { 65 BaseData edata = (BaseData) children.get(i); 66 String id = edata.getId(); 67 IntroElement element = null; 68 String tagName="anchor"; if (edata instanceof SeparatorData) 70 tagName = "hr"; element = new IntroElement(tagName); 72 element.setAttribute("id", id); result.add(element); 74 } 75 } 76 77 public void add(BaseData ed) { 78 children.add(ed); 79 ed.setParent(this); 80 } 81 public void add(int index, BaseData ed) { 82 children.add(index, ed); 83 ed.setParent(this); 84 } 85 public void remove(BaseData ed) { 86 children.remove(ed); 87 ed.setParent(null); 88 } 89 90 public void addImplicitExtension(String id, String name) { 91 ExtensionData ed = new ExtensionData(id, name, IUniversalIntroConstants.LOW, true); 92 add(ed); 93 } 94 95 private void loadSeparator(Element element) { 96 String id = element.getAttribute("id"); SeparatorData sd = new SeparatorData(id); 98 add(sd); 99 } 100 101 private void loadExtension(Element element) { 102 String id = element.getAttribute("id"); String name = element.getAttribute("name"); String importance = element.getAttribute("importance"); ExtensionData ed = new ExtensionData(id, name, importance, false); 106 add(ed); 107 } 108 109 public BaseData[] getChildren() { 110 return (BaseData[])children.toArray(new BaseData[children.size()]); 111 } 112 113 public int getExtensionCount() { 114 int count=0; 115 for (int i=0; i<children.size(); i++) { 116 BaseData data = (BaseData)children.get(i); 117 if (data instanceof ExtensionData) 118 count++; 119 } 120 return count; 121 } 122 133 134 public String getPath() { 135 return path; 136 } 137 138 public boolean contains(String id) { 139 return find(id)!=null; 140 } 141 142 BaseData find(String extensionId) { 143 for (int i = 0; i < children.size(); i++) { 144 BaseData data = (BaseData) children.get(i); 145 if (data.getId().equals(extensionId)) 146 return data; 147 } 148 return null; 149 } 150 151 public int getIndexOf(BaseData ed) { 152 return children.indexOf(ed); 153 } 154 155 public int getIndexOf(String baseId) { 156 for (int i = 0; i < children.size(); i++) { 157 BaseData bd = (BaseData) children.get(i); 158 if (bd.getId().equals(baseId)) 159 return i; 160 } 161 return -1; 162 } 163 164 public boolean canMoveUp(BaseData ed) { 165 int index = children.indexOf(ed); 166 return (index>0); 167 } 168 169 public boolean canMoveDown(BaseData ed) { 170 int index = children.indexOf(ed); 171 return (index!= -1 && index < children.size()-1); 172 } 173 174 public void moveUp(BaseData ed) { 175 int index = children.indexOf(ed); 176 BaseData swapped = (BaseData)children.get(index-1); 177 children.set(index, swapped); 178 children.set(index-1, ed); 179 } 180 181 public void moveDown(BaseData ed) { 182 int index = children.indexOf(ed); 183 BaseData swapped = (BaseData)children.get(index+1); 184 children.set(index, swapped); 185 children.set(index+1, ed); 186 } 187 188 public void addSeparator(BaseData after) { 189 SeparatorData sd = new SeparatorData(); 190 sd.id = ""+sd.hashCode(); if (after!=null) { 192 int index = children.indexOf(after); 193 if (index!= -1) { 194 children.add(index+1, sd); 195 return; 196 } 197 } 198 children.add(sd); 199 } 200 201 public void write(PrintWriter writer, String indent) { 202 writer.print(indent); 203 if (isHidden()) 204 writer.print("<hidden>"); else { 206 writer.print("<group path=\""+path+"\""); if (fDefault) 208 writer.println(" default=\"true\">"); else 210 writer.println(">"); } 212 for (int i=0; i<children.size(); i++) { 213 BaseData ed = (BaseData)children.get(i); 214 ed.write(writer, indent+" "); } 216 writer.print(indent); 217 if (isHidden()) 218 writer.println("</hidden>"); else 220 writer.println("</group>"); } 222 } | Popular Tags |