1 11 package org.eclipse.pde.internal.ui.model.plugin; 12 13 import java.util.*; 14 15 import org.eclipse.core.runtime.*; 16 import org.eclipse.pde.core.*; 17 import org.eclipse.pde.core.plugin.*; 18 import org.eclipse.pde.internal.core.*; 19 import org.eclipse.pde.internal.ui.model.*; 20 21 public abstract class PluginBaseNode extends PluginObjectNode implements IPluginBase { 22 23 24 private String fSchemaVersion; 25 28 public void add(IPluginLibrary library) throws CoreException { 29 IDocumentNode parent = getEnclosingElement("runtime", true); if (library instanceof PluginLibraryNode) { 31 PluginLibraryNode node = (PluginLibraryNode)library; 32 node.setModel(getModel()); 33 library.setInTheModel(true); 34 parent.addChildNode(node); 35 fireStructureChanged(library, IModelChangedEvent.INSERT); 36 } 37 } 38 41 public void add(IPluginImport pluginImport) throws CoreException { 42 IDocumentNode parent = getEnclosingElement("requires", true); if (pluginImport instanceof PluginImportNode) { 44 PluginImportNode node = (PluginImportNode)pluginImport; 45 node.setModel(getModel()); 46 pluginImport.setInTheModel(true); 47 parent.addChildNode(node); 48 fireStructureChanged(pluginImport, IModelChangedEvent.INSERT); 49 } 50 } 51 54 public void remove(IPluginImport pluginImport) throws CoreException { 55 IDocumentNode parent = getEnclosingElement("requires", false); if (parent != null) { 57 parent.removeChildNode((IDocumentNode)pluginImport); 58 pluginImport.setInTheModel(false); 59 fireStructureChanged(pluginImport, IModelChangedEvent.REMOVE); 60 } 61 } 62 63 66 public IPluginLibrary[] getLibraries() { 67 ArrayList result = new ArrayList(); 68 IDocumentNode requiresNode = getEnclosingElement("runtime", false); if (requiresNode != null) { 70 IDocumentNode[] children = requiresNode.getChildNodes(); 71 for (int i = 0; i < children.length; i++) { 72 if (children[i] instanceof IPluginLibrary) 73 result.add(children[i]); 74 } 75 } 76 77 return (IPluginLibrary[]) result.toArray(new IPluginLibrary[result.size()]); 78 } 79 80 private IDocumentNode getEnclosingElement(String elementName, boolean create) { 81 PluginElementNode element = null; 82 IDocumentNode[] children = getChildNodes(); 83 for (int i = 0; i < children.length; i++) { 84 if (children[i] instanceof IPluginElement) { 85 if (((PluginElementNode)children[i]).getXMLTagName().equals(elementName)) { 86 element = (PluginElementNode)children[i]; 87 break; 88 } 89 } 90 } 91 if (element == null && create) { 92 element = new PluginElementNode(); 93 element.setXMLTagName(elementName); 94 element.setParentNode(this); 95 element.setModel(getModel()); 96 element.setInTheModel(true); 97 if (elementName.equals("runtime")) { addChildNode(element, 0); 99 } else if (elementName.equals("requires")) { if (children.length > 0 && children[0].getXMLTagName().equals("runtime")) { addChildNode(element, 1); 102 } else { 103 addChildNode(element, 0); 104 } 105 } 106 } 107 return element; 108 } 109 110 113 public IPluginImport[] getImports() { 114 ArrayList result = new ArrayList(); 115 IDocumentNode requiresNode = getEnclosingElement("requires", false); if (requiresNode != null) { 117 IDocumentNode[] children = requiresNode.getChildNodes(); 118 for (int i = 0; i < children.length; i++) { 119 if (children[i] instanceof IPluginImport) 120 result.add(children[i]); 121 } 122 } 123 124 return (IPluginImport[]) result.toArray(new IPluginImport[result.size()]); 125 } 126 129 public String getProviderName() { 130 return getXMLAttributeValue(P_PROVIDER); 131 } 132 135 public String getVersion() { 136 return getXMLAttributeValue(P_VERSION); 137 } 138 141 public void remove(IPluginLibrary library) throws CoreException { 142 IDocumentNode parent = getEnclosingElement("runtime", false); if (parent != null) { 144 parent.removeChildNode((IDocumentNode)library); 145 library.setInTheModel(false); 146 fireStructureChanged(library, IModelChangedEvent.REMOVE); 147 } 148 } 149 152 public void setProviderName(String providerName) throws CoreException { 153 setXMLAttribute(P_PROVIDER, providerName); 154 } 155 158 public void setVersion(String version) throws CoreException { 159 setXMLAttribute(P_VERSION, version); 160 } 161 164 public void swap(IPluginLibrary l1, IPluginLibrary l2) throws CoreException { 165 IDocumentNode node = getEnclosingElement("runtime", false); if (node != null) { 167 node.swap((IDocumentNode)l1, (IDocumentNode)l2); 168 firePropertyChanged(node, P_LIBRARY_ORDER, l1, l2); 169 } 170 } 171 174 public String getSchemaVersion() { 175 return fSchemaVersion; 176 } 177 180 public void setSchemaVersion(String schemaVersion) throws CoreException { 181 fSchemaVersion = schemaVersion; 182 } 183 186 public void add(IPluginExtension extension) throws CoreException { 187 if (extension instanceof PluginExtensionNode) { 188 PluginExtensionNode node = (PluginExtensionNode)extension; 189 node.setModel(getModel()); 190 extension.setInTheModel(true); 191 addChildNode(node); 192 fireStructureChanged(extension, IModelChangedEvent.INSERT); 193 } 194 } 195 198 public void add(IPluginExtensionPoint extensionPoint) throws CoreException { 199 if (extensionPoint instanceof PluginExtensionPointNode) { 200 PluginExtensionPointNode node = (PluginExtensionPointNode)extensionPoint; 201 node.setModel(getModel()); 202 extensionPoint.setInTheModel(true); 203 node.setParentNode(this); 204 IPluginExtensionPoint[] extPoints = getExtensionPoints(); 205 if (extPoints.length > 0) 206 addChildNode(node, indexOf((IDocumentNode)extPoints[extPoints.length - 1]) + 1); 207 else { 208 IDocumentNode requires = getEnclosingElement("requires", false); if (requires != null) { 210 addChildNode(node, indexOf(requires) + 1); 211 } else { 212 IDocumentNode runtime = getEnclosingElement("runtime", false); if (runtime != null) 214 addChildNode(node, indexOf(runtime) + 1); 215 else 216 addChildNode(node, 0); 217 } 218 } 219 fireStructureChanged(extensionPoint, IModelChangedEvent.INSERT); 220 } 221 } 222 225 public IPluginExtensionPoint[] getExtensionPoints() { 226 ArrayList result = new ArrayList(); 227 IDocumentNode[] children = getChildNodes(); 228 for (int i = 0; i < children.length; i++) { 229 if (children[i] instanceof IPluginExtensionPoint) 230 result.add(children[i]); 231 } 232 return (IPluginExtensionPoint[]) result.toArray(new IPluginExtensionPoint[result.size()]); 233 } 234 237 public IPluginExtension[] getExtensions() { 238 ArrayList result = new ArrayList(); 239 IDocumentNode[] children = getChildNodes(); 240 for (int i = 0; i < children.length; i++) { 241 if (children[i] instanceof IPluginExtension) 242 result.add(children[i]); 243 } 244 return (IPluginExtension[]) result.toArray(new IPluginExtension[result.size()]); 245 } 246 public int getIndexOf(IPluginExtension e) { 247 IPluginExtension [] children = getExtensions(); 248 for (int i=0; i<children.length; i++) { 249 if (children[i].equals(e)) 250 return i; 251 } 252 return -1; 253 } 254 257 public void remove(IPluginExtension extension) throws CoreException { 258 if (extension instanceof IDocumentNode) { 259 removeChildNode((IDocumentNode)extension); 260 extension.setInTheModel(false); 261 fireStructureChanged(extension, IModelChangedEvent.REMOVE); 262 } 263 } 264 267 public void remove(IPluginExtensionPoint extensionPoint) 268 throws CoreException { 269 if (extensionPoint instanceof IDocumentNode) { 270 removeChildNode((IDocumentNode)extensionPoint); 271 extensionPoint.setInTheModel(false); 272 fireStructureChanged(extensionPoint, IModelChangedEvent.REMOVE); 273 } 274 } 275 278 public void swap(IPluginExtension e1, IPluginExtension e2) 279 throws CoreException { 280 swap((IDocumentNode)e1, (IDocumentNode)e2); 281 firePropertyChanged(this, P_EXTENSION_ORDER, e1, e2); 282 } 283 284 287 public void swap(IPluginImport import1, IPluginImport import2) 288 throws CoreException { 289 IDocumentNode node = getEnclosingElement("requires", false); if (node != null) { 291 node.swap((IDocumentNode)import1, (IDocumentNode)import2); 292 firePropertyChanged(node, P_IMPORT_ORDER, import1, import2); 293 } 294 } 295 298 public String getId() { 299 return getXMLAttributeValue(P_ID); 300 } 301 304 public void setId(String id) throws CoreException { 305 setXMLAttribute(P_ID, id); 306 } 307 308 311 public String getName() { 312 return getXMLAttributeValue(P_NAME); 313 } 314 315 318 public void setName(String name) throws CoreException { 319 setXMLAttribute(P_NAME, name); 320 } 321 322 325 public String write(boolean indent) { 326 String newLine = getLineDelimiter(); 327 328 StringBuffer buffer = new StringBuffer (); 329 buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + newLine); if (PDECore.getDefault().getModelManager().isOSGiRuntime()) { 331 buffer.append("<?eclipse version=\"3.0\"?>" + newLine); } 333 buffer.append(writeShallow(false) + newLine); 334 335 IDocumentNode runtime = getEnclosingElement("runtime", false); if (runtime != null) { 337 runtime.setLineIndent(getLineIndent() + 3); 338 buffer.append(runtime.write(true) + newLine); 339 } 340 341 IDocumentNode requires = getEnclosingElement("requires", false); if (requires != null) { 343 requires.setLineIndent(getLineIndent() + 3); 344 buffer.append(requires.write(true) + newLine); 345 } 346 347 IPluginExtensionPoint[] extPoints = getExtensionPoints(); 348 for (int i = 0; i < extPoints.length; i++) { 349 IDocumentNode extPoint = (IDocumentNode)extPoints[i]; 350 extPoint.setLineIndent(getLineIndent() + 3); 351 buffer.append(extPoint.write(true) + newLine); 352 } 353 354 IPluginExtension[] extensions = getExtensions(); 355 for (int i = 0; i < extensions.length; i++) { 356 IDocumentNode extension = (IDocumentNode)extensions[i]; 357 extension.setLineIndent(getLineIndent() + 3); 358 buffer.append(extension.write(true) + newLine); 359 } 360 361 buffer.append("</" + getXMLTagName() + ">"); return buffer.toString(); 363 } 364 365 368 public String writeShallow(boolean terminate) { 369 String newLine = System.getProperty("line.separator"); StringBuffer buffer = new StringBuffer (); 371 buffer.append("<" + getXMLTagName()); buffer.append(newLine); 373 374 String id = getId(); 375 if (id != null && id.trim().length() > 0) 376 buffer.append(" " + P_ID + "=\"" + getWritableString(id) + "\"" + newLine); 378 String name = getName(); 379 if (name != null && name.trim().length() > 0) 380 buffer.append(" " + P_NAME + "=\"" + getWritableString(name) + "\"" + newLine); 382 String version = getVersion(); 383 if (version != null && version.trim().length() > 0) 384 buffer.append(" " + P_VERSION + "=\"" + getWritableString(version) + "\"" + newLine); 386 String provider = getProviderName(); 387 if (provider != null && provider.trim().length() > 0) { 388 buffer.append(" " + P_PROVIDER + "=\"" + getWritableString(provider) + "\""); } 390 391 String [] specific = getSpecificAttributes(); 392 for (int i = 0; i < specific.length; i++) 393 buffer.append(newLine + specific[i]); 394 if (terminate) 395 buffer.append("/"); buffer.append(">"); 398 return buffer.toString(); 399 } 400 401 protected abstract String [] getSpecificAttributes(); 402 } 403 | Popular Tags |