1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
42
43
44
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
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
80
81
82
83
84 public void onMouseDownFromChild(Widget sender, int x, int y) {
85
86
87
88
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 }