View Javadoc
1   /**
2    * This file Copyright (c) 2011-2019 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.model;
35  
36  import info.magnolia.ui.vaadin.gwt.client.editor.dom.MgnlArea;
37  import info.magnolia.ui.vaadin.gwt.client.editor.dom.MgnlElement;
38  import info.magnolia.ui.vaadin.gwt.client.editor.dom.MgnlPage;
39  import info.magnolia.ui.vaadin.gwt.client.shared.AbstractElement;
40  
41  import java.util.HashMap;
42  import java.util.LinkedList;
43  import java.util.List;
44  import java.util.Map;
45  
46  import com.google.gwt.dom.client.Element;
47  import com.google.gwt.dom.client.Node;
48  
49  
50  /**
51   * Singleton keeping the model.
52   */
53  public class ModelImpl implements Model {
54  
55      public MgnlPage rootPage;
56      public List<MgnlArea> rootAreas = new LinkedList<>();
57      private Map<MgnlElement, List<Element>> elements = new HashMap<>();
58      private Map<Element, MgnlElement> mgnlElements = new HashMap<>();
59      private boolean moving = false;
60  
61      @Override
62      public void addElement(MgnlElement mgnlElement, Element element) {
63  
64          if (mgnlElement == null || element == null) {
65              return;
66          }
67          mgnlElements.put(element, mgnlElement);
68  
69          if (elements.get(mgnlElement) != null) {
70              elements.get(mgnlElement).add(element);
71          } else {
72              List<Element> elList = new LinkedList<Element>();
73              elList.add(element);
74              elements.put(mgnlElement, elList);
75          }
76      }
77  
78      @Override
79      public void addElements(MgnlElement mgnlElement, Element element) {
80  
81          if (mgnlElement == null || element == null) {
82              return;
83          }
84          addElement(mgnlElement, element);
85  
86          for (int i = 0; i < element.getChildCount(); i++) {
87              Node childNode = element.getChild(i);
88              if (childNode.getNodeType() == Node.ELEMENT_NODE) {
89                  Element child = childNode.cast();
90                  addElements(mgnlElement, child);
91              }
92          }
93      }
94  
95      @Override
96      public MgnlElement getMgnlElement(Element element) {
97          return mgnlElements.get(element);
98  
99      }
100 
101     @Override
102     public MgnlElement getMgnlElement(AbstractElement element) {
103         if (element == null) {
104             return null;
105         }
106 
107         for (final MgnlElement mgnlElement : mgnlElements.values()) {
108             if (element.equals(mgnlElement.getTypedElement())) {
109                 return mgnlElement;
110             }
111         }
112 
113         return null;
114     }
115 
116     @Override
117     public MgnlPage getRootPage() {
118         return rootPage;
119     }
120 
121     @Override
122     public void setRootPage(MgnlPage rootPage) {
123         this.rootPage = rootPage;
124     }
125 
126     @Override
127     public void addRootArea(MgnlArea area) {
128         this.rootAreas.add(area);
129     }
130 
131     @Override
132     public List<MgnlArea> getRootAreas() {
133         return rootAreas;
134     }
135     @Override
136     public void removeMgnlElement(MgnlElement mgnlElement) {
137 
138         // remove all occurrences of the element
139         if (mgnlElements.containsValue(mgnlElement)) {
140             while (mgnlElements.values().remove(mgnlElement)) {
141                 ;
142             }
143         }
144         elements.remove(mgnlElement);
145 
146 
147         // if the element is a root node, add all children to root list
148         if (rootAreas.contains(mgnlElement)) {
149             rootAreas.remove(mgnlElement);
150             for (MgnlArea childNode : mgnlElement.getAreas()) {
151                 rootAreas.add(childNode);
152             }
153         }
154     }
155 
156     /**
157      * Reset the tree, e.g. when browsing inside the page editor.
158      */
159     @Override
160     public void reset() {
161         this.rootAreas.forEach(MgnlArea::removeBars);
162         this.elements = new HashMap<>();
163         this.mgnlElements = new HashMap<>();
164         this.rootPage = null;
165         this.rootAreas = new LinkedList<>();
166         this.moving = false;
167     }
168 
169     @Override
170     public boolean isMoving() {
171         return moving;
172     }
173 
174     @Override
175     public void setMoving(boolean moving) {
176         this.moving = moving;
177     }
178 }