KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.plugin;
12
13 import java.io.PrintWriter JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.jface.text.IDocument;
17 import org.eclipse.jface.text.TextUtilities;
18 import org.eclipse.pde.core.IModel;
19 import org.eclipse.pde.core.IModelChangeProvider;
20 import org.eclipse.pde.core.IModelChangedEvent;
21 import org.eclipse.pde.core.ModelChangedEvent;
22 import org.eclipse.pde.core.plugin.IPluginBase;
23 import org.eclipse.pde.core.plugin.IPluginModelBase;
24 import org.eclipse.pde.core.plugin.IPluginObject;
25 import org.eclipse.pde.core.plugin.ISharedPluginModel;
26 import org.eclipse.pde.internal.core.ischema.ISchema;
27 import org.eclipse.pde.internal.core.plugin.IWritableDelimiter;
28 import org.eclipse.pde.internal.core.text.IDocumentAttribute;
29 import org.eclipse.pde.internal.core.text.IDocumentNode;
30 import org.eclipse.pde.internal.core.text.IDocumentObject;
31 import org.eclipse.pde.internal.core.text.IDocumentRange;
32 import org.eclipse.pde.internal.core.text.IEditingModel;
33 import org.eclipse.pde.internal.core.util.PDEXMLHelper;
34
35 public class PluginObjectNode extends PluginDocumentNode implements
36         IPluginObject, IDocumentObject, IWritableDelimiter {
37
38     private transient boolean fInTheModel;
39     private transient ISharedPluginModel fModel;
40     
41     private static final long serialVersionUID = 1L;
42     private String JavaDoc fName;
43
44     /*
45      * (non-Javadoc)
46      *
47      * @see org.eclipse.pde.core.plugin.IPluginObject#getModel()
48      */

49     public ISharedPluginModel getModel() {
50         return fModel;
51     }
52     /*
53      * (non-Javadoc)
54      *
55      * @see org.eclipse.pde.core.plugin.IPluginObject#getPluginModel()
56      */

57     public IPluginModelBase getPluginModel() {
58         return (IPluginModelBase) fModel;
59     }
60     /*
61      * (non-Javadoc)
62      *
63      * @see org.eclipse.pde.core.plugin.IPluginObject#getName()
64      */

65     public String JavaDoc getName() {
66         return fName;
67     }
68     /*
69      * (non-Javadoc)
70      *
71      * @see org.eclipse.pde.core.plugin.IPluginObject#isInTheModel()
72      */

73     public boolean isInTheModel() {
74         return fInTheModel;
75     }
76     /*
77      * (non-Javadoc)
78      *
79      * @see org.eclipse.pde.core.plugin.IPluginObject#getTranslatedName()
80      */

81     public String JavaDoc getTranslatedName() {
82         return getResourceString(getName());
83     }
84     /*
85      * (non-Javadoc)
86      *
87      * @see org.eclipse.pde.core.plugin.IPluginObject#getParent()
88      */

89     public IPluginObject getParent() {
90         return (IPluginObject) getParentNode();
91     }
92     /*
93      * (non-Javadoc)
94      *
95      * @see org.eclipse.pde.core.plugin.IPluginObject#getPluginBase()
96      */

97     public IPluginBase getPluginBase() {
98         return fModel != null
99                 ? ((IPluginModelBase) fModel).getPluginBase()
100                 : null;
101     }
102     /*
103      * (non-Javadoc)
104      *
105      * @see org.eclipse.pde.core.plugin.IPluginObject#getResourceString(java.lang.String)
106      */

107     public String JavaDoc getResourceString(String JavaDoc key) {
108         return fModel != null ? fModel.getResourceString(key) : key;
109     }
110     /*
111      * (non-Javadoc)
112      *
113      * @see org.eclipse.pde.core.plugin.IPluginObject#setName(java.lang.String)
114      */

115     public void setName(String JavaDoc name) throws CoreException {
116         fName = name;
117     }
118     /*
119      * (non-Javadoc)
120      *
121      * @see org.eclipse.pde.core.plugin.IPluginObject#isValid()
122      */

123     public boolean isValid() {
124         return false;
125     }
126     /*
127      * (non-Javadoc)
128      *
129      * @see org.eclipse.pde.core.IWritable#write(java.lang.String,
130      * java.io.PrintWriter)
131      */

132     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
133     }
134     /*
135      * (non-Javadoc)
136      *
137      * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
138      */

139     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
140         return null;
141     }
142     /*
143      * (non-Javadoc)
144      *
145      * @see org.eclipse.pde.core.plugin.IPluginObject#setInTheModel(boolean)
146      */

147     public void setInTheModel(boolean inModel) {
148         fInTheModel = inModel;
149     }
150
151     public void setModel(ISharedPluginModel model) {
152         fModel = model;
153     }
154     /*
155      * (non-Javadoc)
156      *
157      * @see org.eclipse.pde.internal.ui.model.IDocumentNode#setXMLAttribute(java.lang.String,
158      * java.lang.String)
159      */

160     public void setXMLAttribute(String JavaDoc name, String JavaDoc value) {
161         String JavaDoc oldValue = getXMLAttributeValue(name);
162         if (oldValue != null && oldValue.equals(value))
163             return;
164         PluginAttribute attr = (PluginAttribute) fAttributes.get(name);
165         try {
166             if (value == null)
167                 value = ""; //$NON-NLS-1$
168
if (attr == null) {
169                     attr = new PluginAttribute();
170                     attr.setName(name);
171                     attr.setEnclosingElement(this);
172                     fAttributes.put(name, attr);
173                 }
174                 attr.setValue(value == null ? "" : value); //$NON-NLS-1$
175
} catch (CoreException e) {
176         }
177         if (fInTheModel)
178             firePropertyChanged(attr.getEnclosingElement(), attr
179                     .getAttributeName(), oldValue, value);
180     }
181
182     protected void firePropertyChanged(IDocumentRange node, String JavaDoc property,
183             Object JavaDoc oldValue, Object JavaDoc newValue) {
184         if (fModel.isEditable()) {
185             fModel.fireModelObjectChanged(node, property, oldValue, newValue);
186         }
187     }
188
189     protected void fireStructureChanged(IPluginObject child, int changeType) {
190         IModel model = getModel();
191         if (model.isEditable() && model instanceof IModelChangeProvider) {
192             IModelChangedEvent e = new ModelChangedEvent(fModel, changeType,
193                     new Object JavaDoc[]{child}, null);
194             fireModelChanged(e);
195         }
196     }
197     
198     protected void fireStructureChanged(IPluginObject[] children, int changeType) {
199         IModel model = getModel();
200         if (model.isEditable() && model instanceof IModelChangeProvider) {
201             IModelChangedEvent e = new ModelChangedEvent(fModel, changeType,
202                     children, null);
203             fireModelChanged(e);
204         }
205     }
206
207     protected void fireModelChanged(IModelChangedEvent e) {
208         IModel model = getModel();
209         if (model.isEditable() && model instanceof IModelChangeProvider) {
210             IModelChangeProvider provider = (IModelChangeProvider) model;
211             provider.fireModelChanged(e);
212         }
213     }
214
215     public String JavaDoc getWritableString(String JavaDoc source) {
216         return PDEXMLHelper.getWritableString(source);
217     }
218     /*
219      * (non-Javadoc)
220      *
221      * @see org.eclipse.pde.internal.ui.model.IDocumentNode#writeShallow()
222      */

223     public String JavaDoc writeShallow(boolean terminate) {
224         return ""; //$NON-NLS-1$
225
}
226
227     /*
228      * (non-Javadoc)
229      *
230      * @see org.eclipse.pde.internal.ui.model.IDocumentNode#write()
231      */

232     public String JavaDoc write(boolean indent) {
233         return ""; //$NON-NLS-1$
234
}
235
236     protected void appendAttribute(StringBuffer JavaDoc buffer, String JavaDoc attrName) {
237         appendAttribute(buffer, attrName, ""); //$NON-NLS-1$
238
}
239     
240     protected void appendAttribute(StringBuffer JavaDoc buffer, String JavaDoc attrName, String JavaDoc defaultValue) {
241         IDocumentAttribute attr = getDocumentAttribute(attrName);
242         if (attr != null) {
243             String JavaDoc value = attr.getAttributeValue();
244             if (value != null && value.trim().length() > 0 && !value.equals(defaultValue))
245                 buffer.append(" " + attr.write()); //$NON-NLS-1$
246
}
247     }
248     
249     /**
250      * @return
251      */

252     public String JavaDoc getLineDelimiter() {
253         ISharedPluginModel model = getModel();
254         IDocument document = ((IEditingModel)model).getDocument();
255         return TextUtilities.getDefaultLineDelimiter(document);
256     }
257     
258     public void addChildNode(IDocumentNode child, int position) {
259         super.addChildNode(child, position);
260         ((IPluginObject)child).setInTheModel(true);
261     }
262     
263     public String JavaDoc toString() {
264         return write(false);
265     }
266     
267     public boolean isRoot() {
268         return false;
269     }
270     
271     /* (non-Javadoc)
272      * @see org.eclipse.pde.internal.core.text.plugin.PluginDocumentNode#reconnect(org.eclipse.pde.core.plugin.ISharedPluginModel, org.eclipse.pde.internal.core.ischema.ISchema, org.eclipse.pde.internal.core.text.IDocumentNode)
273      */

274     public void reconnect(ISharedPluginModel model, ISchema schema, IDocumentNode parent) {
275         super.reconnect(model, schema, parent);
276         // Transient field: In The Model
277
// Value set to true when added to the parent
278
fInTheModel = false;
279         // Transient field: Model
280
// This may not be necessary. When this node is added to the parent,
281
// the parent takes care of this
282
fModel = model;
283     }
284     
285     /* (non-Javadoc)
286      * @see org.eclipse.pde.internal.core.plugin.IWritableDelimeter#writeDelimeter(java.io.PrintWriter)
287      */

288     public void writeDelimeter(PrintWriter JavaDoc writer) {
289         // NO-OP
290
// Child classes to override
291
}
292     
293 }
294
Popular Tags