KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > model > plugin > PluginBaseNode


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.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 JavaDoc fSchemaVersion;
25     /* (non-Javadoc)
26      * @see org.eclipse.pde.core.plugin.IPluginBase#add(org.eclipse.pde.core.plugin.IPluginLibrary)
27      */

28     public void add(IPluginLibrary library) throws CoreException {
29         IDocumentNode parent = getEnclosingElement("runtime", true); //$NON-NLS-1$
30
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     /* (non-Javadoc)
39      * @see org.eclipse.pde.core.plugin.IPluginBase#add(org.eclipse.pde.core.plugin.IPluginImport)
40      */

41     public void add(IPluginImport pluginImport) throws CoreException {
42         IDocumentNode parent = getEnclosingElement("requires", true); //$NON-NLS-1$
43
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     /* (non-Javadoc)
52      * @see org.eclipse.pde.core.plugin.IPluginBase#remove(org.eclipse.pde.core.plugin.IPluginImport)
53      */

54     public void remove(IPluginImport pluginImport) throws CoreException {
55         IDocumentNode parent = getEnclosingElement("requires", false); //$NON-NLS-1$
56
if (parent != null) {
57             parent.removeChildNode((IDocumentNode)pluginImport);
58             pluginImport.setInTheModel(false);
59             fireStructureChanged(pluginImport, IModelChangedEvent.REMOVE);
60         }
61     }
62     
63     /* (non-Javadoc)
64      * @see org.eclipse.pde.core.plugin.IPluginBase#getLibraries()
65      */

66     public IPluginLibrary[] getLibraries() {
67         ArrayList result = new ArrayList();
68         IDocumentNode requiresNode = getEnclosingElement("runtime", false); //$NON-NLS-1$
69
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 JavaDoc 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")) { //$NON-NLS-1$
98
addChildNode(element, 0);
99             } else if (elementName.equals("requires")) { //$NON-NLS-1$
100
if (children.length > 0 && children[0].getXMLTagName().equals("runtime")) { //$NON-NLS-1$
101
addChildNode(element, 1);
102                 } else {
103                     addChildNode(element, 0);
104                 }
105             }
106         }
107         return element;
108     }
109     
110     /* (non-Javadoc)
111      * @see org.eclipse.pde.core.plugin.IPluginBase#getImports()
112      */

113     public IPluginImport[] getImports() {
114         ArrayList result = new ArrayList();
115         IDocumentNode requiresNode = getEnclosingElement("requires", false); //$NON-NLS-1$
116
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     /* (non-Javadoc)
127      * @see org.eclipse.pde.core.plugin.IPluginBase#getProviderName()
128      */

129     public String JavaDoc getProviderName() {
130         return getXMLAttributeValue(P_PROVIDER);
131     }
132     /* (non-Javadoc)
133      * @see org.eclipse.pde.core.plugin.IPluginBase#getVersion()
134      */

135     public String JavaDoc getVersion() {
136         return getXMLAttributeValue(P_VERSION);
137     }
138     /* (non-Javadoc)
139      * @see org.eclipse.pde.core.plugin.IPluginBase#remove(org.eclipse.pde.core.plugin.IPluginLibrary)
140      */

141     public void remove(IPluginLibrary library) throws CoreException {
142         IDocumentNode parent = getEnclosingElement("runtime", false); //$NON-NLS-1$
143
if (parent != null) {
144             parent.removeChildNode((IDocumentNode)library);
145             library.setInTheModel(false);
146             fireStructureChanged(library, IModelChangedEvent.REMOVE);
147         }
148     }
149     /* (non-Javadoc)
150      * @see org.eclipse.pde.core.plugin.IPluginBase#setProviderName(java.lang.String)
151      */

152     public void setProviderName(String JavaDoc providerName) throws CoreException {
153         setXMLAttribute(P_PROVIDER, providerName);
154     }
155     /* (non-Javadoc)
156      * @see org.eclipse.pde.core.plugin.IPluginBase#setVersion(java.lang.String)
157      */

158     public void setVersion(String JavaDoc version) throws CoreException {
159         setXMLAttribute(P_VERSION, version);
160     }
161     /* (non-Javadoc)
162      * @see org.eclipse.pde.core.plugin.IPluginBase#swap(org.eclipse.pde.core.plugin.IPluginLibrary, org.eclipse.pde.core.plugin.IPluginLibrary)
163      */

164     public void swap(IPluginLibrary l1, IPluginLibrary l2) throws CoreException {
165         IDocumentNode node = getEnclosingElement("runtime", false); //$NON-NLS-1$
166
if (node != null) {
167             node.swap((IDocumentNode)l1, (IDocumentNode)l2);
168             firePropertyChanged(node, P_LIBRARY_ORDER, l1, l2);
169         }
170     }
171     /* (non-Javadoc)
172      * @see org.eclipse.pde.core.plugin.IPluginBase#getSchemaVersion()
173      */

174     public String JavaDoc getSchemaVersion() {
175         return fSchemaVersion;
176     }
177     /* (non-Javadoc)
178      * @see org.eclipse.pde.core.plugin.IPluginBase#setSchemaVersion(java.lang.String)
179      */

180     public void setSchemaVersion(String JavaDoc schemaVersion) throws CoreException {
181         fSchemaVersion = schemaVersion;
182     }
183     /* (non-Javadoc)
184      * @see org.eclipse.pde.core.plugin.IExtensions#add(org.eclipse.pde.core.plugin.IPluginExtension)
185      */

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     /* (non-Javadoc)
196      * @see org.eclipse.pde.core.plugin.IExtensions#add(org.eclipse.pde.core.plugin.IPluginExtensionPoint)
197      */

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); //$NON-NLS-1$
209
if (requires != null) {
210                     addChildNode(node, indexOf(requires) + 1);
211                 } else {
212                     IDocumentNode runtime = getEnclosingElement("runtime", false); //$NON-NLS-1$
213
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     /* (non-Javadoc)
223      * @see org.eclipse.pde.core.plugin.IExtensions#getExtensionPoints()
224      */

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     /* (non-Javadoc)
235      * @see org.eclipse.pde.core.plugin.IExtensions#getExtensions()
236      */

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     /* (non-Javadoc)
255      * @see org.eclipse.pde.core.plugin.IExtensions#remove(org.eclipse.pde.core.plugin.IPluginExtension)
256      */

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     /* (non-Javadoc)
265      * @see org.eclipse.pde.core.plugin.IExtensions#remove(org.eclipse.pde.core.plugin.IPluginExtensionPoint)
266      */

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     /* (non-Javadoc)
276      * @see org.eclipse.pde.core.plugin.IExtensions#swap(org.eclipse.pde.core.plugin.IPluginExtension, org.eclipse.pde.core.plugin.IPluginExtension)
277      */

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     /* (non-Javadoc)
285      * @see org.eclipse.pde.core.plugin.IPluginBase#swap(org.eclipse.pde.core.plugin.IPluginImport, org.eclipse.pde.core.plugin.IPluginImport)
286      */

287     public void swap(IPluginImport import1, IPluginImport import2)
288             throws CoreException {
289         IDocumentNode node = getEnclosingElement("requires", false); //$NON-NLS-1$
290
if (node != null) {
291             node.swap((IDocumentNode)import1, (IDocumentNode)import2);
292             firePropertyChanged(node, P_IMPORT_ORDER, import1, import2);
293         }
294     }
295     /* (non-Javadoc)
296      * @see org.eclipse.pde.core.IIdentifiable#getId()
297      */

298     public String JavaDoc getId() {
299         return getXMLAttributeValue(P_ID);
300     }
301     /* (non-Javadoc)
302      * @see org.eclipse.pde.core.IIdentifiable#setId(java.lang.String)
303      */

304     public void setId(String JavaDoc id) throws CoreException {
305         setXMLAttribute(P_ID, id);
306     }
307     
308     /* (non-Javadoc)
309      * @see org.eclipse.pde.core.plugin.IPluginObject#getName()
310      */

311     public String JavaDoc getName() {
312         return getXMLAttributeValue(P_NAME);
313     }
314     
315     /* (non-Javadoc)
316      * @see org.eclipse.pde.core.plugin.IPluginObject#setName(java.lang.String)
317      */

318     public void setName(String JavaDoc name) throws CoreException {
319         setXMLAttribute(P_NAME, name);
320     }
321     
322     /* (non-Javadoc)
323      * @see org.eclipse.pde.internal.ui.model.plugin.PluginObjectNode#write()
324      */

325     public String JavaDoc write(boolean indent) {
326         String JavaDoc newLine = getLineDelimiter();
327         
328         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
329         buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + newLine); //$NON-NLS-1$
330
if (PDECore.getDefault().getModelManager().isOSGiRuntime()) {
331             buffer.append("<?eclipse version=\"3.0\"?>" + newLine); //$NON-NLS-1$
332
}
333         buffer.append(writeShallow(false) + newLine);
334         
335         IDocumentNode runtime = getEnclosingElement("runtime", false); //$NON-NLS-1$
336
if (runtime != null) {
337             runtime.setLineIndent(getLineIndent() + 3);
338             buffer.append(runtime.write(true) + newLine);
339         }
340         
341         IDocumentNode requires = getEnclosingElement("requires", false); //$NON-NLS-1$
342
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() + ">"); //$NON-NLS-1$ //$NON-NLS-2$
362
return buffer.toString();
363     }
364     
365     /* (non-Javadoc)
366      * @see org.eclipse.pde.internal.ui.model.plugin.PluginObjectNode#writeShallow()
367      */

368     public String JavaDoc writeShallow(boolean terminate) {
369         String JavaDoc newLine = System.getProperty("line.separator"); //$NON-NLS-1$
370
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
371         buffer.append("<" + getXMLTagName()); //$NON-NLS-1$
372
buffer.append(newLine);
373         
374         String JavaDoc id = getId();
375         if (id != null && id.trim().length() > 0)
376             buffer.append(" " + P_ID + "=\"" + getWritableString(id) + "\"" + newLine); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
377

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

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

386         String JavaDoc provider = getProviderName();
387         if (provider != null && provider.trim().length() > 0) {
388             buffer.append(" " + P_PROVIDER + "=\"" + getWritableString(provider) + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
389
}
390         
391         String JavaDoc[] specific = getSpecificAttributes();
392         for (int i = 0; i < specific.length; i++)
393             buffer.append(newLine + specific[i]);
394         if (terminate)
395             buffer.append("/"); //$NON-NLS-1$
396
buffer.append(">"); //$NON-NLS-1$
397

398         return buffer.toString();
399     }
400     
401     protected abstract String JavaDoc[] getSpecificAttributes();
402 }
403
Popular Tags