View Javadoc
1   /**
2    * This file Copyright (c) 2011-2016 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 {
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      protected ComponentListener listener;
58  
59      private ControlBarEventManager eventManager = GWT.create(ControlBarEventManager.class);
60      private boolean editable;
61      private boolean movable;
62  
63      protected ComponentBar() {
64          addStyleName(COMPONENT_CLASS_NAME);
65      }
66  
67      public void setDraggable(boolean draggable) {
68          if (DragDropEventBase.isSupported()) {
69              if (draggable) {
70                  this.getElement().setDraggable(Element.DRAGGABLE_TRUE);
71                  getElement().getStyle().setCursor(Style.Cursor.MOVE);
72              } else {
73                  this.getElement().setDraggable(Element.DRAGGABLE_FALSE);
74                  getElement().getStyle().setCursor(Style.Cursor.DEFAULT);
75              }
76          }
77      }
78  
79      protected void createControls() {
80          if (editable) {
81              final Label edit = new Label();
82              edit.setStyleName(ICON_CLASS_NAME);
83              edit.addStyleName(EDIT_CLASS_NAME);
84              eventManager.addClickOrTouchHandler(edit, new ControlBarEventHandler() {
85                  @Override
86                  public void handle(NativeEvent event) {
87                      listener.editComponent();
88                  }
89              });
90              addButton(edit);
91          }
92          if (movable) {
93              final Label move = new Label();
94              move.setStyleName(ICON_CLASS_NAME);
95              move.addStyleName(MOVE_ICON_CLASS_NAME);
96              eventManager.addClickOrTouchHandler(move, new ControlBarEventHandler() {
97                  @Override
98                  public void handle(NativeEvent event) {
99                      listener.onMoveStart(false);
100                 }
101             });
102             addButton(move);
103         }
104     }
105 
106     private void registerDragStartHandler() {
107         eventManager.addDragStartHandler(this, new ControlBarEventHandler() {
108             @Override
109             public void handle(NativeEvent event) {
110                 event.getDataTransfer().setData("text", "dummyPayload");
111                 event.getDataTransfer().setDragImage(getElement(), 10, 10);
112                 listener.onMoveStart(true);
113             }
114         });
115 
116         eventManager.addDragEndHandler(this, new ControlBarEventHandler() {
117             @Override
118             public void handle(NativeEvent event) {
119                 listener.onMoveCancel();
120             }
121         });
122     }
123 
124     public void registerDragAndDropHandlers() {
125         eventManager.addDragOverHandler(this, new ControlBarEventHandler() {
126             @Override
127             public void handle(NativeEvent event) {
128                 setMoveOver(true);
129                 event.stopPropagation();
130             }
131         });
132 
133 
134         eventManager.addDragLeaveHandler(this, new ControlBarEventHandler() {
135             @Override
136             public void handle(NativeEvent event) {
137                 setMoveOver(false);
138                 event.stopPropagation();
139             }
140         });
141 
142         eventManager.addDropHandler(this, new ControlBarEventHandler() {
143             @Override
144             public void handle(NativeEvent event) {
145                 listener.onMoveStop();
146                 event.preventDefault();
147             }
148         });
149     }
150 
151     public void unregisterDragAndDropHandlers() {
152         eventManager.unregisterDnDHandlers(this);
153     }
154 
155     public void registerMoveHandlers() {
156         eventManager.addMouseDownHandler(this, new ControlBarEventHandler() {
157             @Override
158             public void handle(NativeEvent event) {
159                 listener.onMoveStop();
160             }
161         });
162 
163         eventManager.addMouseOverHandler(this, new ControlBarEventHandler() {
164             @Override
165             public void handle(NativeEvent event) {
166                 setMoveOver(true);
167             }
168         });
169 
170         eventManager.addMouseOutHandler(this, new ControlBarEventHandler() {
171             @Override
172             public void handle(NativeEvent event) {
173                 setMoveOver(false);
174             }
175         });
176     }
177 
178     public void unregisterMoveHandlers() {
179         eventManager.unregisterMoveHandlers(this);
180     }
181 
182     public void setMoveTarget(boolean target) {
183         setStyleName(MOVE_TARGET_CLASS_NAME, target);
184     }
185 
186     public void setMoveOver(boolean over) {
187         setStyleName(MOVE_OVER_CLASS_NAME, over);
188     }
189 
190     public void setMoveSource(boolean source) {
191         setStyleName(MOVE_SOURCE_CLASS_NAME, source);
192     }
193 
194     /**
195      * Builder for the component-bar widget.
196      */
197     public static class Builder {
198 
199         private ComponentListener listener;
200         private boolean isMovable;
201         private int level;
202         private String label;
203         private int activationStatus;
204         private boolean edit;
205 
206         public Builder withListener(ComponentListener listener) {
207             this.listener = listener;
208             return this;
209         }
210 
211         public Builder withMovable(boolean movable) {
212             this.isMovable = movable;
213             return this;
214         }
215 
216         public Builder withLevel(int level) {
217             this.level = level;
218             return this;
219         }
220 
221         public Builder withLabel(String label) {
222             this.label = label;
223             return this;
224         }
225 
226         public Builder withActivationStatus(int activationStatus) {
227             this.activationStatus = activationStatus;
228             return this;
229         }
230 
231         public Builder withEdit(boolean edit) {
232             this.edit = edit;
233             return this;
234         }
235 
236         public ComponentBar build() {
237             ComponentBar componentBar = GWT.create(ComponentBar.class);
238             componentBar.listener = listener;
239             componentBar.editable = edit;
240             componentBar.movable = isMovable;
241             componentBar.initLayout();
242             componentBar.createControls();
243             componentBar.addLabel(label, level);
244             componentBar.addStatusIndicator(activationStatus);
245 
246             if (isMovable && DragDropEventBase.isSupported()) {
247                 componentBar.registerDragStartHandler();
248                 componentBar.setDraggable(true);
249             }
250 
251             return componentBar;
252         }
253     }
254 }