View Javadoc

1   /**
2    * This file Copyright (c) 2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.templating.editor.client.dom;
35  
36  import java.util.LinkedList;
37  import java.util.List;
38  
39  import com.google.gwt.dom.client.Element;
40  
41  /**
42  * MgnlElement Constructor.
43  *
44  */
45  public class MgnlElement {
46  
47      private CMSComment comment;
48      private MgnlElement parent;
49      private boolean isArea = false;
50      private boolean isComponent = false;
51  
52      private Element firstElement;
53      private Element lastElement;
54  
55      private LinkedList<MgnlElement> children = new LinkedList<MgnlElement>();
56      private Element componentElement;
57      private Element areaElement;
58      private Element editElement;
59      private CMSComment endComment;
60  
61      private static final String MARKER_AREA = "cms:area";
62      private static final String MARKER_COMPONENT = "cms:component";
63  
64  /**
65   * @throws IllegalArgumentException if comments tagname is not a defined marker.
66   */
67      public MgnlElement(CMSComment comment, MgnlElement parent) throws IllegalArgumentException {
68  
69          if (!isMgnlElement(comment.getTagName())) {
70              throw new IllegalArgumentException("The tagname must be one of the defined marker Strings.");
71          }
72          this.comment = comment;
73          this.parent = parent;
74      }
75      public boolean isMgnlElement(String tagName) {
76          if (tagName.equals(MARKER_AREA)) {
77              this.isArea = true;
78              return true;
79          }
80          else if (tagName.equals(MARKER_COMPONENT)) {
81              this.isComponent = true;
82              return true;
83          }
84  
85          return false;
86      }
87  
88      public boolean isArea() {
89          return isArea;
90      }
91  
92      public boolean isComponent() {
93          return isComponent;
94      }
95  
96      public MgnlElement getParent() {
97          return parent;
98      }
99  
100     public void setParent(MgnlElement parent) {
101         this.parent = parent;
102     }
103 
104     public LinkedList<MgnlElement> getChildren() {
105         return children;
106     }
107 
108     public List<MgnlElement> getDescendants() {
109 
110         List<MgnlElement> descendants = new LinkedList<MgnlElement>();
111 
112         for (MgnlElement element : getChildren()) {
113             descendants.add(element);
114             descendants.addAll(element.getDescendants());
115         }
116         return descendants;
117     }
118 
119     public List<MgnlElement> getAscendants() {
120         List<MgnlElement> ascendants = new LinkedList<MgnlElement>();
121         while (parent != null) {
122             ascendants.add(parent);
123             parent = parent.getParent();
124         }
125         return ascendants;
126     }
127 
128     public MgnlElement getRootArea() {
129         MgnlElement rootArea = null;
130         for (MgnlElement parent = this; parent != null; parent = parent.getParent()) {
131             if (parent.isArea()) {
132                 rootArea = parent;
133             }
134         }
135         return rootArea;
136     }
137 
138     public MgnlElement getParentArea() {
139         MgnlElement parentArea = null;
140         for (MgnlElement parent = this; parent != null; parent = parent.getParent()) {
141             if (parent.isArea()) {
142                 parentArea = parent;
143                 break;
144             }
145         }
146         return parentArea;
147     }
148 
149     public CMSComment getComment() {
150         return comment;
151     }
152 
153     public List<MgnlElement> getComponents() {
154         List<MgnlElement> components = new LinkedList<MgnlElement>();
155         for (MgnlElement element : getChildren()) {
156             if (element.isComponent()) {
157                 components.add(element);
158             }
159         }
160         return components;
161     }
162     public List<MgnlElement> getAreas() {
163         List<MgnlElement> areas = new LinkedList<MgnlElement>();
164         for (MgnlElement element : getChildren()) {
165             if (element.isArea()) {
166                 areas.add(element);
167             }
168         }
169         return areas;
170     }
171 
172     public MgnlElement getRoot() {
173         MgnlElement root = null;
174         for (MgnlElement parent = this; parent != null; parent = parent.getParent()) {
175                 root = parent;
176         }
177         return root;
178     }
179 
180     public boolean isRelated(MgnlElement relative) {
181 
182         if (relative != null && this.getRoot() == relative.getRoot()) {
183                 return true;
184         }
185         return false;
186     }
187 
188     public void delete() {
189         for (MgnlElement child : getChildren()) {
190             if (getParent() != null) {
191                 getParent().getChildren().add(child);
192             }
193             child.setParent(getParent());
194         }
195     }
196 
197     public Element getFirstElement() {
198         return firstElement;
199     }
200 
201     public void setFirstElement(Element firstElement) {
202         this.firstElement = firstElement;
203     }
204 
205     public Element getLastElement() {
206         return lastElement;
207     }
208 
209     public void setLastElement(Element lastElement) {
210         this.lastElement = lastElement;
211     }
212 
213     public void setComponentElement(Element componentElement) {
214         this.componentElement = componentElement;
215     }
216 
217     public void setAreaElement(Element areaElement) {
218         this.areaElement = areaElement;
219     }
220 
221     public void setEditElement(Element editElement) {
222         this.editElement = editElement;
223     }
224 
225     public Element getComponentElement() {
226         return componentElement;
227     }
228 
229     public Element getAreaElement() {
230         return areaElement;
231     }
232 
233     public Element getEditElement() {
234         return editElement;
235     }
236 
237     public void setEndComment(CMSComment endComment) {
238         this.endComment = endComment;
239     }
240 
241     public CMSComment getEndComment() {
242         return this.endComment;
243     }
244 }