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 info.magnolia.templating.editor.client.AbstractBarWidget;
37  
38  import java.util.LinkedList;
39  
40  import com.google.gwt.dom.client.Element;
41  
42  /**
43  * CMSBoundary Constructor.
44  *
45  * @throws IllegalArgumentException if comments tagname is not a defined marker.
46  */
47  public class CMSBoundary {
48  
49      private AbstractBarWidget widget;
50      private CMSComment comment;
51      private CMSBoundary parentBoundary;
52      private boolean isArea = false;
53      private boolean isComponent = false;
54  
55      private Coordinate maxCoordinate = new Coordinate();
56      private Coordinate minCoordinate = new Coordinate();
57  
58      private Element firstElement;
59      public Element getFirstElement() {
60          return firstElement;
61      }
62  
63      public void setFirstElement(Element firstElement) {
64          this.firstElement = firstElement;
65      }
66  
67      public Coordinate getMaxCoordinate() {
68          return maxCoordinate;
69      }
70  
71      public Coordinate getMinCoordinate() {
72          return minCoordinate;
73      }
74  
75      private LinkedList<CMSBoundary> childBoundaries = new LinkedList<CMSBoundary>();
76      private boolean isEdit;
77  
78      public static final String MARKER_AREA = "cms:area";
79      public static final String MARKER_COMPONENT = "cms:component";
80      public static final String MARKER_EDIT = "cms:edit";
81  
82      public CMSBoundary(CMSComment comment, CMSBoundary parentBoundary) throws IllegalArgumentException {
83  
84          if (!isCmsBoundary(comment.getTagName())) {
85              throw new IllegalArgumentException("The tagname must be one of the defined marker Strings.");
86          }
87  
88          this.setComment(comment);
89          this.setParentBoundary(parentBoundary);
90      }
91  
92      public String getContent() {
93          return this.getComment().getAttribute("cms:content");
94      }
95  
96      public void setParentBoundary(CMSBoundary parentBoundary) {
97          this.parentBoundary = parentBoundary;
98      }
99  
100     public CMSBoundary getParentBoundary() {
101         return parentBoundary;
102     }
103 
104     public void setChildBoundaries(LinkedList<CMSBoundary> childBoundaries) {
105         this.childBoundaries = childBoundaries;
106     }
107 
108     public LinkedList<CMSBoundary> getChildBoundaries() {
109         return childBoundaries;
110     }
111 
112     public boolean hasChildBoundaries() {
113         return (getChildBoundaries().size() > 0);
114     }
115 
116     public LinkedList<CMSBoundary> getDescendants() {
117 
118         LinkedList<CMSBoundary> descendants = new LinkedList<CMSBoundary>();
119 
120         for (CMSBoundary boundary : getChildBoundaries()) {
121             descendants.add(boundary);
122             descendants.addAll(boundary.getDescendants());
123         }
124         return descendants;
125     }
126 
127     public LinkedList<CMSBoundary> getAscendants() {
128         LinkedList<CMSBoundary> ascendants = new LinkedList<CMSBoundary>();
129         CMSBoundary parent = this.parentBoundary;
130         while (parent != null) {
131             ascendants.add(parent);
132             parent = parent.getParentBoundary();
133         }
134         return ascendants;
135     }
136 
137     public CMSBoundary getParentArea() {
138 
139         if (isArea()) {
140             return this;
141         }
142         else if (getParentBoundary() != null) {
143             return getParentBoundary().getParentArea();
144         }
145 
146         return getParentBoundary().getParentArea();
147 
148 
149     }
150 
151     public boolean isCmsBoundary(String tagName) {
152         if (tagName.equals(MARKER_AREA)) {
153             this.isArea = true;
154             return true;
155         }
156         else if (tagName.equals(MARKER_COMPONENT)) {
157             this.isComponent = true;
158             return true;
159         }
160         else if (tagName.equals(MARKER_EDIT)) {
161             this.isEdit = true;
162             return true;
163         }
164 
165         return false;
166     }
167     public boolean isArea() {
168         return isArea;
169     }
170     public boolean isEdit() {
171         return isEdit;
172     }
173 
174     public boolean isComponent() {
175         return isComponent;
176     }
177     public void setComment(CMSComment comment) {
178         this.comment = comment;
179     }
180 
181     public CMSComment getComment() {
182         return comment;
183     }
184 
185     public void setWidget(AbstractBarWidget widget) {
186         this.widget = widget;
187     }
188 
189     public AbstractBarWidget getWidget() {
190         return widget;
191     }
192 
193     /**
194      * Coordinate.
195      */
196     public class Coordinate {
197         private int left = 0;
198         private int top = 0;
199 
200         public void setLeft(int left) {
201             this.left = left;
202         }
203         public int getLeft() {
204             return left;
205         }
206         public void setTop(int top) {
207             this.top = top;
208         }
209         public int getTop() {
210             return top;
211         }
212 
213     }
214 }