litesoft
@ 948
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
/* * Copyright 2008 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package com.google.gwt.widgetideas.client; import com.google.gwt.event.logical.shared.*; import com.google.gwt.event.shared.*; import com.google.gwt.user.client.Timer; import com.google.gwt.user.client.*; import java.util.*; /** * A collection of {@link ResizableWidget} that periodically checks the outer * dimensions of a widget and redraws it as necessary. Every * {@link ResizableWidgetCollection} uses a timer, so consider the cost when * adding one. * <p/> * Typically, a {@link ResizableWidgetCollection} is only needed if you expect * your widgets to resize based on window resizing or other events. Fixed sized * Widgets do not need to be added to a {@link ResizableWidgetCollection} as * they cannot be resized. */ public class ResizableWidgetCollection implements WindowResizeListener, Iterable<ResizableWidget> { /** * Information about a widgets size. */ static class ResizableWidgetInfo { private ResizableWidget widget; private int curOffsetHeight = 0; private int curOffsetWidth = 0; private int curClientHeight = 0; private int curClientWidth = 0; /** * Constructor. * * @param widget the widget that will be monitored */ public ResizableWidgetInfo( ResizableWidget widget ) { this.widget = widget; updateSizes(); } public int getClientHeight() { return curClientHeight; } public int getClientWidth() { return curClientWidth; } public int getOffsetHeight() { return curOffsetHeight; } public int getOffsetWidth() { return curOffsetWidth; } /** * Update the current sizes. * * @return true if the sizes changed, false if not. */ public boolean updateSizes() { int offsetWidth = widget.getElement().getOffsetWidth(); int offsetHeight = widget.getElement().getOffsetHeight(); int clientWidth = widget.getElement().getClientWidth(); int clientHeight = widget.getElement().getClientHeight(); if ( offsetWidth != curOffsetWidth || offsetHeight != curOffsetHeight || clientWidth != curClientWidth || clientHeight != curClientHeight ) { this.curOffsetWidth = offsetWidth; this.curOffsetHeight = offsetHeight; this.curClientWidth = clientWidth; this.curClientHeight = clientHeight; return true; } return false; } } /** * The default delay between resize checks in milliseconds. */ private static final int DEFAULT_RESIZE_CHECK_DELAY = 400; /** * A static {@link ResizableWidgetCollection} that can be used in most cases. */ private static ResizableWidgetCollection staticCollection = null; /** * Get the globally accessible {@link ResizableWidgetCollection}. In most * cases, the global collection can be used for all {@link ResizableWidget}s. * * @return the global {@link ResizableWidgetCollection} */ public static ResizableWidgetCollection get() { if ( staticCollection == null ) { staticCollection = new ResizableWidgetCollection(); } return staticCollection; } /** * The timer used to periodically compare the dimensions of elements to their * old dimensions. */ private Timer resizeCheckTimer = new Timer() { @Override public void run() { // Ignore changes that result from window resize events if ( windowHeight != Window.getClientHeight() || windowWidth != Window.getClientWidth() ) { windowHeight = Window.getClientHeight(); windowWidth = Window.getClientWidth(); schedule( resizeCheckDelay ); return; } // Look for elements that have new dimensions checkWidgetSize(); // Start checking again if ( resizeCheckingEnabled ) { schedule( resizeCheckDelay ); } } }; /** * A hash map of the resizable widgets this collection is checking. */ private Map<ResizableWidget, ResizableWidgetInfo> widgets = new HashMap<ResizableWidget, ResizableWidgetInfo>(); /** * The current window height. */ private int windowHeight = 0; /** * The current window width. */ private int windowWidth = 0; /** * The hook used to remove the window handler. */ private HandlerRegistration windowHandler; /** * The delay between resize checks. */ private int resizeCheckDelay = DEFAULT_RESIZE_CHECK_DELAY; /** * A boolean indicating that resize checking should run. */ private boolean resizeCheckingEnabled; /** * Create a ResizableWidget. */ public ResizableWidgetCollection() { this( DEFAULT_RESIZE_CHECK_DELAY ); } /** * Constructor. * * @param resizeCheckingEnabled false to disable resize checking */ public ResizableWidgetCollection( boolean resizeCheckingEnabled ) { this( DEFAULT_RESIZE_CHECK_DELAY, resizeCheckingEnabled ); } /** * Constructor. * * @param resizeCheckDelay the delay between checks in milliseconds */ public ResizableWidgetCollection( int resizeCheckDelay ) { this( resizeCheckDelay, true ); } /** * Constructor. */ protected ResizableWidgetCollection( int resizeCheckDelay, boolean resizeCheckingEnabled ) { setResizeCheckDelay( resizeCheckDelay ); setResizeCheckingEnabled( resizeCheckingEnabled ); } /** * Add a resizable widget to the collection. * * @param widget the resizable widget to add */ public void add( ResizableWidget widget ) { widgets.put( widget, new ResizableWidgetInfo( widget ) ); } /** * Check to see if any Widgets have been resized and call their handlers * appropriately. */ public void checkWidgetSize() { for ( Map.Entry<ResizableWidget, ResizableWidgetInfo> entry : widgets.entrySet() ) { ResizableWidget widget = entry.getKey(); ResizableWidgetInfo info = entry.getValue(); // Call the onResize method only if the widget is attached if ( info.updateSizes() ) { // Check that the offset width and height are greater than 0. if ( info.getOffsetWidth() > 0 && info.getOffsetHeight() > 0 && widget.isAttached() ) { // Send the client dimensions, which is the space available for // rendering. widget.onResize( info.getOffsetWidth(), info.getOffsetHeight() ); } } } } /** * Get the delay between resize checks in milliseconds. * * @return the resize check delay */ public int getResizeCheckDelay() { return resizeCheckDelay; } /** * Check whether or not resize checking is enabled. * * @return true is resize checking is enabled */ public boolean isResizeCheckingEnabled() { return resizeCheckingEnabled; } @Override public Iterator<ResizableWidget> iterator() { return widgets.keySet().iterator(); } /** * Called when the browser window is resized. * * @param width the width of the window's client area. * @param height the height of the window's client area. */ @Deprecated @Override public void onWindowResized( int width, int height ) { checkWidgetSize(); } /** * Remove a {@link ResizableWidget} from the collection. * * @param widget the widget to remove */ public void remove( ResizableWidget widget ) { widgets.remove( widget ); } /** * Set the delay between resize checks in milliseconds. * * @param resizeCheckDelay the new delay */ public void setResizeCheckDelay( int resizeCheckDelay ) { this.resizeCheckDelay = resizeCheckDelay; } /** * Set whether or not resize checking is enabled. If disabled, elements will * still be resized on window events, but the timer will not check their * dimensions periodically. * * @param enabled true to enable the resize checking timer */ public void setResizeCheckingEnabled( boolean enabled ) { if ( enabled && !resizeCheckingEnabled ) { resizeCheckingEnabled = true; if ( windowHandler == null ) { windowHandler = Window.addResizeHandler( new ResizeHandler() { @Override public void onResize( ResizeEvent event ) { onWindowResized( event.getWidth(), event.getHeight() ); } } ); } resizeCheckTimer.schedule( resizeCheckDelay ); } else if ( !enabled && resizeCheckingEnabled ) { resizeCheckingEnabled = false; if ( windowHandler != null ) { windowHandler.removeHandler(); windowHandler = null; } resizeCheckTimer.cancel(); } } /** * Inform the {@link ResizableWidgetCollection} that the size of a widget has * changed and already been redrawn. This will prevent the widget from being * redrawn on the next loop. * * @param widget the widget's size that changed */ public void updateWidgetSize( ResizableWidget widget ) { if ( !widget.isAttached() ) { return; } ResizableWidgetInfo info = widgets.get( widget ); if ( info != null ) { info.updateSizes(); } } } |