View Javadoc

1   /**
2    * This file Copyright (c) 2011-2013 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.listener.ComponentListener;
38  
39  import java.util.Iterator;
40  import java.util.LinkedList;
41  import java.util.List;
42  
43  import com.google.gwt.dom.client.Style;
44  import com.google.gwt.event.dom.client.DragDropEventBase;
45  import com.google.gwt.event.dom.client.DragEndEvent;
46  import com.google.gwt.event.dom.client.DragEndHandler;
47  import com.google.gwt.event.dom.client.DragLeaveEvent;
48  import com.google.gwt.event.dom.client.DragLeaveHandler;
49  import com.google.gwt.event.dom.client.DragOverEvent;
50  import com.google.gwt.event.dom.client.DragOverHandler;
51  import com.google.gwt.event.dom.client.DragStartEvent;
52  import com.google.gwt.event.dom.client.DragStartHandler;
53  import com.google.gwt.event.dom.client.DropEvent;
54  import com.google.gwt.event.dom.client.DropHandler;
55  import com.google.gwt.event.dom.client.MouseDownEvent;
56  import com.google.gwt.event.dom.client.MouseDownHandler;
57  import com.google.gwt.event.dom.client.MouseOutEvent;
58  import com.google.gwt.event.dom.client.MouseOutHandler;
59  import com.google.gwt.event.dom.client.MouseOverEvent;
60  import com.google.gwt.event.dom.client.MouseOverHandler;
61  import com.google.gwt.event.shared.HandlerRegistration;
62  import com.google.gwt.user.client.Element;
63  import com.google.gwt.user.client.ui.Label;
64  import com.googlecode.mgwt.dom.client.event.touch.TouchEndEvent;
65  import com.googlecode.mgwt.dom.client.event.touch.TouchEndHandler;
66  import com.googlecode.mgwt.ui.client.widget.touch.TouchDelegate;
67  
68  /**
69   * Control bar for components. Injected at the beginning of a component.
70   */
71  public class ComponentBar extends AbstractBar {
72  
73      private static final String MOVE_ICON_CLASS_NAME = "icon-move";
74      private static final String MOVE_SOURCE_CLASS_NAME = "moveSource";
75      private static final String MOVE_TARGET_CLASS_NAME = "moveTarget";
76      private static final String MOVE_OVER_CLASS_NAME = "moveOver";
77  
78      private final ComponentListener listener;
79      List<HandlerRegistration> dndHandlers = new LinkedList<HandlerRegistration>();
80      List<HandlerRegistration> moveHandlers = new LinkedList<HandlerRegistration>();
81  
82      public ComponentBar(MgnlComponent mgnlElement) {
83          super(mgnlElement);
84  
85          this.listener = mgnlElement;
86          addStyleName(COMPONENT_CLASS_NAME);
87  
88          initLayout();
89  
90          if (listener.isMovable() && DragDropEventBase.isSupported()) {
91              registerDragStartHandler();
92              setDraggable(true);
93          }
94      }
95  
96      public void setDraggable(boolean draggable) {
97          if (DragDropEventBase.isSupported()) {
98              if (draggable) {
99                  this.getElement().setDraggable(Element.DRAGGABLE_TRUE);
100                 getStyle().setCursor(Style.Cursor.MOVE);
101             } else {
102                 this.getElement().setDraggable(Element.DRAGGABLE_FALSE);
103                 getStyle().setCursor(Style.Cursor.DEFAULT);
104             }
105         }
106     }
107 
108     @Override
109     protected String getLabel() {
110         return listener.getLabel();
111     }
112 
113     @Override
114     protected void createControls() {
115         if (listener.hasEditButton()) {
116             final Label edit = new Label();
117             edit.setStyleName(ICON_CLASS_NAME);
118             edit.addStyleName(EDIT_CLASS_NAME);
119 
120             TouchDelegate td = new TouchDelegate(edit);
121             td.addTouchEndHandler(new TouchEndHandler() {
122                 @Override
123                 public void onTouchEnd(TouchEndEvent touchEndEvent) {
124                     listener.editComponent();
125                 }
126             });
127 
128             addButton(edit);
129         }
130         if (listener.isMovable()) {
131             final Label move = new Label();
132             move.setStyleName(ICON_CLASS_NAME);
133             move.addStyleName(MOVE_ICON_CLASS_NAME);
134 
135             TouchDelegate td = new TouchDelegate(move);
136             td.addTouchEndHandler(new TouchEndHandler() {
137                 @Override
138                 public void onTouchEnd(TouchEndEvent touchEndEvent) {
139                     listener.onMoveStart(false);
140                 }
141             });
142 
143             addButton(move);
144         }
145 
146     }
147     private void registerDragStartHandler() {
148 
149         addDomHandler(new DragStartHandler() {
150             @Override
151             public void onDragStart(DragStartEvent event) {
152                 event.setData("text", "dummyPayload");
153                 event.getDataTransfer().setDragImage(getElement(), 10, 10);
154 
155                 listener.onMoveStart(true);
156             }
157         }, DragStartEvent.getType());
158 
159         addDomHandler(new DragEndHandler() {
160             @Override
161             public void onDragEnd(DragEndEvent event) {
162                 listener.onMoveCancel();
163             }
164         }, DragEndEvent.getType());
165 
166     }
167     public void registerDragAndDropHandlers() {
168 
169         dndHandlers.add(addDomHandler(new DragOverHandler() {
170             @Override
171             public void onDragOver(DragOverEvent event) {
172                 setMoveOver(true);
173                 event.stopPropagation();
174             }
175         }, DragOverEvent.getType()));
176 
177         dndHandlers.add(addDomHandler(new DragLeaveHandler() {
178 
179             @Override
180             public void onDragLeave(DragLeaveEvent event) {
181                 setMoveOver(false);
182                 event.stopPropagation();
183             }
184         }, DragLeaveEvent.getType()));
185 
186         dndHandlers.add(addDomHandler(new DropHandler() {
187             @Override
188             public void onDrop(DropEvent event) {
189                 listener.onMoveStop();
190                 event.preventDefault();
191             }
192         }, DropEvent.getType()));
193     }
194 
195     public void unregisterDragAndDropHandlers() {
196         Iterator<HandlerRegistration> it = dndHandlers.iterator();
197         while (it.hasNext()) {
198             it.next().removeHandler();
199             it.remove();
200         }
201     }
202 
203     public void registerMoveHandlers() {
204 
205         moveHandlers.add(addDomHandler(new MouseDownHandler() {
206 
207             @Override
208             public void onMouseDown(MouseDownEvent event) {
209                 listener.onMoveStop();
210             }
211         }, MouseDownEvent.getType()));
212 
213         moveHandlers.add(addDomHandler(new MouseOverHandler() {
214 
215             @Override
216             public void onMouseOver(MouseOverEvent event) {
217                 setMoveOver(true);
218             }
219         }, MouseOverEvent.getType()));
220 
221         moveHandlers.add(addDomHandler(new MouseOutHandler() {
222 
223             @Override
224             public void onMouseOut(MouseOutEvent event) {
225                 setMoveOver(false);
226             }
227         }, MouseOutEvent.getType()));
228     }
229 
230     public void unregisterMoveHandlers() {
231         Iterator<HandlerRegistration> it = moveHandlers.iterator();
232         while (it.hasNext()) {
233             it.next().removeHandler();
234             it.remove();
235         }
236     }
237 
238     public void setMoveTarget(boolean target) {
239         setStyleName(MOVE_TARGET_CLASS_NAME, target);
240     }
241 
242     public void setMoveOver(boolean over) {
243         setStyleName(MOVE_OVER_CLASS_NAME, over);
244     }
245 
246     public void setMoveSource(boolean source) {
247         setStyleName(MOVE_SOURCE_CLASS_NAME, source);
248     }
249 
250 }