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