1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.plugin.views.outline.items;
13
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.Vector;
19
20 import org.eclipse.ui.views.properties.IPropertyDescriptor;
21 import org.eclipse.ui.views.properties.IPropertySource;
22 import org.eclipse.ui.views.properties.TextPropertyDescriptor;
23 import org.w3c.dom.Attr;
24 import org.w3c.dom.Element;
25 import org.w3c.dom.NamedNodeMap;
26 import org.w3c.dom.Node;
27
28 import sk.baka.ikslibs.DOMUtils;
29 import sk.baka.xml.gene.ExportUtils;
30 import sk.uniba.euromath.plugin.views.outline.OutlineConsts;
31
32 /***
33 * @author TV Created on 21.5.2004
34 *
35 */
36 public class ElementOutlineItem extends CompoundOutlineItem {
37
38 private List childElements;
39
40 private int attributeCount;
41
42 private int textNodeCount;
43
44 public ElementOutlineItem(Element element) {
45 super(element);
46 setChildElements(new ArrayList());
47 }
48
49 @Override
50 protected void analyze() {
51 invalidate();
52 for (Iterator i = getChildren().iterator(); i.hasNext();) {
53 OutlineItem child = (OutlineItem) i.next();
54 Node node = child.getModel();
55 switch (node.getNodeType()) {
56 case Node.ELEMENT_NODE:
57 getChildElements().add(child);
58 break;
59 case Node.ATTRIBUTE_NODE:
60 attributeCount++;
61 break;
62 case Node.TEXT_NODE:
63 textNodeCount++;
64 break;
65 default:
66 break;
67 }
68 }
69 }
70
71
72
73
74
75
76 @Override
77 protected IPropertySource createPropertySource() {
78 return new ElementPropertySource(getElement());
79 }
80
81 public Element getElement() {
82 return (Element) getModel();
83 }
84
85 @Override
86 public String getImageKey() {
87 return OutlineConsts.ELEM_KEY;
88 }
89
90 protected void invalidate() {
91 if (isAnalyzed()) {
92 setAnalyzed(false);
93 getChildElements().clear();
94 attributeCount = textNodeCount = 0;
95 }
96 }
97
98 /***
99 * Determines whether the element which is the model of this item is an
100 * empty element in XML terminology. Simple empty element example:
101 * <element/>
102 *
103 * @return False if element contains other child elements. True otherwise.
104 *
105 */
106 public boolean isEmpty() {
107 if (!isAnalyzed())
108 analyze();
109
110 return (getChildElements().size() == 0) && (textNodeCount == 0);
111 }
112
113 public boolean isMixed() {
114 if (!isAnalyzed())
115 analyze();
116
117 return (getChildElements().size() >= 1) && (textNodeCount >= 1);
118 }
119
120
121
122
123
124
125 @Override
126 public String toString() {
127 Element element = getElement();
128
129 Attr idAttr = element.getAttributeNode(OutlineConsts.ID_ATTR_EM);
130 if (idAttr == null) {
131 idAttr = element.getAttributeNode(OutlineConsts.ID_ATTR_NOR);
132 }
133
134 String result = "<" + element.getNodeName();
135 if (idAttr != null)
136 result += " (id: " + idAttr.getNodeValue() + ")";
137
138 if (isEmpty())
139 result += "/";
140
141 result += ">";
142
143 return result;
144 }
145
146
147
148
149
150
151
152 private class ElementPropertySource implements IPropertySource {
153
154 private Element element;
155
156 private IPropertyDescriptor[] propDescs;
157
158 private HashMap origValues;
159
160 public ElementPropertySource(Element element) {
161 super();
162 assert (element != null);
163 this.element = element;
164 }
165
166 public Object getEditableValue() {
167 return new String();
168 }
169
170 public IPropertyDescriptor[] getPropertyDescriptors() {
171
172 propDescs = (IPropertyDescriptor[]) prepareDescriptors(getElement())
173 .toArray();
174 return propDescs;
175 }
176
177 public Object getPropertyValue(Object arg0) {
178 return element.getAttribute((String) arg0);
179 }
180
181 public boolean isPropertySet(Object arg0) {
182 if (element.getAttributeNode((String) arg0) != null) {
183 return true;
184 }
185 return false;
186 }
187
188 protected List prepareDescriptors(Element e) {
189 List result = new Vector();
190
191 if (e != null) {
192 NamedNodeMap attrs = element.getAttributes();
193 origValues = new HashMap();
194 for (int i = 0; i < attrs.getLength(); i++) {
195 Node n = attrs.item(i);
196 if (!DOMUtils.equalsURI(n.getNamespaceURI(),ExportUtils.GENE_ID_ATTRIBUTE_QNAME.getNamespaceURI())) {
197 result.add(new TextPropertyDescriptor(n.getNodeName(),
198 n.getNodeName()));
199 origValues.put(n.getNodeName(), n.getNodeValue());
200 }
201 }
202 }
203 return result;
204 }
205
206 public void resetPropertyValue(Object arg0) {
207 assert (arg0 != null);
208 Node n = element.getAttributeNode(arg0.toString());
209 if (n != null) {
210 n.setNodeValue((String) origValues.get(arg0));
211 }
212 }
213
214 public void setPropertyValue(Object arg0, Object arg1) {
215 Node n = element.getAttributeNode((String) arg0);
216 if (n != null) {
217 n.setNodeValue((String) arg1);
218 }
219 }
220
221 }
222
223 /***
224 * @return Returns the childElements.
225 */
226 protected List getChildElements() {
227 return this.childElements;
228 }
229
230 /***
231 * @param childElements The childElements to set.
232 */
233 protected void setChildElements(List childElements) {
234 this.childElements = childElements;
235 }
236 }