KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > plugin > AbstractExtensions


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.plugin;
12
13 import java.io.PrintWriter JavaDoc;
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.pde.core.IModelChangedEvent;
18 import org.eclipse.pde.core.plugin.IExtensions;
19 import org.eclipse.pde.core.plugin.IPluginBase;
20 import org.eclipse.pde.core.plugin.IPluginExtension;
21 import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
22 import org.eclipse.pde.core.plugin.IPluginObject;
23 import org.eclipse.pde.internal.core.PDECoreMessages;
24 import org.w3c.dom.Node JavaDoc;
25
26 public abstract class AbstractExtensions extends PluginObject implements IExtensions {
27     
28     protected String JavaDoc fSchemaVersion;
29     
30     protected ArrayList JavaDoc fExtensions = new ArrayList JavaDoc(1);
31     protected ArrayList JavaDoc fExtensionPoints = new ArrayList JavaDoc(1);
32
33     public void add(IPluginExtension extension) throws CoreException {
34         ensureModelEditable();
35         fExtensions.add(extension);
36         ((PluginExtension) extension).setInTheModel(true);
37         ((PluginExtension) extension).setParent(this);
38         fireStructureChanged(extension, IModelChangedEvent.INSERT);
39     }
40     
41     public void add(IPluginExtensionPoint extensionPoint)
42         throws CoreException {
43         ensureModelEditable();
44         fExtensionPoints.add(extensionPoint);
45         ((PluginExtensionPoint) extensionPoint).setInTheModel(true);
46         ((PluginExtensionPoint) extensionPoint).setParent(this);
47         fireStructureChanged(extensionPoint, IModelChangedEvent.INSERT);
48     }
49
50     public IPluginExtensionPoint[] getExtensionPoints() {
51         return (IPluginExtensionPoint[])fExtensionPoints.toArray(new IPluginExtensionPoint[fExtensionPoints.size()]);
52     }
53     
54     public IPluginExtension[] getExtensions() {
55         return (IPluginExtension[])fExtensions.toArray(new IPluginExtension[fExtensions.size()]);
56     }
57
58     public void restoreProperty(String JavaDoc name, Object JavaDoc oldValue, Object JavaDoc newValue)
59         throws CoreException {
60         if (name.equals(P_EXTENSION_ORDER)) {
61             swap((IPluginExtension) oldValue, (IPluginExtension) newValue);
62             return;
63         }
64         super.restoreProperty(name, oldValue, newValue);
65     }
66
67     public void load(IExtensions srcExtensions) {
68         addArrayToVector(fExtensions, srcExtensions.getExtensions());
69         addArrayToVector(fExtensionPoints, srcExtensions.getExtensionPoints());
70     }
71
72     protected void addArrayToVector(ArrayList JavaDoc vector, Object JavaDoc[] array) {
73         for (int i = 0; i < array.length; i++) {
74             Object JavaDoc obj= array[i];
75             if (obj instanceof PluginObject)
76                 ((PluginObject) obj).setParent(this);
77             vector.add(obj);
78         }
79     }
80
81     protected void processChild(Node JavaDoc child) {
82         String JavaDoc name = child.getNodeName();
83         if (name.equals("extension")) { //$NON-NLS-1$
84
PluginExtension extension = new PluginExtension();
85             extension.setModel(getModel());
86             extension.setParent(this);
87             fExtensions.add(extension);
88             extension.setInTheModel(true);
89             extension.load(child);
90         } else if (name.equals("extension-point")) { //$NON-NLS-1$
91
PluginExtensionPoint point = new PluginExtensionPoint();
92             point.setModel(getModel());
93             point.setParent(this);
94             point.setInTheModel(true);
95             fExtensionPoints.add(point);
96             point.load(child);
97         }
98     }
99     
100     public void remove(IPluginExtension extension) throws CoreException {
101         ensureModelEditable();
102         fExtensions.remove(extension);
103         ((PluginExtension) extension).setInTheModel(false);
104         fireStructureChanged(extension, IModelChangedEvent.REMOVE);
105     }
106     
107     public void remove(IPluginExtensionPoint extensionPoint)
108         throws CoreException {
109         ensureModelEditable();
110         fExtensionPoints.remove(extensionPoint);
111         ((PluginExtensionPoint) extensionPoint).setInTheModel(false);
112         fireStructureChanged(extensionPoint, IModelChangedEvent.REMOVE);
113     }
114
115     public void reset() {
116         fExtensions = new ArrayList JavaDoc();
117         fExtensionPoints = new ArrayList JavaDoc();
118     }
119
120     public int getExtensionCount() {
121         return fExtensions.size();
122     }
123
124     public int getIndexOf(IPluginExtension e) {
125         return fExtensions.indexOf(e);
126     }
127     
128     public void swap(IPluginExtension e1, IPluginExtension e2)
129         throws CoreException {
130         ensureModelEditable();
131         int index1 = fExtensions.indexOf(e1);
132         int index2 = fExtensions.indexOf(e2);
133         if (index1 == -1 || index2 == -1)
134             throwCoreException(PDECoreMessages.AbstractExtensions_extensionsNotFoundException);
135         fExtensions.set(index2, e1);
136         fExtensions.set(index2, e2);
137         firePropertyChanged(this, P_EXTENSION_ORDER, e1, e2);
138     }
139     
140     protected void writeChildren(
141         String JavaDoc indent,
142         String JavaDoc tag,
143         Object JavaDoc[] children,
144         PrintWriter JavaDoc writer) {
145         writer.println(indent + "<" + tag + ">"); //$NON-NLS-1$ //$NON-NLS-2$
146
for (int i = 0; i < children.length; i++) {
147             IPluginObject obj = (IPluginObject) children[i];
148             obj.write(indent + " ", writer); //$NON-NLS-1$
149
}
150         writer.println(indent + "</" + tag + ">"); //$NON-NLS-1$ //$NON-NLS-2$
151
}
152
153     protected boolean hasRequiredAttributes(){
154         // validate extensions
155
for (int i = 0; i < fExtensions.size(); i++) {
156             IPluginExtension extension = (IPluginExtension)fExtensions.get(i);
157             if (!extension.isValid()) return false;
158         }
159         // validate extension points
160
for (int i = 0; i < fExtensionPoints.size(); i++) {
161             IPluginExtensionPoint expoint = (IPluginExtensionPoint)fExtensionPoints.get(i);
162             if (!expoint.isValid()) return false;
163         }
164         return true;
165     }
166     
167     public String JavaDoc getSchemaVersion() {
168         return fSchemaVersion;
169     }
170     
171     public void setSchemaVersion(String JavaDoc schemaVersion) throws CoreException {
172         ensureModelEditable();
173         String JavaDoc oldValue = fSchemaVersion;
174         fSchemaVersion = schemaVersion;
175         firePropertyChanged(IPluginBase.P_SCHEMA_VERSION, oldValue, schemaVersion);
176     }
177     
178
179 }
180
Popular Tags