1 11 package org.eclipse.pde.internal.core.plugin; 12 13 import java.io.PrintWriter ; 14 import java.io.Serializable ; 15 import java.util.Vector ; 16 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.core.runtime.PlatformObject; 20 import org.eclipse.core.runtime.Status; 21 import org.eclipse.pde.core.IModel; 22 import org.eclipse.pde.core.IModelChangeProvider; 23 import org.eclipse.pde.core.IModelChangedEvent; 24 import org.eclipse.pde.core.ISourceObject; 25 import org.eclipse.pde.core.ModelChangedEvent; 26 import org.eclipse.pde.core.plugin.IPluginBase; 27 import org.eclipse.pde.core.plugin.IPluginModelBase; 28 import org.eclipse.pde.core.plugin.IPluginObject; 29 import org.eclipse.pde.core.plugin.ISharedPluginModel; 30 import org.eclipse.pde.internal.core.PDECore; 31 import org.eclipse.pde.internal.core.PDECoreMessages; 32 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelProvider; 33 import org.eclipse.pde.internal.core.util.PDEXMLHelper; 34 import org.w3c.dom.Comment ; 35 import org.w3c.dom.Node ; 36 37 public abstract class PluginObject extends PlatformObject implements 38 IPluginObject, ISourceObject, Serializable , IWritableDelimiter { 39 40 protected String fName; 41 42 private transient String fTranslatedName; 43 private transient IPluginObject fParent; 44 private transient ISharedPluginModel fModel; 45 private transient boolean fInTheModel; 46 47 protected int fStartLine = 1; 48 49 public PluginObject() { 50 } 51 public boolean isValid() { 52 return true; 53 } 54 protected void ensureModelEditable() throws CoreException { 55 if (!fModel.isEditable()) { 56 throwCoreException(PDECoreMessages.PluginObject_readOnlyChange); 57 } 58 } 59 60 public void setInTheModel(boolean value) { 61 fInTheModel = value; 62 } 63 64 public boolean isInTheModel() { 65 return fInTheModel; 66 } 67 68 protected void firePropertyChanged( 69 String property, 70 Object oldValue, 71 Object newValue) { 72 firePropertyChanged(this, property, oldValue, newValue); 73 } 74 protected void firePropertyChanged( 75 IPluginObject object, 76 String property, 77 Object oldValue, 78 Object newValue) { 79 if (fModel.isEditable()) { 80 IModelChangeProvider provider = fModel; 81 provider.fireModelObjectChanged( 82 object, 83 property, 84 oldValue, 85 newValue); 86 } 87 } 88 protected void fireStructureChanged(IPluginObject child, int changeType) { 89 IModel model = getModel(); 90 if (model.isEditable() && model instanceof IModelChangeProvider) { 91 IModelChangedEvent e = 92 new ModelChangedEvent((IModelChangeProvider)model, changeType, new Object [] { child }, null); 93 fireModelChanged(e); 94 } 95 } 96 protected void fireStructureChanged(IPluginObject[] children, int changeType) { 97 IModel model = getModel(); 98 if (model.isEditable() && model instanceof IModelChangeProvider) { 99 IModelChangedEvent e = 100 new ModelChangedEvent((IModelChangeProvider)model, changeType, children, null); 101 fireModelChanged(e); 102 } 103 } 104 protected void fireModelChanged(IModelChangedEvent e) { 105 IModel model = getModel(); 106 if (model.isEditable() && model instanceof IModelChangeProvider) { 107 IModelChangeProvider provider = (IModelChangeProvider) model; 108 provider.fireModelChanged(e); 109 } 110 } 111 public ISharedPluginModel getModel() { 112 return fModel; 113 } 114 115 public IPluginModelBase getPluginModel() { 116 if (fModel instanceof IBundlePluginModelProvider) 117 return ((IBundlePluginModelProvider)fModel).getBundlePluginModel(); 118 119 return fModel instanceof IPluginModelBase? (IPluginModelBase)fModel : null; 120 } 121 122 public String getName() { 123 return fName; 124 } 125 126 public String getTranslatedName() { 127 if (fTranslatedName != null && !fModel.isEditable()) 128 return fTranslatedName; 129 if (fTranslatedName == null && fName != null && fModel != null) { 130 fTranslatedName = fModel.getResourceString(fName); 131 } 132 return fTranslatedName; 133 } 134 135 String getNodeAttribute(Node node, String name) { 136 Node attribute = node.getAttributes().getNamedItem(name); 137 if (attribute != null) 138 return attribute.getNodeValue(); 139 return null; 140 } 141 public IPluginObject getParent() { 142 return fParent; 143 } 144 public IPluginBase getPluginBase() { 145 IPluginModelBase pluginModel = getPluginModel(); 146 return pluginModel != null ? pluginModel.getPluginBase() : null; 147 } 148 public String getResourceString(String key) { 149 return fModel.getResourceString(key); 150 } 151 static boolean isNotEmpty(String text) { 152 for (int i = 0; i < text.length(); i++) { 153 if (Character.isWhitespace(text.charAt(i)) == false) 154 return true; 155 } 156 return false; 157 } 158 159 public void restoreProperty(String name, Object oldValue, Object newValue) 160 throws CoreException { 161 if (name.equals(P_NAME)) { 162 setName(newValue != null ? newValue.toString() : null); 163 } 164 } 165 166 public void setModel(ISharedPluginModel model) { 167 this.fModel = model; 168 fTranslatedName = null; 169 } 170 public void setName(String name) throws CoreException { 171 ensureModelEditable(); 172 String oldValue = this.fName; 173 this.fName = name; 174 firePropertyChanged(P_NAME, oldValue, name); 175 } 176 public void setParent(IPluginObject parent) { 177 this.fParent = parent; 178 } 179 protected void throwCoreException(String message) throws CoreException { 180 Status status = 181 new Status( 182 IStatus.ERROR, 183 PDECore.PLUGIN_ID, 184 IStatus.OK, 185 message, 186 null); 187 CoreException ce = new CoreException(status); 188 ce.fillInStackTrace(); 189 throw ce; 190 } 191 public String toString() { 192 if (fName != null) 193 return fName; 194 return super.toString(); 195 } 196 197 public Vector addComments(Node node, Vector result) { 198 for (Node prev = node.getPreviousSibling(); 199 prev != null; 200 prev = prev.getPreviousSibling()) { 201 if (prev.getNodeType() == Node.TEXT_NODE) 202 continue; 203 if (prev instanceof Comment ) { 204 String comment = prev.getNodeValue(); 205 if (result == null) 206 result = new Vector (); 207 result.add(0,comment); 208 } else 209 break; 210 } 211 return result; 212 } 213 214 void writeComments(PrintWriter writer, Vector source) { 215 if (source == null) 216 return; 217 for (int i = 0; i < source.size(); i++) { 218 String comment = (String ) source.elementAt(i); 219 writer.println("<!--" + comment + "-->"); } 221 } 222 223 protected boolean stringEqualWithNull(String a, String b) { 224 return a == null && b == null || a != null && b != null && a.equals(b); 225 } 226 227 public String getWritableString(String source) { 228 return PDEXMLHelper.getWritableString(source); 229 } 230 231 public int getStartLine() { 232 return fStartLine; 233 } 234 235 public int getStopLine() { 236 return fStartLine; 237 } 238 239 public Object getAdapter(Class adapter) { 240 if(adapter.equals(IPluginModelBase.class)) { 241 return getPluginModel(); 242 } 243 return super.getAdapter(adapter); 244 } 245 246 250 public void reconnect(ISharedPluginModel model, IPluginObject parent) { 251 fInTheModel = false; 253 fModel = model; 255 fParent = parent; 257 fTranslatedName = null; 259 } 260 261 264 public void writeDelimeter(PrintWriter writer) { 265 } 268 269 } 270 | Popular Tags |