001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core.site;
011:
012: import java.io.PrintWriter;
013:
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.core.runtime.IStatus;
016: import org.eclipse.core.runtime.PlatformObject;
017: import org.eclipse.core.runtime.Status;
018: import org.eclipse.pde.core.ModelChangedEvent;
019: import org.eclipse.pde.internal.core.PDECore;
020: import org.eclipse.pde.internal.core.PDECoreMessages;
021: import org.eclipse.pde.internal.core.isite.ISite;
022: import org.eclipse.pde.internal.core.isite.ISiteModel;
023: import org.eclipse.pde.internal.core.isite.ISiteObject;
024: import org.w3c.dom.NamedNodeMap;
025: import org.w3c.dom.Node;
026:
027: public abstract class SiteObject extends PlatformObject implements
028: ISiteObject {
029: transient ISiteModel model;
030: transient ISiteObject parent;
031: protected String label;
032: boolean inTheModel;
033:
034: void setInTheModel(boolean value) {
035: inTheModel = value;
036: }
037:
038: public boolean isInTheModel() {
039: return inTheModel;
040: }
041:
042: protected void ensureModelEditable() throws CoreException {
043: if (!model.isEditable()) {
044: throwCoreException(PDECoreMessages.SiteObject_readOnlyChange);
045: }
046: }
047:
048: protected void firePropertyChanged(String property,
049: Object oldValue, Object newValue) {
050: firePropertyChanged(this , property, oldValue, newValue);
051: }
052:
053: protected void firePropertyChanged(ISiteObject object,
054: String property, Object oldValue, Object newValue) {
055: if (model.isEditable()) {
056: model.fireModelObjectChanged(object, property, oldValue,
057: newValue);
058: }
059: }
060:
061: protected void fireStructureChanged(ISiteObject child,
062: int changeType) {
063: fireStructureChanged(new ISiteObject[] { child }, changeType);
064: }
065:
066: protected void fireStructureChanged(ISiteObject[] children,
067: int changeType) {
068: ISiteModel model = getModel();
069: if (model.isEditable()) {
070: model.fireModelChanged(new ModelChangedEvent(model,
071: changeType, children, null));
072: }
073: }
074:
075: public ISite getSite() {
076: return model.getSite();
077: }
078:
079: public String getLabel() {
080: return label;
081: }
082:
083: public String getTranslatableLabel() {
084: if (label == null)
085: return ""; //$NON-NLS-1$
086: return model.getResourceString(label);
087: }
088:
089: public ISiteModel getModel() {
090: return model;
091: }
092:
093: String getNodeAttribute(Node node, String name) {
094: NamedNodeMap atts = node.getAttributes();
095: Node attribute = null;
096: if (atts != null)
097: attribute = atts.getNamedItem(name);
098: if (attribute != null)
099: return attribute.getNodeValue();
100: return null;
101: }
102:
103: int getIntegerAttribute(Node node, String name) {
104: String value = getNodeAttribute(node, name);
105: if (value != null) {
106: try {
107: return Integer.parseInt(value);
108: } catch (NumberFormatException e) {
109: }
110: }
111: return 0;
112: }
113:
114: boolean getBooleanAttribute(Node node, String name) {
115: String value = getNodeAttribute(node, name);
116: if (value != null) {
117: return value.equalsIgnoreCase("true"); //$NON-NLS-1$
118: }
119: return false;
120: }
121:
122: protected String getNormalizedText(String source) {
123: String result = source.replace('\t', ' ');
124: result = result.trim();
125:
126: return result;
127: }
128:
129: public ISiteObject getParent() {
130: return parent;
131: }
132:
133: protected void parse(Node node) {
134: label = getNodeAttribute(node, "label"); //$NON-NLS-1$
135: }
136:
137: protected void reset() {
138: label = null;
139: }
140:
141: public void setLabel(String newLabel) throws CoreException {
142: ensureModelEditable();
143: Object oldValue = this .label;
144: label = newLabel;
145: firePropertyChanged(P_LABEL, oldValue, newLabel);
146: }
147:
148: protected void throwCoreException(String message)
149: throws CoreException {
150: Status status = new Status(IStatus.ERROR, PDECore.PLUGIN_ID,
151: IStatus.OK, message, null);
152: CoreException ce = new CoreException(status);
153: ce.fillInStackTrace();
154: throw ce;
155: }
156:
157: public static String getWritableString(String source) {
158: if (source == null)
159: return ""; //$NON-NLS-1$
160: StringBuffer buf = new StringBuffer();
161: for (int i = 0; i < source.length(); i++) {
162: char c = source.charAt(i);
163: switch (c) {
164: case '&':
165: buf.append("&"); //$NON-NLS-1$
166: break;
167: case '<':
168: buf.append("<"); //$NON-NLS-1$
169: break;
170: case '>':
171: buf.append(">"); //$NON-NLS-1$
172: break;
173: case '\'':
174: buf.append("'"); //$NON-NLS-1$
175: break;
176: case '\"':
177: buf.append("""); //$NON-NLS-1$
178: break;
179: default:
180: buf.append(c);
181: break;
182: }
183: }
184: return buf.toString();
185: }
186:
187: public void restoreProperty(String name, Object oldValue,
188: Object newValue) throws CoreException {
189: if (name.equals(P_LABEL)) {
190: setLabel(newValue != null ? newValue.toString() : null);
191: }
192: }
193:
194: public void write(String indent, PrintWriter writer) {
195: }
196:
197: public void setModel(ISiteModel model) {
198: this .model = model;
199: }
200:
201: public void setParent(ISiteObject parent) {
202: this.parent = parent;
203: }
204: }
|