KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > text > plugin > PluginDocumentNode


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.text.plugin;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.TreeMap JavaDoc;
17
18 import org.eclipse.pde.core.plugin.ISharedPluginModel;
19 import org.eclipse.pde.internal.core.ischema.ISchema;
20 import org.eclipse.pde.internal.core.text.IDocumentAttribute;
21 import org.eclipse.pde.internal.core.text.IDocumentNode;
22 import org.eclipse.pde.internal.core.text.IDocumentTextNode;
23
24 public abstract class PluginDocumentNode implements IDocumentNode {
25     
26     private transient IDocumentNode fParent;
27     private transient boolean fIsErrorNode;
28     private transient int fLength = -1;
29     private transient int fOffset = -1;
30     private transient IDocumentNode fPreviousSibling;
31     private transient int fIndent = 0;
32     
33     private ArrayList JavaDoc fChildren = new ArrayList JavaDoc();
34     protected Map JavaDoc fAttributes = new TreeMap JavaDoc();
35     private String JavaDoc fTag;
36     protected IDocumentTextNode fTextNode;
37
38     /* (non-Javadoc)
39      * @see org.eclipse.pde.internal.ui.neweditor.model.IDocumentNode#getChildNodes()
40      */

41     public IDocumentNode[] getChildNodes() {
42         return (IDocumentNode[]) fChildren.toArray(new IDocumentNode[fChildren.size()]);
43     }
44     
45     /* (non-Javadoc)
46      * @see org.eclipse.pde.internal.ui.model.IDocumentNode#indexOf(org.eclipse.pde.internal.ui.model.IDocumentNode)
47      */

48     public int indexOf(IDocumentNode child) {
49         return fChildren.indexOf(child);
50     }
51     
52     /* (non-Javadoc)
53      * @see org.eclipse.pde.internal.ui.model.IDocumentNode#getChildAt(int)
54      */

55     public IDocumentNode getChildAt(int index) {
56         if (index < fChildren.size())
57             return (IDocumentNode)fChildren.get(index);
58         return null;
59     }
60     /* (non-Javadoc)
61      * @see org.eclipse.pde.internal.ui.neweditor.model.IDocumentNode#getParentNode()
62      */

63     public IDocumentNode getParentNode() {
64         return fParent;
65     }
66     /* (non-Javadoc)
67      * @see org.eclipse.pde.internal.ui.neweditor.model.IDocumentNode#setParentNode(org.eclipse.pde.internal.ui.neweditor.model.IDocumentNode)
68      */

69     public void setParentNode(IDocumentNode node) {
70         fParent = node;
71     }
72     /* (non-Javadoc)
73      * @see org.eclipse.pde.internal.ui.neweditor.model.IDocumentNode#addChildNode(org.eclipse.pde.internal.ui.neweditor.model.IDocumentNode)
74      */

75     public void addChildNode(IDocumentNode child) {
76         addChildNode(child, fChildren.size());
77     }
78     
79     /* (non-Javadoc)
80      * @see org.eclipse.pde.internal.ui.model.IDocumentNode#addChildNode(org.eclipse.pde.internal.ui.model.IDocumentNode, int)
81      */

82     public void addChildNode(IDocumentNode child, int position) {
83         fChildren.add(position, child);
84         if (position > 0 && fChildren.size() > 1)
85             child.setPreviousSibling((IDocumentNode)fChildren.get(position - 1));
86         if (fChildren.size() > 1 && position < fChildren.size() - 1)
87             ((IDocumentNode)fChildren.get(position + 1)).setPreviousSibling(child);
88         child.setParentNode(this);
89     }
90     
91     /* (non-Javadoc)
92      * @see org.eclipse.pde.internal.ui.model.IDocumentNode#removeChildNode(org.eclipse.pde.internal.ui.model.IDocumentNode)
93      */

94     public IDocumentNode removeChildNode(IDocumentNode child) {
95         int index = fChildren.indexOf(child);
96         if (index != -1) {
97             fChildren.remove(child);
98             if (index < fChildren.size()) {
99                 IDocumentNode prevSibling = index == 0 ? null : (IDocumentNode)fChildren.get(index - 1);
100                 ((IDocumentNode)fChildren.get(index)).setPreviousSibling(prevSibling);
101                 return child;
102             }
103         }
104         return null;
105     }
106     /* (non-Javadoc)
107      * @see org.eclipse.pde.internal.ui.neweditor.model.IDocumentNode#isErrorNode()
108      */

109     public boolean isErrorNode() {
110         return fIsErrorNode;
111     }
112     /* (non-Javadoc)
113      * @see org.eclipse.pde.internal.ui.neweditor.model.IDocumentNode#setIsErrorNode(boolean)
114      */

115     public void setIsErrorNode(boolean isErrorNode) {
116         fIsErrorNode = isErrorNode;
117     }
118     /* (non-Javadoc)
119      * @see org.eclipse.pde.internal.ui.model.IDocumentNode#setOffset(int)
120      */

121     public void setOffset(int offset) {
122         fOffset = offset;
123     }
124     /* (non-Javadoc)
125      * @see org.eclipse.pde.internal.ui.model.IDocumentNode#setLength(int)
126      */

127     public void setLength(int length) {
128         fLength = length;
129     }
130     /* (non-Javadoc)
131      * @see org.eclipse.pde.internal.ui.model.IDocumentNode#getOffset()
132      */

133     public int getOffset() {
134         return fOffset;
135     }
136     /* (non-Javadoc)
137      * @see org.eclipse.pde.internal.ui.model.IDocumentNode#getLength()
138      */

139     public int getLength() {
140         return fLength;
141     }
142     /* (non-Javadoc)
143      * @see org.eclipse.pde.internal.ui.model.IDocumentNode#setAttribute(org.eclipse.pde.internal.ui.model.IDocumentAttribute)
144      */

145     public void setXMLAttribute(IDocumentAttribute attribute) {
146         fAttributes.put(attribute.getAttributeName(), attribute);
147     }
148     /* (non-Javadoc)
149      * @see org.eclipse.pde.internal.ui.model.IDocumentNode#getXMLAttributeValue(java.lang.String)
150      */

151     public String JavaDoc getXMLAttributeValue(String JavaDoc name) {
152         PluginAttribute attr = (PluginAttribute)fAttributes.get(name);
153         return attr == null ? null : attr.getValue();
154     }
155     
156     /* (non-Javadoc)
157      * @see org.eclipse.pde.internal.ui.model.IDocumentNode#setXMLTagName(java.lang.String)
158      */

159     public void setXMLTagName(String JavaDoc tag) {
160         fTag = tag;
161     }
162     
163     /* (non-Javadoc)
164      * @see org.eclipse.pde.internal.ui.model.IDocumentNode#getXMLTagName()
165      */

166     public String JavaDoc getXMLTagName() {
167         return fTag;
168     }
169     
170     /* (non-Javadoc)
171      * @see org.eclipse.pde.internal.ui.model.IDocumentNode#getDocumentAttribute(java.lang.String)
172      */

173     public IDocumentAttribute getDocumentAttribute(String JavaDoc name) {
174         return (IDocumentAttribute)fAttributes.get(name);
175     }
176     
177     /* (non-Javadoc)
178      * @see org.eclipse.pde.internal.ui.model.IDocumentNode#getLineIndent()
179      */

180     public int getLineIndent() {
181         return fIndent;
182     }
183     
184     /* (non-Javadoc)
185      * @see org.eclipse.pde.internal.ui.model.IDocumentNode#setLineIndent(int)
186      */

187     public void setLineIndent(int indent) {
188         fIndent = indent;
189     }
190     
191     /* (non-Javadoc)
192      * @see org.eclipse.pde.internal.ui.model.IDocumentNode#getAttributes()
193      */

194     public IDocumentAttribute[] getNodeAttributes() {
195         ArrayList JavaDoc list = new ArrayList JavaDoc();
196         Iterator JavaDoc iter = fAttributes.values().iterator();
197         while (iter.hasNext())
198             list.add(iter.next());
199         return (IDocumentAttribute[])list.toArray(new IDocumentAttribute[list.size()]);
200     }
201     
202     /* (non-Javadoc)
203      * @see org.eclipse.pde.internal.ui.model.IDocumentNode#getPreviousSibling()
204      */

205     public IDocumentNode getPreviousSibling() {
206         return fPreviousSibling;
207     }
208     
209     /* (non-Javadoc)
210      * @see org.eclipse.pde.internal.ui.model.IDocumentNode#setPreviousSibling(org.eclipse.pde.internal.ui.model.IDocumentNode)
211      */

212     public void setPreviousSibling(IDocumentNode sibling) {
213         fPreviousSibling = sibling;
214     }
215     
216     protected String JavaDoc getIndent() {
217         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
218         for (int i = 0; i < fIndent; i++) {
219             buffer.append(" "); //$NON-NLS-1$
220
}
221         return buffer.toString();
222     }
223     
224     /* (non-Javadoc)
225      * @see org.eclipse.pde.internal.ui.model.IDocumentNode#swap(org.eclipse.pde.internal.ui.model.IDocumentNode, org.eclipse.pde.internal.ui.model.IDocumentNode)
226      */

227     public void swap(IDocumentNode child1, IDocumentNode child2) {
228         int index1 = fChildren.indexOf(child1);
229         int index2 = fChildren.indexOf(child2);
230         
231         fChildren.set(index1, child2);
232         fChildren.set(index2, child1);
233         
234         child1.setPreviousSibling(index2 == 0 ? null : (IDocumentNode)fChildren.get(index2 - 1));
235         child2.setPreviousSibling(index1 == 0 ? null : (IDocumentNode)fChildren.get(index1 - 1));
236         
237         if (index1 < fChildren.size() - 1)
238             ((IDocumentNode)fChildren.get(index1 + 1)).setPreviousSibling(child2);
239         
240         if (index2 < fChildren.size() - 1)
241             ((IDocumentNode)fChildren.get(index2 + 1)).setPreviousSibling(child1);
242     }
243     
244     /* (non-Javadoc)
245      * @see org.eclipse.pde.internal.ui.model.IDocumentNode#addTextNode(org.eclipse.pde.internal.ui.model.IDocumentTextNode)
246      */

247     public void addTextNode(IDocumentTextNode textNode) {
248         fTextNode = textNode;
249     }
250     
251     /* (non-Javadoc)
252      * @see org.eclipse.pde.internal.ui.model.IDocumentNode#getTextNode()
253      */

254     public IDocumentTextNode getTextNode() {
255         return fTextNode;
256     }
257     /* (non-Javadoc)
258      * @see org.eclipse.pde.internal.ui.model.IDocumentNode#removeTextNode()
259      */

260     public void removeTextNode() {
261         fTextNode = null;
262     }
263     
264     /* (non-Javadoc)
265      * @see org.eclipse.pde.internal.ui.model.IDocumentNode#removeDocumentAttribute(org.eclipse.pde.internal.ui.model.IDocumentAttribute)
266      */

267     public void removeDocumentAttribute(IDocumentAttribute attr) {
268         fAttributes.remove(attr.getAttributeName());
269     }
270     
271     /* (non-Javadoc)
272      * @see org.eclipse.pde.internal.core.text.IDocumentNode#reconnectRoot(org.eclipse.pde.core.plugin.ISharedPluginModel)
273      */

274     public void reconnect(ISharedPluginModel model, ISchema schema, IDocumentNode parent) {
275         // TODO: MP: CCP: Remove all unnecessary reconnected attributes: parent, sibling, model, element schema (investigate)
276
// Reconnect XML document characteristics
277
reconnectDocument();
278         // Reconnect parent
279
// This may not be necessary. When this node is added to the parent,
280
// the parent takes care of this
281
reconnectParent(parent);
282         // Reconnect previous sibling
283
// This may not be necessary. When this node is added to the parent,
284
// the parent takes care of this
285
reconnectPreviousSibling(parent);
286         // Reconnect text node
287
reconnectText();
288         // Reconnect attribute nodes
289
reconnectAttributes(model, schema);
290         // Reconnect children nodes
291
reconnectChildren(model, schema);
292     }
293     
294     /**
295      * @param model
296      * @param schema
297      */

298     private void reconnectAttributes(ISharedPluginModel model, ISchema schema) {
299         // Get all attributes
300
Iterator JavaDoc keys = fAttributes.keySet().iterator();
301         // Fill in appropriate transient field values for all attributes
302
while (keys.hasNext()) {
303             String JavaDoc key = (String JavaDoc)keys.next();
304             IDocumentAttribute attribute = (IDocumentAttribute)fAttributes.get(key);
305             attribute.reconnect(model, schema, this);
306         }
307     }
308     
309     /**
310      * @param model
311      * @param schema
312      */

313     private void reconnectChildren(ISharedPluginModel model, ISchema schema) {
314         // Fill in appropriate transient field values
315
for (int i = 0; i < fChildren.size(); i++) {
316             IDocumentNode child = (IDocumentNode)fChildren.get(i);
317             // Reconnect child
318
child.reconnect(model, schema, this);
319         }
320     }
321     
322     /**
323      *
324      */

325     private void reconnectDocument() {
326         // Transient field: Indent
327
fIndent = 0;
328         // Transient field: Error Node
329
fIsErrorNode = false;
330         // Transient field: Length
331
fLength = -1;
332         // Transient field: Offset
333
fOffset = -1;
334     }
335     
336     /**
337      * @param parent
338      */

339     private void reconnectParent(IDocumentNode parent) {
340         // Transient field: Parent
341
fParent = parent;
342     }
343     
344     /**
345      * @param parent
346      */

347     private void reconnectPreviousSibling(IDocumentNode parent) {
348         // Transient field: Previous Sibling
349
int childCount = parent.getChildCount();
350         if (childCount < 1) {
351             fPreviousSibling = null;
352         } else {
353             // The last item is the previous sibling; since, we have not added
354
// overselves to the end of the parents children yet
355
fPreviousSibling = (IDocumentNode)parent.getChildAt(childCount - 1);
356         }
357     }
358     
359     /**
360      *
361      */

362     private void reconnectText() {
363         // Transient field: Text Node
364
if (fTextNode != null) {
365             fTextNode.reconnect(this);
366         }
367     }
368     
369     /* (non-Javadoc)
370      * @see org.eclipse.pde.internal.core.text.IDocumentNode#getChildCount()
371      */

372     public int getChildCount() {
373         return fChildren.size();
374     }
375     
376 }
377
Popular Tags