KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > text > plugin > PluginBaseNode


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.core.text.plugin;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.pde.core.IModelChangedEvent;
17 import org.eclipse.pde.core.plugin.IPluginBase;
18 import org.eclipse.pde.core.plugin.IPluginElement;
19 import org.eclipse.pde.core.plugin.IPluginExtension;
20 import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
21 import org.eclipse.pde.core.plugin.IPluginImport;
22 import org.eclipse.pde.core.plugin.IPluginLibrary;
23 import org.eclipse.pde.core.plugin.IPluginObject;
24 import org.eclipse.pde.internal.core.text.IDocumentNode;
25
26 public abstract class PluginBaseNode extends PluginObjectNode implements IPluginBase {
27         
28     private String JavaDoc fSchemaVersion;
29     
30     /* (non-Javadoc)
31      * @see org.eclipse.pde.core.plugin.IPluginBase#add(org.eclipse.pde.core.plugin.IPluginLibrary)
32      */

33     public void add(IPluginLibrary library) throws CoreException {
34         IDocumentNode parent = getEnclosingElement("runtime", true); //$NON-NLS-1$
35
if (library instanceof PluginLibraryNode) {
36             PluginLibraryNode node = (PluginLibraryNode)library;
37             node.setModel(getModel());
38             parent.addChildNode(node);
39             fireStructureChanged(library, IModelChangedEvent.INSERT);
40         }
41     }
42     /* (non-Javadoc)
43      * @see org.eclipse.pde.core.plugin.IPluginBase#add(org.eclipse.pde.core.plugin.IPluginImport)
44      */

45     public void add(IPluginImport pluginImport) throws CoreException {
46         IDocumentNode parent = getEnclosingElement("requires", true); //$NON-NLS-1$
47
if (pluginImport instanceof PluginImportNode) {
48             PluginImportNode node = (PluginImportNode)pluginImport;
49             parent.addChildNode(node);
50             fireStructureChanged(pluginImport, IModelChangedEvent.INSERT);
51         }
52     }
53     
54     public void add(IPluginImport[] pluginImports) throws CoreException {
55         IDocumentNode parent = getEnclosingElement("requires", true); //$NON-NLS-1$
56
for (int i = 0; i < pluginImports.length; i++) {
57             if (pluginImports[i] != null && pluginImports[i] instanceof PluginImportNode) {
58                 PluginImportNode node = (PluginImportNode)pluginImports[i];
59                 parent.addChildNode(node);
60             }
61         }
62         fireStructureChanged(pluginImports, IModelChangedEvent.INSERT);
63     }
64     /* (non-Javadoc)
65      * @see org.eclipse.pde.core.plugin.IPluginBase#remove(org.eclipse.pde.core.plugin.IPluginImport)
66      */

67     public void remove(IPluginImport pluginImport) throws CoreException {
68         IDocumentNode parent = getEnclosingElement("requires", false); //$NON-NLS-1$
69
if (parent != null) {
70             parent.removeChildNode((IDocumentNode)pluginImport);
71             pluginImport.setInTheModel(false);
72             fireStructureChanged(pluginImport, IModelChangedEvent.REMOVE);
73         }
74     }
75     
76     public void remove(IPluginImport[] pluginImports) throws CoreException {
77         IDocumentNode parent = getEnclosingElement("requires", false); //$NON-NLS-1$
78
if (parent != null) {
79             for (int i = 0; i < pluginImports.length; i++) {
80                 parent.removeChildNode((IDocumentNode)pluginImports[i]);
81                 pluginImports[i].setInTheModel(false);
82             }
83             fireStructureChanged(pluginImports, IModelChangedEvent.REMOVE);
84         }
85     }
86     
87     /* (non-Javadoc)
88      * @see org.eclipse.pde.core.plugin.IPluginBase#getLibraries()
89      */

90     public IPluginLibrary[] getLibraries() {
91         ArrayList JavaDoc result = new ArrayList JavaDoc();
92         IDocumentNode requiresNode = getEnclosingElement("runtime", false); //$NON-NLS-1$
93
if (requiresNode != null) {
94             IDocumentNode[] children = requiresNode.getChildNodes();
95             for (int i = 0; i < children.length; i++) {
96                 if (children[i] instanceof IPluginLibrary)
97                     result.add(children[i]);
98             }
99         }
100         
101         return (IPluginLibrary[]) result.toArray(new IPluginLibrary[result.size()]);
102     }
103     
104     private IDocumentNode getEnclosingElement(String JavaDoc elementName, boolean create) {
105         PluginElementNode element = null;
106         IDocumentNode[] children = getChildNodes();
107         for (int i = 0; i < children.length; i++) {
108             if (children[i] instanceof IPluginElement) {
109                 if (((PluginElementNode)children[i]).getXMLTagName().equals(elementName)) {
110                     element = (PluginElementNode)children[i];
111                     break;
112                 }
113             }
114         }
115         if (element == null && create) {
116             element = new PluginElementNode();
117             element.setXMLTagName(elementName);
118             element.setParentNode(this);
119             element.setModel(getModel());
120             element.setInTheModel(true);
121             if (elementName.equals("runtime")) { //$NON-NLS-1$
122
addChildNode(element, 0);
123             } else if (elementName.equals("requires")) { //$NON-NLS-1$
124
if (children.length > 0 && children[0].getXMLTagName().equals("runtime")) { //$NON-NLS-1$
125
addChildNode(element, 1);
126                 } else {
127                     addChildNode(element, 0);
128                 }
129             }
130         }
131         return element;
132     }
133     
134     /* (non-Javadoc)
135      * @see org.eclipse.pde.core.plugin.IPluginBase#getImports()
136      */

137     public IPluginImport[] getImports() {
138         ArrayList JavaDoc result = new ArrayList JavaDoc();
139         IDocumentNode requiresNode = getEnclosingElement("requires", false); //$NON-NLS-1$
140
if (requiresNode != null) {
141             IDocumentNode[] children = requiresNode.getChildNodes();
142             for (int i = 0; i < children.length; i++) {
143                 if (children[i] instanceof IPluginImport)
144                     result.add(children[i]);
145             }
146         }
147         
148         return (IPluginImport[]) result.toArray(new IPluginImport[result.size()]);
149     }
150     /* (non-Javadoc)
151      * @see org.eclipse.pde.core.plugin.IPluginBase#getProviderName()
152      */

153     public String JavaDoc getProviderName() {
154         return getXMLAttributeValue(P_PROVIDER);
155     }
156     /* (non-Javadoc)
157      * @see org.eclipse.pde.core.plugin.IPluginBase#getVersion()
158      */

159     public String JavaDoc getVersion() {
160         return getXMLAttributeValue(P_VERSION);
161     }
162     /* (non-Javadoc)
163      * @see org.eclipse.pde.core.plugin.IPluginBase#remove(org.eclipse.pde.core.plugin.IPluginLibrary)
164      */

165     public void remove(IPluginLibrary library) throws CoreException {
166         IDocumentNode parent = getEnclosingElement("runtime", false); //$NON-NLS-1$
167
if (parent != null) {
168             parent.removeChildNode((IDocumentNode)library);
169             library.setInTheModel(false);
170             fireStructureChanged(library, IModelChangedEvent.REMOVE);
171         }
172     }
173     /* (non-Javadoc)
174      * @see org.eclipse.pde.core.plugin.IPluginBase#setProviderName(java.lang.String)
175      */

176     public void setProviderName(String JavaDoc providerName) throws CoreException {
177         setXMLAttribute(P_PROVIDER, providerName);
178     }
179     /* (non-Javadoc)
180      * @see org.eclipse.pde.core.plugin.IPluginBase#setVersion(java.lang.String)
181      */

182     public void setVersion(String JavaDoc version) throws CoreException {
183         setXMLAttribute(P_VERSION, version);
184     }
185     /* (non-Javadoc)
186      * @see org.eclipse.pde.core.plugin.IPluginBase#swap(org.eclipse.pde.core.plugin.IPluginLibrary, org.eclipse.pde.core.plugin.IPluginLibrary)
187      */

188     public void swap(IPluginLibrary l1, IPluginLibrary l2) throws CoreException {
189         IDocumentNode node = getEnclosingElement("runtime", false); //$NON-NLS-1$
190
if (node != null) {
191             node.swap((IDocumentNode)l1, (IDocumentNode)l2);
192             firePropertyChanged(node, P_LIBRARY_ORDER, l1, l2);
193         }
194     }
195     /* (non-Javadoc)
196      * @see org.eclipse.pde.core.plugin.IPluginBase#getSchemaVersion()
197      */

198     public String JavaDoc getSchemaVersion() {
199         return fSchemaVersion;
200     }
201     /* (non-Javadoc)
202      * @see org.eclipse.pde.core.plugin.IPluginBase#setSchemaVersion(java.lang.String)
203      */

204     public void setSchemaVersion(String JavaDoc schemaVersion) throws CoreException {
205         fSchemaVersion = schemaVersion;
206     }
207     /* (non-Javadoc)
208      * @see org.eclipse.pde.core.plugin.IExtensions#add(org.eclipse.pde.core.plugin.IPluginExtension)
209      */

210     public void add(IPluginExtension extension) throws CoreException {
211         if (extension instanceof PluginExtensionNode) {
212             PluginExtensionNode node = (PluginExtensionNode)extension;
213             node.setModel(getModel());
214             addChildNode(node);
215             fireStructureChanged(extension, IModelChangedEvent.INSERT);
216         }
217     }
218     /* (non-Javadoc)
219      * @see org.eclipse.pde.core.plugin.IExtensions#add(org.eclipse.pde.core.plugin.IPluginExtensionPoint)
220      */

221     public void add(IPluginExtensionPoint extensionPoint) throws CoreException {
222         if (extensionPoint instanceof PluginExtensionPointNode) {
223             PluginExtensionPointNode node = (PluginExtensionPointNode)extensionPoint;
224             node.setModel(getModel());
225             extensionPoint.setInTheModel(true);
226             node.setParentNode(this);
227             IPluginExtensionPoint[] extPoints = getExtensionPoints();
228             if (extPoints.length > 0)
229                 addChildNode(node, indexOf((IDocumentNode)extPoints[extPoints.length - 1]) + 1);
230             else {
231                 IDocumentNode requires = getEnclosingElement("requires", false); //$NON-NLS-1$
232
if (requires != null) {
233                     addChildNode(node, indexOf(requires) + 1);
234                 } else {
235                     IDocumentNode runtime = getEnclosingElement("runtime", false); //$NON-NLS-1$
236
if (runtime != null)
237                         addChildNode(node, indexOf(runtime) + 1);
238                     else
239                         addChildNode(node, 0);
240                 }
241             }
242             fireStructureChanged(extensionPoint, IModelChangedEvent.INSERT);
243         }
244     }
245     /* (non-Javadoc)
246      * @see org.eclipse.pde.core.plugin.IExtensions#getExtensionPoints()
247      */

248     public IPluginExtensionPoint[] getExtensionPoints() {
249         ArrayList JavaDoc result = new ArrayList JavaDoc();
250         IDocumentNode[] children = getChildNodes();
251         for (int i = 0; i < children.length; i++) {
252             if (children[i] instanceof IPluginExtensionPoint)
253                 result.add(children[i]);
254         }
255         return (IPluginExtensionPoint[]) result.toArray(new IPluginExtensionPoint[result.size()]);
256     }
257     /* (non-Javadoc)
258      * @see org.eclipse.pde.core.plugin.IExtensions#getExtensions()
259      */

260     public IPluginExtension[] getExtensions() {
261         ArrayList JavaDoc result = new ArrayList JavaDoc();
262         IDocumentNode[] children = getChildNodes();
263         for (int i = 0; i < children.length; i++) {
264             if (children[i] instanceof IPluginExtension)
265                 result.add(children[i]);
266         }
267         return (IPluginExtension[]) result.toArray(new IPluginExtension[result.size()]);
268     }
269     public int getIndexOf(IPluginExtension e) {
270         IPluginExtension [] children = getExtensions();
271         for (int i=0; i<children.length; i++) {
272             if (children[i].equals(e))
273                 return i;
274         }
275         return -1;
276     }
277     /* (non-Javadoc)
278      * @see org.eclipse.pde.core.plugin.IExtensions#remove(org.eclipse.pde.core.plugin.IPluginExtension)
279      */

280     public void remove(IPluginExtension extension) throws CoreException {
281         if (extension instanceof IDocumentNode) {
282             removeChildNode((IDocumentNode)extension);
283             extension.setInTheModel(false);
284             fireStructureChanged(extension, IModelChangedEvent.REMOVE);
285         }
286     }
287     /* (non-Javadoc)
288      * @see org.eclipse.pde.core.plugin.IExtensions#remove(org.eclipse.pde.core.plugin.IPluginExtensionPoint)
289      */

290     public void remove(IPluginExtensionPoint extensionPoint)
291             throws CoreException {
292         if (extensionPoint instanceof IDocumentNode) {
293             removeChildNode((IDocumentNode)extensionPoint);
294             extensionPoint.setInTheModel(false);
295             fireStructureChanged(extensionPoint, IModelChangedEvent.REMOVE);
296         }
297     }
298     
299     public void remove(IPluginObject node) {
300         if (node instanceof IDocumentNode) {
301             removeChildNode((IDocumentNode)node);
302             node.setInTheModel(false);
303             fireStructureChanged(node, IModelChangedEvent.REMOVE);
304         }
305     }
306     /* (non-Javadoc)
307      * @see org.eclipse.pde.core.plugin.IExtensions#swap(org.eclipse.pde.core.plugin.IPluginExtension, org.eclipse.pde.core.plugin.IPluginExtension)
308      */

309     public void swap(IPluginExtension e1, IPluginExtension e2)
310             throws CoreException {
311         swap((IDocumentNode)e1, (IDocumentNode)e2);
312         firePropertyChanged(this, P_EXTENSION_ORDER, e1, e2);
313     }
314     
315     /* (non-Javadoc)
316      * @see org.eclipse.pde.core.plugin.IPluginBase#swap(org.eclipse.pde.core.plugin.IPluginImport, org.eclipse.pde.core.plugin.IPluginImport)
317      */

318     public void swap(IPluginImport import1, IPluginImport import2)
319             throws CoreException {
320         IDocumentNode node = getEnclosingElement("requires", false); //$NON-NLS-1$
321
if (node != null) {
322             node.swap((IDocumentNode)import1, (IDocumentNode)import2);
323             firePropertyChanged(node, P_IMPORT_ORDER, import1, import2);
324         }
325     }
326     /* (non-Javadoc)
327      * @see org.eclipse.pde.core.IIdentifiable#getId()
328      */

329     public String JavaDoc getId() {
330         return getXMLAttributeValue(P_ID);
331     }
332     /* (non-Javadoc)
333      * @see org.eclipse.pde.core.IIdentifiable#setId(java.lang.String)
334      */

335     public void setId(String JavaDoc id) throws CoreException {
336         setXMLAttribute(P_ID, id);
337     }
338     
339     /* (non-Javadoc)
340      * @see org.eclipse.pde.core.plugin.IPluginObject#getName()
341      */

342     public String JavaDoc getName() {
343         return getXMLAttributeValue(P_NAME);
344     }
345     
346     /* (non-Javadoc)
347      * @see org.eclipse.pde.core.plugin.IPluginObject#setName(java.lang.String)
348      */

349     public void setName(String JavaDoc name) throws CoreException {
350         setXMLAttribute(P_NAME, name);
351     }
352     
353     /* (non-Javadoc)
354      * @see org.eclipse.pde.internal.ui.model.plugin.PluginObjectNode#write()
355      */

356     public String JavaDoc write(boolean indent) {
357         String JavaDoc newLine = getLineDelimiter();
358         
359         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
360         buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + newLine); //$NON-NLS-1$
361
buffer.append("<?eclipse version=\"3.0\"?>" + newLine); //$NON-NLS-1$
362

363         buffer.append(writeShallow(false) + newLine);
364         
365         IDocumentNode runtime = getEnclosingElement("runtime", false); //$NON-NLS-1$
366
if (runtime != null) {
367             runtime.setLineIndent(getLineIndent() + 3);
368             buffer.append(runtime.write(true) + newLine);
369         }
370         
371         IDocumentNode requires = getEnclosingElement("requires", false); //$NON-NLS-1$
372
if (requires != null) {
373             requires.setLineIndent(getLineIndent() + 3);
374             buffer.append(requires.write(true) + newLine);
375         }
376         
377         IPluginExtensionPoint[] extPoints = getExtensionPoints();
378         for (int i = 0; i < extPoints.length; i++) {
379             IDocumentNode extPoint = (IDocumentNode)extPoints[i];
380             extPoint.setLineIndent(getLineIndent() + 3);
381             buffer.append(extPoint.write(true) + newLine);
382         }
383         
384         IPluginExtension[] extensions = getExtensions();
385         for (int i = 0; i < extensions.length; i++) {
386             IDocumentNode extension = (IDocumentNode)extensions[i];
387             extension.setLineIndent(getLineIndent() + 3);
388             buffer.append(extension.write(true) + newLine);
389         }
390         
391         buffer.append("</" + getXMLTagName() + ">"); //$NON-NLS-1$ //$NON-NLS-2$
392
return buffer.toString();
393     }
394     
395     /* (non-Javadoc)
396      * @see org.eclipse.pde.internal.ui.model.plugin.PluginObjectNode#writeShallow()
397      */

398     public String JavaDoc writeShallow(boolean terminate) {
399         String JavaDoc newLine = System.getProperty("line.separator"); //$NON-NLS-1$
400
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
401         buffer.append("<" + getXMLTagName()); //$NON-NLS-1$
402
buffer.append(newLine);
403         
404         String JavaDoc id = getId();
405         if (id != null && id.trim().length() > 0)
406             buffer.append(" " + P_ID + "=\"" + getWritableString(id) + "\"" + newLine); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
407

408         String JavaDoc name = getName();
409         if (name != null && name.trim().length() > 0)
410             buffer.append(" " + P_NAME + "=\"" + getWritableString(name) + "\"" + newLine); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
411

412         String JavaDoc version = getVersion();
413         if (version != null && version.trim().length() > 0)
414             buffer.append(" " + P_VERSION + "=\"" + getWritableString(version) + "\"" + newLine); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
415

416         String JavaDoc provider = getProviderName();
417         if (provider != null && provider.trim().length() > 0) {
418             buffer.append(" " + P_PROVIDER + "=\"" + getWritableString(provider) + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
419
}
420         
421         String JavaDoc[] specific = getSpecificAttributes();
422         for (int i = 0; i < specific.length; i++)
423             buffer.append(newLine + specific[i]);
424         if (terminate)
425             buffer.append("/"); //$NON-NLS-1$
426
buffer.append(">"); //$NON-NLS-1$
427

428         return buffer.toString();
429     }
430     
431     protected abstract String JavaDoc[] getSpecificAttributes();
432     
433     public boolean isRoot() {
434         return true;
435     }
436 }
437
Popular Tags