1 11 package org.eclipse.pde.internal.core.site; 12 13 import java.io.PrintWriter ; 14 import java.util.Vector ; 15 16 import org.eclipse.core.resources.IFile; 17 import org.eclipse.core.resources.IProject; 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.Path; 21 import org.eclipse.pde.core.IModelChangedEvent; 22 import org.eclipse.pde.internal.core.isite.ISiteCategory; 23 import org.eclipse.pde.internal.core.isite.ISiteFeature; 24 import org.w3c.dom.Node ; 25 import org.w3c.dom.NodeList ; 26 27 public class SiteFeature extends VersionableObject implements ISiteFeature { 28 private static final long serialVersionUID = 1L; 29 private Vector fCategories = new Vector (); 30 private String fType; 31 private String fUrl; 32 private String fOS; 33 private String fWS; 34 private String fArch; 35 private String fNL; 36 private boolean fIsPatch; 37 38 public boolean isValid() { 39 if (fUrl == null) 40 return false; 41 for (int i = 0; i < fCategories.size(); i++) { 42 ISiteCategory category = (ISiteCategory) fCategories.get(i); 43 if (!category.isValid()) 44 return false; 45 } 46 return true; 47 } 48 49 52 public void addCategories(ISiteCategory[] newCategories) 53 throws CoreException { 54 ensureModelEditable(); 55 for (int i = 0; i < newCategories.length; i++) { 56 ISiteCategory category = newCategories[i]; 57 ((SiteCategory) category).setInTheModel(true); 58 fCategories.add(newCategories[i]); 59 } 60 fireStructureChanged(newCategories, IModelChangedEvent.INSERT); 61 } 62 63 66 public void removeCategories(ISiteCategory[] newCategories) 67 throws CoreException { 68 ensureModelEditable(); 69 for (int i = 0; i < newCategories.length; i++) { 70 ISiteCategory category = newCategories[i]; 71 ((SiteCategory) category).setInTheModel(false); 72 fCategories.remove(newCategories[i]); 73 } 74 fireStructureChanged(newCategories, IModelChangedEvent.REMOVE); 75 } 76 77 80 public ISiteCategory[] getCategories() { 81 return (ISiteCategory[]) fCategories.toArray( 82 new ISiteCategory[fCategories.size()]); 83 } 84 85 88 public String getType() { 89 return fType; 90 } 91 92 95 public String getURL() { 96 return fUrl; 97 } 98 99 102 public void setType(String type) throws CoreException { 103 ensureModelEditable(); 104 Object oldValue = this.fType; 105 this.fType = type; 106 firePropertyChanged(P_TYPE, oldValue, fType); 107 } 108 109 112 public void setURL(String url) throws CoreException { 113 ensureModelEditable(); 114 Object oldValue = this.fUrl; 115 this.fUrl = url; 116 firePropertyChanged(P_TYPE, oldValue, url); 117 } 118 119 protected void parse(Node node) { 120 super.parse(node); 121 fType = getNodeAttribute(node, "type"); fUrl = getNodeAttribute(node, "url"); fOS = getNodeAttribute(node, "os"); fNL = getNodeAttribute(node, "nl"); fWS = getNodeAttribute(node, "ws"); fArch = getNodeAttribute(node, "arch"); String value = getNodeAttribute(node, "patch"); fIsPatch = value != null && value.equals("true"); NodeList children = node.getChildNodes(); 130 for (int i = 0; i < children.getLength(); i++) { 131 Node child = children.item(i); 132 if (child.getNodeType() == Node.ELEMENT_NODE 133 && child.getNodeName().equalsIgnoreCase("category")) { SiteCategory category = 135 (SiteCategory) getModel().getFactory().createCategory(this); 136 category.parse(child); 137 category.setInTheModel(true); 138 fCategories.add(category); 139 } 140 } 141 } 142 143 protected void reset() { 144 super.reset(); 145 fType = null; 146 fUrl = null; 147 fOS = null; 148 fWS = null; 149 fArch = null; 150 fNL = null; 151 fIsPatch = false; 152 fCategories.clear(); 153 } 154 155 public void restoreProperty(String name, Object oldValue, Object newValue) 156 throws CoreException { 157 if (name.equals(P_TYPE)) { 158 setType(newValue != null ? newValue.toString() : null); 159 } else if (name.equals(P_URL)) { 160 setURL(newValue != null ? newValue.toString() : null); 161 } else if (name.equals(P_ARCH)) { 162 setArch(newValue != null ? newValue.toString() : null); 163 } else if (name.equals(P_NL)) { 164 setNL(newValue != null ? newValue.toString() : null); 165 } else if (name.equals(P_OS)) { 166 setOS(newValue != null ? newValue.toString() : null); 167 } else if (name.equals(P_WS)) { 168 setWS(newValue != null ? newValue.toString() : null); 169 } else if (name.equals(P_PATCH)) { 170 setIsPatch(((Boolean )newValue).booleanValue()); 171 } else { 172 super.restoreProperty(name, oldValue, newValue); 173 } 174 } 175 176 179 public void write(String indent, PrintWriter writer) { 180 writer.print(indent); 181 writer.print("<feature"); if (fType != null) 183 writer.print(" type=\"" + fType + "\""); if (fUrl != null) 185 writer.print(" url=\"" + fUrl + "\""); if (id != null) 187 writer.print(" id=\"" + getId() + "\""); if (version != null) 189 writer.print(" version=\"" + getVersion() + "\""); if (label != null) 191 writer.print(" label=\"" + getLabel() + "\""); if (fOS != null) 193 writer.print(" os=\"" + fOS + "\""); if (fWS != null) 195 writer.print(" ws=\"" + fWS + "\""); if (fNL != null) 197 writer.print(" nl=\"" + fNL + "\""); if (fArch != null) 199 writer.print(" arch=\"" + fArch + "\""); if (fIsPatch) 201 writer.print(" patch=\"true\""); if (fCategories.size() > 0) { 203 writer.println(">"); String indent2 = indent + " "; for (int i = 0; i < fCategories.size(); i++) { 206 ISiteCategory category = (ISiteCategory) fCategories.get(i); 207 category.write(indent2, writer); 208 } 209 writer.println(indent + "</feature>"); } else 211 writer.println("/>"); } 213 214 public IFile getArchiveFile() { 215 if (fUrl==null) return null; 216 IResource resource = getModel().getUnderlyingResource(); 217 if (resource==null) return null; 218 IProject project = resource.getProject(); 219 IFile file = project.getFile(new Path(fUrl)); 220 if (file.exists()) return file; 221 return null; 222 } 223 224 227 public String getOS() { 228 return fOS; 229 } 230 231 234 public String getNL() { 235 return fNL; 236 } 237 238 241 public String getArch() { 242 return fArch; 243 } 244 245 248 public String getWS() { 249 return fWS; 250 } 251 252 255 public void setOS(String os) throws CoreException{ 256 ensureModelEditable(); 257 Object oldValue = fOS; 258 fOS = os; 259 firePropertyChanged(P_OS, oldValue, fOS); 260 } 261 262 265 public void setWS(String ws) throws CoreException { 266 ensureModelEditable(); 267 Object oldValue = fWS; 268 fWS = ws; 269 firePropertyChanged(P_WS, oldValue, fWS); 270 } 271 272 275 public void setArch(String arch) throws CoreException { 276 ensureModelEditable(); 277 Object oldValue = fArch; 278 fArch = arch; 279 firePropertyChanged(P_ARCH, oldValue, fArch); 280 } 281 282 285 public void setNL(String nl) throws CoreException { 286 ensureModelEditable(); 287 Object oldValue = fNL; 288 fNL = nl; 289 firePropertyChanged(P_NL, oldValue, fNL); 290 } 291 292 295 public boolean isPatch() { 296 return fIsPatch; 297 } 298 299 302 public void setIsPatch(boolean patch) throws CoreException { 303 ensureModelEditable(); 304 Object oldValue = new Boolean (fIsPatch); 305 fIsPatch = patch; 306 firePropertyChanged(P_PATCH, oldValue, new Boolean (fIsPatch)); 307 } 308 } 309 | Popular Tags |