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.plugin;
011:
012: import java.io.PrintWriter;
013: import java.io.Serializable;
014: import java.util.Vector;
015:
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.core.runtime.IStatus;
018: import org.eclipse.core.runtime.PlatformObject;
019: import org.eclipse.core.runtime.Status;
020: import org.eclipse.pde.core.IModel;
021: import org.eclipse.pde.core.IModelChangeProvider;
022: import org.eclipse.pde.core.IModelChangedEvent;
023: import org.eclipse.pde.core.ISourceObject;
024: import org.eclipse.pde.core.ModelChangedEvent;
025: import org.eclipse.pde.core.plugin.IPluginBase;
026: import org.eclipse.pde.core.plugin.IPluginModelBase;
027: import org.eclipse.pde.core.plugin.IPluginObject;
028: import org.eclipse.pde.core.plugin.ISharedPluginModel;
029: import org.eclipse.pde.internal.core.PDECore;
030: import org.eclipse.pde.internal.core.PDECoreMessages;
031: import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelProvider;
032: import org.eclipse.pde.internal.core.util.PDEXMLHelper;
033: import org.w3c.dom.Comment;
034: import org.w3c.dom.Node;
035:
036: public abstract class PluginObject extends PlatformObject implements
037: IPluginObject, ISourceObject, Serializable, IWritableDelimiter {
038:
039: protected String fName;
040:
041: private transient String fTranslatedName;
042: private transient IPluginObject fParent;
043: private transient ISharedPluginModel fModel;
044: private transient boolean fInTheModel;
045:
046: protected int fStartLine = 1;
047:
048: public PluginObject() {
049: }
050:
051: public boolean isValid() {
052: return true;
053: }
054:
055: protected void ensureModelEditable() throws CoreException {
056: if (!fModel.isEditable()) {
057: throwCoreException(PDECoreMessages.PluginObject_readOnlyChange);
058: }
059: }
060:
061: public void setInTheModel(boolean value) {
062: fInTheModel = value;
063: }
064:
065: public boolean isInTheModel() {
066: return fInTheModel;
067: }
068:
069: protected void firePropertyChanged(String property,
070: Object oldValue, Object newValue) {
071: firePropertyChanged(this , property, oldValue, newValue);
072: }
073:
074: protected void firePropertyChanged(IPluginObject object,
075: String property, Object oldValue, Object newValue) {
076: if (fModel.isEditable()) {
077: IModelChangeProvider provider = fModel;
078: provider.fireModelObjectChanged(object, property, oldValue,
079: newValue);
080: }
081: }
082:
083: protected void fireStructureChanged(IPluginObject child,
084: int changeType) {
085: IModel model = getModel();
086: if (model.isEditable() && model instanceof IModelChangeProvider) {
087: IModelChangedEvent e = new ModelChangedEvent(
088: (IModelChangeProvider) model, changeType,
089: new Object[] { child }, null);
090: fireModelChanged(e);
091: }
092: }
093:
094: protected void fireStructureChanged(IPluginObject[] children,
095: int changeType) {
096: IModel model = getModel();
097: if (model.isEditable() && model instanceof IModelChangeProvider) {
098: IModelChangedEvent e = new ModelChangedEvent(
099: (IModelChangeProvider) model, changeType, children,
100: null);
101: fireModelChanged(e);
102: }
103: }
104:
105: protected void fireModelChanged(IModelChangedEvent e) {
106: IModel model = getModel();
107: if (model.isEditable() && model instanceof IModelChangeProvider) {
108: IModelChangeProvider provider = (IModelChangeProvider) model;
109: provider.fireModelChanged(e);
110: }
111: }
112:
113: public ISharedPluginModel getModel() {
114: return fModel;
115: }
116:
117: public IPluginModelBase getPluginModel() {
118: if (fModel instanceof IBundlePluginModelProvider)
119: return ((IBundlePluginModelProvider) fModel)
120: .getBundlePluginModel();
121:
122: return fModel instanceof IPluginModelBase ? (IPluginModelBase) fModel
123: : null;
124: }
125:
126: public String getName() {
127: return fName;
128: }
129:
130: public String getTranslatedName() {
131: if (fTranslatedName != null && !fModel.isEditable())
132: return fTranslatedName;
133: if (fTranslatedName == null && fName != null && fModel != null) {
134: fTranslatedName = fModel.getResourceString(fName);
135: }
136: return fTranslatedName;
137: }
138:
139: String getNodeAttribute(Node node, String name) {
140: Node attribute = node.getAttributes().getNamedItem(name);
141: if (attribute != null)
142: return attribute.getNodeValue();
143: return null;
144: }
145:
146: public IPluginObject getParent() {
147: return fParent;
148: }
149:
150: public IPluginBase getPluginBase() {
151: IPluginModelBase pluginModel = getPluginModel();
152: return pluginModel != null ? pluginModel.getPluginBase() : null;
153: }
154:
155: public String getResourceString(String key) {
156: return fModel.getResourceString(key);
157: }
158:
159: static boolean isNotEmpty(String text) {
160: for (int i = 0; i < text.length(); i++) {
161: if (Character.isWhitespace(text.charAt(i)) == false)
162: return true;
163: }
164: return false;
165: }
166:
167: public void restoreProperty(String name, Object oldValue,
168: Object newValue) throws CoreException {
169: if (name.equals(P_NAME)) {
170: setName(newValue != null ? newValue.toString() : null);
171: }
172: }
173:
174: public void setModel(ISharedPluginModel model) {
175: this .fModel = model;
176: fTranslatedName = null;
177: }
178:
179: public void setName(String name) throws CoreException {
180: ensureModelEditable();
181: String oldValue = this .fName;
182: this .fName = name;
183: firePropertyChanged(P_NAME, oldValue, name);
184: }
185:
186: public void setParent(IPluginObject parent) {
187: this .fParent = parent;
188: }
189:
190: protected void throwCoreException(String message)
191: throws CoreException {
192: Status status = new Status(IStatus.ERROR, PDECore.PLUGIN_ID,
193: IStatus.OK, message, null);
194: CoreException ce = new CoreException(status);
195: ce.fillInStackTrace();
196: throw ce;
197: }
198:
199: public String toString() {
200: if (fName != null)
201: return fName;
202: return super .toString();
203: }
204:
205: public Vector addComments(Node node, Vector result) {
206: for (Node prev = node.getPreviousSibling(); prev != null; prev = prev
207: .getPreviousSibling()) {
208: if (prev.getNodeType() == Node.TEXT_NODE)
209: continue;
210: if (prev instanceof Comment) {
211: String comment = prev.getNodeValue();
212: if (result == null)
213: result = new Vector();
214: result.add(0, comment);
215: } else
216: break;
217: }
218: return result;
219: }
220:
221: void writeComments(PrintWriter writer, Vector source) {
222: if (source == null)
223: return;
224: for (int i = 0; i < source.size(); i++) {
225: String comment = (String) source.elementAt(i);
226: writer.println("<!--" + comment + "-->"); //$NON-NLS-1$ //$NON-NLS-2$
227: }
228: }
229:
230: protected boolean stringEqualWithNull(String a, String b) {
231: return a == null && b == null || a != null && b != null
232: && a.equals(b);
233: }
234:
235: public String getWritableString(String source) {
236: return PDEXMLHelper.getWritableString(source);
237: }
238:
239: public int getStartLine() {
240: return fStartLine;
241: }
242:
243: public int getStopLine() {
244: return fStartLine;
245: }
246:
247: public Object getAdapter(Class adapter) {
248: if (adapter.equals(IPluginModelBase.class)) {
249: return getPluginModel();
250: }
251: return super .getAdapter(adapter);
252: }
253:
254: /**
255: * @param model
256: * @param parent
257: */
258: public void reconnect(ISharedPluginModel model, IPluginObject parent) {
259: // Transient Field: In The Model
260: fInTheModel = false;
261: // Transient Field: Model
262: fModel = model;
263: // Transient Field: Parent
264: fParent = parent;
265: // Transient Field: Translated Name
266: fTranslatedName = null;
267: }
268:
269: /* (non-Javadoc)
270: * @see org.eclipse.pde.internal.core.plugin.IWritableDelimeter#writeDelimeter(java.io.PrintWriter)
271: */
272: public void writeDelimeter(PrintWriter writer) {
273: // NO-OP
274: // Child classes to override
275: }
276:
277: }
|