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.HashMap ; 18 import java.util.List ; 19 20 import org.eclipse.pde.internal.core.XMLPrintHandler; 21 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSConstants; 22 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModel; 23 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModelFactory; 24 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject; 25 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSRunContainerObject; 26 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItem; 27 import org.eclipse.pde.internal.core.util.PDETextHelper; 28 import org.w3c.dom.Element ; 29 import org.w3c.dom.Node ; 30 import org.w3c.dom.NodeList ; 31 32 36 public class SimpleCSSubItem extends SimpleCSObject implements ISimpleCSSubItem { 37 38 39 42 private String fLabel; 43 44 47 private boolean fSkip; 48 49 52 private String fWhen; 53 54 57 private ISimpleCSRunContainerObject fExecutable; 58 59 62 private static final long serialVersionUID = 1L; 63 64 private static final HashMap LABEL_SUBSTITUTE_CHARS = new HashMap (7); 65 66 static { 67 LABEL_SUBSTITUTE_CHARS.putAll(SUBSTITUTE_CHARS); 68 LABEL_SUBSTITUTE_CHARS.put(new Character ('\n'), " "); LABEL_SUBSTITUTE_CHARS.put(new Character ('\r'), ""); } 71 72 76 public SimpleCSSubItem(ISimpleCSModel model, ISimpleCSObject parent) { 77 super(model, parent); 78 reset(); 79 } 80 81 84 public ISimpleCSRunContainerObject getExecutable() { 85 return fExecutable; 86 } 87 88 91 public String getLabel() { 92 return fLabel; 93 } 94 95 98 public boolean getSkip() { 99 return fSkip; 100 } 101 102 105 public String getWhen() { 106 return fWhen; 107 } 108 109 112 public void setExecutable(ISimpleCSRunContainerObject executable) { 113 ISimpleCSObject old = fExecutable; 114 fExecutable = executable; 115 116 if (isEditable()) { 117 fireStructureChanged(executable, old); 118 } 119 } 120 121 124 public void setLabel(String label) { 125 String old = fLabel; 126 fLabel = label; 127 if (isEditable()) { 128 firePropertyChanged(ATTRIBUTE_LABEL, old, fLabel); 129 } 130 } 131 132 135 public void setSkip(boolean skip) { 136 Boolean old = Boolean.valueOf(fSkip); 137 fSkip = skip; 138 if (isEditable()) { 139 firePropertyChanged(ATTRIBUTE_SKIP, old, Boolean.valueOf(fSkip)); 140 } 141 } 142 143 146 public void setWhen(String when) { 147 String old = fWhen; 148 fWhen = when; 149 if (isEditable()) { 150 firePropertyChanged(ATTRIBUTE_WHEN, old, fWhen); 151 } 152 } 153 154 157 public void parse(Element element) { 158 fLabel = element.getAttribute(ATTRIBUTE_LABEL).trim(); 160 if (element.getAttribute(ATTRIBUTE_SKIP).compareTo( 162 ATTRIBUTE_VALUE_TRUE) == 0) { 163 fSkip = true; 164 } 165 fWhen = element.getAttribute(ATTRIBUTE_WHEN); 168 NodeList children = element.getChildNodes(); 170 ISimpleCSModelFactory factory = getModel().getFactory(); 171 for (int i = 0; i < children.getLength(); i++) { 172 Node child = children.item(i); 173 if (child.getNodeType() == Node.ELEMENT_NODE) { 174 String name = child.getNodeName(); 175 Element childElement = (Element )child; 176 177 if (name.equals(ELEMENT_ACTION)) { 178 fExecutable = factory.createSimpleCSAction(this); 179 fExecutable.parse(childElement); 180 } else if (name.equals(ELEMENT_COMMAND)) { 181 fExecutable = factory.createSimpleCSCommand(this); 182 fExecutable.parse(childElement); 183 } else if (name.equals(ELEMENT_PERFORM_WHEN)) { 184 fExecutable = factory.createSimpleCSPerformWhen(this); 185 fExecutable.parse(childElement); 186 } 187 } 188 } 189 190 } 191 192 195 public void write(String indent, PrintWriter writer) { 196 197 StringBuffer buffer = new StringBuffer (); 198 String newIndent = indent + XMLPrintHandler.XML_INDENT; 199 200 try { 201 buffer.append(ELEMENT_SUBITEM); if ((fLabel != null) && 205 (fLabel.length() > 0)) { 206 buffer.append(XMLPrintHandler.wrapAttribute( 207 ATTRIBUTE_LABEL, 208 PDETextHelper.translateWriteText( 209 fLabel.trim(), LABEL_SUBSTITUTE_CHARS))); 210 } 211 buffer.append(XMLPrintHandler.wrapAttribute( 213 ATTRIBUTE_SKIP, new Boolean (fSkip).toString())); 214 if ((fWhen != null) && 216 (fWhen.length() > 0)) { 217 buffer.append(XMLPrintHandler.wrapAttribute( 219 ATTRIBUTE_WHEN, fWhen)); 220 } 221 XMLPrintHandler.printBeginElement(writer, buffer.toString(), 223 indent, false); 224 if (fExecutable != null) { 226 fExecutable.write(newIndent, writer); 227 } 228 XMLPrintHandler.printEndElement(writer, ELEMENT_SUBITEM, indent); 230 231 } catch (IOException e) { 232 } 235 236 } 237 238 241 public void reset() { 242 fLabel = null; 243 fSkip = false; 244 fWhen = null; 245 fExecutable = null; 246 } 247 248 251 public int getType() { 252 return TYPE_SUBITEM; 253 } 254 255 258 public String getName() { 259 return fLabel; 260 } 261 262 265 public List getChildren() { 266 ArrayList list = new ArrayList (); 267 if ((fExecutable != null) && 268 (fExecutable.getType() == ISimpleCSConstants.TYPE_PERFORM_WHEN)) { 269 list.add(fExecutable); 270 } 271 return list; 272 } 273 274 } 275 | Popular Tags |