1 11 12 package org.eclipse.pde.internal.core.cheatsheet.simple; 13 14 import java.io.IOException ; 15 import java.io.PrintWriter ; 16 import java.util.ArrayList ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 20 import org.eclipse.pde.core.IModelChangedEvent; 21 import org.eclipse.pde.internal.core.XMLPrintHandler; 22 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSConditionalSubItem; 23 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModel; 24 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModelFactory; 25 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject; 26 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItem; 27 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItemObject; 28 import org.w3c.dom.Element ; 29 import org.w3c.dom.Node ; 30 import org.w3c.dom.NodeList ; 31 32 36 public class SimpleCSConditionalSubItem extends SimpleCSObject implements 37 ISimpleCSConditionalSubItem { 38 39 42 private String fCondition; 43 44 47 private ArrayList fSubItems; 48 49 52 private static final long serialVersionUID = 1L; 53 54 58 public SimpleCSConditionalSubItem(ISimpleCSModel model, ISimpleCSObject parent) { 59 super(model, parent); 60 reset(); 61 } 62 63 66 public String getCondition() { 67 return fCondition; 68 } 69 70 73 public ISimpleCSSubItem[] getSubItems() { 74 return (ISimpleCSSubItem[]) fSubItems.toArray( 75 new ISimpleCSSubItemObject[fSubItems.size()]); 76 } 77 78 81 public void setCondition(String condition) { 82 String old = fCondition; 83 fCondition = condition; 84 if (isEditable()) { 85 firePropertyChanged(ATTRIBUTE_CONDITION, old, fCondition); 86 } 87 } 88 89 92 public void parse(Element element) { 93 94 fCondition = element.getAttribute(ATTRIBUTE_CONDITION); 97 98 100 NodeList children = element.getChildNodes(); 101 ISimpleCSModelFactory factory = getModel().getFactory(); 102 for (int i = 0; i < children.getLength(); i++) { 103 Node child = children.item(i); 104 if (child.getNodeType() == Node.ELEMENT_NODE) { 105 String name = child.getNodeName(); 106 Element childElement = (Element )child; 107 108 if (name.equals(ELEMENT_SUBITEM)) { 109 ISimpleCSSubItem subitem = factory.createSimpleCSSubItem(this); 110 fSubItems.add(subitem); 111 subitem.parse(childElement); 112 } 113 } 114 } 115 116 } 117 118 121 public void write(String indent, PrintWriter writer) { 122 123 StringBuffer buffer = new StringBuffer (); 124 String newIndent = indent + XMLPrintHandler.XML_INDENT; 125 126 try { 127 buffer.append(ELEMENT_CONDITIONAL_SUBITEM); 129 if ((fCondition != null) && 132 (fCondition.length() > 0)) { 133 buffer.append(XMLPrintHandler.wrapAttribute( 134 ATTRIBUTE_CONDITION, fCondition)); 135 } 136 XMLPrintHandler.printBeginElement(writer, buffer.toString(), 138 indent, false); 139 Iterator iterator = fSubItems.iterator(); 141 while (iterator.hasNext()) { 142 ISimpleCSSubItem subitem = (ISimpleCSSubItem)iterator.next(); 143 subitem.write(newIndent, writer); 144 } 145 XMLPrintHandler.printEndElement(writer, ELEMENT_CONDITIONAL_SUBITEM, indent); 147 148 } catch (IOException e) { 149 } 152 153 } 154 155 158 public void reset() { 159 fCondition = null; 160 fSubItems = new ArrayList (); 161 } 162 163 166 public int getType() { 167 return TYPE_CONDITIONAL_SUBITEM; 168 } 169 170 173 public String getName() { 174 return ELEMENT_CONDITIONAL_SUBITEM; 176 } 177 178 181 public List getChildren() { 182 ArrayList list = new ArrayList (); 183 if (fSubItems.size() > 0) { 185 list.addAll(fSubItems); 186 } 187 return list; 188 } 189 190 193 public void addSubItem(ISimpleCSSubItem subitem) { 194 fSubItems.add(subitem); 195 196 if (isEditable()) { 197 fireStructureChanged(subitem, IModelChangedEvent.INSERT); 198 } 199 } 200 201 204 public void removeSubItem(ISimpleCSSubItem subitem) { 205 fSubItems.remove(subitem); 206 207 if (isEditable()) { 208 fireStructureChanged(subitem, IModelChangedEvent.REMOVE); 209 } 210 } 211 212 } 213 | Popular Tags |