KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > site > SiteObject


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.site;
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.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 JavaDoc;
26 import org.w3c.dom.Node JavaDoc;
27
28 public abstract class SiteObject
29     extends PlatformObject
30     implements ISiteObject {
31     transient ISiteModel model;
32     transient ISiteObject parent;
33     protected String JavaDoc 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 JavaDoc property,
51         Object JavaDoc oldValue,
52         Object JavaDoc newValue) {
53         firePropertyChanged(this, property, oldValue, newValue);
54     }
55     protected void firePropertyChanged(
56         ISiteObject object,
57         String JavaDoc property,
58         Object JavaDoc oldValue,
59         Object JavaDoc 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 JavaDoc getLabel() {
79         return label;
80     }
81
82     public String JavaDoc getTranslatableLabel() {
83         if (label == null)
84             return ""; //$NON-NLS-1$
85
return model.getResourceString(label);
86     }
87     public ISiteModel getModel() {
88         return model;
89     }
90     String JavaDoc getNodeAttribute(Node JavaDoc node, String JavaDoc name) {
91         NamedNodeMap JavaDoc atts = node.getAttributes();
92         Node JavaDoc 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 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 ISiteObject 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 static String JavaDoc getWritableString(String JavaDoc source) {
153         if (source == null)
154             return ""; //$NON-NLS-1$
155
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
156         for (int i = 0; i < source.length(); i++) {
157             char c = source.charAt(i);
158             switch (c) {
159                 case '&' :
160                     buf.append("&amp;"); //$NON-NLS-1$
161
break;
162                 case '<' :
163                     buf.append("&lt;"); //$NON-NLS-1$
164
break;
165                 case '>' :
166                     buf.append("&gt;"); //$NON-NLS-1$
167
break;
168                 case '\'' :
169                     buf.append("&apos;"); //$NON-NLS-1$
170
break;
171                 case '\"' :
172                     buf.append("&quot;"); //$NON-NLS-1$
173
break;
174                 default :
175                     buf.append(c);
176                     break;
177             }
178         }
179         return buf.toString();
180     }
181
182     public void restoreProperty(String JavaDoc name, Object JavaDoc oldValue, Object JavaDoc 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 JavaDoc indent, PrintWriter JavaDoc 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