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.IFragment;
016: import org.eclipse.pde.core.plugin.IFragmentModel;
017: import org.eclipse.pde.core.plugin.IPluginBase;
018: import org.eclipse.pde.core.plugin.IPluginModel;
019: import org.eclipse.pde.core.plugin.IPluginModelBase;
020: import org.eclipse.pde.core.plugin.ModelEntry;
021: import org.eclipse.pde.core.plugin.PluginRegistry;
022: import org.eclipse.pde.internal.core.ifeature.IFeaturePlugin;
023: import org.w3c.dom.Node;
024:
025: public class FeaturePlugin extends FeatureData implements
026: IFeaturePlugin {
027: private static final long serialVersionUID = 1L;
028: private boolean fFragment;
029: private String fVersion;
030: private boolean fUnpack = true;
031:
032: public FeaturePlugin() {
033: }
034:
035: protected void reset() {
036: super .reset();
037: fVersion = null;
038: fFragment = false;
039: }
040:
041: public boolean isFragment() {
042: return fFragment;
043: }
044:
045: public IPluginBase getPluginBase() {
046: if (id == null) {
047: return null;
048: }
049: String version = getVersion();
050: IPluginModelBase model = null;
051: if (version == null || version.equals("0.0.0")) //$NON-NLS-1$
052: model = PluginRegistry.findModel(id);
053: else {
054: ModelEntry entry = PluginRegistry.findEntry(id);
055: // if no plug-ins match the id, entry == null
056: if (entry != null) {
057: IPluginModelBase bases[] = entry.getActiveModels();
058: for (int i = 0; i < bases.length; i++) {
059: if (bases[i].getPluginBase().getVersion().equals(
060: version)) {
061: model = bases[i];
062: break;
063: }
064: }
065: }
066: }
067: if (fFragment && model instanceof IFragmentModel)
068: return model.getPluginBase();
069: if (!fFragment && model instanceof IPluginModel)
070: return model.getPluginBase();
071: return null;
072: }
073:
074: public String getVersion() {
075: return fVersion;
076: }
077:
078: public boolean isUnpack() {
079: return fUnpack;
080: }
081:
082: public void setVersion(String version) throws CoreException {
083: ensureModelEditable();
084: Object oldValue = this .fVersion;
085: this .fVersion = version;
086: firePropertyChanged(this , P_VERSION, oldValue, version);
087: }
088:
089: public void setUnpack(boolean unpack) throws CoreException {
090: ensureModelEditable();
091: boolean oldValue = fUnpack;
092: this .fUnpack = unpack;
093: firePropertyChanged(this , P_UNPACK, new Boolean(oldValue),
094: new Boolean(unpack));
095: }
096:
097: public void restoreProperty(String name, Object oldValue,
098: Object newValue) throws CoreException {
099: if (name.equals(P_VERSION)) {
100: setVersion(newValue != null ? newValue.toString() : null);
101: } else
102: super .restoreProperty(name, oldValue, newValue);
103: }
104:
105: public void setFragment(boolean fragment) throws CoreException {
106: ensureModelEditable();
107: this .fFragment = fragment;
108: }
109:
110: protected void parse(Node node) {
111: super .parse(node);
112: fVersion = getNodeAttribute(node, "version"); //$NON-NLS-1$
113: String f = getNodeAttribute(node, "fragment"); //$NON-NLS-1$
114: if (f != null && f.equalsIgnoreCase("true")) //$NON-NLS-1$
115: fFragment = true;
116: String unpack = getNodeAttribute(node, "unpack"); //$NON-NLS-1$
117: if (unpack != null && unpack.equalsIgnoreCase("false")) //$NON-NLS-1$
118: fUnpack = false;
119: }
120:
121: public void loadFrom(IPluginBase plugin) {
122: id = plugin.getId();
123: label = plugin.getTranslatedName();
124: fVersion = plugin.getVersion();
125: fFragment = plugin instanceof IFragment;
126: }
127:
128: public void write(String indent, PrintWriter writer) {
129: writer.print(indent + "<plugin"); //$NON-NLS-1$
130: String indent2 = indent + Feature.INDENT + Feature.INDENT;
131: writeAttributes(indent2, writer);
132: if (getVersion() != null) {
133: writer.println();
134: writer.print(indent2 + "version=\"" + getVersion() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
135: }
136: if (isFragment()) {
137: writer.println();
138: writer.print(indent2 + "fragment=\"true\""); //$NON-NLS-1$
139: }
140: if (!isUnpack()) {
141: writer.println();
142: writer.print(indent2 + "unpack=\"false\""); //$NON-NLS-1$
143: }
144: writer.println("/>"); //$NON-NLS-1$
145: //writer.println(indent + "</plugin>");
146: }
147:
148: public String getLabel() {
149: IPluginBase pluginBase = getPluginBase();
150: if (pluginBase != null) {
151: return pluginBase.getTranslatedName();
152: }
153: String name = super .getLabel();
154: if (name == null)
155: name = getId();
156: return name;
157: }
158:
159: public String toString() {
160: return getLabel();
161: }
162:
163: }
|