001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core.text.plugin;
011:
012: import java.io.PrintWriter;
013:
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.jface.text.IDocument;
016: import org.eclipse.jface.text.TextUtilities;
017: import org.eclipse.pde.core.IModel;
018: import org.eclipse.pde.core.IModelChangeProvider;
019: import org.eclipse.pde.core.IModelChangedEvent;
020: import org.eclipse.pde.core.ModelChangedEvent;
021: import org.eclipse.pde.core.plugin.IPluginBase;
022: import org.eclipse.pde.core.plugin.IPluginModelBase;
023: import org.eclipse.pde.core.plugin.IPluginObject;
024: import org.eclipse.pde.core.plugin.ISharedPluginModel;
025: import org.eclipse.pde.internal.core.plugin.IWritableDelimiter;
026: import org.eclipse.pde.internal.core.text.IDocumentAttributeNode;
027: import org.eclipse.pde.internal.core.text.IDocumentElementNode;
028: import org.eclipse.pde.internal.core.text.IDocumentRange;
029: import org.eclipse.pde.internal.core.text.IEditingModel;
030: import org.eclipse.pde.internal.core.text.DocumentElementNode;
031: import org.eclipse.pde.internal.core.util.PDEXMLHelper;
032:
033: public class PluginObjectNode extends DocumentElementNode implements
034: IPluginObject, IWritableDelimiter {
035:
036: private transient boolean fInTheModel;
037: private transient ISharedPluginModel fModel;
038:
039: private static final long serialVersionUID = 1L;
040: private String fName;
041:
042: /**
043: *
044: */
045: public PluginObjectNode() {
046: super ();
047: }
048:
049: /*
050: * (non-Javadoc)
051: *
052: * @see org.eclipse.pde.core.plugin.IPluginObject#getModel()
053: */
054: public ISharedPluginModel getModel() {
055: return fModel;
056: }
057:
058: /*
059: * (non-Javadoc)
060: *
061: * @see org.eclipse.pde.core.plugin.IPluginObject#getPluginModel()
062: */
063: public IPluginModelBase getPluginModel() {
064: return (IPluginModelBase) fModel;
065: }
066:
067: /*
068: * (non-Javadoc)
069: *
070: * @see org.eclipse.pde.core.plugin.IPluginObject#getName()
071: */
072: public String getName() {
073: return fName;
074: }
075:
076: /*
077: * (non-Javadoc)
078: *
079: * @see org.eclipse.pde.core.plugin.IPluginObject#isInTheModel()
080: */
081: public boolean isInTheModel() {
082: return fInTheModel;
083: }
084:
085: /*
086: * (non-Javadoc)
087: *
088: * @see org.eclipse.pde.core.plugin.IPluginObject#getTranslatedName()
089: */
090: public String getTranslatedName() {
091: return getResourceString(getName());
092: }
093:
094: /*
095: * (non-Javadoc)
096: *
097: * @see org.eclipse.pde.core.plugin.IPluginObject#getParent()
098: */
099: public IPluginObject getParent() {
100: return (IPluginObject) getParentNode();
101: }
102:
103: /*
104: * (non-Javadoc)
105: *
106: * @see org.eclipse.pde.core.plugin.IPluginObject#getPluginBase()
107: */
108: public IPluginBase getPluginBase() {
109: return fModel != null ? ((IPluginModelBase) fModel)
110: .getPluginBase() : null;
111: }
112:
113: /*
114: * (non-Javadoc)
115: *
116: * @see org.eclipse.pde.core.plugin.IPluginObject#getResourceString(java.lang.String)
117: */
118: public String getResourceString(String key) {
119: return fModel != null ? fModel.getResourceString(key) : key;
120: }
121:
122: /*
123: * (non-Javadoc)
124: *
125: * @see org.eclipse.pde.core.plugin.IPluginObject#setName(java.lang.String)
126: */
127: public void setName(String name) throws CoreException {
128: fName = name;
129: }
130:
131: /*
132: * (non-Javadoc)
133: *
134: * @see org.eclipse.pde.core.plugin.IPluginObject#isValid()
135: */
136: public boolean isValid() {
137: return false;
138: }
139:
140: /*
141: * (non-Javadoc)
142: *
143: * @see org.eclipse.pde.core.IWritable#write(java.lang.String,
144: * java.io.PrintWriter)
145: */
146: public void write(String indent, PrintWriter writer) {
147: }
148:
149: /*
150: * (non-Javadoc)
151: *
152: * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
153: */
154: public Object getAdapter(Class adapter) {
155: return null;
156: }
157:
158: /*
159: * (non-Javadoc)
160: *
161: * @see org.eclipse.pde.core.plugin.IPluginObject#setInTheModel(boolean)
162: */
163: public void setInTheModel(boolean inModel) {
164: fInTheModel = inModel;
165: }
166:
167: public void setModel(ISharedPluginModel model) {
168: fModel = model;
169: }
170:
171: /*
172: * (non-Javadoc)
173: *
174: * @see org.eclipse.pde.internal.ui.model.IDocumentNode#setXMLAttribute(java.lang.String,
175: * java.lang.String)
176: */
177: public boolean setXMLAttribute(String name, String value) {
178: // Overrided by necessity - dealing with different objects
179: String oldValue = getXMLAttributeValue(name);
180: if (oldValue != null && oldValue.equals(value))
181: return false;
182: PluginAttribute attr = (PluginAttribute) getNodeAttributesMap()
183: .get(name);
184: try {
185: if (value == null)
186: value = ""; //$NON-NLS-1$
187: if (attr == null) {
188: attr = new PluginAttribute();
189: attr.setName(name);
190: attr.setEnclosingElement(this );
191: getNodeAttributesMap().put(name, attr);
192: }
193: attr.setValue(value == null ? "" : value); //$NON-NLS-1$
194: } catch (CoreException e) {
195: }
196: if (fInTheModel)
197: firePropertyChanged(attr.getEnclosingElement(), attr
198: .getAttributeName(), oldValue, value);
199: return true;
200: }
201:
202: protected void firePropertyChanged(IDocumentRange node,
203: String property, Object oldValue, Object newValue) {
204: if (fModel.isEditable()) {
205: fModel.fireModelObjectChanged(node, property, oldValue,
206: newValue);
207: }
208: }
209:
210: protected void fireStructureChanged(IPluginObject child,
211: int changeType) {
212: IModel model = getModel();
213: if (model.isEditable() && model instanceof IModelChangeProvider) {
214: IModelChangedEvent e = new ModelChangedEvent(fModel,
215: changeType, new Object[] { child }, null);
216: fireModelChanged(e);
217: }
218: }
219:
220: protected void fireStructureChanged(IPluginObject[] children,
221: int changeType) {
222: IModel model = getModel();
223: if (model.isEditable() && model instanceof IModelChangeProvider) {
224: IModelChangedEvent e = new ModelChangedEvent(fModel,
225: changeType, children, null);
226: fireModelChanged(e);
227: }
228: }
229:
230: private void fireModelChanged(IModelChangedEvent e) {
231: IModel model = getModel();
232: if (model.isEditable() && model instanceof IModelChangeProvider) {
233: IModelChangeProvider provider = (IModelChangeProvider) model;
234: provider.fireModelChanged(e);
235: }
236: }
237:
238: public String getWritableString(String source) {
239: return PDEXMLHelper.getWritableString(source);
240: }
241:
242: protected void appendAttribute(StringBuffer buffer, String attrName) {
243: appendAttribute(buffer, attrName, ""); //$NON-NLS-1$
244: }
245:
246: protected void appendAttribute(StringBuffer buffer,
247: String attrName, String defaultValue) {
248: IDocumentAttributeNode attr = getDocumentAttribute(attrName);
249: if (attr != null) {
250: String value = attr.getAttributeValue();
251: if (value != null && value.trim().length() > 0
252: && !value.equals(defaultValue))
253: buffer.append(" " + attr.write()); //$NON-NLS-1$
254: }
255: }
256:
257: /**
258: * @return
259: */
260: public String getLineDelimiter() {
261: ISharedPluginModel model = getModel();
262: IDocument document = ((IEditingModel) model).getDocument();
263: return TextUtilities.getDefaultLineDelimiter(document);
264: }
265:
266: public void addChildNode(IDocumentElementNode child, int position) {
267: super .addChildNode(child, position);
268: ((IPluginObject) child).setInTheModel(true);
269: }
270:
271: public String toString() {
272: return write(false);
273: }
274:
275: /* (non-Javadoc)
276: * @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.IDocumentElementNode)
277: */
278: public void reconnect(IDocumentElementNode parent, IModel model) {
279: super .reconnect(parent, model);
280: // Transient field: In The Model
281: // Value set to true when added to the parent; however, serialized
282: // children's value remains unchanged. Since, reconnect and add calls
283: // are made so close together, set value to true for parent and all
284: // children
285: fInTheModel = true;
286: // Transient field: Model
287: if (model instanceof ISharedPluginModel) {
288: fModel = (ISharedPluginModel) model;
289: }
290: }
291:
292: /* (non-Javadoc)
293: * @see org.eclipse.pde.internal.core.plugin.IWritableDelimeter#writeDelimeter(java.io.PrintWriter)
294: */
295: public void writeDelimeter(PrintWriter writer) {
296: // NO-OP
297: // Child classes to override
298: }
299:
300: /* (non-Javadoc)
301: * @see org.eclipse.pde.internal.ui.model.IDocumentNode#getXMLAttributeValue(java.lang.String)
302: */
303: public String getXMLAttributeValue(String name) {
304: // Overrided by necessity - dealing with different objects
305: PluginAttribute attr = (PluginAttribute) getNodeAttributesMap()
306: .get(name);
307: return attr == null ? null : attr.getValue();
308: }
309:
310: /* (non-Javadoc)
311: * @see org.eclipse.pde.internal.core.text.IDocumentElementNode#write(boolean)
312: */
313: public String write(boolean indent) {
314: // Used by text edit operations
315: // Subclasses to override
316: return ""; //$NON-NLS-1$
317: }
318:
319: /* (non-Javadoc)
320: * @see org.eclipse.pde.internal.core.text.IDocumentElementNode#writeShallow(boolean)
321: */
322: public String writeShallow(boolean terminate) {
323: // Used by text edit operations
324: // Subclasses to override
325: return ""; //$NON-NLS-1$
326: }
327:
328: /* (non-Javadoc)
329: * @see org.eclipse.pde.internal.core.text.plugin.PluginDocumentNode#getFileEncoding()
330: */
331: protected String getFileEncoding() {
332: if ((fModel != null) && (fModel instanceof IEditingModel)) {
333: return ((IEditingModel) fModel).getCharset();
334: }
335: return super.getFileEncoding();
336: }
337:
338: }
|