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