KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > feature > FeatureObject


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.core.feature;
12
13 import java.io.PrintWriter JavaDoc;
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 JavaDoc;
28
29 public abstract class FeatureObject
30     extends PlatformObject
31     implements IFeatureObject {
32     transient IFeatureModel model;
33     transient IFeatureObject parent;
34     protected String JavaDoc 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 JavaDoc property,
52         Object JavaDoc oldValue,
53         Object JavaDoc newValue) {
54         firePropertyChanged(this, property, oldValue, newValue);
55     }
56     protected void firePropertyChanged(
57         IFeatureObject object,
58         String JavaDoc property,
59         Object JavaDoc oldValue,
60         Object JavaDoc 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 JavaDoc getLabel() {
82         return label;
83     }
84
85     public String JavaDoc getTranslatableLabel() {
86         if (label == null)
87             return ""; //$NON-NLS-1$
88
return model.getResourceString(label);
89     }
90     public IFeatureModel getModel() {
91         return model;
92     }
93     String JavaDoc getNodeAttribute(Node JavaDoc node, String JavaDoc name) {
94         Node JavaDoc attribute = node.getAttributes().getNamedItem(name);
95         if (attribute != null)
96             return attribute.getNodeValue();
97         return null;
98     }
99
100     int getIntegerAttribute(Node JavaDoc node, String JavaDoc name) {
101         String JavaDoc value = getNodeAttribute(node, name);
102         if (value != null) {
103             try {
104                 return Integer.parseInt(value);
105             } catch (NumberFormatException JavaDoc e) {
106             }
107         }
108         return 0;
109     }
110     
111     boolean getBooleanAttribute(Node JavaDoc node, String JavaDoc name) {
112         String JavaDoc value = getNodeAttribute(node, name);
113         if (value != null) {
114             return value.equalsIgnoreCase("true"); //$NON-NLS-1$
115
}
116         return false;
117     }
118     
119     protected String JavaDoc getNormalizedText(String JavaDoc source) {
120         String JavaDoc 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 JavaDoc node) {
131         label = getNodeAttribute(node, "label"); //$NON-NLS-1$
132
}
133
134     protected void reset() {
135         label = null;
136     }
137
138     public void setLabel(String JavaDoc newLabel) throws CoreException {
139         ensureModelEditable();
140         Object JavaDoc oldValue = this.label;
141         label = newLabel;
142         firePropertyChanged(P_LABEL, oldValue, newLabel);
143     }
144     protected void throwCoreException(String JavaDoc 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 JavaDoc name, Object JavaDoc oldValue, Object JavaDoc 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 JavaDoc indent, PrintWriter JavaDoc 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 JavaDoc getWritableString(String JavaDoc source) {
170         return PDEXMLHelper.getWritableString(source);
171     }
172 }
173
Popular Tags