1 /*
2 * Copyright 2007 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16 package com.google.gwt.widgetideas.client;
17
18 import com.google.gwt.user.client.DOM;
19 import com.google.gwt.user.client.Timer;
20 import com.google.gwt.user.client.Window;
21 import com.google.gwt.user.client.WindowResizeListener;
22
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.Map;
26
27 /**
28 * A collection of {@link ResizableWidget} that periodically checks the outer
29 * dimensions of a widget and redraws it as necessary. Every
30 * {@link ResizableWidgetCollection} uses a timer, so consider the cost when
31 * adding one.
32 *
33 * Typically, a {@link ResizableWidgetCollection} is only needed if you expect
34 * your widgets to resize based on window resizing or other events. Fixed sized
35 * Widgets do not need to be added to a {@link ResizableWidgetCollection} as
36 * they cannot be resized.
37 */
38 public class ResizableWidgetCollection implements WindowResizeListener {
39 /**
40 * Information about a widgets size.
41 */
42 private static class ResizableWidgetInfo {
43 /**
44 * The current clientHeight.
45 */
46 private int curHeight = 0;
47
48 /**
49 * The current clientWidth.
50 */
51 private int curWidth = 0;
52
53 /**
54 * Constructor.
55 *
56 * @param widget the widget that will be monitored
57 */
58 public ResizableWidgetInfo(ResizableWidget widget) {
59 curWidth = DOM.getElementPropertyInt(widget.getElement(), "clientWidth");
60 curHeight = DOM.getElementPropertyInt(widget.getElement(), "clientHeight");
61 }
62
63 /**
64 * Set the new dimensions of the widget if they changed.
65 *
66 * @param width the new width
67 * @param height the new height
68 * @return true if the dimensions have changed
69 */
70 public boolean setClientSize(int width, int height) {
71 if (width != curWidth || height != curHeight) {
72 this.curWidth = width;
73 this.curHeight = height;
74 return true;
75 } else {
76 return false;
77 }
78 }
79 }
80
81 /**
82 * The default delay between resize checks in milliseconds.
83 */
84 private static final int DEFAULT_RESIZE_CHECK_DELAY = 400;
85
86 /**
87 * A static {@link ResizableWidgetCollection} that can be used in most cases.
88 */
89 private static ResizableWidgetCollection staticCollection = null;
90
91 /**
92 * Get the globally accessible {@link ResizableWidgetCollection}. In most
93 * cases, the global collection can be used for all {@link ResizableWidget}s.
94 *
95 * @return the global {@link ResizableWidgetCollection}
96 */
97 public static ResizableWidgetCollection get() {
98 if (staticCollection == null) {
99 staticCollection = new ResizableWidgetCollection();
100 }
101 return staticCollection;
102 }
103
104 /**
105 * The timer used to periodically compare the dimensions of elements to their
106 * old dimensions.
107 */
108 private Timer resizeCheckTimer = new Timer() {
109 public void run() {
110 // Ignore changes that result from window resize events
111 if (windowHeight != Window.getClientHeight()
112 || windowWidth != Window.getClientWidth()) {
113 windowHeight = Window.getClientHeight();
114 windowWidth = Window.getClientWidth();
115 schedule(resizeCheckDelay);
116 return;
117 }
118
119 // Look for elements that have new dimensions
120 checkWidgetSize();
121
122 // Start checking again
123 if (resizeCheckingEnabled) {
124 schedule(resizeCheckDelay);
125 }
126 }
127 };
128
129 /**
130 * A hash map of the resizable widgets this collection is checking.
131 */
132 private HashMap/* <ResizableWidget, ResizableWidgetInfo> */widgets = new HashMap();
133
134 /**
135 * The current window height.
136 */
137 private int windowHeight = 0;
138
139 /**
140 * The current window width.
141 */
142 private int windowWidth = 0;
143
144 /**
145 * The delay between resize checks.
146 */
147 private int resizeCheckDelay = DEFAULT_RESIZE_CHECK_DELAY;
148
149 /**
150 * A boolean indicating that resize checking should run.
151 */
152 private boolean resizeCheckingEnabled = true;
153
154 /**
155 * Create a ResizableWidget.
156 */
157 public ResizableWidgetCollection() {
158 this(DEFAULT_RESIZE_CHECK_DELAY);
159 }
160
161 /**
162 * Constructor.
163 *
164 * @param resizeCheckingEnabled false to disable resize checking
165 */
166 public ResizableWidgetCollection(boolean resizeCheckingEnabled) {
167 this(DEFAULT_RESIZE_CHECK_DELAY, false);
168 }
169
170 /**
171 * Constructor.
172 *
173 * @param resizeCheckDelay the delay between checks in milliseconds
174 */
175 public ResizableWidgetCollection(int resizeCheckDelay) {
176 this(resizeCheckDelay, true);
177 }
178
179 /**
180 * Constructor.
181 */
182 protected ResizableWidgetCollection(int resizeCheckDelay,
183 boolean resizeCheckingEnabled) {
184 Window.addWindowResizeListener(this);
185 setResizeCheckDelay(resizeCheckDelay);
186 if (resizeCheckingEnabled) {
187 resizeCheckTimer.schedule(resizeCheckDelay);
188 } else {
189 this.resizeCheckingEnabled = false;
190 }
191 }
192
193 /**
194 * Add a resizable widget to the collection.
195 *
196 * @param widget the resizable widget to add
197 */
198 public void add(ResizableWidget widget) {
199 widgets.put(widget, new ResizableWidgetInfo(widget));
200 }
201
202 /**
203 * Get the delay between resize checks in milliseconds.
204 *
205 * @return the resize check delay
206 */
207 public int getResizeCheckDelay() {
208 return resizeCheckDelay;
209 }
210
211 /**
212 * Check whether or not resize checking is enabled.
213 *
214 * @return true is resize checking is enabled
215 */
216 public boolean isResizeCheckingEnabled() {
217 return resizeCheckingEnabled;
218 }
219
220 /**
221 * Called when the browser window is resized.
222 *
223 * @param width the width of the window's client area.
224 * @param height the height of the window's client area.
225 */
226 public void onWindowResized(int width, int height) {
227 checkWidgetSize();
228 }
229
230 /**
231 * Remove a {@link ResizableWidget} from the collection.
232 *
233 * @param widget the widget to remove
234 */
235 public void remove(ResizableWidget widget) {
236 widgets.remove(widget);
237 }
238
239 /**
240 * Set the delay between resize checks in milliseconds.
241 *
242 * @param resizeCheckDelay the new delay
243 */
244 public void setResizeCheckDelay(int resizeCheckDelay) {
245 this.resizeCheckDelay = resizeCheckDelay;
246 }
247
248 /**
249 * Set whether or not resize checking is enabled. If disabled, elements will
250 * still be resized on window events, but the timer will not check their
251 * dimensions periodically.
252 *
253 * @param enabled true to enable the resize checking timer
254 */
255 public void setResizeCheckingEnabled(boolean enabled) {
256 if (enabled && !resizeCheckingEnabled) {
257 resizeCheckingEnabled = true;
258 resizeCheckTimer.schedule(resizeCheckDelay);
259 } else if (!enabled && resizeCheckingEnabled) {
260 resizeCheckingEnabled = false;
261 resizeCheckTimer.cancel();
262 }
263 }
264
265 /**
266 * Check to see if any Widgets have been resized and call their handlers
267 * appropriately.
268 */
269 private void checkWidgetSize() {
270 Iterator it = widgets.entrySet().iterator();
271 while (it.hasNext()) {
272 Map.Entry entry = (Map.Entry) it.next();
273 ResizableWidget widget = (ResizableWidget) entry.getKey();
274 ResizableWidgetInfo info = (ResizableWidgetInfo) entry.getValue();
275 int curWidth = DOM.getElementPropertyInt(widget.getElement(),
276 "clientWidth");
277 int curHeight = DOM.getElementPropertyInt(widget.getElement(),
278 "clientHeight");
279
280 // Call the onResize method only if the widget is attached
281 if (info.setClientSize(curWidth, curHeight)) {
282 if (widget.isAttached()) {
283 widget.onResize(curWidth, curHeight);
284 }
285 }
286 }
287 }
288 }