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.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      private final int width;
60      private final int height;
61  
62      private FrameBodyWrapper wrapper;
63  
64      private PageEditorFrame frame;
65  
66      private ControlBarEventManager eventManager = GWT.create(ControlBarEventManager.class);
67  
68      public MoveWidget(Element element, int width, int height) {
69          this.width = width;
70          this.height = height;
71          setElement(element);
72          getElement().setId(CLASS_NAME);
73      }
74  
75      public void attach(final PageEditorFrame frame) {
76          this.frame = frame;
77  
78          // no other way to get the value marked !important
79          String widthImp = "width: " +  width + Unit.PX.getType() + " !important;";
80          String heightImp = "height: " +  height + Unit.PX.getType() + " !important;";
81          getElement().setAttribute("style", widthImp + heightImp);
82  
83          final BodyElement frameBody = frame.getBody();
84          frameBody.appendChild(this.getElement());
85  
86          this.wrapper = new FrameBodyWrapper(frameBody);
87          wrapper.onAttach();
88  
89          eventManager.addMouseMoveHandler(wrapper, new ControlBarEventHandler() {
90              @Override
91              public void handle(NativeEvent event) {
92                  int x = event.getClientX() + frame.getContentDocument().getScrollLeft();
93                  int y = event.getClientY() + OFFSET_FROM_MOUSE + frame.getContentDocument().getScrollTop();
94                  int maxX = frame.getBody().getOffsetWidth() - width;
95                  int maxY = frame.getBody().getOffsetHeight() - height - OFFSET_FROM_MOUSE;
96  
97                  x = (x > maxX) ? maxX : x;
98                  y = (y > maxY) ? maxY : y;
99  
100                 getElement().getStyle().setTop(y, Unit.PX);
101                 getElement().getStyle().setLeft(x, Unit.PX);
102             }
103         });
104         super.onAttach();
105 
106     }
107 
108     public void detach() {
109         frame.getBody().removeChild(this.getElement());
110         eventManager.removeMouseMoveHandler(wrapper);
111         wrapper.onDetach();
112         super.onDetach();
113     }
114 
115     /**
116      * Wrapper to attach {@link HasMouseMoveHandlers} to the frames body element.
117      */
118     class FrameBodyWrapper extends Widget implements HasMouseMoveHandlers {
119 
120         FrameBodyWrapper(Element frame) {
121             setElement(frame);
122         }
123 
124         @Override
125         public HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler) {
126             return addDomHandler(handler, MouseMoveEvent.getType());
127         }
128 
129         @Override
130         protected void onAttach() {
131             super.onAttach();
132         }
133 
134         @Override
135         protected void onDetach() {
136             super.onDetach();
137         }
138     }
139 
140 }