summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShawn O. Pearce <sop@google.com>2009-02-27 09:41:42 -0800
committerShawn O. Pearce <sop@google.com>2009-02-27 09:41:42 -0800
commite1a47fd933252b1f09fe719a7348e37f5dd9899f (patch)
tree47913fc336f0142d827881e9c9ba11b75c2046a0
parentc433772b1db094b273a3e6078c15eaa1d40d283e (diff)
downloadgwtexpui-e1a47fd933252b1f09fe719a7348e37f5dd9899f.tar.gz
Add a simple dialog box that automatically centers itself
Blatantly stolen from Gerrit Code Review, this is a simple dialog that registers a window listener to recenter itself whenever the main browser window resizes. Signed-off-by: Shawn O. Pearce <sop@google.com>
-rw-r--r--src/main/java/com/google/gwtexpui/user/client/AutoCenterDialogBox.java72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/main/java/com/google/gwtexpui/user/client/AutoCenterDialogBox.java b/src/main/java/com/google/gwtexpui/user/client/AutoCenterDialogBox.java
new file mode 100644
index 0000000..ad28d3a
--- /dev/null
+++ b/src/main/java/com/google/gwtexpui/user/client/AutoCenterDialogBox.java
@@ -0,0 +1,72 @@
+// 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.gwtexpui.user.client;
+
+import com.google.gwt.user.client.Window;
+import com.google.gwt.user.client.WindowResizeListener;
+import com.google.gwt.user.client.ui.DialogBox;
+
+/** A DialogBox that automatically re-centers itself if the window changes */
+public class AutoCenterDialogBox extends DialogBox {
+ private WindowResizeListener recenter;
+
+ public AutoCenterDialogBox() {
+ }
+
+ public AutoCenterDialogBox(final boolean autoHide) {
+ super(autoHide);
+ }
+
+ public AutoCenterDialogBox(final boolean autoHide, final boolean modal) {
+ super(autoHide, modal);
+ }
+
+ @Override
+ public void show() {
+ if (recenter == null) {
+ recenter = new WindowResizeListener() {
+ public void onWindowResized(final int width, final int height) {
+ onResize(width, height);
+ }
+ };
+ Window.addWindowResizeListener(recenter);
+ }
+ super.show();
+ }
+
+ @Override
+ protected void onUnload() {
+ if (recenter != null) {
+ Window.removeWindowResizeListener(recenter);
+ recenter = null;
+ }
+ super.onUnload();
+ }
+
+ /**
+ * Invoked when the outer browser window resizes.
+ * <p>
+ * Subclasses may override (but should ensure they still call super.onResize)
+ * to implement custom logic when a window resize occurs.
+ *
+ * @param width new browser window width
+ * @param height new browser window height
+ */
+ protected void onResize(final int width, final int height) {
+ if (isAttached()) {
+ center();
+ }
+ }
+}