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.dnd;
35  
36  import info.magnolia.ui.vaadin.gwt.client.widget.PageEditorFrame;
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  
40  import com.google.gwt.core.client.GWT;
41  import com.google.gwt.dom.client.BodyElement;
42  import com.google.gwt.dom.client.Element;
43  import com.google.gwt.dom.client.NativeEvent;
44  import com.google.gwt.dom.client.Style.Unit;
45  import com.google.gwt.event.dom.client.HasMouseMoveHandlers;
46  import com.google.gwt.event.dom.client.MouseMoveEvent;
47  import com.google.gwt.event.dom.client.MouseMoveHandler;
48  import com.google.gwt.event.shared.HandlerRegistration;
49  import com.google.gwt.user.client.ui.Widget;
50  
51  /**
52   * Widget used for moving {@link info.magnolia.ui.vaadin.gwt.client.widget.controlbar.ComponentBar}.
53   * Wraps the moved widget and attaches {@link MouseMoveHandler} to the {@link BodyElement} of the {@link PageEditorFrame}.
54   */
55  public class MoveWidget extends Widget {
56  
57      public static final String CLASS_NAME = "mgnlEditorMoveDiv";
58      public static final int OFFSET_FROM_MOUSE = 15;
59  
60      private FrameBodyWrapper wrapper;
61  
62      private PageEditorFrame frame;
63  
64      private ControlBarEventManager eventManager = GWT.create(ControlBarEventManager.class);
65  
66      public MoveWidget(Element element) {
67          setElement(element);
68          getElement().setId(CLASS_NAME);
69      }
70  
71      public void attach(final PageEditorFrame frame, final int width, final int height) {
72          this.frame = frame;
73  
74          // no other way to get the value marked !important
75          String widthImp = "width: " +  width + Unit.PX.getType() + " !important;";
76          String heightImp = "height: " +  height + Unit.PX.getType() + " !important;";
77          getElement().setAttribute("style", widthImp + heightImp);
78  
79          final BodyElement frameBody = frame.getBody();
80          frameBody.appendChild(this.getElement());
81  
82          this.wrapper = new FrameBodyWrapper(frameBody);
83          wrapper.onAttach();
84  
85          eventManager.addMouseMoveHandler(wrapper, new ControlBarEventHandler() {
86              @Override
87              public void handle(NativeEvent event) {
88                  int x = event.getClientX() + frame.getContentDocument().getScrollLeft();
89                  int y = event.getClientY() + OFFSET_FROM_MOUSE + frame.getContentDocument().getScrollTop();
90                  int maxX = frame.getBody().getOffsetWidth() - width;
91                  int maxY = frame.getBody().getOffsetHeight() - height - OFFSET_FROM_MOUSE;
92  
93                  x = (x > maxX) ? maxX : x;
94                  y = (y > maxY) ? maxY : y;
95  
96                  getElement().getStyle().setTop(y, Unit.PX);
97                  getElement().getStyle().setLeft(x, Unit.PX);
98              }
99          });
100         super.onAttach();
101 
102     }
103 
104     public void detach() {
105         frame.getBody().removeChild(this.getElement());
106         eventManager.removeMouseMoveHandler(wrapper);
107         wrapper.onDetach();
108         super.onDetach();
109     }
110 
111     /**
112      * Wrapper to attach {@link HasMouseMoveHandlers} to the frames body element.
113      */
114     class FrameBodyWrapper extends Widget implements HasMouseMoveHandlers {
115 
116         FrameBodyWrapper(Element frame) {
117             setElement(frame);
118         }
119 
120         @Override
121         public HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler) {
122             return addDomHandler(handler, MouseMoveEvent.getType());
123         }
124 
125         @Override
126         protected void onAttach() {
127             super.onAttach();
128         }
129 
130         @Override
131         protected void onDetach() {
132             super.onDetach();
133         }
134     }
135 
136 }