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.feature;
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.IModelChangeProvider;
019: import org.eclipse.pde.core.ModelChangedEvent;
020: import org.eclipse.pde.internal.core.PDECore;
021: import org.eclipse.pde.internal.core.PDECoreMessages;
022: import org.eclipse.pde.internal.core.ifeature.IFeature;
023: import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
024: import org.eclipse.pde.internal.core.ifeature.IFeatureObject;
025: import org.eclipse.pde.internal.core.util.PDEXMLHelper;
026: import org.w3c.dom.Node;
027:
028: public abstract class FeatureObject extends PlatformObject implements
029: IFeatureObject {
030: transient IFeatureModel model;
031: transient IFeatureObject parent;
032: protected String label;
033: boolean inTheModel;
034:
035: void setInTheModel(boolean value) {
036: inTheModel = value;
037: }
038:
039: public boolean isInTheModel() {
040: return inTheModel;
041: }
042:
043: protected void ensureModelEditable() throws CoreException {
044: if (!model.isEditable()) {
045: throwCoreException(PDECoreMessages.FeatureObject_readOnlyChange);
046: }
047: }
048:
049: protected void firePropertyChanged(String property,
050: Object oldValue, Object newValue) {
051: firePropertyChanged(this , property, oldValue, newValue);
052: }
053:
054: protected void firePropertyChanged(IFeatureObject object,
055: String property, Object oldValue, Object newValue) {
056: if (model.isEditable()) {
057: IModelChangeProvider provider = model;
058: provider.fireModelObjectChanged(object, property, oldValue,
059: newValue);
060: }
061: }
062:
063: protected void fireStructureChanged(IFeatureObject child,
064: int changeType) {
065: fireStructureChanged(new IFeatureObject[] { child }, changeType);
066: }
067:
068: protected void fireStructureChanged(IFeatureObject[] children,
069: int changeType) {
070: IFeatureModel model = getModel();
071: if (model.isEditable()) {
072: IModelChangeProvider provider = model;
073: provider.fireModelChanged(new ModelChangedEvent(provider,
074: changeType, children, null));
075: }
076: }
077:
078: public IFeature getFeature() {
079: return model.getFeature();
080: }
081:
082: public String getLabel() {
083: return label;
084: }
085:
086: public String getTranslatableLabel() {
087: if (label == null)
088: return ""; //$NON-NLS-1$
089: return model.getResourceString(label);
090: }
091:
092: public IFeatureModel getModel() {
093: return model;
094: }
095:
096: String getNodeAttribute(Node node, String name) {
097: Node attribute = node.getAttributes().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 IFeatureObject 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 void restoreProperty(String name, Object oldValue,
158: Object newValue) throws CoreException {
159: if (name.equals(P_LABEL)) {
160: setLabel(newValue != null ? newValue.toString() : null);
161: }
162: }
163:
164: public void write(String indent, PrintWriter writer) {
165: }
166:
167: public void setModel(IFeatureModel model) {
168: this .model = model;
169: }
170:
171: public void setParent(IFeatureObject parent) {
172: this .parent = parent;
173: }
174:
175: protected String getWritableString(String source) {
176: return PDEXMLHelper.getWritableString(source);
177: }
178: }
|