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.util.ArrayList;
014:
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.core.runtime.IConfigurationElement;
017: import org.eclipse.core.runtime.IExtension;
018: import org.eclipse.pde.core.plugin.IPluginElement;
019: import org.eclipse.pde.core.plugin.IPluginExtension;
020: import org.eclipse.pde.core.plugin.IPluginObject;
021: import org.eclipse.pde.internal.core.PDECore;
022: import org.eclipse.pde.internal.core.ischema.ISchema;
023: import org.eclipse.pde.internal.core.schema.SchemaRegistry;
024:
025: public class PluginExtension extends PluginParent implements
026: IPluginExtension {
027:
028: private static final long serialVersionUID = 1L;
029: protected String fPoint;
030: private transient ISchema schema;
031: private IExtension fExtension = null;
032:
033: public PluginExtension() {
034: }
035:
036: public PluginExtension(IExtension extension) {
037: fExtension = extension;
038: }
039:
040: public String getPoint() {
041: if (fPoint == null && fExtension != null)
042: fPoint = fExtension.getExtensionPointUniqueIdentifier();
043: return fPoint;
044: }
045:
046: public boolean isValid() {
047: return getPoint() != null;
048: }
049:
050: public Object getSchema() {
051: if (schema == null) {
052: SchemaRegistry registry = PDECore.getDefault()
053: .getSchemaRegistry();
054: schema = registry.getSchema(fPoint);
055: } else if (schema.isDisposed()) {
056: schema = null;
057: }
058: return schema;
059: }
060:
061: public boolean equals(Object obj) {
062: if (obj == this )
063: return true;
064: if (obj == null)
065: return false;
066: if (obj instanceof IPluginExtension) {
067: IPluginExtension target = (IPluginExtension) obj;
068: // Objects from the same model must be
069: // binary equal
070: if (target.getModel().equals(getModel()))
071: return false;
072: if (!stringEqualWithNull(target.getId(), getId()))
073: return false;
074: if (!stringEqualWithNull(target.getPoint(), getPoint()))
075: return false;
076: if (!nameEqual(target.getName()))
077: return false;
078: // Children
079: return super .equals(obj);
080: }
081: return false;
082: }
083:
084: private boolean nameEqual(String targetName) {
085: // Since extension registry returns "" when an extension's name == null, we have to do the same when comparing the name of the target.
086: // Note, we only do this if the PluginExtension has an fExtension element which means it's name comes from the extension registry.
087: if (fExtension != null && targetName == null)
088: targetName = ""; //$NON-NLS-1$
089: return stringEqualWithNull(targetName, getName());
090: }
091:
092: public void setPoint(String point) throws CoreException {
093: ensureModelEditable();
094: String oldValue = fPoint;
095: fPoint = point;
096: firePropertyChanged(P_POINT, oldValue, point);
097: }
098:
099: public void restoreProperty(String name, Object oldValue,
100: Object newValue) throws CoreException {
101: if (name.equals(P_POINT)) {
102: setPoint(newValue != null ? newValue.toString() : null);
103: return;
104: }
105: super .restoreProperty(name, oldValue, newValue);
106: }
107:
108: public String toString() {
109: if (getName() != null)
110: return getName();
111: return getPoint();
112: }
113:
114: public void write(String indent, PrintWriter writer) {
115: writer.print(indent);
116: writer.print("<extension"); //$NON-NLS-1$
117: String attIndent = indent + PluginElement.ATTRIBUTE_SHIFT;
118: if (getId() != null) {
119: writer.println();
120: writer.print(attIndent + "id=\"" + getId() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
121: }
122: if (getName() != null) {
123: writer.println();
124: writer.print(attIndent
125: + "name=\"" + getWritableString(getName()) + "\""); //$NON-NLS-1$ //$NON-NLS-2$
126: }
127: if (getPoint() != null) {
128: writer.println();
129: writer.print(attIndent + "point=\"" + getPoint() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
130: }
131: writer.println(">"); //$NON-NLS-1$
132: IPluginObject[] children = getChildren();
133: for (int i = 0; i < children.length; i++) {
134: IPluginElement child = (IPluginElement) children[i];
135: child.write(indent + PluginElement.ELEMENT_SHIFT, writer);
136: }
137: writer.println(indent + "</extension>"); //$NON-NLS-1$
138: }
139:
140: public String getName() {
141: if (fName == null && fExtension != null) {
142: fName = fExtension.getLabel();
143: }
144: return fName;
145: }
146:
147: public String getId() {
148: if (fID == null && fExtension != null) {
149: fID = fExtension.getUniqueIdentifier();
150: if (fID != null) {
151: String pluginId = getPluginBase().getId();
152: if (fID.startsWith(pluginId)) {
153: String sub = fID.substring(pluginId.length());
154: if (sub.lastIndexOf('.') == 0)
155: fID = sub.substring(1);
156: }
157: }
158: }
159: return fID;
160: }
161:
162: protected ArrayList getChildrenList() {
163: if (fChildren == null) {
164: fChildren = new ArrayList();
165: if (fExtension != null) {
166: if (fExtension != null) {
167: IConfigurationElement[] elements = fExtension
168: .getConfigurationElements();
169: for (int i = 0; i < elements.length; i++) {
170: PluginElement element = new PluginElement(
171: elements[i]);
172: element.setModel(getModel());
173: element.setParent(this);
174: fChildren.add(element);
175: }
176: }
177: }
178: }
179: return fChildren;
180: }
181: }
|