View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics
3    * and Informatics, Comenius University, Bratislava. This file is protected by
4    * the Mozilla Public License version 1.1 (the License); you may not use this
5    * file except in compliance with the License. You may obtain a copy of the
6    * License at http://euromath2.sourceforge.net/license.html Unless required by
7    * applicable law or agreed to in writing, software distributed under the
8    * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
9    * OF ANY KIND, either express or implied. See the License for the specific
10   * language governing permissions and limitations under the License.
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.uniba.euromath.Const;
29  import sk.uniba.euromath.plugin.views.outline.OutlineConsts;
30  
31  /***
32   * @author TV Created on 21.5.2004
33   * 
34   */
35  public class ElementOutlineItem extends CompoundOutlineItem {
36  
37      private List childElements;
38  
39      private int attributeCount;
40  
41      private int textNodeCount;
42  
43      public ElementOutlineItem(Element element) {
44          super(element);
45          setChildElements(new ArrayList());
46      }
47  
48      @Override
49      protected void analyze() {
50          invalidate();
51          for (Iterator i = getChildren().iterator(); i.hasNext();) {
52              OutlineItem child = (OutlineItem) i.next();
53              Node node = child.getModel();
54              switch (node.getNodeType()) {
55              case Node.ELEMENT_NODE:
56                  getChildElements().add(child);
57                  break;
58              case Node.ATTRIBUTE_NODE:
59                  attributeCount++;
60                  break;
61              case Node.TEXT_NODE:
62                  textNodeCount++;
63                  break;
64              default:
65                  break;
66              }
67          }
68      }
69  
70      /*
71       * (non-Javadoc)
72       * 
73       * @see sk.uniba.euromath.plugin.views.outline.OutlineNode#createPropertySource()
74       */
75      @Override
76      protected IPropertySource createPropertySource() {
77          return new ElementPropertySource(getElement());
78      }
79  
80      public Element getElement() {
81          return (Element) getModel();
82      }
83  
84      @Override
85      public String getImageKey() {
86          return OutlineConsts.ELEM_KEY;
87      }
88  
89      protected void invalidate() {
90          if (isAnalyzed()) {
91              setAnalyzed(false);
92              getChildElements().clear();
93              attributeCount = textNodeCount = 0;
94          }
95      }
96  
97      /***
98       * Determines whether the element which is the model of this item is an
99       * empty element in XML terminology. Simple empty element example:
100      * <element/>
101      * 
102      * @return False if element contains other child elements. True otherwise.
103      * 
104      */
105     public boolean isEmpty() {
106         if (!isAnalyzed())
107             analyze();
108 
109         return (getChildElements().size() == 0) && (textNodeCount == 0);
110     }
111 
112     public boolean isMixed() {
113         if (!isAnalyzed())
114             analyze();
115 
116         return (getChildElements().size() >= 1) && (textNodeCount >= 1);
117     }
118 
119     /*
120      * (non-Javadoc)
121      * 
122      * @see java.lang.Object#toString()
123      */
124     @Override
125     public String toString() {
126         Element element = getElement();
127 
128         Attr idAttr = element.getAttributeNode(OutlineConsts.ID_ATTR_EM);
129         if (idAttr == null) {
130             idAttr = element.getAttributeNode(OutlineConsts.ID_ATTR_NOR);
131         }
132 
133         String result = "<" + element.getNodeName();
134         if (idAttr != null)
135             result += " (id: " + idAttr.getNodeValue() + ")";
136 
137         if (isEmpty())
138             result += "/";
139 
140         result += ">";
141 
142         return result;
143     }
144 
145     //
146     //
147     //
148     //
149     //
150 
151     private class ElementPropertySource implements IPropertySource {
152 
153         private Element element;
154 
155         private IPropertyDescriptor[] propDescs;
156 
157         private HashMap origValues;
158 
159         public ElementPropertySource(Element element) {
160             super();
161             assert (element != null);
162             this.element = element;
163         }
164 
165         public Object getEditableValue() {
166             return new String();
167         }
168 
169         public IPropertyDescriptor[] getPropertyDescriptors() {
170 
171             propDescs = (IPropertyDescriptor[]) prepareDescriptors(getElement())
172                     .toArray();
173             return propDescs;
174         }
175 
176         public Object getPropertyValue(Object arg0) {
177             return element.getAttribute((String) arg0);
178         }
179 
180         public boolean isPropertySet(Object arg0) {
181             if (element.getAttributeNode((String) arg0) != null) {
182                 return true;
183             }
184             return false;
185         }
186 
187         protected List prepareDescriptors(Element e) {
188             List result = new Vector();
189 
190             if (e != null) {
191                 NamedNodeMap attrs = element.getAttributes();
192                 origValues = new HashMap();
193                 for (int i = 0; i < attrs.getLength(); i++) {
194                     Node n = attrs.item(i);
195                     if (n.getNamespaceURI() != Const.EM_URI) {
196                         result.add(new TextPropertyDescriptor(n.getNodeName(),
197                                 n.getNodeName()));
198                         origValues.put(n.getNodeName(), n.getNodeValue());
199                     }
200                 }
201             }
202             return result;
203         }
204 
205         public void resetPropertyValue(Object arg0) {
206             assert (arg0 != null);
207             Node n = element.getAttributeNode(arg0.toString());
208             if (n != null) {
209                 n.setNodeValue((String) origValues.get(arg0));
210             }
211         }
212 
213         public void setPropertyValue(Object arg0, Object arg1) {
214             Node n = element.getAttributeNode((String) arg0);
215             if (n != null) {
216                 n.setNodeValue((String) arg1);
217             }
218         }
219 
220     }
221 
222     /***
223      * @return Returns the childElements.
224      */
225     protected List getChildElements() {
226         return this.childElements;
227     }
228 
229     /***
230      * @param childElements The childElements to set.
231      */
232     protected void setChildElements(List childElements) {
233         this.childElements = childElements;
234     }
235 }