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.core.runtime.IStatus; 17 import org.eclipse.core.runtime.PlatformObject; 18 import org.eclipse.core.runtime.Status; 19 import org.eclipse.pde.core.IModelChangeProvider; 20 import org.eclipse.pde.core.ModelChangedEvent; 21 import org.eclipse.pde.internal.core.PDECore; 22 import org.eclipse.pde.internal.core.PDECoreMessages; 23 import org.eclipse.pde.internal.core.ifeature.IFeature; 24 import org.eclipse.pde.internal.core.ifeature.IFeatureModel; 25 import org.eclipse.pde.internal.core.ifeature.IFeatureObject; 26 import org.eclipse.pde.internal.core.util.PDEXMLHelper; 27 import org.w3c.dom.Node ; 28 29 public abstract class FeatureObject 30 extends PlatformObject 31 implements IFeatureObject { 32 transient IFeatureModel model; 33 transient IFeatureObject parent; 34 protected String label; 35 boolean inTheModel; 36 37 void setInTheModel(boolean value) { 38 inTheModel = value; 39 } 40 41 public boolean isInTheModel() { 42 return inTheModel; 43 } 44 45 protected void ensureModelEditable() throws CoreException { 46 if (!model.isEditable()) { 47 throwCoreException(PDECoreMessages.FeatureObject_readOnlyChange); 48 } 49 } 50 protected void firePropertyChanged( 51 String property, 52 Object oldValue, 53 Object newValue) { 54 firePropertyChanged(this, property, oldValue, newValue); 55 } 56 protected void firePropertyChanged( 57 IFeatureObject object, 58 String property, 59 Object oldValue, 60 Object newValue) { 61 if (model.isEditable()) { 62 IModelChangeProvider provider = model; 63 provider.fireModelObjectChanged(object, property, oldValue, newValue); 64 } 65 } 66 protected void fireStructureChanged(IFeatureObject child, int changeType) { 67 fireStructureChanged(new IFeatureObject[] { child }, changeType); 68 } 69 protected void fireStructureChanged( 70 IFeatureObject[] children, 71 int changeType) { 72 IFeatureModel model = getModel(); 73 if (model.isEditable()) { 74 IModelChangeProvider provider = model; 75 provider.fireModelChanged(new ModelChangedEvent(provider, changeType, children, null)); 76 } 77 } 78 public IFeature getFeature() { 79 return model.getFeature(); 80 } 81 public String getLabel() { 82 return label; 83 } 84 85 public String getTranslatableLabel() { 86 if (label == null) 87 return ""; return model.getResourceString(label); 89 } 90 public IFeatureModel getModel() { 91 return model; 92 } 93 String getNodeAttribute(Node node, String name) { 94 Node attribute = node.getAttributes().getNamedItem(name); 95 if (attribute != null) 96 return attribute.getNodeValue(); 97 return null; 98 } 99 100 int getIntegerAttribute(Node node, String name) { 101 String value = getNodeAttribute(node, name); 102 if (value != null) { 103 try { 104 return Integer.parseInt(value); 105 } catch (NumberFormatException e) { 106 } 107 } 108 return 0; 109 } 110 111 boolean getBooleanAttribute(Node node, String name) { 112 String value = getNodeAttribute(node, name); 113 if (value != null) { 114 return value.equalsIgnoreCase("true"); } 116 return false; 117 } 118 119 protected String getNormalizedText(String source) { 120 String result = source.replace('\t', ' '); 121 result = result.trim(); 122 123 return result; 124 } 125 126 public IFeatureObject getParent() { 127 return parent; 128 } 129 130 protected void parse(Node node) { 131 label = getNodeAttribute(node, "label"); } 133 134 protected void reset() { 135 label = null; 136 } 137 138 public void setLabel(String newLabel) throws CoreException { 139 ensureModelEditable(); 140 Object oldValue = this.label; 141 label = newLabel; 142 firePropertyChanged(P_LABEL, oldValue, newLabel); 143 } 144 protected void throwCoreException(String message) throws CoreException { 145 Status status = 146 new Status(IStatus.ERROR, PDECore.PLUGIN_ID, IStatus.OK, message, null); 147 CoreException ce = new CoreException(status); 148 ce.fillInStackTrace(); 149 throw ce; 150 } 151 152 public void restoreProperty(String name, Object oldValue, Object newValue) 153 throws CoreException { 154 if (name.equals(P_LABEL)) { 155 setLabel(newValue != null ? newValue.toString() : null); 156 } 157 } 158 159 public void write(String indent, PrintWriter writer) { 160 } 161 public void setModel(IFeatureModel model) { 162 this.model = model; 163 } 164 165 public void setParent(IFeatureObject parent) { 166 this.parent = parent; 167 } 168 169 protected String getWritableString(String source) { 170 return PDEXMLHelper.getWritableString(source); 171 } 172 } 173 | Popular Tags |