1 11 package org.eclipse.pde.internal.core.plugin; 12 13 import java.io.PrintWriter ; 14 import java.util.ArrayList ; 15 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.pde.core.IModelChangedEvent; 18 import org.eclipse.pde.core.plugin.IExtensions; 19 import org.eclipse.pde.core.plugin.IPluginBase; 20 import org.eclipse.pde.core.plugin.IPluginExtension; 21 import org.eclipse.pde.core.plugin.IPluginExtensionPoint; 22 import org.eclipse.pde.core.plugin.IPluginObject; 23 import org.eclipse.pde.internal.core.PDECoreMessages; 24 import org.w3c.dom.Node ; 25 26 public abstract class AbstractExtensions extends PluginObject implements IExtensions { 27 28 protected String fSchemaVersion; 29 30 protected ArrayList fExtensions = new ArrayList (1); 31 protected ArrayList fExtensionPoints = new ArrayList (1); 32 33 public void add(IPluginExtension extension) throws CoreException { 34 ensureModelEditable(); 35 fExtensions.add(extension); 36 ((PluginExtension) extension).setInTheModel(true); 37 ((PluginExtension) extension).setParent(this); 38 fireStructureChanged(extension, IModelChangedEvent.INSERT); 39 } 40 41 public void add(IPluginExtensionPoint extensionPoint) 42 throws CoreException { 43 ensureModelEditable(); 44 fExtensionPoints.add(extensionPoint); 45 ((PluginExtensionPoint) extensionPoint).setInTheModel(true); 46 ((PluginExtensionPoint) extensionPoint).setParent(this); 47 fireStructureChanged(extensionPoint, IModelChangedEvent.INSERT); 48 } 49 50 public IPluginExtensionPoint[] getExtensionPoints() { 51 return (IPluginExtensionPoint[])fExtensionPoints.toArray(new IPluginExtensionPoint[fExtensionPoints.size()]); 52 } 53 54 public IPluginExtension[] getExtensions() { 55 return (IPluginExtension[])fExtensions.toArray(new IPluginExtension[fExtensions.size()]); 56 } 57 58 public void restoreProperty(String name, Object oldValue, Object newValue) 59 throws CoreException { 60 if (name.equals(P_EXTENSION_ORDER)) { 61 swap((IPluginExtension) oldValue, (IPluginExtension) newValue); 62 return; 63 } 64 super.restoreProperty(name, oldValue, newValue); 65 } 66 67 public void load(IExtensions srcExtensions) { 68 addArrayToVector(fExtensions, srcExtensions.getExtensions()); 69 addArrayToVector(fExtensionPoints, srcExtensions.getExtensionPoints()); 70 } 71 72 protected void addArrayToVector(ArrayList vector, Object [] array) { 73 for (int i = 0; i < array.length; i++) { 74 Object obj= array[i]; 75 if (obj instanceof PluginObject) 76 ((PluginObject) obj).setParent(this); 77 vector.add(obj); 78 } 79 } 80 81 protected void processChild(Node child) { 82 String name = child.getNodeName(); 83 if (name.equals("extension")) { PluginExtension extension = new PluginExtension(); 85 extension.setModel(getModel()); 86 extension.setParent(this); 87 fExtensions.add(extension); 88 extension.setInTheModel(true); 89 extension.load(child); 90 } else if (name.equals("extension-point")) { PluginExtensionPoint point = new PluginExtensionPoint(); 92 point.setModel(getModel()); 93 point.setParent(this); 94 point.setInTheModel(true); 95 fExtensionPoints.add(point); 96 point.load(child); 97 } 98 } 99 100 public void remove(IPluginExtension extension) throws CoreException { 101 ensureModelEditable(); 102 fExtensions.remove(extension); 103 ((PluginExtension) extension).setInTheModel(false); 104 fireStructureChanged(extension, IModelChangedEvent.REMOVE); 105 } 106 107 public void remove(IPluginExtensionPoint extensionPoint) 108 throws CoreException { 109 ensureModelEditable(); 110 fExtensionPoints.remove(extensionPoint); 111 ((PluginExtensionPoint) extensionPoint).setInTheModel(false); 112 fireStructureChanged(extensionPoint, IModelChangedEvent.REMOVE); 113 } 114 115 public void reset() { 116 fExtensions = new ArrayList (); 117 fExtensionPoints = new ArrayList (); 118 } 119 120 public int getExtensionCount() { 121 return fExtensions.size(); 122 } 123 124 public int getIndexOf(IPluginExtension e) { 125 return fExtensions.indexOf(e); 126 } 127 128 public void swap(IPluginExtension e1, IPluginExtension e2) 129 throws CoreException { 130 ensureModelEditable(); 131 int index1 = fExtensions.indexOf(e1); 132 int index2 = fExtensions.indexOf(e2); 133 if (index1 == -1 || index2 == -1) 134 throwCoreException(PDECoreMessages.AbstractExtensions_extensionsNotFoundException); 135 fExtensions.set(index2, e1); 136 fExtensions.set(index2, e2); 137 firePropertyChanged(this, P_EXTENSION_ORDER, e1, e2); 138 } 139 140 protected void writeChildren( 141 String indent, 142 String tag, 143 Object [] children, 144 PrintWriter writer) { 145 writer.println(indent + "<" + tag + ">"); for (int i = 0; i < children.length; i++) { 147 IPluginObject obj = (IPluginObject) children[i]; 148 obj.write(indent + " ", writer); } 150 writer.println(indent + "</" + tag + ">"); } 152 153 protected boolean hasRequiredAttributes(){ 154 for (int i = 0; i < fExtensions.size(); i++) { 156 IPluginExtension extension = (IPluginExtension)fExtensions.get(i); 157 if (!extension.isValid()) return false; 158 } 159 for (int i = 0; i < fExtensionPoints.size(); i++) { 161 IPluginExtensionPoint expoint = (IPluginExtensionPoint)fExtensionPoints.get(i); 162 if (!expoint.isValid()) return false; 163 } 164 return true; 165 } 166 167 public String getSchemaVersion() { 168 return fSchemaVersion; 169 } 170 171 public void setSchemaVersion(String schemaVersion) throws CoreException { 172 ensureModelEditable(); 173 String oldValue = fSchemaVersion; 174 fSchemaVersion = schemaVersion; 175 firePropertyChanged(IPluginBase.P_SCHEMA_VERSION, oldValue, schemaVersion); 176 } 177 178 179 } 180 | Popular Tags |