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 onMouseUp(Element element) {
58  
59      }
60  
61      @Override
62      public void onMouseUp(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  
74      @Override
75      public void onMouseDown(Element element) {
76          // TODO Auto-generated method stub
77  
78      }
79  
80      @Override
81      public void reset() {
82          deSelect();
83          showRoot();
84          computeOverlay();
85      }
86  
87      protected void select(MgnlElement mgnlElement) {
88  
89          if (mgnlElement != null) {
90  
91                  if (storage.getOverlay(mgnlElement) != null) {
92                      storage.getOverlay(mgnlElement).getElement().getStyle().setProperty("pointerEvents", "none");
93                  }
94                      if (storage.getEditBar(mgnlElement) != null) {
95                          storage.getEditBar(mgnlElement).setVisible(true);
96                      }
97  
98                  for (MgnlElement component : mgnlElement.getComponents()) {
99                      if (storage.getOverlay(component) != null) {
100                         storage.getOverlay(component).getElement().getStyle().setProperty("pointerEvents", "none");
101                     }
102                     if (component != null && storage.getEditBar(component) != null) {
103                         storage.getEditBar(component).setVisible(true);
104                     }
105 
106                 }
107                 for (MgnlElement parentArea = mgnlElement.getParentArea(); parentArea != null; parentArea= parentArea.getParentArea()) {
108                     if (storage.getOverlay(parentArea) != null) {
109                         storage.getOverlay(parentArea).getElement().getStyle().setProperty("pointerEvents", "none");
110 
111                     }
112                 }
113                 computeOverlay();
114            }
115     }
116 
117     public void deSelect() {
118         if (storage.getSelectedMgnlElement() != null) {
119             deSelect(storage.getSelectedMgnlElement());
120         }
121     }
122 
123     public void deSelect(MgnlElement mgnlElement) {
124         mgnlElement = mgnlElement.getRoot();
125         if (mgnlElement != null) {
126 
127             if (storage.getOverlay(mgnlElement) != null) {
128                 storage.getOverlay(mgnlElement).getElement().getStyle().clearProperty("pointerEvents");
129 
130             }
131             for (MgnlElement descendant : mgnlElement.getDescendants()) {
132                 if (storage.getOverlay(descendant) != null) {
133                     storage.getOverlay(descendant).getElement().getStyle().clearProperty("pointerEvents");
134 
135                 }
136                 if (storage.getEditBar(descendant) != null) {
137                     if (!descendant.getParent().equals(mgnlElement)) {
138                         storage.getEditBar(descendant).setVisible(false);
139                     }
140 
141                 }
142 
143             }
144             computeOverlay();
145 
146         }
147     }
148 
149     public void showRoot() {
150         for (MgnlElement root : storage.getRootElements()) {
151             if (root != null && storage.getEditBar(root) != null) {
152                 storage.getEditBar(root).setVisible(true);
153 
154             }
155         }
156     }
157     public void hideRoot() {
158         for (MgnlElement root : storage.getRootElements()) {
159             if (root != null && storage.getEditBar(root) != null) {
160                 storage.getEditBar(root).setVisible(false);
161 
162             }
163         }
164     }
165 
166     public void computeOverlay () {
167         for (MgnlElement root : storage.getRootElements()) {
168             List<MgnlElement> mgnlElements = root.getDescendants();
169             mgnlElements.add(root);
170             for (MgnlElement mgnlElement : mgnlElements) {
171 
172                 if (storage.getOverlay(mgnlElement) == null) {
173                     continue;
174                 }
175 
176                 Element firstElement = mgnlElement.getFirstElement();
177                 if (firstElement == null) {
178                     continue;
179                 }
180                 storage.getOverlay(mgnlElement).getElement().getStyle().setTop(firstElement.getAbsoluteTop(), Unit.PX);
181                 storage.getOverlay(mgnlElement).getElement().getStyle().setLeft(firstElement.getAbsoluteLeft(), Unit.PX);
182                 storage.getOverlay(mgnlElement).getElement().getStyle().setWidth(firstElement.getAbsoluteRight() - firstElement.getAbsoluteLeft(), Unit.PX);
183 
184                 Element lastElement = mgnlElement.getLastElement();
185                 if (lastElement != null) {
186                     storage.getOverlay(mgnlElement).getElement().getStyle().setHeight(lastElement.getAbsoluteBottom() - storage.getOverlay(mgnlElement).getElement().getAbsoluteTop(), Unit.PX);
187                 }
188             }
189 
190 /*            for (MgnlElement mgnlElement : mgnlElements) {
191 
192                 if (storage.getOverlay(mgnlElement) == null) {
193                     continue;
194                 }
195 
196                 double top = Double.MAX_VALUE;
197                 double bottom = 0;
198                 double left = Double.MAX_VALUE;
199                 double right = 0;
200 
201                 for (Element element : storage.getElements(mgnlElement)) {
202                     if (top > element.getAbsoluteTop()) top = element.getAbsoluteTop();
203                     if (bottom < element.getAbsoluteBottom()) bottom = element.getAbsoluteBottom();
204                     if (left > element.getAbsoluteLeft()) left = element.getAbsoluteLeft();
205                     if (right < element.getAbsoluteRight()) right = element.getAbsoluteRight();
206                 }
207 
208                 storage.getOverlay(mgnlElement).getElement().getStyle().setTop(top, Unit.PX);
209                 storage.getOverlay(mgnlElement).getElement().getStyle().setLeft(left, Unit.PX);
210                 storage.getOverlay(mgnlElement).getElement().getStyle().setWidth(right - left, Unit.PX);
211                 storage.getOverlay(mgnlElement).getElement().getStyle().setHeight(bottom - top, Unit.PX);
212 
213             }*/
214         }
215     }
216 
217 }