-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
WindowEx.java
173 lines (144 loc) · 4.75 KB
/
WindowEx.java
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* WindowEx.java
*
* Copyright (C) 2009-12 by RStudio, Inc.
*
* This program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
package org.rstudio.core.client.dom;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArrayInteger;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.event.shared.HasHandlers;
import org.rstudio.core.client.Point;
public class WindowEx extends JavaScriptObject
{
public static native WindowEx get() /*-{
return $wnd;
}-*/;
protected WindowEx()
{
}
public final native void focus() /*-{
this.focus();
}-*/;
public final native void print() /*-{
this.print() ;
}-*/;
public final native void back() /*-{
this.history.back() ;
}-*/;
public final native void forward() /*-{
this.history.forward() ;
}-*/;
public final native void removeSelection() /*-{
selection = this.getSelection();
if (selection != null) {
selection.removeAllRanges();
}
}-*/;
public final native boolean find(String term,
boolean matchCase,
boolean searchUpward,
boolean wrapAround,
boolean wholeWord) /*-{
return this.find(term, matchCase, searchUpward, wrapAround, wholeWord);
}-*/;
public final native String getLocationHref() /*-{
return this.location.href ;
}-*/;
public final native boolean isSecure() /*-{
return 'https:' == this.location.protocol;
}-*/;
public final native void reload() /*-{
this.location.reload();
}-*/;
public final native void setLocationHref(String helpURL) /*-{
this.location.href = helpURL ;
}-*/;
public final native void replaceLocationHref(String helpURL) /*-{
this.location.replace(helpURL) ;
}-*/;
public final Point getScrollPosition()
{
JsArrayInteger pos = getScrollPositionInternal();
return new Point(pos.get(0), pos.get(1));
}
public final void setScrollPosition(Point pos)
{
setScrollPositionInternal(pos.x, pos.y);
}
private final native JsArrayInteger getScrollPositionInternal() /*-{
return [this.scrollX, this.scrollY];
}-*/;
private final native void setScrollPositionInternal(int x, int y) /*-{
this.scrollTo(x, y);
}-*/;
public final native void close() /*-{
this.close();
}-*/;
public final native boolean isClosed() /*-{
return this.closed;
}-*/;
public final native void resizeTo(int width, int height) /*-{
this.resizeTo(width, height);
}-*/;
public final native Document getDocument() /*-{
return this.document;
}-*/;
public static HandlerRegistration addFocusHandler(FocusHandler handler)
{
return handlers_.addHandler(FocusEvent.getType(), handler);
}
public static HandlerRegistration addBlurHandler(BlurHandler handler)
{
return handlers_.addHandler(BlurEvent.getType(), handler);
}
private static void fireFocusHandlers()
{
NativeEvent nativeEvent = Document.get().createFocusEvent();
FocusEvent.fireNativeEvent(nativeEvent, new HasHandlers()
{
public void fireEvent(GwtEvent<?> event)
{
handlers_.fireEvent(event);
}
});
}
private static void fireBlurHandlers()
{
NativeEvent nativeEvent = Document.get().createBlurEvent();
BlurEvent.fireNativeEvent(nativeEvent, new HasHandlers()
{
public void fireEvent(GwtEvent<?> event)
{
handlers_.fireEvent(event);
}
});
}
static {
registerNativeListeners();
}
private static native void registerNativeListeners() /*-{
$wnd.onfocus = function() {
@org.rstudio.core.client.dom.WindowEx::fireFocusHandlers()();
};
$wnd.onblur = function() {
@org.rstudio.core.client.dom.WindowEx::fireBlurHandlers()();
};
}-*/;
private static final HandlerManager handlers_ = new HandlerManager(null);
}