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.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  
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 boolean isMovable() {
146         boolean inherited = Boolean.parseBoolean(getAttribute("inherited"));
147         MgnlArea parentArea = this.getParentArea();
148         boolean parentAreaIsTypeSingle = parentArea != null && "single".equals(parentArea.getAttribute("type"));
149         boolean movable = true;
150         if (getAttributes().containsKey(OperationPermissionDefinition.MOVEABLE)) {
151             movable = Boolean.parseBoolean(getAttribute(OperationPermissionDefinition.MOVEABLE));
152         }
153         return movable && !inherited && !parentAreaIsTypeSingle;
154     }
155 
156     /**
157      * Callback for {@link ComponentBar} when starting a drag or move event. Depending on whether it is a drag or a move
158      * it will either notify the server by firing a {@link ComponentStartMoveEvent} or register the handlers in {@link #doStartMove(boolean)}.
159      *
160      * @param isDrag whether we are dragging the component or moving it
161      */
162     @Override
163     public void onMoveStart(boolean isDrag) {
164         if (isDrag) {
165             doStartMove(isDrag);
166         } else {
167             eventBus.fireEvent(new ComponentStartMoveEvent());
168         }
169     }
170 
171     /**
172      * Registers the sibling components as move targets and registers a handler for {@link ComponentStopMoveEvent} on the source component which will call {@link #sortComponent(MgnlComponent)}.
173      *
174      * @param isDrag whether we are dragging the component or moving it
175      */
176     public void doStartMove(boolean isDrag) {
177         setMoveSource(true);
178 
179         for (MgnlComponent component : getSiblingComponents()) {
180             component.registerMoveTarget(isDrag);
181         }
182 
183         handlers.add(eventBus.addHandler(ComponentStopMoveEvent.TYPE, new ComponentStopMoveEvent.ComponentStopMoveEventHandler() {
184             @Override
185             public void onStop(ComponentStopMoveEvent componentStopMoveEvent) {
186 
187                 setMoveSource(false);
188 
189                 Iterator<HandlerRegistration> it = handlers.iterator();
190                 while (it.hasNext()) {
191                     it.next().removeHandler();
192                     it.remove();
193                 }
194                 MgnlComponent target = componentStopMoveEvent.getTargetComponent();
195                 if (target != null) {
196                     sortComponent(target);
197                 }
198             }
199         }));
200     }
201 
202     /**
203      * Callback for {@link ComponentBar} targets when a move or drag event is dropped on or moved to this target.
204      * Fires {@link ComponentStopMoveEvent} to notify the system. Holds itself as payload for handling by the source,
205      * see handler registered in {@link #doStartMove}.
206      */
207     @Override
208     public void onMoveStop() {
209         eventBus.fireEvent(new ComponentStopMoveEvent(this, false));
210     }
211 
212     /**
213      * Callback for {@link ComponentBar} source when a drag is stopped.
214      * Fires {@link ComponentStopMoveEvent} to notify the system about the cancel. Will cause target components to
215      * unregister themselves as targets.
216      *
217      * @see #unregisterMoveTarget(boolean)
218      */
219     @Override
220     public void onMoveCancel() {
221         eventBus.fireEvent(new ComponentStopMoveEvent(null, false));
222     }
223 
224     /**
225      * Registers a {@link MgnlComponent} as a target.
226      * Registers the ui events for move or DnD on {@link ComponentBar} and adds an handler for {@link ComponentStopMoveEvent}.
227      */
228     private void registerMoveTarget(final boolean isDrag) {
229         setMoveTarget(true);
230 
231         if (isDrag) {
232             registerDragAndDropHandlers();
233         } else {
234             setDraggable(false);
235             registerMoveHandlers();
236         }
237         handlers.add(eventBus.addHandler(ComponentStopMoveEvent.TYPE, new ComponentStopMoveEvent.ComponentStopMoveEventHandler() {
238             @Override
239             public void onStop(ComponentStopMoveEvent componentMoveEvent) {
240                 unregisterMoveTarget(isDrag);
241                 setMoveOver(false);
242             }
243         }));
244     }
245 
246     /**
247      * Unregisters a {@link MgnlComponent} as a target.
248      * Removes the ui event handlers for move or DnD on {@link ComponentBar} and removes the handler for {@link ComponentStopMoveEvent}.
249      */
250     private void unregisterMoveTarget(boolean isDrag) {
251         setMoveTarget(false);
252 
253         if (isDrag) {
254             unregisterDragAndDropHandlers();
255         } else {
256             setDraggable(true);
257             unregisterMoveHandlers();
258         }
259 
260         Iterator<HandlerRegistration> it = handlers.iterator();
261         while (it.hasNext()) {
262             it.next().removeHandler();
263             it.remove();
264         }
265     }
266 
267     private void registerMoveHandlers() {
268         if (getControlBar() != null) {
269             getControlBar().registerMoveHandlers();
270         }
271     }
272 
273     private void unregisterMoveHandlers() {
274         if (getControlBar() != null) {
275             getControlBar().unregisterMoveHandlers();
276         }
277     }
278 
279     private void registerDragAndDropHandlers() {
280         if (getControlBar() != null) {
281             getControlBar().registerDragAndDropHandlers();
282         }
283     }
284 
285     private void unregisterDragAndDropHandlers() {
286         if (getControlBar() != null) {
287             getControlBar().unregisterDragAndDropHandlers();
288         }
289     }
290 
291     private void setDraggable(boolean draggable) {
292         if (getControlBar() != null) {
293             getControlBar().setDraggable(draggable);
294         }
295     }
296 
297     public void setVisible(boolean visible) {
298         if (getControlBar() != null) {
299             getControlBar().setVisible(visible);
300         }
301     }
302 
303     public void removeFocus() {
304         if (getControlBar() != null) {
305             getControlBar().removeFocus();
306         }
307     }
308 
309     public void setFocus() {
310         if (getControlBar() != null) {
311             getControlBar().setFocus(false);
312         }
313     }
314 
315     public void setMoveTarget(boolean moveTarget) {
316         if (getControlBar() != null) {
317             getControlBar().setMoveTarget(moveTarget);
318         }
319     }
320 
321     public void setMoveOver(boolean moveTarget) {
322         if (getControlBar() != null) {
323             getControlBar().setMoveOver(moveTarget);
324         }
325     }
326 
327     private void setMoveSource(boolean source) {
328         if (getControlBar() != null) {
329             getControlBar().setMoveSource(source);
330         }
331     }
332 
333     @Override
334     public ComponentBar getControlBar() {
335         return (ComponentBar) super.getControlBar();
336     }
337 
338     private String getSortOrder(MgnlComponent target) {
339 
340         int xTarget = target.getControlBar().getAbsoluteLeft();
341         int yTarget = target.getControlBar().getAbsoluteTop();
342         int xThis = getControlBar().getAbsoluteLeft();
343         int yThis = getControlBar().getAbsoluteTop();
344 
345         boolean isDragUp = yThis > yTarget;
346         boolean isDragDown = !isDragUp;
347         boolean isDragLeft = xThis > xTarget;
348         boolean isDragRight = !isDragLeft;
349 
350         String order = null;
351 
352         if (isDragUp || isDragLeft) {
353             order = "before";
354         } else if (isDragDown || isDragRight) {
355             order = "after";
356         }
357         return order;
358     }
359 
360     private List<MgnlComponent> getSiblingComponents() {
361         List<MgnlComponent> siblings = new LinkedList<MgnlComponent>();
362         MgnlArea area = getParentArea();
363         for (MgnlComponent component : area.getComponents()) {
364             if (component != this) {
365                 siblings.add(component);
366             }
367         }
368         return siblings;
369     }
370 
371     public int getHeight() {
372         if (getControlBar() != null) {
373             return getControlBar().getOffsetHeight();
374         }
375         return 0;
376     }
377 
378     public int getWidth() {
379         if (getControlBar() != null) {
380             return getControlBar().getOffsetWidth();
381         }
382         return 0;
383     }
384 }