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 java.util.List;
40  
41  import com.google.gwt.dom.client.Element;
42  import com.google.gwt.dom.client.Style.Unit;
43  
44  /**
45   * Helper class to keep tack on selected items.
46   */
47  public class FocusModelImpl2 implements FocusModel {
48  
49      private ModelStorage storage;
50  
51      public FocusModelImpl2(ModelStorage storage) {
52          super();
53          this.storage = storage;
54      }
55  
56      @Override
57      public void handleClick(Element element) {
58  
59      }
60  
61      @Override
62      public void handleClick (MgnlElement mgnlElement) {
63  
64          hideRoot();
65          if (storage.getSelectedMgnlElement() != null) {
66              deSelect(storage.getSelectedMgnlElement());
67          }
68          select(mgnlElement);
69  
70          storage.setSelectedMgnlElement(mgnlElement);
71      }
72  
73      @Override
74      public void reset() {
75          deSelect();
76          showRoot();
77          computeOverlay();
78      }
79  
80      protected void select(MgnlElement mgnlElement) {
81  
82          if (mgnlElement != null) {
83  
84                  if (storage.getOverlay(mgnlElement) != null) {
85                      storage.getOverlay(mgnlElement).getElement().getStyle().setProperty("pointerEvents", "none");
86                  }
87                      if (storage.getEditBar(mgnlElement) != null) {
88                          storage.getEditBar(mgnlElement).setVisible(true);
89                      }
90  
91                  for (MgnlElement component : mgnlElement.getComponents()) {
92                      if (storage.getOverlay(component) != null) {
93                          storage.getOverlay(component).getElement().getStyle().setProperty("pointerEvents", "none");
94                      }
95                      if (component != null && storage.getEditBar(component) != null) {
96                          storage.getEditBar(component).setVisible(true);
97                      }
98  
99                  }
100                 for (MgnlElement parentArea = mgnlElement.getParentArea(); parentArea != null; parentArea= parentArea.getParentArea()) {
101                     if (storage.getOverlay(parentArea) != null) {
102                         storage.getOverlay(parentArea).getElement().getStyle().setProperty("pointerEvents", "none");
103 
104                     }
105                 }
106                 computeOverlay();
107            }
108     }
109 
110     public void deSelect() {
111         if (storage.getSelectedMgnlElement() != null) {
112             deSelect(storage.getSelectedMgnlElement());
113         }
114     }
115 
116     public void deSelect(MgnlElement mgnlElement) {
117         mgnlElement = mgnlElement.getRoot();
118         if (mgnlElement != null) {
119 
120             if (storage.getOverlay(mgnlElement) != null) {
121                 storage.getOverlay(mgnlElement).getElement().getStyle().clearProperty("pointerEvents");
122 
123             }
124             for (MgnlElement descendant : mgnlElement.getDescendants()) {
125                 if (storage.getOverlay(descendant) != null) {
126                     storage.getOverlay(descendant).getElement().getStyle().clearProperty("pointerEvents");
127 
128                 }
129                 if (storage.getEditBar(descendant) != null) {
130                     if (!descendant.getParent().equals(mgnlElement)) {
131                         storage.getEditBar(descendant).setVisible(false);
132                     }
133 
134                 }
135 
136             }
137             computeOverlay();
138 
139         }
140     }
141 
142     public void showRoot() {
143         for (MgnlElement root : storage.getRootElements()) {
144             if (root != null && storage.getEditBar(root) != null) {
145                 storage.getEditBar(root).setVisible(true);
146 
147             }
148         }
149     }
150     public void hideRoot() {
151         for (MgnlElement root : storage.getRootElements()) {
152             if (root != null && storage.getEditBar(root) != null) {
153                 storage.getEditBar(root).setVisible(false);
154 
155             }
156         }
157     }
158 
159     public void computeOverlay () {
160         for (MgnlElement root : storage.getRootElements()) {
161             List<MgnlElement> mgnlElements = root.getDescendants();
162             mgnlElements.add(root);
163             for (MgnlElement mgnlElement : mgnlElements) {
164 
165                 if (storage.getOverlay(mgnlElement) == null) {
166                     continue;
167                 }
168 
169                 Element firstElement = mgnlElement.getFirstElement();
170                 if (firstElement == null) {
171                     continue;
172                 }
173                 storage.getOverlay(mgnlElement).getElement().getStyle().setTop(firstElement.getAbsoluteTop(), Unit.PX);
174                 storage.getOverlay(mgnlElement).getElement().getStyle().setLeft(firstElement.getAbsoluteLeft(), Unit.PX);
175                 storage.getOverlay(mgnlElement).getElement().getStyle().setWidth(firstElement.getAbsoluteRight() - firstElement.getAbsoluteLeft(), Unit.PX);
176 
177                 Element lastElement = mgnlElement.getLastElement();
178                 if (lastElement != null) {
179                     storage.getOverlay(mgnlElement).getElement().getStyle().setHeight(lastElement.getAbsoluteBottom() - storage.getOverlay(mgnlElement).getElement().getAbsoluteTop(), Unit.PX);
180                 }
181             }
182 
183 /*            for (MgnlElement mgnlElement : mgnlElements) {
184 
185                 if (storage.getOverlay(mgnlElement) == null) {
186                     continue;
187                 }
188 
189                 double top = Double.MAX_VALUE;
190                 double bottom = 0;
191                 double left = Double.MAX_VALUE;
192                 double right = 0;
193 
194                 for (Element element : storage.getElements(mgnlElement)) {
195                     if (top > element.getAbsoluteTop()) top = element.getAbsoluteTop();
196                     if (bottom < element.getAbsoluteBottom()) bottom = element.getAbsoluteBottom();
197                     if (left > element.getAbsoluteLeft()) left = element.getAbsoluteLeft();
198                     if (right < element.getAbsoluteRight()) right = element.getAbsoluteRight();
199                 }
200 
201                 storage.getOverlay(mgnlElement).getElement().getStyle().setTop(top, Unit.PX);
202                 storage.getOverlay(mgnlElement).getElement().getStyle().setLeft(left, Unit.PX);
203                 storage.getOverlay(mgnlElement).getElement().getStyle().setWidth(right - left, Unit.PX);
204                 storage.getOverlay(mgnlElement).getElement().getStyle().setHeight(bottom - top, Unit.PX);
205 
206             }*/
207         }
208     }
209 
210 }