1 11 package org.eclipse.pde.internal.core.feature; 12 13 import java.io.PrintWriter ; 14 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.pde.core.plugin.IPlugin; 17 import org.eclipse.pde.core.plugin.IPluginModel; 18 import org.eclipse.pde.core.plugin.IPluginModelBase; 19 import org.eclipse.pde.core.plugin.PluginRegistry; 20 import org.eclipse.pde.internal.core.PDECore; 21 import org.eclipse.pde.internal.core.ifeature.IFeature; 22 import org.eclipse.pde.internal.core.ifeature.IFeatureImport; 23 import org.eclipse.pde.internal.core.ifeature.IFeatureModel; 24 import org.eclipse.pde.internal.core.util.VersionUtil; 25 import org.w3c.dom.Node ; 26 27 public class FeatureImport 28 extends VersionableObject 29 implements IFeatureImport { 30 private static final long serialVersionUID = 1L; 31 private int fMatch = NONE; 32 private int fIdMatch = PERFECT; 33 private int fType = PLUGIN; 34 private boolean fPatch = false; 35 36 public FeatureImport() { 37 } 38 39 public IPlugin getPlugin() { 40 if (id != null && fType == PLUGIN) { 41 IPluginModelBase model = PluginRegistry.findModel(id); 42 return model instanceof IPluginModel ? ((IPluginModel)model).getPlugin() : null; 43 } 44 return null; 45 } 46 47 public IFeature getFeature() { 48 if (id != null && fType == FEATURE) { 49 return findFeature(id, getVersion(), fMatch); 50 } 51 return null; 52 } 53 54 private IFeature findFeature( 55 IFeatureModel[] models, 56 String id, 57 String version, 58 int match) { 59 60 for (int i = 0; i < models.length; i++) { 61 IFeatureModel model = models[i]; 62 63 IFeature feature = model.getFeature(); 64 String pid = feature.getId(); 65 String pversion = feature.getVersion(); 66 if (VersionUtil.compare(id, version, pid, pversion, match)) 67 return feature; 68 } 69 return null; 70 } 71 72 80 public IFeature findFeature(String id, String version, int match) { 81 IFeatureModel[] models = PDECore.getDefault().getFeatureModelManager().findFeatureModels(id); 82 return findFeature(models, id, version, match); 83 } 84 85 public int getIdMatch() { 86 return fIdMatch; 87 } 88 89 protected void reset() { 90 super.reset(); 91 fPatch = false; 92 fType = PLUGIN; 93 fMatch = NONE; 94 fIdMatch = PERFECT; 95 } 96 97 protected void parse(Node node) { 98 super.parse(node); 99 this.id = getNodeAttribute(node, "plugin"); if (id != null) 101 fType = PLUGIN; 102 else { 103 this.id = getNodeAttribute(node, "feature"); if (id != null) 105 fType = FEATURE; 106 } 107 String mvalue = getNodeAttribute(node, "match"); if (mvalue != null && mvalue.length() > 0) { 109 String [] choices = RULE_NAME_TABLE; 110 for (int i = 0; i < choices.length; i++) { 111 if (mvalue.equalsIgnoreCase(choices[i])) { 112 fMatch = i; 113 break; 114 } 115 } 116 } 117 mvalue = getNodeAttribute(node, "id-match"); 119 if (mvalue != null && mvalue.length() > 0) { 120 if (mvalue.equalsIgnoreCase(RULE_PREFIX)) 121 fIdMatch = PREFIX; 122 } 123 fPatch = getBooleanAttribute(node, "patch"); } 125 126 public void loadFrom(IFeature feature) { 127 reset(); 128 fType = FEATURE; 129 id = feature.getId(); 130 version = feature.getVersion(); 131 } 132 133 public int getMatch() { 134 return fMatch; 135 } 136 137 public void setMatch(int match) throws CoreException { 138 ensureModelEditable(); 139 Integer oldValue = new Integer (this.fMatch); 140 this.fMatch = match; 141 firePropertyChanged(P_MATCH, oldValue, new Integer (match)); 142 } 143 144 public void setIdMatch(int idMatch) throws CoreException { 145 ensureModelEditable(); 146 Integer oldValue = new Integer (this.fIdMatch); 147 this.fIdMatch = idMatch; 148 firePropertyChanged(P_ID_MATCH, oldValue, new Integer (idMatch)); 149 } 150 151 public int getType() { 152 return fType; 153 } 154 155 public void setType(int type) throws CoreException { 156 ensureModelEditable(); 157 Integer oldValue = new Integer (this.fType); 158 this.fType = type; 159 firePropertyChanged(P_TYPE, oldValue, new Integer (type)); 160 } 161 162 public boolean isPatch() { 163 return fPatch; 164 } 165 166 public void setPatch(boolean patch) throws CoreException { 167 ensureModelEditable(); 168 Boolean oldValue = new Boolean (this.fPatch); 169 this.fPatch = patch; 170 firePropertyChanged(P_PATCH, oldValue, new Boolean (patch)); 171 } 172 173 public void restoreProperty(String name, Object oldValue, Object newValue) 174 throws CoreException { 175 if (name.equals(P_MATCH)) { 176 setMatch(newValue != null ? ((Integer ) newValue).intValue() : 0); 177 } else if (name.equals(P_ID_MATCH)) { 178 setIdMatch(newValue != null ? ((Integer ) newValue).intValue() : 0); 179 } else if (name.equals(P_TYPE)) { 180 setType( 181 newValue != null ? ((Integer ) newValue).intValue() : PLUGIN); 182 } else if (name.equals(P_PATCH)) { 183 setPatch( 184 newValue != null ? ((Boolean ) newValue).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"; writer.print(indent + "<import " + typeAtt + "=\"" + getId() + "\""); String version = getVersion(); 193 if (version != null && version.length() > 0) { 194 writer.print(" version=\"" + version + "\""); } 196 if (!fPatch && fMatch != NONE) { 197 writer.print(" match=\"" + RULE_NAME_TABLE[fMatch] + "\""); } 199 if (fIdMatch == PREFIX) { 200 writer.print(" id-match=\"prefix\""); } 202 if (fPatch) { 203 writer.print(" patch=\"true\""); } 205 writer.println("/>"); } 207 208 public String toString() { 209 IPlugin plugin = getPlugin(); 210 if (plugin != null) 211 return plugin.getTranslatedName(); 212 IFeature feature = getFeature(); 213 if (feature != null) 214 return feature.getLabel(); 215 return getId(); 216 } 217 } 218 | Popular Tags |