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