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.ISimpleCSAction; 23 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSCommand; 24 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModel; 25 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModelFactory; 26 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject; 27 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSPerformWhen; 28 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSRunObject; 29 import org.w3c.dom.Element ; 30 import org.w3c.dom.Node ; 31 import org.w3c.dom.NodeList ; 32 33 37 public class SimpleCSPerformWhen extends SimpleCSObject implements 38 ISimpleCSPerformWhen { 39 40 43 private String fCondition; 44 45 48 private ArrayList fExecutables; 49 50 53 private static final long serialVersionUID = 1L; 54 55 59 public SimpleCSPerformWhen(ISimpleCSModel model, ISimpleCSObject parent) { 60 super(model, parent); 61 reset(); 62 } 63 64 67 public String getCondition() { 68 return fCondition; 69 } 70 71 74 public ISimpleCSRunObject[] getExecutables() { 75 return (ISimpleCSRunObject[]) fExecutables.toArray( 76 new ISimpleCSRunObject[fExecutables.size()]); 77 } 78 79 82 public void setCondition(String condition) { 83 String old = fCondition; 84 fCondition = condition; 85 if (isEditable()) { 86 firePropertyChanged(ATTRIBUTE_CONDITION, old, fCondition); 87 } 88 } 89 90 93 public void parse(Element element) { 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_COMMAND)) { 109 ISimpleCSCommand command = factory.createSimpleCSCommand(this); 110 fExecutables.add(command); 111 command.parse(childElement); 112 } else if (name.equals(ELEMENT_ACTION)) { 113 ISimpleCSAction action = factory.createSimpleCSAction(this); 114 fExecutables.add(action); 115 action.parse(childElement); 116 } 117 } 118 } 119 120 } 121 122 125 public void write(String indent, PrintWriter writer) { 126 127 StringBuffer buffer = new StringBuffer (); 128 String newIndent = indent + XMLPrintHandler.XML_INDENT; 129 130 try { 131 buffer.append(ELEMENT_PERFORM_WHEN); 133 if ((fCondition != null) && 135 (fCondition.length() > 0)) { 136 buffer.append(XMLPrintHandler.wrapAttribute( 138 ATTRIBUTE_CONDITION, fCondition)); 139 } 140 XMLPrintHandler.printBeginElement(writer, buffer.toString(), 142 indent, false); 143 Iterator iterator = fExecutables.iterator(); 145 while (iterator.hasNext()) { 146 ISimpleCSRunObject executable = (ISimpleCSRunObject)iterator.next(); 147 executable.write(newIndent, writer); 148 } 149 XMLPrintHandler.printEndElement(writer, ELEMENT_PERFORM_WHEN, indent); 151 152 } catch (IOException e) { 153 } 156 } 157 158 161 public void reset() { 162 fCondition = null; 163 fExecutables = new ArrayList (); 164 } 165 166 169 public int getType() { 170 return TYPE_PERFORM_WHEN; 171 } 172 173 176 public String getName() { 177 return ELEMENT_PERFORM_WHEN; 179 } 180 181 184 public List getChildren() { 185 return new ArrayList (); 186 } 187 188 191 public void addExecutable(ISimpleCSRunObject executable) { 192 fExecutables.add(executable); 193 194 if (isEditable()) { 195 fireStructureChanged(executable, IModelChangedEvent.INSERT); 196 } 197 } 198 199 202 public void removeExecutable(ISimpleCSRunObject executable) { 203 fExecutables.remove(executable); 204 205 if (isEditable()) { 206 fireStructureChanged(executable, IModelChangedEvent.REMOVE); 207 } 208 } 209 210 } 211 | Popular Tags |