View Javadoc
1   /**
2    * This file Copyright (c) 2011-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.widget.controlbar;
35  
36  import info.magnolia.ui.vaadin.gwt.client.editor.dom.MgnlComponent;
37  import info.magnolia.ui.vaadin.gwt.client.widget.controlbar.eventmanager.ControlBarEventHandler;
38  import info.magnolia.ui.vaadin.gwt.client.widget.controlbar.eventmanager.ControlBarEventManager;
39  import info.magnolia.ui.vaadin.gwt.client.widget.controlbar.listener.ComponentListener;
40  
41  import com.google.gwt.core.client.GWT;
42  import com.google.gwt.dom.client.NativeEvent;
43  import com.google.gwt.dom.client.Style;
44  import com.google.gwt.event.dom.client.DragDropEventBase;
45  import com.google.gwt.user.client.Element;
46  import com.google.gwt.user.client.ui.Label;
47  
48  /**
49   * Control bar for components. Injected at the beginning of a component.
50   */
51  public class ComponentBar extends AbstractBar {
52  
53      private static final String MOVE_ICON_CLASS_NAME = "icon-move";
54      private static final String MOVE_SOURCE_CLASS_NAME = "moveSource";
55      private static final String MOVE_TARGET_CLASS_NAME = "moveTarget";
56      private static final String MOVE_OVER_CLASS_NAME = "moveOver";
57  
58      private final ComponentListener listener;
59  
60      private ControlBarEventManager eventManager = GWT.create(ControlBarEventManager.class);
61  
62      public ComponentBar(MgnlComponent mgnlElement) {
63          super(mgnlElement);
64  
65          this.listener = mgnlElement;
66          addStyleName(COMPONENT_CLASS_NAME);
67  
68          initLayout();
69  
70          if (listener.isMovable() && DragDropEventBase.isSupported()) {
71              registerDragStartHandler();
72              setDraggable(true);
73          }
74      }
75  
76      public void setDraggable(boolean draggable) {
77          if (DragDropEventBase.isSupported()) {
78              if (draggable) {
79                  this.getElement().setDraggable(Element.DRAGGABLE_TRUE);
80                  getStyle().setCursor(Style.Cursor.MOVE);
81              } else {
82                  this.getElement().setDraggable(Element.DRAGGABLE_FALSE);
83                  getStyle().setCursor(Style.Cursor.DEFAULT);
84              }
85          }
86      }
87  
88      @Override
89      protected String getLabel() {
90          return listener.getLabel();
91      }
92  
93      @Override
94      protected void createControls() {
95          if (listener.hasEditButton()) {
96              final Label edit = new Label();
97              edit.setStyleName(ICON_CLASS_NAME);
98              edit.addStyleName(EDIT_CLASS_NAME);
99              eventManager.addClickOrTouchHandler(edit, new ControlBarEventHandler() {
100                 @Override
101                 public void handle(NativeEvent event) {
102                     listener.editComponent();
103                 }
104             });
105             addButton(edit);
106         }
107         if (listener.isMovable()) {
108             final Label move = new Label();
109             move.setStyleName(ICON_CLASS_NAME);
110             move.addStyleName(MOVE_ICON_CLASS_NAME);
111             eventManager.addClickOrTouchHandler(move, new ControlBarEventHandler() {
112                 @Override
113                 public void handle(NativeEvent event) {
114                     listener.onMoveStart(false);
115                 }
116             });
117             addButton(move);
118         }
119 
120     }
121 
122     private void registerDragStartHandler() {
123         eventManager.addDragStartHandler(this, new ControlBarEventHandler() {
124             @Override
125             public void handle(NativeEvent event) {
126                 event.getDataTransfer().setData("text", "dummyPayload");
127                 event.getDataTransfer().setDragImage(getElement(), 10, 10);
128                 listener.onMoveStart(true);
129             }
130         });
131 
132         eventManager.addDragEndHandler(this, new ControlBarEventHandler() {
133             @Override
134             public void handle(NativeEvent event) {
135                 listener.onMoveCancel();
136             }
137         });
138     }
139 
140     public void registerDragAndDropHandlers() {
141         eventManager.addDragOverHandler(this, new ControlBarEventHandler() {
142             @Override
143             public void handle(NativeEvent event) {
144                 setMoveOver(true);
145                 event.stopPropagation();
146             }
147         });
148 
149 
150         eventManager.addDragLeaveHandler(this, new ControlBarEventHandler() {
151             @Override
152             public void handle(NativeEvent event) {
153                 setMoveOver(false);
154                 event.stopPropagation();
155             }
156         });
157 
158         eventManager.addDropHandler(this, new ControlBarEventHandler() {
159             @Override
160             public void handle(NativeEvent event) {
161                 listener.onMoveStop();
162                 event.preventDefault();
163             }
164         });
165     }
166 
167     public void unregisterDragAndDropHandlers() {
168         eventManager.unregisterDnDHandlers(this);
169     }
170 
171     public void registerMoveHandlers() {
172         eventManager.addMouseDownHandler(this, new ControlBarEventHandler() {
173             @Override
174             public void handle(NativeEvent event) {
175                 listener.onMoveStop();
176             }
177         });
178 
179         eventManager.addMouseOverHandler(this, new ControlBarEventHandler() {
180             @Override
181             public void handle(NativeEvent event) {
182                 setMoveOver(true);
183             }
184         });
185 
186         eventManager.addMouseOutHandler(this, new ControlBarEventHandler() {
187             @Override
188             public void handle(NativeEvent event) {
189                 setMoveOver(false);
190             }
191         });
192     }
193 
194     public void unregisterMoveHandlers() {
195         eventManager.unregisterMoveHandlers(this);
196     }
197 
198     public void setMoveTarget(boolean target) {
199         setStyleName(MOVE_TARGET_CLASS_NAME, target);
200     }
201 
202     public void setMoveOver(boolean over) {
203         setStyleName(MOVE_OVER_CLASS_NAME, over);
204     }
205 
206     public void setMoveSource(boolean source) {
207         setStyleName(MOVE_SOURCE_CLASS_NAME, source);
208     }
209 
210     @Override
211     protected int getActivationStatus() {
212         return listener.getActivationStatus();
213     }
214 }