|
@@ -15,13 +15,13 @@ |
15 |
15 |
|
*/ |
16 |
16 |
|
package com.google.gwt.widgetideas.client; |
17 |
17 |
|
|
18 |
|
- |
import java.util.*; |
19 |
|
- |
|
20 |
18 |
|
import com.google.gwt.event.logical.shared.*; |
21 |
19 |
|
import com.google.gwt.event.shared.*; |
22 |
20 |
|
import com.google.gwt.user.client.Timer; |
23 |
21 |
|
import com.google.gwt.user.client.*; |
24 |
22 |
|
|
|
23 |
+ |
import java.util.*; |
|
24 |
+ |
|
25 |
25 |
|
/** |
26 |
26 |
|
* A collection of {@link ResizableWidget} that periodically checks the outer |
27 |
27 |
|
* dimensions of a widget and redraws it as necessary. Every |
|
@@ -34,13 +34,11 @@ |
34 |
34 |
|
* they cannot be resized. |
35 |
35 |
|
*/ |
36 |
36 |
|
public class ResizableWidgetCollection implements WindowResizeListener, |
37 |
|
- |
Iterable<ResizableWidget> |
38 |
|
- |
{ |
|
37 |
+ |
Iterable<ResizableWidget> { |
39 |
38 |
|
/** |
40 |
39 |
|
* Information about a widgets size. |
41 |
40 |
|
*/ |
42 |
|
- |
static class ResizableWidgetInfo |
43 |
|
- |
{ |
|
41 |
+ |
static class ResizableWidgetInfo { |
44 |
42 |
|
|
45 |
43 |
|
private ResizableWidget widget; |
46 |
44 |
|
private int curOffsetHeight = 0; |
|
@@ -53,29 +51,24 @@ |
53 |
51 |
|
* |
54 |
52 |
|
* @param widget the widget that will be monitored |
55 |
53 |
|
*/ |
56 |
|
- |
public ResizableWidgetInfo( ResizableWidget widget ) |
57 |
|
- |
{ |
|
54 |
+ |
public ResizableWidgetInfo( ResizableWidget widget ) { |
58 |
55 |
|
this.widget = widget; |
59 |
56 |
|
updateSizes(); |
60 |
57 |
|
} |
61 |
58 |
|
|
62 |
|
- |
public int getClientHeight() |
63 |
|
- |
{ |
|
59 |
+ |
public int getClientHeight() { |
64 |
60 |
|
return curClientHeight; |
65 |
61 |
|
} |
66 |
62 |
|
|
67 |
|
- |
public int getClientWidth() |
68 |
|
- |
{ |
|
63 |
+ |
public int getClientWidth() { |
69 |
64 |
|
return curClientWidth; |
70 |
65 |
|
} |
71 |
66 |
|
|
72 |
|
- |
public int getOffsetHeight() |
73 |
|
- |
{ |
|
67 |
+ |
public int getOffsetHeight() { |
74 |
68 |
|
return curOffsetHeight; |
75 |
69 |
|
} |
76 |
70 |
|
|
77 |
|
- |
public int getOffsetWidth() |
78 |
|
- |
{ |
|
71 |
+ |
public int getOffsetWidth() { |
79 |
72 |
|
return curOffsetWidth; |
80 |
73 |
|
} |
81 |
74 |
|
|
|
@@ -84,14 +77,12 @@ |
84 |
77 |
|
* |
85 |
78 |
|
* @return true if the sizes changed, false if not. |
86 |
79 |
|
*/ |
87 |
|
- |
public boolean updateSizes() |
88 |
|
- |
{ |
|
80 |
+ |
public boolean updateSizes() { |
89 |
81 |
|
int offsetWidth = widget.getElement().getOffsetWidth(); |
90 |
82 |
|
int offsetHeight = widget.getElement().getOffsetHeight(); |
91 |
83 |
|
int clientWidth = widget.getElement().getClientWidth(); |
92 |
84 |
|
int clientHeight = widget.getElement().getClientHeight(); |
93 |
|
- |
if ( offsetWidth != curOffsetWidth || offsetHeight != curOffsetHeight || clientWidth != curClientWidth || clientHeight != curClientHeight ) |
94 |
|
- |
{ |
|
85 |
+ |
if ( offsetWidth != curOffsetWidth || offsetHeight != curOffsetHeight || clientWidth != curClientWidth || clientHeight != curClientHeight ) { |
95 |
86 |
|
this.curOffsetWidth = offsetWidth; |
96 |
87 |
|
this.curOffsetHeight = offsetHeight; |
97 |
88 |
|
this.curClientWidth = clientWidth; |
|
@@ -119,10 +110,8 @@ |
119 |
110 |
|
* |
120 |
111 |
|
* @return the global {@link ResizableWidgetCollection} |
121 |
112 |
|
*/ |
122 |
|
- |
public static ResizableWidgetCollection get() |
123 |
|
- |
{ |
124 |
|
- |
if ( staticCollection == null ) |
125 |
|
- |
{ |
|
113 |
+ |
public static ResizableWidgetCollection get() { |
|
114 |
+ |
if ( staticCollection == null ) { |
126 |
115 |
|
staticCollection = new ResizableWidgetCollection(); |
127 |
116 |
|
} |
128 |
117 |
|
return staticCollection; |
|
@@ -132,14 +121,11 @@ |
132 |
121 |
|
* The timer used to periodically compare the dimensions of elements to their |
133 |
122 |
|
* old dimensions. |
134 |
123 |
|
*/ |
135 |
|
- |
private Timer resizeCheckTimer = new Timer() |
136 |
|
- |
{ |
|
124 |
+ |
private Timer resizeCheckTimer = new Timer() { |
137 |
125 |
|
@Override |
138 |
|
- |
public void run() |
139 |
|
- |
{ |
|
126 |
+ |
public void run() { |
140 |
127 |
|
// Ignore changes that result from window resize events |
141 |
|
- |
if ( windowHeight != Window.getClientHeight() || windowWidth != Window.getClientWidth() ) |
142 |
|
- |
{ |
|
128 |
+ |
if ( windowHeight != Window.getClientHeight() || windowWidth != Window.getClientWidth() ) { |
143 |
129 |
|
windowHeight = Window.getClientHeight(); |
144 |
130 |
|
windowWidth = Window.getClientWidth(); |
145 |
131 |
|
schedule( resizeCheckDelay ); |
|
@@ -150,8 +136,7 @@ |
150 |
136 |
|
checkWidgetSize(); |
151 |
137 |
|
|
152 |
138 |
|
// Start checking again |
153 |
|
- |
if ( resizeCheckingEnabled ) |
154 |
|
- |
{ |
|
139 |
+ |
if ( resizeCheckingEnabled ) { |
155 |
140 |
|
schedule( resizeCheckDelay ); |
156 |
141 |
|
} |
157 |
142 |
|
} |
|
@@ -190,8 +175,7 @@ |
190 |
175 |
|
/** |
191 |
176 |
|
* Create a ResizableWidget. |
192 |
177 |
|
*/ |
193 |
|
- |
public ResizableWidgetCollection() |
194 |
|
- |
{ |
|
178 |
+ |
public ResizableWidgetCollection() { |
195 |
179 |
|
this( DEFAULT_RESIZE_CHECK_DELAY ); |
196 |
180 |
|
} |
197 |
181 |
|
|
|
@@ -200,8 +184,7 @@ |
200 |
184 |
|
* |
201 |
185 |
|
* @param resizeCheckingEnabled false to disable resize checking |
202 |
186 |
|
*/ |
203 |
|
- |
public ResizableWidgetCollection( boolean resizeCheckingEnabled ) |
204 |
|
- |
{ |
|
187 |
+ |
public ResizableWidgetCollection( boolean resizeCheckingEnabled ) { |
205 |
188 |
|
this( DEFAULT_RESIZE_CHECK_DELAY, resizeCheckingEnabled ); |
206 |
189 |
|
} |
207 |
190 |
|
|
|
@@ -210,16 +193,14 @@ |
210 |
193 |
|
* |
211 |
194 |
|
* @param resizeCheckDelay the delay between checks in milliseconds |
212 |
195 |
|
*/ |
213 |
|
- |
public ResizableWidgetCollection( int resizeCheckDelay ) |
214 |
|
- |
{ |
|
196 |
+ |
public ResizableWidgetCollection( int resizeCheckDelay ) { |
215 |
197 |
|
this( resizeCheckDelay, true ); |
216 |
198 |
|
} |
217 |
199 |
|
|
218 |
200 |
|
/** |
219 |
201 |
|
* Constructor. |
220 |
202 |
|
*/ |
221 |
|
- |
protected ResizableWidgetCollection( int resizeCheckDelay, boolean resizeCheckingEnabled ) |
222 |
|
- |
{ |
|
203 |
+ |
protected ResizableWidgetCollection( int resizeCheckDelay, boolean resizeCheckingEnabled ) { |
223 |
204 |
|
setResizeCheckDelay( resizeCheckDelay ); |
224 |
205 |
|
setResizeCheckingEnabled( resizeCheckingEnabled ); |
225 |
206 |
|
} |
|
@@ -229,8 +210,7 @@ |
229 |
210 |
|
* |
230 |
211 |
|
* @param widget the resizable widget to add |
231 |
212 |
|
*/ |
232 |
|
- |
public void add( ResizableWidget widget ) |
233 |
|
- |
{ |
|
213 |
+ |
public void add( ResizableWidget widget ) { |
234 |
214 |
|
widgets.put( widget, new ResizableWidgetInfo( widget ) ); |
235 |
215 |
|
} |
236 |
216 |
|
|
|
@@ -238,19 +218,15 @@ |
238 |
218 |
|
* Check to see if any Widgets have been resized and call their handlers |
239 |
219 |
|
* appropriately. |
240 |
220 |
|
*/ |
241 |
|
- |
public void checkWidgetSize() |
242 |
|
- |
{ |
243 |
|
- |
for ( Map.Entry<ResizableWidget, ResizableWidgetInfo> entry : widgets.entrySet() ) |
244 |
|
- |
{ |
|
221 |
+ |
public void checkWidgetSize() { |
|
222 |
+ |
for ( Map.Entry<ResizableWidget, ResizableWidgetInfo> entry : widgets.entrySet() ) { |
245 |
223 |
|
ResizableWidget widget = entry.getKey(); |
246 |
224 |
|
ResizableWidgetInfo info = entry.getValue(); |
247 |
225 |
|
|
248 |
226 |
|
// Call the onResize method only if the widget is attached |
249 |
|
- |
if ( info.updateSizes() ) |
250 |
|
- |
{ |
|
227 |
+ |
if ( info.updateSizes() ) { |
251 |
228 |
|
// Check that the offset width and height are greater than 0. |
252 |
|
- |
if ( info.getOffsetWidth() > 0 && info.getOffsetHeight() > 0 && widget.isAttached() ) |
253 |
|
- |
{ |
|
229 |
+ |
if ( info.getOffsetWidth() > 0 && info.getOffsetHeight() > 0 && widget.isAttached() ) { |
254 |
230 |
|
// Send the client dimensions, which is the space available for |
255 |
231 |
|
// rendering. |
256 |
232 |
|
widget.onResize( info.getOffsetWidth(), info.getOffsetHeight() ); |
|
@@ -264,8 +240,7 @@ |
264 |
240 |
|
* |
265 |
241 |
|
* @return the resize check delay |
266 |
242 |
|
*/ |
267 |
|
- |
public int getResizeCheckDelay() |
268 |
|
- |
{ |
|
243 |
+ |
public int getResizeCheckDelay() { |
269 |
244 |
|
return resizeCheckDelay; |
270 |
245 |
|
} |
271 |
246 |
|
|
|
@@ -274,14 +249,12 @@ |
274 |
249 |
|
* |
275 |
250 |
|
* @return true is resize checking is enabled |
276 |
251 |
|
*/ |
277 |
|
- |
public boolean isResizeCheckingEnabled() |
278 |
|
- |
{ |
|
252 |
+ |
public boolean isResizeCheckingEnabled() { |
279 |
253 |
|
return resizeCheckingEnabled; |
280 |
254 |
|
} |
281 |
255 |
|
|
282 |
256 |
|
@Override |
283 |
|
- |
public Iterator<ResizableWidget> iterator() |
284 |
|
- |
{ |
|
257 |
+ |
public Iterator<ResizableWidget> iterator() { |
285 |
258 |
|
return widgets.keySet().iterator(); |
286 |
259 |
|
} |
287 |
260 |
|
|
|
@@ -291,9 +264,9 @@ |
291 |
264 |
|
* @param width the width of the window's client area. |
292 |
265 |
|
* @param height the height of the window's client area. |
293 |
266 |
|
*/ |
294 |
|
- |
@Deprecated @Override |
295 |
|
- |
public void onWindowResized( int width, int height ) |
296 |
|
- |
{ |
|
267 |
+ |
@Deprecated |
|
268 |
+ |
@Override |
|
269 |
+ |
public void onWindowResized( int width, int height ) { |
297 |
270 |
|
checkWidgetSize(); |
298 |
271 |
|
} |
299 |
272 |
|
|
|
@@ -302,8 +275,7 @@ |
302 |
275 |
|
* |
303 |
276 |
|
* @param widget the widget to remove |
304 |
277 |
|
*/ |
305 |
|
- |
public void remove( ResizableWidget widget ) |
306 |
|
- |
{ |
|
278 |
+ |
public void remove( ResizableWidget widget ) { |
307 |
279 |
|
widgets.remove( widget ); |
308 |
280 |
|
} |
309 |
281 |
|
|
|
@@ -312,8 +284,7 @@ |
312 |
284 |
|
* |
313 |
285 |
|
* @param resizeCheckDelay the new delay |
314 |
286 |
|
*/ |
315 |
|
- |
public void setResizeCheckDelay( int resizeCheckDelay ) |
316 |
|
- |
{ |
|
287 |
+ |
public void setResizeCheckDelay( int resizeCheckDelay ) { |
317 |
288 |
|
this.resizeCheckDelay = resizeCheckDelay; |
318 |
289 |
|
} |
319 |
290 |
|
|
|
@@ -324,29 +295,21 @@ |
324 |
295 |
|
* |
325 |
296 |
|
* @param enabled true to enable the resize checking timer |
326 |
297 |
|
*/ |
327 |
|
- |
public void setResizeCheckingEnabled( boolean enabled ) |
328 |
|
- |
{ |
329 |
|
- |
if ( enabled && !resizeCheckingEnabled ) |
330 |
|
- |
{ |
|
298 |
+ |
public void setResizeCheckingEnabled( boolean enabled ) { |
|
299 |
+ |
if ( enabled && !resizeCheckingEnabled ) { |
331 |
300 |
|
resizeCheckingEnabled = true; |
332 |
|
- |
if ( windowHandler == null ) |
333 |
|
- |
{ |
334 |
|
- |
windowHandler = Window.addResizeHandler( new ResizeHandler() |
335 |
|
- |
{ |
|
301 |
+ |
if ( windowHandler == null ) { |
|
302 |
+ |
windowHandler = Window.addResizeHandler( new ResizeHandler() { |
336 |
303 |
|
@Override |
337 |
|
- |
public void onResize( ResizeEvent event ) |
338 |
|
- |
{ |
|
304 |
+ |
public void onResize( ResizeEvent event ) { |
339 |
305 |
|
onWindowResized( event.getWidth(), event.getHeight() ); |
340 |
306 |
|
} |
341 |
307 |
|
} ); |
342 |
308 |
|
} |
343 |
309 |
|
resizeCheckTimer.schedule( resizeCheckDelay ); |
344 |
|
- |
} |
345 |
|
- |
else if ( !enabled && resizeCheckingEnabled ) |
346 |
|
- |
{ |
|
310 |
+ |
} else if ( !enabled && resizeCheckingEnabled ) { |
347 |
311 |
|
resizeCheckingEnabled = false; |
348 |
|
- |
if ( windowHandler != null ) |
349 |
|
- |
{ |
|
312 |
+ |
if ( windowHandler != null ) { |
350 |
313 |
|
windowHandler.removeHandler(); |
351 |
314 |
|
windowHandler = null; |
352 |
315 |
|
} |
|
@@ -361,16 +324,13 @@ |
361 |
324 |
|
* |
362 |
325 |
|
* @param widget the widget's size that changed |
363 |
326 |
|
*/ |
364 |
|
- |
public void updateWidgetSize( ResizableWidget widget ) |
365 |
|
- |
{ |
366 |
|
- |
if ( !widget.isAttached() ) |
367 |
|
- |
{ |
|
327 |
+ |
public void updateWidgetSize( ResizableWidget widget ) { |
|
328 |
+ |
if ( !widget.isAttached() ) { |
368 |
329 |
|
return; |
369 |
330 |
|
} |
370 |
331 |
|
|
371 |
332 |
|
ResizableWidgetInfo info = widgets.get( widget ); |
372 |
|
- |
if ( info != null ) |
373 |
|
- |
{ |
|
333 |
+ |
if ( info != null ) { |
374 |
334 |
|
info.updateSizes(); |
375 |
335 |
|
} |
376 |
336 |
|
} |