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.dom.MgnlElement;
39  import info.magnolia.templating.editor.client.model.focus.FocusModel;
40  import info.magnolia.templating.editor.client.model.focus.FocusModelImpl3;
41  
42  import java.util.HashMap;
43  import java.util.LinkedList;
44  import java.util.List;
45  import java.util.Map;
46  
47  import com.google.gwt.dom.client.Element;
48  
49  /**
50   * Singleton keeping the model.
51   */
52  public class ModelStorage {
53  
54      private static ModelStorage storage = null;
55  
56      private FocusModel focusModel = new FocusModelImpl3(this);
57  
58      private Map<MgnlElement, AbstractBarWidget> editBars = new HashMap<MgnlElement, AbstractBarWidget>();
59      private Map<MgnlElement, AbstractOverlayWidget> overlays = new HashMap<MgnlElement, AbstractOverlayWidget>();
60      private Map<MgnlElement, List<Element>> elements = new HashMap<MgnlElement, List<Element>>();
61      private Map<Element, MgnlElement> mgnlElements = new HashMap<Element, MgnlElement>();
62  
63  
64      public List<MgnlElement> rootElements = new LinkedList<MgnlElement>();
65  
66  
67      private MgnlElement selectedMgnlElement = null;
68  
69      public static ModelStorage getInstance() {
70          if (storage == null) {
71              storage = new ModelStorage();
72          }
73          return storage;
74      }
75  
76      public void addOverlay(MgnlElement mgnlElement, AbstractOverlayWidget overlayWidget) {
77          overlays.put(mgnlElement, overlayWidget);
78      }
79  
80      public AbstractOverlayWidget getOverlay(MgnlElement mgnlElement) {
81          return overlays.get(mgnlElement);
82      }
83  
84      public void addEditBar(MgnlElement mgnlElement, AbstractBarWidget editBar) {
85          editBars.put(mgnlElement, editBar);
86      }
87  
88      public AbstractBarWidget getEditBar(MgnlElement mgnlElement) {
89          return editBars.get(mgnlElement);
90      }
91  
92      public void addElement(MgnlElement mgnlElement, Element element) {
93  
94          mgnlElements.put(element, mgnlElement);
95  
96          if (elements.get(mgnlElement) != null) {
97              elements.get(mgnlElement).add(element);
98          }
99          else {
100             List<Element> elList = new LinkedList<Element>();
101             elList.add(element);
102             elements.put(mgnlElement, elList);
103         }
104     }
105 
106     public MgnlElement getMgnlElement(Element element) {
107         return mgnlElements.get(element);
108     }
109 
110     public List<Element> getElements(MgnlElement mgnlElement) {
111         return elements.get(mgnlElement);
112     }
113 
114     public void addRoot(MgnlElement boundary) {
115         this.rootElements.add(boundary);
116     }
117 
118     public List<MgnlElement> getRootElements() {
119         return rootElements;
120     }
121 
122     public void setSelectedMgnlElement(MgnlElement selectedMgnlElement) {
123         this.selectedMgnlElement = selectedMgnlElement;
124     }
125 
126     public MgnlElement getSelectedMgnlElement() {
127         return selectedMgnlElement;
128     }
129 
130     public FocusModel getFocusModel() {
131         return focusModel;
132     }
133 
134 }