KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.io.Serializable JavaDoc;
15 import java.util.Vector JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.PlatformObject;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.pde.core.IModel;
22 import org.eclipse.pde.core.IModelChangeProvider;
23 import org.eclipse.pde.core.IModelChangedEvent;
24 import org.eclipse.pde.core.ISourceObject;
25 import org.eclipse.pde.core.ModelChangedEvent;
26 import org.eclipse.pde.core.plugin.IPluginBase;
27 import org.eclipse.pde.core.plugin.IPluginModelBase;
28 import org.eclipse.pde.core.plugin.IPluginObject;
29 import org.eclipse.pde.core.plugin.ISharedPluginModel;
30 import org.eclipse.pde.internal.core.PDECore;
31 import org.eclipse.pde.internal.core.PDECoreMessages;
32 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelProvider;
33 import org.eclipse.pde.internal.core.util.PDEXMLHelper;
34 import org.w3c.dom.Comment JavaDoc;
35 import org.w3c.dom.Node JavaDoc;
36
37 public abstract class PluginObject extends PlatformObject implements
38         IPluginObject, ISourceObject, Serializable JavaDoc, IWritableDelimiter {
39     
40     protected String JavaDoc fName;
41
42     private transient String JavaDoc fTranslatedName;
43     private transient IPluginObject fParent;
44     private transient ISharedPluginModel fModel;
45     private transient boolean fInTheModel;
46
47     protected int fStartLine = 1;
48
49     public PluginObject() {
50     }
51     public boolean isValid() {
52         return true;
53     }
54     protected void ensureModelEditable() throws CoreException {
55         if (!fModel.isEditable()) {
56             throwCoreException(PDECoreMessages.PluginObject_readOnlyChange);
57         }
58     }
59
60     public void setInTheModel(boolean value) {
61         fInTheModel = value;
62     }
63
64     public boolean isInTheModel() {
65         return fInTheModel;
66     }
67
68     protected void firePropertyChanged(
69         String JavaDoc property,
70         Object JavaDoc oldValue,
71         Object JavaDoc newValue) {
72         firePropertyChanged(this, property, oldValue, newValue);
73     }
74     protected void firePropertyChanged(
75         IPluginObject object,
76         String JavaDoc property,
77         Object JavaDoc oldValue,
78         Object JavaDoc newValue) {
79         if (fModel.isEditable()) {
80             IModelChangeProvider provider = fModel;
81             provider.fireModelObjectChanged(
82                 object,
83                 property,
84                 oldValue,
85                 newValue);
86         }
87     }
88     protected void fireStructureChanged(IPluginObject child, int changeType) {
89         IModel model = getModel();
90         if (model.isEditable() && model instanceof IModelChangeProvider) {
91             IModelChangedEvent e =
92                 new ModelChangedEvent((IModelChangeProvider)model, changeType, new Object JavaDoc[] { child }, null);
93             fireModelChanged(e);
94         }
95     }
96     protected void fireStructureChanged(IPluginObject[] children, int changeType) {
97         IModel model = getModel();
98         if (model.isEditable() && model instanceof IModelChangeProvider) {
99             IModelChangedEvent e =
100                 new ModelChangedEvent((IModelChangeProvider)model, changeType, children, null);
101             fireModelChanged(e);
102         }
103     }
104     protected void fireModelChanged(IModelChangedEvent e) {
105         IModel model = getModel();
106         if (model.isEditable() && model instanceof IModelChangeProvider) {
107             IModelChangeProvider provider = (IModelChangeProvider) model;
108             provider.fireModelChanged(e);
109         }
110     }
111     public ISharedPluginModel getModel() {
112         return fModel;
113     }
114     
115     public IPluginModelBase getPluginModel() {
116         if (fModel instanceof IBundlePluginModelProvider)
117             return ((IBundlePluginModelProvider)fModel).getBundlePluginModel();
118         
119         return fModel instanceof IPluginModelBase? (IPluginModelBase)fModel : null;
120     }
121     
122     public String JavaDoc getName() {
123         return fName;
124     }
125
126     public String JavaDoc getTranslatedName() {
127         if (fTranslatedName != null && !fModel.isEditable())
128             return fTranslatedName;
129         if (fTranslatedName == null && fName != null && fModel != null) {
130             fTranslatedName = fModel.getResourceString(fName);
131         }
132         return fTranslatedName;
133     }
134
135     String JavaDoc getNodeAttribute(Node JavaDoc node, String JavaDoc name) {
136         Node JavaDoc attribute = node.getAttributes().getNamedItem(name);
137         if (attribute != null)
138             return attribute.getNodeValue();
139         return null;
140     }
141     public IPluginObject getParent() {
142         return fParent;
143     }
144     public IPluginBase getPluginBase() {
145         IPluginModelBase pluginModel = getPluginModel();
146         return pluginModel != null ? pluginModel.getPluginBase() : null;
147     }
148     public String JavaDoc getResourceString(String JavaDoc key) {
149         return fModel.getResourceString(key);
150     }
151     static boolean isNotEmpty(String JavaDoc text) {
152         for (int i = 0; i < text.length(); i++) {
153             if (Character.isWhitespace(text.charAt(i)) == false)
154                 return true;
155         }
156         return false;
157     }
158
159     public void restoreProperty(String JavaDoc name, Object JavaDoc oldValue, Object JavaDoc newValue)
160         throws CoreException {
161         if (name.equals(P_NAME)) {
162             setName(newValue != null ? newValue.toString() : null);
163         }
164     }
165
166     public void setModel(ISharedPluginModel model) {
167         this.fModel = model;
168         fTranslatedName = null;
169     }
170     public void setName(String JavaDoc name) throws CoreException {
171         ensureModelEditable();
172         String JavaDoc oldValue = this.fName;
173         this.fName = name;
174         firePropertyChanged(P_NAME, oldValue, name);
175     }
176     public void setParent(IPluginObject parent) {
177         this.fParent = parent;
178     }
179     protected void throwCoreException(String JavaDoc message) throws CoreException {
180         Status status =
181             new Status(
182                 IStatus.ERROR,
183                 PDECore.PLUGIN_ID,
184                 IStatus.OK,
185                 message,
186                 null);
187         CoreException ce = new CoreException(status);
188         ce.fillInStackTrace();
189         throw ce;
190     }
191     public String JavaDoc toString() {
192         if (fName != null)
193             return fName;
194         return super.toString();
195     }
196
197     public Vector JavaDoc addComments(Node JavaDoc node, Vector JavaDoc result) {
198         for (Node JavaDoc prev = node.getPreviousSibling();
199             prev != null;
200             prev = prev.getPreviousSibling()) {
201             if (prev.getNodeType() == Node.TEXT_NODE)
202                 continue;
203             if (prev instanceof Comment JavaDoc) {
204                 String JavaDoc comment = prev.getNodeValue();
205                 if (result == null)
206                     result = new Vector JavaDoc();
207                 result.add(0,comment);
208             } else
209                 break;
210         }
211         return result;
212     }
213
214     void writeComments(PrintWriter JavaDoc writer, Vector JavaDoc source) {
215         if (source == null)
216             return;
217         for (int i = 0; i < source.size(); i++) {
218             String JavaDoc comment = (String JavaDoc) source.elementAt(i);
219             writer.println("<!--" + comment + "-->"); //$NON-NLS-1$ //$NON-NLS-2$
220
}
221     }
222
223     protected boolean stringEqualWithNull(String JavaDoc a, String JavaDoc b) {
224         return a == null && b == null || a != null && b != null && a.equals(b);
225     }
226
227     public String JavaDoc getWritableString(String JavaDoc source) {
228         return PDEXMLHelper.getWritableString(source);
229     }
230
231     public int getStartLine() {
232         return fStartLine;
233     }
234     
235     public int getStopLine() {
236         return fStartLine;
237     }
238     
239     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
240         if(adapter.equals(IPluginModelBase.class)) {
241             return getPluginModel();
242         }
243         return super.getAdapter(adapter);
244     }
245     
246     /**
247      * @param model
248      * @param parent
249      */

250     public void reconnect(ISharedPluginModel model, IPluginObject parent) {
251         // Transient Field: In The Model
252
fInTheModel = false;
253         // Transient Field: Model
254
fModel = model;
255         // Transient Field: Parent
256
fParent = parent;
257         // Transient Field: Translated Name
258
fTranslatedName = null;
259     }
260
261     /* (non-Javadoc)
262      * @see org.eclipse.pde.internal.core.plugin.IWritableDelimeter#writeDelimeter(java.io.PrintWriter)
263      */

264     public void writeDelimeter(PrintWriter JavaDoc writer) {
265         // NO-OP
266
// Child classes to override
267
}
268     
269 }
270
Popular Tags