View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2007 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // GenomeBrowser Project
18  //
19  // MouseMoveListener.java
20  // Since: Jun 27, 2007
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.ui;
26  
27  import com.google.gwt.event.dom.client.HasAllMouseHandlers;
28  import com.google.gwt.event.dom.client.MouseDownEvent;
29  import com.google.gwt.event.dom.client.MouseDownHandler;
30  import com.google.gwt.event.dom.client.MouseMoveEvent;
31  import com.google.gwt.event.dom.client.MouseMoveHandler;
32  import com.google.gwt.event.dom.client.MouseUpEvent;
33  import com.google.gwt.event.dom.client.MouseUpHandler;
34  import com.google.gwt.user.client.Event;
35  import com.google.gwt.user.client.ui.PopupPanel;
36  
37  public class MouseMoveListener implements MouseDownHandler, MouseMoveHandler, MouseUpHandler {
38  
39  	PopupPanel widgetToMove;
40  	boolean isDragged = false;
41  	int dragStartX;
42  	int dragStartY;
43  
44  	public MouseMoveListener(PopupPanel widgetToMove) {
45  		this.widgetToMove = widgetToMove;
46  	}
47  
48  	public void register(HasAllMouseHandlers w) {
49  		w.addMouseDownHandler(this);
50  		w.addMouseMoveHandler(this);
51  		w.addMouseUpHandler(this);
52  	}
53  
54  	public void onMouseDown(MouseDownEvent e) {
55  		isDragged = true;
56  		dragStartX = e.getX();
57  		dragStartY = e.getY();
58  		Event.setCapture(e.getRelativeElement());
59  	}
60  
61  	public void onMouseMove(MouseMoveEvent e) {
62  		if (isDragged) {
63  			widgetToMove.setPopupPosition(widgetToMove.getAbsoluteLeft() + e.getX() - dragStartX, widgetToMove.getAbsoluteTop() + e.getY() - dragStartY);
64  		}
65  	}
66  
67  	public void onMouseUp(MouseUpEvent e) {
68  		isDragged = false;
69  		Event.releaseCapture(e.getRelativeElement());
70  	}
71  
72  }