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  // UTGB Gallery Project
18  //
19  // TrackRangeSelector.java
20  // Since: Apr 3, 2007
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.track;
26  
27  import org.utgenome.gwt.utgb.client.ui.AbsoluteFocusPanel;
28  import org.utgenome.gwt.widget.client.Style;
29  
30  import com.google.gwt.core.client.GWT;
31  import com.google.gwt.event.dom.client.MouseDownEvent;
32  import com.google.gwt.event.dom.client.MouseDownHandler;
33  import com.google.gwt.event.dom.client.MouseMoveEvent;
34  import com.google.gwt.event.dom.client.MouseMoveHandler;
35  import com.google.gwt.user.client.Element;
36  import com.google.gwt.user.client.Event;
37  import com.google.gwt.user.client.ui.HTML;
38  import com.google.gwt.user.client.ui.Widget;
39  
40  /**
41   * {@link TrackRangeSelector} supports range selection on a displayed track. In order to use {@link TrackRangeSelector},
42   * your track must implement the {@link RangeSelectable} interface.
43   * 
44   * @author leo
45   * 
46   */
47  public class TrackRangeSelector implements MouseDownHandler, MouseMoveHandler {
48  	boolean isInRangeSelectMode = false;
49  	HTML rangeIndicator = new HTML();
50  
51  	RangeSelectable track;
52  	AbsoluteFocusPanel trackPanel;
53  	int x1 = -1;
54  	int x2 = -1;
55  	int windowWidth = 800;
56  
57  	public TrackRangeSelector(RangeSelectable track) {
58  		this.track = track;
59  		this.trackPanel = track.getAbsoluteFocusPanel();
60  
61  		Style.fontSize(rangeIndicator, 8);
62  		Style.border(rangeIndicator, 1, "solid", "#66CCFF");
63  		Style.margin(rangeIndicator, 0);
64  		Style.padding(rangeIndicator, 0);
65  		Style.zIndex(rangeIndicator, 2000);
66  		//rangeIndicator.setStyleName("track-range");
67  		rangeIndicator.setHeight("10px");
68  
69  		trackPanel.addMouseMoveHandler(this);
70  		trackPanel.addMouseDownHandler(this);
71  
72  	}
73  
74  	public void setWindowWidth(int windowWidth) {
75  		this.windowWidth = windowWidth;
76  	}
77  
78  	/**
79  	 * An event handler when a mouse down event is reported from a child widget of the trackPanel.
80  	 * 
81  	 * @param x
82  	 * @param y
83  	 */
84  	public void onMouseDownFromChild(Widget sender, int x, int y) {
85  		//		int xOnWindow = x - trackPanel.getFocusPanel().getAbsoluteLeft();
86  		//		if (xOnWindow < 0)
87  		//			xOnWindow = 0;
88  		//		onMouseDown(trackPanel.getFocusPanel(), xOnWindow, y);
89  		onMouseDown(trackPanel.getFocusPanel().getElement(), x + sender.getAbsoluteLeft() - trackPanel.getAbsoluteLeft(), y);
90  	}
91  
92  	public void onMouseDownFromChild(MouseDownEvent e) {
93  		onMouseDown(trackPanel.getFocusPanel().getElement(), e.getScreenX() - trackPanel.getAbsoluteLeft(), e.getY());
94  
95  	}
96  
97  	public void onMouseDown(MouseDownEvent e) {
98  		onMouseDown((Element) e.getRelativeElement().cast(), e.getX(), e.getY());
99  	}
100 
101 	public void onMouseDown(Element element, int x, int y) {
102 
103 		if (!isInRangeSelectMode) {
104 			GWT.log("range start: x1= " + x, null);
105 			Event.setCapture(element);
106 			x1 = x;
107 			rangeIndicator.setWidth("1px");
108 			trackPanel.add(rangeIndicator, x1, 0);
109 		}
110 		else {
111 			Event.releaseCapture(element);
112 			x2 = (x < windowWidth) ? x : windowWidth;
113 			GWT.log("range   end: x2= " + x2, null);
114 			trackPanel.remove(rangeIndicator);
115 			if (x1 >= 0 && x2 >= 0)
116 				track.onRangeSelect(x1, x2);
117 		}
118 
119 		isInRangeSelectMode = !isInRangeSelectMode;
120 	}
121 
122 	public void onMouseMove(MouseMoveEvent e) {
123 		if (!isInRangeSelectMode)
124 			return;
125 
126 		int x = e.getX();
127 		int width = x - x1;
128 		if (width <= 0) {
129 			rangeIndicator.removeFromParent();
130 			trackPanel.add(rangeIndicator, x, 0);
131 			width = -width;
132 		}
133 		if (width >= windowWidth)
134 			width = windowWidth;
135 		rangeIndicator.setWidth(width + "px");
136 	}
137 
138 }