View Javadoc

1   /**
2    * This file Copyright (c) 2013-2014 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.ui.vaadin.gwt.client.editor.dom;
35  
36  import info.magnolia.cms.security.operations.OperationPermissionDefinition;
37  import info.magnolia.ui.vaadin.gwt.client.editor.event.ComponentStartMoveEvent;
38  import info.magnolia.ui.vaadin.gwt.client.editor.event.ComponentStopMoveEvent;
39  import info.magnolia.ui.vaadin.gwt.client.editor.event.EditComponentEvent;
40  import info.magnolia.ui.vaadin.gwt.client.editor.event.SortComponentEvent;
41  import info.magnolia.ui.vaadin.gwt.client.shared.AreaElement;
42  import info.magnolia.ui.vaadin.gwt.client.shared.ComponentElement;
43  import info.magnolia.ui.vaadin.gwt.client.widget.controlbar.ComponentBar;
44  import info.magnolia.ui.vaadin.gwt.client.widget.controlbar.listener.ComponentListener;
45  
46  import java.util.Iterator;
47  import java.util.LinkedList;
48  import java.util.List;
49  
50  import com.google.gwt.event.shared.EventBus;
51  import com.google.gwt.event.shared.HandlerRegistration;
52  
53  /**
54   * Represents a component inside the {@link CmsNode}-tree.
55   * Implements a listener interface for the associated {@link info.magnolia.ui.vaadin.gwt.client.widget.controlbar.ComponentBar}.
56   * Handles DnD and move Events for components and provides wrapper functions used by the {@link info.magnolia.ui.vaadin.gwt.client.editor.model.focus.FocusModel}.
57   */
58  public class MgnlComponent extends MgnlElement implements ComponentListener {
59  
60      private final EventBus eventBus;
61      private List<HandlerRegistration> handlers = new LinkedList<HandlerRegistration>();
62  
63      public MgnlComponent(MgnlElement parent, EventBus eventBus) {
64          super(parent);
65          this.eventBus = eventBus;
66      }
67  
68      @Override
69      public ComponentElement getTypedElement() {
70          ComponentElement component = new ComponentElement(getAttribute("workspace"), getAttribute("path"), getAttribute("dialog"));
71          boolean inherited = Boolean.parseBoolean(getAttribute("inherited"));
72  
73          boolean deletable = true;
74          if (getAttributes().containsKey(OperationPermissionDefinition.DELETABLE)) {
75              deletable = Boolean.parseBoolean(getAttribute(OperationPermissionDefinition.DELETABLE));
76          }
77  
78          component.setDeletable(deletable && !inherited);
79          component.setWritable(hasEditButton());
80          component.setMoveable(isMovable());
81          return component;
82      }
83  
84      @Override
85      public void editComponent() {
86          eventBus.fireEvent(new EditComponentEvent(getTypedElement()));
87      }
88  
89      /**
90       * Fires a {@link SortComponentEvent} with the parent {@link AreaElement}.
91       * Sets the source component this component and the target to the component passed with the {@link ComponentStopMoveEvent}.
92       */
93      private void sortComponent(MgnlComponent target) {
94          MgnlArea area = getParentArea();
95          if (area != null) {
96              // area.onDragStart(true);
97  
98              AreaElement areaElement = area.getTypedElement();
99              areaElement.setSourceComponent(getTypedElement());
100             areaElement.setTargetComponent(target.getTypedElement());
101             areaElement.setSortOrder(getSortOrder(target));
102 
103             SortComponentEvent sortComponentEvent = new SortComponentEvent(areaElement);
104             eventBus.fireEvent(sortComponentEvent);
105         }
106     }
107 
108     @Override
109     public String getLabel() {
110         return getAttribute("label");
111     }
112 
113     @Override
114     public boolean hasEditButton() {
115         boolean inherited = Boolean.parseBoolean(getAttribute("inherited"));
116         boolean hasDialog = getAttribute("dialog") != null && getAttribute("dialog") != "";
117         boolean writable = true;
118         if (getAttributes().containsKey(OperationPermissionDefinition.WRITABLE)) {
119             writable = Boolean.parseBoolean(getAttribute(OperationPermissionDefinition.WRITABLE));
120         }
121         return writable && hasDialog && !inherited;
122     }
123 
124     @Override
125     public boolean isMovable() {
126         boolean inherited = Boolean.parseBoolean(getAttribute("inherited"));
127         MgnlArea parentArea = this.getParentArea();
128         boolean parentAreaIsTypeSingle = parentArea != null && "single".equals(parentArea.getAttribute("type"));
129         boolean movable = true;
130         if (getAttributes().containsKey(OperationPermissionDefinition.MOVEABLE)) {
131             movable = Boolean.parseBoolean(getAttribute(OperationPermissionDefinition.MOVEABLE));
132         }
133         return movable && !inherited && !parentAreaIsTypeSingle;
134     }
135 
136     /**
137      * Callback for {@link ComponentBar} when starting a drag or move event. Depending on whether it is a drag or a move
138      * it will either notify the server by firing a {@link ComponentStartMoveEvent} or register the handlers in {@link #doStartMove(boolean)}.
139      * 
140      * @param isDrag whether we are dragging the component or moving it
141      */
142     @Override
143     public void onMoveStart(boolean isDrag) {
144         if (isDrag) {
145             doStartMove(isDrag);
146         } else {
147             eventBus.fireEvent(new ComponentStartMoveEvent());
148         }
149     }
150 
151     /**
152      * Registers the sibling components as move targets and registers a handler for {@link ComponentStopMoveEvent} on the source component which will call {@link #sortComponent(MgnlComponent)}.
153      * 
154      * @param isDrag whether we are dragging the component or moving it
155      */
156     public void doStartMove(boolean isDrag) {
157         setMoveSource(true);
158 
159         for (MgnlComponent component : getSiblingComponents()) {
160             component.registerMoveTarget(isDrag);
161         }
162 
163         handlers.add(eventBus.addHandler(ComponentStopMoveEvent.TYPE, new ComponentStopMoveEvent.ComponentStopMoveEventHandler() {
164             @Override
165             public void onStop(ComponentStopMoveEvent componentStopMoveEvent) {
166 
167                 setMoveSource(false);
168 
169                 Iterator<HandlerRegistration> it = handlers.iterator();
170                 while (it.hasNext()) {
171                     it.next().removeHandler();
172                     it.remove();
173                 }
174                 MgnlComponent target = componentStopMoveEvent.getTargetComponent();
175                 if (target != null) {
176                     sortComponent(target);
177                 }
178             }
179         }));
180     }
181 
182     /**
183      * Callback for {@link ComponentBar} targets when a move or drag event is dropped on or moved to this target.
184      * Fires {@link ComponentStopMoveEvent} to notify the system. Holds itself as payload for handling by the source,
185      * see handler registered in {@link #doStartMove}.
186      */
187     @Override
188     public void onMoveStop() {
189         eventBus.fireEvent(new ComponentStopMoveEvent(this, false));
190     }
191 
192     /**
193      * Callback for {@link ComponentBar} source when a drag is stopped.
194      * Fires {@link ComponentStopMoveEvent} to notify the system about the cancel. Will cause target components to
195      * unregister themselves as targets.
196      * 
197      * @see #unregisterMoveTarget(boolean)
198      */
199     @Override
200     public void onMoveCancel() {
201         eventBus.fireEvent(new ComponentStopMoveEvent(null, false));
202     }
203 
204     /**
205      * Registers a {@link MgnlComponent} as a target.
206      * Registers the ui events for move or DnD on {@link ComponentBar} and adds an handler for {@link ComponentStopMoveEvent}.
207      */
208     private void registerMoveTarget(final boolean isDrag) {
209         setMoveTarget(true);
210 
211         if (isDrag) {
212             registerDragAndDropHandlers();
213         } else {
214             setDraggable(false);
215             registerMoveHandlers();
216         }
217         handlers.add(eventBus.addHandler(ComponentStopMoveEvent.TYPE, new ComponentStopMoveEvent.ComponentStopMoveEventHandler() {
218             @Override
219             public void onStop(ComponentStopMoveEvent componentMoveEvent) {
220                 unregisterMoveTarget(isDrag);
221                 setMoveOver(false);
222             }
223         }));
224     }
225 
226     /**
227      * Unregisters a {@link MgnlComponent} as a target.
228      * Removes the ui event handlers for move or DnD on {@link ComponentBar} and removes the handler for {@link ComponentStopMoveEvent}.
229      */
230     private void unregisterMoveTarget(boolean isDrag) {
231         setMoveTarget(false);
232 
233         if (isDrag) {
234             unregisterDragAndDropHandlers();
235         } else {
236             setDraggable(true);
237             unregisterMoveHandlers();
238         }
239 
240         Iterator<HandlerRegistration> it = handlers.iterator();
241         while (it.hasNext()) {
242             it.next().removeHandler();
243             it.remove();
244         }
245     }
246 
247     private void registerMoveHandlers() {
248         if (getControlBar() != null) {
249             getControlBar().registerMoveHandlers();
250         }
251     }
252 
253     private void unregisterMoveHandlers() {
254         if (getControlBar() != null) {
255             getControlBar().unregisterMoveHandlers();
256         }
257     }
258 
259     private void registerDragAndDropHandlers() {
260         if (getControlBar() != null) {
261             getControlBar().registerDragAndDropHandlers();
262         }
263     }
264 
265     private void unregisterDragAndDropHandlers() {
266         if (getControlBar() != null) {
267             getControlBar().unregisterDragAndDropHandlers();
268         }
269     }
270 
271     private void setDraggable(boolean draggable) {
272         if (getControlBar() != null) {
273             getControlBar().setDraggable(draggable);
274         }
275     }
276 
277     public void setVisible(boolean visible) {
278         if (getControlBar() != null) {
279             getControlBar().setVisible(visible);
280         }
281     }
282 
283     public void removeFocus() {
284         if (getControlBar() != null) {
285             getControlBar().removeFocus();
286         }
287     }
288 
289     public void setFocus() {
290         if (getControlBar() != null) {
291             getControlBar().setFocus(false);
292         }
293     }
294 
295     public void setMoveTarget(boolean moveTarget) {
296         if (getControlBar() != null) {
297             getControlBar().setMoveTarget(moveTarget);
298         }
299     }
300 
301     public void setMoveOver(boolean moveTarget) {
302         if (getControlBar() != null) {
303             getControlBar().setMoveOver(moveTarget);
304         }
305     }
306 
307     private void setMoveSource(boolean source) {
308         if (getControlBar() != null) {
309             getControlBar().setMoveSource(source);
310         }
311     }
312 
313     @Override
314     public ComponentBar getControlBar() {
315         return (ComponentBar) super.getControlBar();
316     }
317 
318     private String getSortOrder(MgnlComponent target) {
319 
320         int xTarget = target.getControlBar().getAbsoluteLeft();
321         int yTarget = target.getControlBar().getAbsoluteTop();
322         int xThis = getControlBar().getAbsoluteLeft();
323         int yThis = getControlBar().getAbsoluteTop();
324 
325         boolean isDragUp = yThis > yTarget;
326         boolean isDragDown = !isDragUp;
327         boolean isDragLeft = xThis > xTarget;
328         boolean isDragRight = !isDragLeft;
329 
330         String order = null;
331 
332         if (isDragUp || isDragLeft) {
333             order = "before";
334         } else if (isDragDown || isDragRight) {
335             order = "after";
336         }
337         return order;
338     }
339 
340     private List<MgnlComponent> getSiblingComponents() {
341         List<MgnlComponent> siblings = new LinkedList<MgnlComponent>();
342         MgnlArea area = getParentArea();
343         for (MgnlComponent component : area.getComponents()) {
344             if (component != this) {
345                 siblings.add(component);
346             }
347         }
348         return siblings;
349     }
350 
351     public int getHeight() {
352         if (getControlBar() != null) {
353             return getControlBar().getOffsetHeight();
354         }
355         return 0;
356     }
357 
358     public int getWidth() {
359         if (getControlBar() != null) {
360             return getControlBar().getOffsetWidth();
361         }
362         return 0;
363     }
364 }