View Javadoc
1   /**
2    * This file Copyright (c) 2012-2018 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   * Represents a node inside the tree structure built by the {@link info.magnolia.ui.vaadin.gwt.client.editor.dom.processor.CommentProcessor}.
41   * Holds a reference to its parent node and a list to its children and allows navigating inside the tree structure.
42   *
43   * @see MgnlPage
44   * @see MgnlArea
45   * @see MgnlComponent
46   */
47  public class CmsNode {
48      protected CmsNode parent;
49      private LinkedList<CmsNode> children = new LinkedList<CmsNode>();
50  
51      public CmsNodee" href="../../../../../../../../info/magnolia/ui/vaadin/gwt/client/editor/dom/CmsNode.html#CmsNode">CmsNode(CmsNode parent) {
52          this.parent = parent;
53      }
54  
55      public CmsNode getParent() {
56          return parent;
57      }
58  
59      public void setParent(CmsNode parent) {
60          this.parent = parent;
61      }
62  
63      public LinkedList<CmsNode> getChildren() {
64          return children;
65      }
66  
67      public List<CmsNode> getDescendants() {
68  
69          List<CmsNode> descendants = new LinkedList<CmsNode>();
70  
71          for (CmsNode element : getChildren()) {
72              descendants.add(element);
73              descendants.addAll(element.getDescendants());
74          }
75          return descendants;
76      }
77  
78      public List<CmsNode> getAscendants() {
79          List<CmsNode> ascendants = new LinkedList<CmsNode>();
80          CmsNode ascendant = parent;
81          while (ascendant != null) {
82              ascendants.add(ascendant);
83              ascendant = ascendant.getParent();
84          }
85          return ascendants;
86      }
87  
88      public MgnlArea getParentArea() {
89          MgnlArea parentArea = null;
90          for (CmsNode parent = getParent(); parent != null; parent = parent.getParent()) {
91              if (parent instanceof MgnlArea) {
92                  parentArea = (MgnlArea) parent;
93                  break;
94              }
95          }
96          return parentArea;
97      }
98  
99      public MgnlArea getRootArea() {
100         MgnlAreaef="../../../../../../../../info/magnolia/ui/vaadin/gwt/client/editor/dom/MgnlArea.html#MgnlArea">MgnlAreaeditor/dom/MgnlArea.html#MgnlArea">MgnlArea parentArea = (this instanceof MgnlAreaef="../../../../../../../../info/magnolia/ui/vaadin/gwt/client/editor/dom/MgnlArea.html#MgnlArea">MgnlArea) ? (MgnlArea) this : null;
101         for (CmsNode parent = getParent(); parent != null; parent = parent.getParent()) {
102             if (parent instanceof MgnlArea) {
103                 parentArea = (MgnlArea) parent;
104             }
105         }
106         return parentArea;
107     }
108 
109     public List<MgnlComponent> getComponents() {
110         List<MgnlComponent> components = new LinkedList<MgnlComponent>();
111         for (CmsNode element : getChildren()) {
112             if (element instanceof MgnlComponent) {
113                 components.add((MgnlComponent) element);
114             }
115         }
116         return components;
117     }
118 
119     public List<MgnlArea> getAreas() {
120         List<MgnlArea> areas = new LinkedList<MgnlArea>();
121         for (CmsNode element : getChildren()) {
122             if (element instanceof MgnlArea) {
123                 areas.add((MgnlArea) element);
124             }
125         }
126         return areas;
127     }
128 
129     public CmsNode getRoot() {
130         CmsNode root = null;
131         for (CmsNode parent = this; parent != null; parent = parent.getParent()) {
132             root = parent;
133         }
134         return root;
135     }
136 
137     public boolean isRelated(CmsNode relative) {
138         return relative != null && this.getRootArea() == relative.getRootArea();
139     }
140 
141     public void delete() {
142         for (CmsNode child : getChildren()) {
143             if (getParent() != null) {
144                 getParent().getChildren().add(child);
145             }
146             child.setParent(getParent());
147         }
148     }
149 
150     public MgnlElement asMgnlElement() {
151         return (MgnlElement) this;
152     }
153 
154     public int getLevel() {
155         int level = 0;
156         for (CmsNode parent = getParent(); parent != null; parent = parent.getParent()) {
157             level++;
158         }
159         return level;
160     }
161 
162 }