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.feature;
011:
012: import java.io.PrintWriter;
013:
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.pde.core.plugin.IPlugin;
016: import org.eclipse.pde.core.plugin.IPluginModel;
017: import org.eclipse.pde.core.plugin.IPluginModelBase;
018: import org.eclipse.pde.core.plugin.PluginRegistry;
019: import org.eclipse.pde.internal.core.PDECore;
020: import org.eclipse.pde.internal.core.ifeature.IFeature;
021: import org.eclipse.pde.internal.core.ifeature.IFeatureImport;
022: import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
023: import org.eclipse.pde.internal.core.util.VersionUtil;
024: import org.w3c.dom.Node;
025:
026: public class FeatureImport extends VersionableObject implements
027: IFeatureImport {
028: private static final long serialVersionUID = 1L;
029: private int fMatch = NONE;
030: private int fIdMatch = PERFECT;
031: private int fType = PLUGIN;
032: private boolean fPatch = false;
033:
034: public FeatureImport() {
035: }
036:
037: public IPlugin getPlugin() {
038: if (id != null && fType == PLUGIN) {
039: IPluginModelBase model = PluginRegistry.findModel(id);
040: return model instanceof IPluginModel ? ((IPluginModel) model)
041: .getPlugin()
042: : null;
043: }
044: return null;
045: }
046:
047: public IFeature getFeature() {
048: if (id != null && fType == FEATURE) {
049: return findFeature(id, getVersion(), fMatch);
050: }
051: return null;
052: }
053:
054: private IFeature findFeature(IFeatureModel[] models, String id,
055: String version, int match) {
056:
057: for (int i = 0; i < models.length; i++) {
058: IFeatureModel model = models[i];
059:
060: IFeature feature = model.getFeature();
061: String pid = feature.getId();
062: String pversion = feature.getVersion();
063: if (VersionUtil.compare(pid, pversion, id, version, match))
064: return feature;
065: }
066: return null;
067: }
068:
069: /**
070: * Finds a feature with the given ID and satisfying constraints
071: * of the version and the match.
072: * @param id
073: * @param version
074: * @param match
075: * @return IFeature or null
076: */
077: public IFeature findFeature(String id, String version, int match) {
078: IFeatureModel[] models = PDECore.getDefault()
079: .getFeatureModelManager().findFeatureModels(id);
080: return findFeature(models, id, version, match);
081: }
082:
083: public int getIdMatch() {
084: return fIdMatch;
085: }
086:
087: protected void reset() {
088: super .reset();
089: fPatch = false;
090: fType = PLUGIN;
091: fMatch = NONE;
092: fIdMatch = PERFECT;
093: }
094:
095: protected void parse(Node node) {
096: super .parse(node);
097: this .id = getNodeAttribute(node, "plugin"); //$NON-NLS-1$
098: if (id != null)
099: fType = PLUGIN;
100: else {
101: this .id = getNodeAttribute(node, "feature"); //$NON-NLS-1$
102: if (id != null)
103: fType = FEATURE;
104: }
105: String mvalue = getNodeAttribute(node, "match"); //$NON-NLS-1$
106: if (mvalue != null && mvalue.length() > 0) {
107: String[] choices = RULE_NAME_TABLE;
108: for (int i = 0; i < choices.length; i++) {
109: if (mvalue.equalsIgnoreCase(choices[i])) {
110: fMatch = i;
111: break;
112: }
113: }
114: }
115: mvalue = getNodeAttribute(node, "id-match"); //$NON-NLS-1$
116:
117: if (mvalue != null && mvalue.length() > 0) {
118: if (mvalue.equalsIgnoreCase(RULE_PREFIX))
119: fIdMatch = PREFIX;
120: }
121: fPatch = getBooleanAttribute(node, "patch"); //$NON-NLS-1$
122: }
123:
124: public void loadFrom(IFeature feature) {
125: reset();
126: fType = FEATURE;
127: id = feature.getId();
128: version = feature.getVersion();
129: }
130:
131: public int getMatch() {
132: return fMatch;
133: }
134:
135: public void setMatch(int match) throws CoreException {
136: ensureModelEditable();
137: Integer oldValue = new Integer(this .fMatch);
138: this .fMatch = match;
139: firePropertyChanged(P_MATCH, oldValue, new Integer(match));
140: }
141:
142: public void setIdMatch(int idMatch) throws CoreException {
143: ensureModelEditable();
144: Integer oldValue = new Integer(this .fIdMatch);
145: this .fIdMatch = idMatch;
146: firePropertyChanged(P_ID_MATCH, oldValue, new Integer(idMatch));
147: }
148:
149: public int getType() {
150: return fType;
151: }
152:
153: public void setType(int type) throws CoreException {
154: ensureModelEditable();
155: Integer oldValue = new Integer(this .fType);
156: this .fType = type;
157: firePropertyChanged(P_TYPE, oldValue, new Integer(type));
158: }
159:
160: public boolean isPatch() {
161: return fPatch;
162: }
163:
164: public void setPatch(boolean patch) throws CoreException {
165: ensureModelEditable();
166: Boolean oldValue = new Boolean(this .fPatch);
167: this .fPatch = patch;
168: firePropertyChanged(P_PATCH, oldValue, new Boolean(patch));
169: }
170:
171: public void restoreProperty(String name, Object oldValue,
172: Object newValue) throws CoreException {
173: if (name.equals(P_MATCH)) {
174: setMatch(newValue != null ? ((Integer) newValue).intValue()
175: : 0);
176: } else if (name.equals(P_ID_MATCH)) {
177: setIdMatch(newValue != null ? ((Integer) newValue)
178: .intValue() : 0);
179: } else if (name.equals(P_TYPE)) {
180: setType(newValue != null ? ((Integer) newValue).intValue()
181: : PLUGIN);
182: } else if (name.equals(P_PATCH)) {
183: setPatch(newValue != null ? ((Boolean) newValue)
184: .booleanValue() : false);
185: } else
186: super .restoreProperty(name, oldValue, newValue);
187: }
188:
189: public void write(String indent, PrintWriter writer) {
190: String typeAtt = fType == FEATURE ? "feature" : "plugin"; //$NON-NLS-1$ //$NON-NLS-2$
191: writer.print(indent
192: + "<import " + typeAtt + "=\"" + getId() + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
193: String version = getVersion();
194: if (version != null && version.length() > 0) {
195: writer.print(" version=\"" + version + "\""); //$NON-NLS-1$ //$NON-NLS-2$
196: }
197: if (!fPatch && fMatch != NONE) {
198: writer.print(" match=\"" + RULE_NAME_TABLE[fMatch] + "\""); //$NON-NLS-1$ //$NON-NLS-2$
199: }
200: if (fIdMatch == PREFIX) {
201: writer.print(" id-match=\"prefix\""); //$NON-NLS-1$
202: }
203: if (fPatch) {
204: writer.print(" patch=\"true\""); //$NON-NLS-1$
205: }
206: writer.println("/>"); //$NON-NLS-1$
207: }
208:
209: public String toString() {
210: IPlugin plugin = getPlugin();
211: if (plugin != null)
212: return plugin.getTranslatedName();
213: IFeature feature = getFeature();
214: if (feature != null)
215: return feature.getLabel();
216: return getId();
217: }
218: }
|