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.focus;
35  
36  import info.magnolia.templating.editor.client.dom.MgnlElement;
37  import info.magnolia.templating.editor.client.model.ModelStorage;
38  
39  import com.google.gwt.dom.client.Element;
40  import com.google.gwt.user.client.Cookies;
41  
42  /**
43   * Helper class to keep tack on selected items.
44   */
45  public class FocusModelImpl3 implements FocusModel {
46  
47      private ModelStorage storage;
48      private boolean rootSelected = false;
49  
50      public FocusModelImpl3(ModelStorage storage) {
51          super();
52          this.storage = storage;
53      }
54  
55      @Override
56      public void onMouseUp(MgnlElement mgnlElement) {
57  
58      }
59  
60      @Override
61      public void onMouseUp(Element element) {
62  
63          MgnlElement mgnlElement = storage.getMgnlElement(element);
64  
65          if (mgnlElement == null) {
66              reset();
67          }
68  
69          else if (mgnlElement.getParentArea() != null) {
70              MgnlElement area = mgnlElement.getParentArea();
71              if (area != storage.getSelectedMgnlElement()) {
72                  deSelect();
73                  hideRoot();
74                  toggleSelection(area, true);
75              }
76          }
77      }
78  
79  
80      @Override
81      public void onMouseDown(Element element) {}
82  
83      @Override
84      public void reset() {
85          deSelect();
86          toggleRootAreaBar(true);
87          showRootPlaceHolder();
88      }
89  
90      @Override
91      public void toggleSelection(MgnlElement mgnlElement, boolean visible) {
92          String contentId = mgnlElement.getComment().getAttribute("content");
93          Cookies.setCookie("editor-content-id", contentId);
94  
95          if (storage.getEditBar(mgnlElement) != null) {
96              storage.getEditBar(mgnlElement).setVisible(visible);
97          }
98          if (storage.getAreaEndBar(mgnlElement) != null) {
99              storage.getAreaEndBar(mgnlElement).setVisible(visible);
100         }
101 
102         // toggle all direct child-areas placeholders visibility
103         for (MgnlElement area : mgnlElement.getAreas()) {
104 
105             if (storage.getAreaPlaceHolder(area) != null) {
106                 storage.getAreaPlaceHolder(area).setVisible(visible);
107             }
108         }
109 
110         for (MgnlElement component : mgnlElement.getComponents()) {
111 
112             // toggle all child-components editbar visibility - does this case occure?
113             if (storage.getEditBar(component) != null) {
114                 storage.getEditBar(component).setVisible(visible);
115             }
116             // toggle all child-components-area placeholder visibility
117             for (MgnlElement area : component.getAreas()) {
118 
119                 if (storage.getAreaPlaceHolder(area) != null) {
120                     storage.getAreaPlaceHolder(area).setVisible(visible);
121                 }
122             }
123         }
124 
125         if (storage.getAreaPlaceHolder(mgnlElement)!= null) {
126             if (mgnlElement.getParent() != null) {
127                 storage.getAreaPlaceHolder(mgnlElement).setVisible(visible);
128             }
129             storage.getAreaPlaceHolder(mgnlElement).setActive(visible);
130         }
131 
132         if (storage.getComponentPlaceHolder(mgnlElement) != null) {
133             storage.getComponentPlaceHolder(mgnlElement).setVisible(visible);
134         }
135         storage.setSelectedMgnlElement(mgnlElement);
136 
137     }
138 
139     public void deSelect() {
140         if (storage.getSelectedMgnlElement() != null && storage.getSelectedMgnlElement().getParentArea() != null) {
141             toggleSelection(storage.getSelectedMgnlElement().getParentArea(), false);
142             storage.setSelectedMgnlElement(null);
143         }
144     }
145 
146     @Override
147     public void toggleRootAreaBar(boolean visible) {
148         deSelect();
149 
150         this.rootSelected = !this.rootSelected;
151         for (MgnlElement root : storage.getRootElements()) {
152             if (storage.getEditBar(root) != null) {
153                 storage.getEditBar(root).setVisible(visible);
154             }
155             if (storage.getAreaEndBar(root) != null) {
156                 storage.getAreaEndBar(root).setVisible(visible);
157             }
158             if (storage.getAreaPlaceHolder(root) != null) {
159                 storage.getAreaPlaceHolder(root).setVisible(visible);
160             }
161         }
162     }
163 
164     public void showRootPlaceHolder() {
165         for (MgnlElement root : storage.getRootElements()) {
166             if (storage.getAreaPlaceHolder(root) != null) {
167                 storage.getAreaPlaceHolder(root).setVisible(true);
168                 storage.getAreaPlaceHolder(root).setActive(false);
169             }
170         }
171     }
172 
173     public void hideRoot() {
174         for (MgnlElement root : storage.getRootElements()) {
175             if (storage.getEditBar(root) != null) {
176                 storage.getEditBar(root).setVisible(false);
177             }
178             if (storage.getAreaEndBar(root) != null) {
179                 storage.getAreaEndBar(root).setVisible(false);
180             }
181             if (storage.getComponentPlaceHolder(root) != null) {
182                 storage.getComponentPlaceHolder(root).setVisible(false);
183             }
184             if (storage.getAreaPlaceHolder(root) != null) {
185                 storage.getAreaPlaceHolder(root).addStyleName("inactive");
186             }
187         }
188     }
189 
190 }