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