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