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.AbstractBarWidget;
37  import info.magnolia.templating.editor.client.AbstractOverlayWidget;
38  import info.magnolia.templating.editor.client.AbstractPlaceHolder;
39  import info.magnolia.templating.editor.client.AreaPlaceHolderWidget;
40  import info.magnolia.templating.editor.client.ComponentPlaceHolderWidget;
41  import info.magnolia.templating.editor.client.dom.MgnlElement;
42  import info.magnolia.templating.editor.client.model.focus.FocusModel;
43  import info.magnolia.templating.editor.client.model.focus.FocusModelImpl3;
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, AbstractBarWidget> editBars = new HashMap<MgnlElement, AbstractBarWidget>();
63      private Map<MgnlElement, AbstractOverlayWidget> overlays = new HashMap<MgnlElement, AbstractOverlayWidget>();
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, AreaPlaceHolderWidget> areaPlaceHolders = new HashMap<MgnlElement, AreaPlaceHolderWidget>();
67      private Map<MgnlElement, ComponentPlaceHolderWidget> componentPlaceHolders = new HashMap<MgnlElement, ComponentPlaceHolderWidget>();
68  
69  
70      public List<MgnlElement> rootElements = new LinkedList<MgnlElement>();
71  
72  
73      private MgnlElement selectedMgnlElement = null;
74  
75      public static ModelStorage getInstance() {
76          if (storage == null) {
77              storage = new ModelStorage();
78          }
79          return storage;
80      }
81  
82      public void addOverlay(MgnlElement mgnlElement, AbstractOverlayWidget overlayWidget) {
83          overlays.put(mgnlElement, overlayWidget);
84      }
85  
86      public AbstractOverlayWidget getOverlay(MgnlElement mgnlElement) {
87          return overlays.get(mgnlElement);
88      }
89  
90      public void addEditBar(MgnlElement mgnlElement, AbstractBarWidget editBar) {
91          editBars.put(mgnlElement, editBar);
92      }
93  
94      public AbstractBarWidget getEditBar(MgnlElement mgnlElement) {
95          return editBars.get(mgnlElement);
96      }
97  
98      public void addElement(MgnlElement mgnlElement, Element element) {
99  
100         mgnlElements.put(element, mgnlElement);
101 
102         if (elements.get(mgnlElement) != null) {
103             elements.get(mgnlElement).add(element);
104         }
105         else {
106             List<Element> elList = new LinkedList<Element>();
107             elList.add(element);
108             elements.put(mgnlElement, elList);
109         }
110     }
111 
112     public void addElements(MgnlElement mgnlElement, Element element) {
113 
114         addElement(mgnlElement, element);
115         for (int i = 0; i < element.getChildCount(); i++) {
116             Node childNode = element.getChild(i);
117             if (childNode.getNodeType() == Node.ELEMENT_NODE) {
118                 Element child = childNode.cast();
119                 addElement(mgnlElement, child);
120             }
121         }
122 
123     }
124     public MgnlElement getMgnlElement(Element element) {
125         return mgnlElements.get(element);
126     }
127 
128     public List<Element> getElements(MgnlElement mgnlElement) {
129         return elements.get(mgnlElement);
130     }
131 
132     public void addRoot(MgnlElement boundary) {
133         this.rootElements.add(boundary);
134     }
135 
136     public List<MgnlElement> getRootElements() {
137         return rootElements;
138     }
139 
140     public void setSelectedMgnlElement(MgnlElement selectedMgnlElement) {
141         this.selectedMgnlElement = selectedMgnlElement;
142     }
143 
144     public MgnlElement getSelectedMgnlElement() {
145         return selectedMgnlElement;
146     }
147 
148     public FocusModel getFocusModel() {
149         return focusModel;
150     }
151 
152     public void addAreaPlaceHolder(MgnlElement mgnlElement, AreaPlaceHolderWidget placeHolder) {
153         areaPlaceHolders.put(mgnlElement, placeHolder);
154     }
155 
156     public AbstractPlaceHolder getAreaPlaceHolder(MgnlElement mgnlElement) {
157         return areaPlaceHolders.get(mgnlElement);
158     }
159 
160     public void addComponentPlaceHolder(MgnlElement mgnlElement, ComponentPlaceHolderWidget placeHolder) {
161         componentPlaceHolders.put(mgnlElement, placeHolder);
162     }
163 
164     public ComponentPlaceHolderWidget getComponentPlaceHolder(MgnlElement mgnlElement) {
165         return componentPlaceHolders.get(mgnlElement);
166     }
167 
168     public void removeMgnlElement(MgnlElement mgnlElement) {
169         if (mgnlElements.containsValue(mgnlElement)) {
170             while(mgnlElements.values().remove(mgnlElement));
171         }
172         elements.remove(mgnlElement);
173     }
174 
175 }