1 11 package org.eclipse.pde.internal.core.site; 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.ModelChangedEvent; 20 import org.eclipse.pde.internal.core.PDECore; 21 import org.eclipse.pde.internal.core.PDECoreMessages; 22 import org.eclipse.pde.internal.core.isite.ISite; 23 import org.eclipse.pde.internal.core.isite.ISiteModel; 24 import org.eclipse.pde.internal.core.isite.ISiteObject; 25 import org.w3c.dom.NamedNodeMap ; 26 import org.w3c.dom.Node ; 27 28 public abstract class SiteObject 29 extends PlatformObject 30 implements ISiteObject { 31 transient ISiteModel model; 32 transient ISiteObject parent; 33 protected String label; 34 boolean inTheModel; 35 36 void setInTheModel(boolean value) { 37 inTheModel = value; 38 } 39 40 public boolean isInTheModel() { 41 return inTheModel; 42 } 43 44 protected void ensureModelEditable() throws CoreException { 45 if (!model.isEditable()) { 46 throwCoreException(PDECoreMessages.SiteObject_readOnlyChange); 47 } 48 } 49 protected void firePropertyChanged( 50 String property, 51 Object oldValue, 52 Object newValue) { 53 firePropertyChanged(this, property, oldValue, newValue); 54 } 55 protected void firePropertyChanged( 56 ISiteObject object, 57 String property, 58 Object oldValue, 59 Object newValue) { 60 if (model.isEditable()) { 61 model.fireModelObjectChanged(object, property, oldValue, newValue); 62 } 63 } 64 protected void fireStructureChanged(ISiteObject child, int changeType) { 65 fireStructureChanged(new ISiteObject[] { child }, changeType); 66 } 67 protected void fireStructureChanged( 68 ISiteObject[] children, 69 int changeType) { 70 ISiteModel model = getModel(); 71 if (model.isEditable()) { 72 model.fireModelChanged(new ModelChangedEvent(model, changeType, children, null)); 73 } 74 } 75 public ISite getSite() { 76 return model.getSite(); 77 } 78 public String getLabel() { 79 return label; 80 } 81 82 public String getTranslatableLabel() { 83 if (label == null) 84 return ""; return model.getResourceString(label); 86 } 87 public ISiteModel getModel() { 88 return model; 89 } 90 String getNodeAttribute(Node node, String name) { 91 NamedNodeMap atts = node.getAttributes(); 92 Node attribute = null; 93 if (atts != null) 94 attribute = atts.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 ISiteObject 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 static String getWritableString(String source) { 153 if (source == null) 154 return ""; StringBuffer buf = new StringBuffer (); 156 for (int i = 0; i < source.length(); i++) { 157 char c = source.charAt(i); 158 switch (c) { 159 case '&' : 160 buf.append("&"); break; 162 case '<' : 163 buf.append("<"); break; 165 case '>' : 166 buf.append(">"); break; 168 case '\'' : 169 buf.append("'"); break; 171 case '\"' : 172 buf.append("""); break; 174 default : 175 buf.append(c); 176 break; 177 } 178 } 179 return buf.toString(); 180 } 181 182 public void restoreProperty(String name, Object oldValue, Object newValue) 183 throws CoreException { 184 if (name.equals(P_LABEL)) { 185 setLabel(newValue != null ? newValue.toString() : null); 186 } 187 } 188 189 public void write(String indent, PrintWriter writer) { 190 } 191 public void setModel(ISiteModel model) { 192 this.model = model; 193 } 194 195 public void setParent(ISiteObject parent) { 196 this.parent = parent; 197 } 198 } 199 | Popular Tags |