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.model;
35  
36  import info.magnolia.templating.editor.client.dom.MgnlElement;
37  import info.magnolia.templating.editor.client.model.focus.FocusModel;
38  import info.magnolia.templating.editor.client.model.focus.FocusModelImpl3;
39  import info.magnolia.templating.editor.client.widget.controlbar.AbstractBar;
40  import info.magnolia.templating.editor.client.widget.controlbar.AreaEndBar;
41  import info.magnolia.templating.editor.client.widget.overlay.AbstractOverlay;
42  import info.magnolia.templating.editor.client.widget.placeholder.AreaPlaceHolder;
43  import info.magnolia.templating.editor.client.widget.placeholder.ComponentPlaceHolder;
44  
45  import java.util.HashMap;
46  import java.util.LinkedList;
47  import java.util.List;
48  import java.util.Map;
49  import com.google.gwt.dom.client.Node;
50  
51  import com.google.gwt.dom.client.Element;
52  
53  /**
54   * Singleton keeping the model.
55   */
56  public class ModelStorage {
57  
58      private static ModelStorage storage = null;
59  
60      private FocusModel focusModel = new FocusModelImpl3(this);
61  
62      private Map<MgnlElement, AbstractBar> editBars = new HashMap<MgnlElement, AbstractBar>();
63      private Map<MgnlElement, AbstractOverlay> overlays = new HashMap<MgnlElement, AbstractOverlay>();
64      private Map<MgnlElement, List<Element>> elements = new HashMap<MgnlElement, List<Element>>();
65      private Map<Element, MgnlElement> mgnlElements = new HashMap<Element, MgnlElement>();
66      private Map<MgnlElement, AreaPlaceHolder> areaPlaceHolders = new HashMap<MgnlElement, AreaPlaceHolder>();
67      private Map<MgnlElement, ComponentPlaceHolder> componentPlaceHolders = new HashMap<MgnlElement, ComponentPlaceHolder>();
68      private Map<MgnlElement, AreaEndBar> areaEndBars = new HashMap<MgnlElement, AreaEndBar>();
69  
70  
71      public List<MgnlElement> rootElements = new LinkedList<MgnlElement>();
72  
73  
74      private MgnlElement selectedMgnlElement = null;
75  
76      public static ModelStorage getInstance() {
77          if (storage == null) {
78              storage = new ModelStorage();
79          }
80          return storage;
81      }
82  
83      public void addOverlay(MgnlElement mgnlElement, AbstractOverlay overlayWidget) {
84          overlays.put(mgnlElement, overlayWidget);
85      }
86  
87      public AbstractOverlay getOverlay(MgnlElement mgnlElement) {
88          return overlays.get(mgnlElement);
89      }
90  
91      public void addEditBar(MgnlElement mgnlElement, AbstractBar editBar) {
92          editBars.put(mgnlElement, editBar);
93      }
94  
95      public AbstractBar getEditBar(MgnlElement mgnlElement) {
96          return editBars.get(mgnlElement);
97      }
98  
99      public void addElement(MgnlElement mgnlElement, Element element) {
100 
101         if (mgnlElement == null || element == null) {
102             return;
103         }
104         mgnlElements.put(element, mgnlElement);
105 
106         if (elements.get(mgnlElement) != null) {
107             elements.get(mgnlElement).add(element);
108         }
109         else {
110             List<Element> elList = new LinkedList<Element>();
111             elList.add(element);
112             elements.put(mgnlElement, elList);
113         }
114     }
115 
116     public void addElements(MgnlElement mgnlElement, Element element) {
117 
118         if (mgnlElement == null || element == null) {
119             return;
120         }
121         addElement(mgnlElement, element);
122 
123         for (int i = 0; i < element.getChildCount(); i++) {
124             Node childNode = element.getChild(i);
125             if (childNode.getNodeType() == Node.ELEMENT_NODE) {
126                 Element child = childNode.cast();
127                 addElements(mgnlElement, child);
128             }
129         }
130 
131     }
132     public MgnlElement getMgnlElement(Element element) {
133         return mgnlElements.get(element);
134     }
135 
136     public List<Element> getElements(MgnlElement mgnlElement) {
137         return elements.get(mgnlElement);
138     }
139 
140     public void addRoot(MgnlElement boundary) {
141         this.rootElements.add(boundary);
142     }
143 
144     public List<MgnlElement> getRootElements() {
145         return rootElements;
146     }
147 
148     public void setSelectedMgnlElement(MgnlElement selectedMgnlElement) {
149         this.selectedMgnlElement = selectedMgnlElement;
150     }
151 
152     public MgnlElement getSelectedMgnlElement() {
153         return selectedMgnlElement;
154     }
155 
156     public FocusModel getFocusModel() {
157         return focusModel;
158     }
159 
160     public void addAreaPlaceHolder(MgnlElement mgnlElement, AreaPlaceHolder placeHolder) {
161         areaPlaceHolders.put(mgnlElement, placeHolder);
162     }
163 
164     public AreaPlaceHolder getAreaPlaceHolder(MgnlElement mgnlElement) {
165         return areaPlaceHolders.get(mgnlElement);
166     }
167 
168     public void addComponentPlaceHolder(MgnlElement mgnlElement, ComponentPlaceHolder placeHolder) {
169         componentPlaceHolders.put(mgnlElement, placeHolder);
170     }
171 
172     public ComponentPlaceHolder getComponentPlaceHolder(MgnlElement mgnlElement) {
173         return componentPlaceHolders.get(mgnlElement);
174     }
175 
176     public void addAreaEndBar(MgnlElement mgnlElement, AreaEndBar areaEndBar) {
177         areaEndBars.put(mgnlElement, areaEndBar);
178     }
179 
180     public AreaEndBar getAreaEndBar(MgnlElement mgnlElement) {
181         return areaEndBars.get(mgnlElement);
182     }
183 
184     public void removeMgnlElement(MgnlElement mgnlElement) {
185 
186         // remove all occurrences of the element
187         if (mgnlElements.containsValue(mgnlElement)) {
188             while(mgnlElements.values().remove(mgnlElement));
189         }
190         elements.remove(mgnlElement);
191 
192         // if the element is a root node, add all children to root list
193         if (rootElements.contains(mgnlElement)) {
194             rootElements.remove(mgnlElement);
195             rootElements.addAll(mgnlElement.getChildren());
196         }
197     }
198 
199     public MgnlElement findMgnlElementByContentId(String contentId) {
200         for (MgnlElement element : elements.keySet()) {
201             if(contentId.equals(element.getComment().getAttribute("content"))){
202                 return element;
203             }
204         }
205         return null;
206     }
207 
208 }