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