View Javadoc

1   /**
2    * This file Copyright (c) 2010-2012 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.ui.vaadin.gwt.client.editor.dom;
35  
36  import java.util.LinkedList;
37  import java.util.List;
38  
39  /**
40   * CmsNode.
41   */
42  public class CmsNode {
43      protected CmsNode parent;
44      private LinkedList<CmsNode> children = new LinkedList<CmsNode>();
45      private boolean isPage = false;
46      private boolean isArea = false;
47      private boolean isComponent = false;
48  
49      public CmsNode(CmsNode parent) {
50          this.parent = parent;
51      }
52  
53      public CmsNode getParent() {
54          return parent;
55      }
56  
57      public void setParent(CmsNode parent) {
58          this.parent = parent;
59      }
60  
61      public LinkedList<CmsNode> getChildren() {
62          return children;
63      }
64  
65      public List<CmsNode> getDescendants() {
66  
67          List<CmsNode> descendants = new LinkedList<CmsNode>();
68  
69          for (CmsNode element : getChildren()) {
70              descendants.add(element);
71              descendants.addAll(element.getDescendants());
72          }
73          return descendants;
74      }
75  
76      public List<CmsNode> getAscendants() {
77          List<CmsNode> ascendants = new LinkedList<CmsNode>();
78          CmsNode ascendant = parent;
79          while (ascendant != null) {
80              ascendants.add(ascendant);
81              ascendant = ascendant.getParent();
82          }
83          return ascendants;
84      }
85  
86      public CmsNode getParentArea() {
87          CmsNode parentArea = null;
88          for (CmsNode parent = getParent(); parent != null; parent = parent.getParent()) {
89              if (parent.isArea()) {
90                  parentArea = parent;
91                  break;
92              }
93          }
94          return parentArea;
95      }
96  
97      public List<CmsNode> getComponents() {
98          List<CmsNode> components = new LinkedList<CmsNode>();
99          for (CmsNode element : getChildren()) {
100             if (element.isComponent()) {
101                 components.add(element);
102             }
103         }
104         return components;
105     }
106 
107     public List<CmsNode> getAreas() {
108         List<CmsNode> areas = new LinkedList<CmsNode>();
109         for (CmsNode element : getChildren()) {
110             if (element.isArea()) {
111                 areas.add(element);
112             }
113         }
114         return areas;
115     }
116 
117     public CmsNode getRoot() {
118         CmsNode root = null;
119         for (CmsNode parent = this; parent != null; parent = parent.getParent()) {
120                 root = parent;
121         }
122         return root;
123     }
124 
125     public boolean isRelated(CmsNode relative) {
126 
127         return relative != null && this.getRoot() == relative.getRoot();
128     }
129 
130     public void delete() {
131         for (CmsNode child : getChildren()) {
132             if (getParent() != null) {
133                 getParent().getChildren().add(child);
134             }
135             child.setParent(getParent());
136         }
137     }
138 
139     public void setPage(boolean isPage) {
140         this.isPage = isPage;
141     }
142 
143     public void setArea(boolean isArea) {
144         this.isArea = isArea;
145     }
146 
147     public void setComponent(boolean isComponent) {
148         this.isComponent = isComponent;
149     }
150 
151     public boolean isPage() {
152         return isPage;
153     }
154 
155     public boolean isArea() {
156         return isArea;
157     }
158 
159     public boolean isComponent() {
160         return isComponent;
161     }
162 
163     public MgnlElement asMgnlElement() {
164         return (MgnlElement) this;
165     }
166 }