KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > model > plugin > PluginElementNode


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ui.model.plugin;
12
13 import org.eclipse.core.runtime.*;
14 import org.eclipse.pde.core.plugin.*;
15 import org.eclipse.pde.internal.core.ischema.*;
16 import org.eclipse.pde.internal.ui.model.*;
17
18 public class PluginElementNode extends PluginParentNode
19         implements
20             IPluginElement {
21
22     private static final long serialVersionUID = 1L;
23
24     private transient ISchemaElement elementInfo;
25     /* (non-Javadoc)
26      * @see org.eclipse.pde.core.plugin.IPluginElement#createCopy()
27      */

28     public IPluginElement createCopy() {
29         return null;
30     }
31     /* (non-Javadoc)
32      * @see org.eclipse.pde.core.plugin.IPluginElement#getAttribute(java.lang.String)
33      */

34     public IPluginAttribute getAttribute(String JavaDoc name) {
35         return (IPluginAttribute)fAttributes.get(name);
36     }
37     /* (non-Javadoc)
38      * @see org.eclipse.pde.core.plugin.IPluginElement#getAttributes()
39      */

40     public IPluginAttribute[] getAttributes() {
41         return (IPluginAttribute[])fAttributes.values().toArray(new IPluginAttribute[fAttributes.size()]);
42     }
43     /* (non-Javadoc)
44      * @see org.eclipse.pde.core.plugin.IPluginElement#getAttributeCount()
45      */

46     public int getAttributeCount() {
47         return fAttributes.size();
48     }
49     /* (non-Javadoc)
50      * @see org.eclipse.pde.core.plugin.IPluginElement#getText()
51      */

52     public String JavaDoc getText() {
53         IDocumentTextNode node = getTextNode();
54         return node == null ? "" : node.getText(); //$NON-NLS-1$
55
}
56     /* (non-Javadoc)
57      * @see org.eclipse.pde.core.plugin.IPluginElement#setAttribute(java.lang.String, java.lang.String)
58      */

59     public void setAttribute(String JavaDoc name, String JavaDoc value) throws CoreException {
60         setXMLAttribute(name, value);
61     }
62     /* (non-Javadoc)
63      * @see org.eclipse.pde.core.plugin.IPluginElement#setText(java.lang.String)
64      */

65     public void setText(String JavaDoc text) throws CoreException {
66         IDocumentTextNode node = getTextNode();
67         if (node == null) {
68             node = new DocumentTextNode();
69             node.setEnclosingElement(this);
70             addTextNode(node);
71         }
72         node.setText(text);
73         firePropertyChanged(this, P_TEXT, node, node);
74     }
75     
76     /* (non-Javadoc)
77      * @see org.eclipse.pde.internal.ui.model.plugin.PluginObjectNode#write()
78      */

79     public String JavaDoc write(boolean indent) {
80         String JavaDoc sep = getLineDelimiter();
81         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
82         if (indent)
83             buffer.append(getIndent());
84         
85         IDocumentNode[] children = getChildNodes();
86         String JavaDoc text = getText();
87         if (children.length > 0 || text.length() > 0) {
88             buffer.append(writeShallow(false) + sep);
89             if (text.length() > 0)
90                 buffer.append(getIndent() + " " + text + sep); //$NON-NLS-1$
91
for (int i = 0; i < children.length; i++) {
92                 children[i].setLineIndent(getLineIndent() + 3);
93                 buffer.append(children[i].write(true) + sep);
94             }
95             buffer.append(getIndent() + "</" + getXMLTagName() + ">"); //$NON-NLS-1$ //$NON-NLS-2$
96
} else {
97             buffer.append(writeShallow(true));
98         }
99     
100         return buffer.toString();
101     }
102     
103     /* (non-Javadoc)
104      * @see org.eclipse.pde.internal.ui.model.plugin.PluginObjectNode#writeShallow(boolean)
105      */

106     public String JavaDoc writeShallow(boolean terminate) {
107         String JavaDoc sep = getLineDelimiter();
108         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("<" + getXMLTagName()); //$NON-NLS-1$
109

110         IDocumentAttribute[] attrs = getNodeAttributes();
111         if (attrs.length == 1) {
112             if (attrs[0].getAttributeValue().length() > 0)
113                 buffer.append(" " + attrs[0].write()); //$NON-NLS-1$
114
} else {
115             for (int i = 0; i < attrs.length; i++) {
116                 if (attrs[i].getAttributeValue().length() > 0)
117                     buffer.append(sep + getIndent() + " " + attrs[i].write()); //$NON-NLS-1$
118
}
119         }
120         if (terminate)
121             buffer.append("/"); //$NON-NLS-1$
122
buffer.append(">"); //$NON-NLS-1$
123
return buffer.toString();
124     }
125     
126     /* (non-Javadoc)
127      * @see org.eclipse.pde.core.plugin.IPluginObject#getName()
128      */

129     public String JavaDoc getName() {
130         return getXMLTagName();
131     }
132     
133     /* (non-Javadoc)
134      * @see org.eclipse.pde.core.plugin.IPluginObject#setName(java.lang.String)
135      */

136     public void setName(String JavaDoc name) throws CoreException {
137         setXMLTagName(name);
138     }
139     
140     /* (non-Javadoc)
141      * @see org.eclipse.pde.core.plugin.IPluginElement#getElementInfo()
142      */

143     public Object JavaDoc getElementInfo() {
144         if (elementInfo == null) {
145             IDocumentNode node = getParentNode();
146             for (;;) {
147                 if (node == null || node instanceof IPluginExtension)
148                     break;
149                 node = node.getParentNode();
150             }
151             if (node != null) {
152                 IPluginExtension extension = (IPluginExtension) node;
153                 ISchema schema = (ISchema)extension.getSchema();
154                 if (schema != null) {
155                     elementInfo = schema.findElement(getName());
156                 }
157             }
158         }
159         return elementInfo;
160     }
161     
162 }
163
Popular Tags