View Javadoc

1   /**
2    * This file Copyright (c) 2013 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
92       * {@link ComponentStopMoveEvent}.
93       */
94      private void sortComponent(MgnlComponent target) {
95          MgnlArea area = getParentArea();
96          if (area != null) {
97              //area.onDragStart(true);
98  
99              AreaElement areaElement = area.getTypedElement();
100             areaElement.setSourceComponent(getTypedElement());
101             areaElement.setTargetComponent(target.getTypedElement());
102             areaElement.setSortOrder(getSortOrder(target));
103 
104             SortComponentEvent sortComponentEvent = new SortComponentEvent(areaElement);
105             eventBus.fireEvent(sortComponentEvent);
106         }
107     }
108 
109     @Override
110     public String getLabel() {
111         return getAttribute("label");
112     }
113 
114     @Override
115     public boolean hasEditButton() {
116         boolean inherited = Boolean.parseBoolean(getAttribute("inherited"));
117         boolean hasDialog = getAttribute("dialog") != null && getAttribute("dialog") != "";
118         boolean writable = true;
119         if (getAttributes().containsKey(OperationPermissionDefinition.WRITABLE)) {
120             writable = Boolean.parseBoolean(getAttribute(OperationPermissionDefinition.WRITABLE));
121         }
122         return writable && hasDialog && !inherited;
123     }
124 
125     @Override
126     public boolean isMovable() {
127         boolean inherited = Boolean.parseBoolean(getAttribute("inherited"));
128         boolean movable = true;
129         if (getAttributes().containsKey(OperationPermissionDefinition.MOVEABLE)) {
130             movable = Boolean.parseBoolean(getAttribute(OperationPermissionDefinition.MOVEABLE));
131         }
132         return movable && !inherited;
133     }
134 
135     /**
136      * Callback for {@link ComponentBar} when starting a drag or move event. Depending on whether it is a drag or a move
137      * it will either notify the server by firing a {@link ComponentStartMoveEvent} or register the handlers in
138      * {@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}
153      * on the source component which will call {@link #sortComponent(MgnlComponent)}.
154      *
155      * @param isDrag whether we are dragging the component or moving it
156      */
157     public void doStartMove(boolean isDrag) {
158         setMoveSource(true);
159 
160         for (MgnlComponent component : getSiblingComponents()) {
161             component.registerMoveTarget(isDrag);
162         }
163 
164         handlers.add(eventBus.addHandler(ComponentStopMoveEvent.TYPE, new ComponentStopMoveEvent.ComponentStopMoveEventHandler() {
165             @Override
166             public void onStop(ComponentStopMoveEvent componentStopMoveEvent) {
167 
168                 setMoveSource(false);
169 
170                 Iterator<HandlerRegistration> it = handlers.iterator();
171                 while (it.hasNext()) {
172                     it.next().removeHandler();
173                     it.remove();
174                 }
175                 MgnlComponent target = componentStopMoveEvent.getTargetComponent();
176                 if (target != null) {
177                     sortComponent(target);
178                 }
179             }
180         }));
181     }
182 
183     /**
184      * Callback for {@link ComponentBar} targets when a move or drag event is dropped on or moved to this target.
185      * Fires {@link ComponentStopMoveEvent} to notify the system. Holds itself as payload for handling by the source,
186      * see handler registered in {@link #doStartMove}.
187      */
188     @Override
189     public void onMoveStop() {
190         eventBus.fireEvent(new ComponentStopMoveEvent(this, false));
191     }
192 
193     /**
194      * Callback for {@link ComponentBar} source when a drag is stopped.
195      * Fires {@link ComponentStopMoveEvent} to notify the system about the cancel. Will cause target components to
196      * unregister themselves as targets.
197      *
198      * @see #unregisterMoveTarget(boolean)
199      */
200     @Override
201     public void onMoveCancel() {
202         eventBus.fireEvent(new ComponentStopMoveEvent(null, false));
203     }
204 
205     /**
206      * Registers a {@link MgnlComponent} as a target.
207      * Registers the ui events for move or DnD on {@link ComponentBar} and adds an handler for {@link ComponentStopMoveEvent}.
208      */
209     private void registerMoveTarget(final boolean isDrag) {
210         setMoveTarget(true);
211 
212         if (isDrag) {
213             registerDragAndDropHandlers();
214         } else {
215             setDraggable(false);
216             registerMoveHandlers();
217         }
218         handlers.add(eventBus.addHandler(ComponentStopMoveEvent.TYPE, new ComponentStopMoveEvent.ComponentStopMoveEventHandler() {
219             @Override
220             public void onStop(ComponentStopMoveEvent componentMoveEvent) {
221                 unregisterMoveTarget(isDrag);
222                 setMoveOver(false);
223             }
224         }));
225     }
226 
227     /**
228      * Unregisters a {@link MgnlComponent} as a target.
229      * Removes the ui event handlers for move or DnD on {@link ComponentBar} and removes the handler for {@link ComponentStopMoveEvent}.
230      */
231     private void unregisterMoveTarget(boolean isDrag) {
232         setMoveTarget(false);
233 
234         if (isDrag) {
235             unregisterDragAndDropHandlers();
236         } else {
237             setDraggable(true);
238             unregisterMoveHandlers();
239         }
240 
241         Iterator<HandlerRegistration> it = handlers.iterator();
242         while (it.hasNext()) {
243             it.next().removeHandler();
244             it.remove();
245         }
246     }
247 
248     private void registerMoveHandlers() {
249         if (getControlBar() != null) {
250             getControlBar().registerMoveHandlers();
251         }
252     }
253 
254     private void unregisterMoveHandlers() {
255         if (getControlBar() != null) {
256             getControlBar().unregisterMoveHandlers();
257         }
258     }
259 
260     private void registerDragAndDropHandlers() {
261         if (getControlBar() != null) {
262             getControlBar().registerDragAndDropHandlers();
263         }
264     }
265 
266     private void unregisterDragAndDropHandlers() {
267         if (getControlBar() != null) {
268             getControlBar().unregisterDragAndDropHandlers();
269         }
270     }
271 
272     private void setDraggable(boolean draggable) {
273         if (getControlBar() != null) {
274             getControlBar().setDraggable(draggable);
275         }
276     }
277 
278     public void setVisible(boolean visible) {
279         if (getControlBar() != null) {
280             getControlBar().setVisible(visible);
281         }
282     }
283 
284     public void removeFocus() {
285         if (getControlBar() != null) {
286             getControlBar().removeFocus();
287         }
288     }
289 
290     public void setFocus() {
291         if (getControlBar() != null) {
292             getControlBar().setFocus(false);
293         }
294     }
295 
296     public void setMoveTarget(boolean moveTarget) {
297         if (getControlBar() != null) {
298             getControlBar().setMoveTarget(moveTarget);
299         }
300     }
301 
302     public void setMoveOver(boolean moveTarget) {
303         if (getControlBar() != null) {
304             getControlBar().setMoveOver(moveTarget);
305         }
306     }
307 
308     private void setMoveSource(boolean source) {
309         if (getControlBar() != null) {
310             getControlBar().setMoveSource(source);
311         }
312     }
313 
314 
315     @Override
316     public ComponentBar getControlBar() {
317         return (ComponentBar) super.getControlBar();
318     }
319 
320     private String getSortOrder(MgnlComponent target) {
321 
322         int xTarget = target.getControlBar().getAbsoluteLeft();
323         int yTarget = target.getControlBar().getAbsoluteTop();
324         int xThis = getControlBar().getAbsoluteLeft();
325         int yThis = getControlBar().getAbsoluteTop();
326 
327         boolean isDragUp = yThis > yTarget;
328         boolean isDragDown = !isDragUp;
329         boolean isDragLeft = xThis > xTarget;
330         boolean isDragRight = !isDragLeft;
331 
332         String order = null;
333 
334         if (isDragUp || isDragLeft) {
335             order = "before";
336         } else if (isDragDown || isDragRight) {
337             order = "after";
338         }
339         return order;
340     }
341 
342     private List<MgnlComponent> getSiblingComponents() {
343         List<MgnlComponent> siblings = new LinkedList<MgnlComponent>();
344         MgnlArea area = getParentArea();
345         for(MgnlComponent component : area.getComponents()) {
346             if (component != this) {
347                 siblings.add(component);
348             }
349         }
350         return siblings;
351     }
352 
353     public int getHeight() {
354         if (getControlBar() != null) {
355             return getControlBar().getOffsetHeight();
356         }
357         return 0;
358     }
359 
360     public int getWidth() {
361         if (getControlBar() != null) {
362             return getControlBar().getOffsetWidth();
363         }
364         return 0;
365     }
366 }