]> source.dussan.org Git - vaadin-framework.git/commitdiff
Implements #2509 PopupView.addListener(PopupVisibilityListener)
authorMarc Englund <marc.englund@itmill.com>
Wed, 28 Jan 2009 12:38:40 +0000 (12:38 +0000)
committerMarc Englund <marc.englund@itmill.com>
Wed, 28 Jan 2009 12:38:40 +0000 (12:38 +0000)
svn changeset:6673/svn branch:trunk

src/com/itmill/toolkit/ui/PopupView.java

index 807bdd796df34d4573616715bb83101962874b6c..9bb901d57e996248b0e278c3a0e695eb02af0bbc 100644 (file)
@@ -1,5 +1,6 @@
 package com.itmill.toolkit.ui;
 
+import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.Map;
@@ -23,6 +24,19 @@ public class PopupView extends AbstractComponentContainer {
     private boolean hideOnMouseOut;
     private final ArrayList<Component> componentList;
 
+    private static final Method POPUP_VISIBILITY_METHOD;
+    static {
+        try {
+            POPUP_VISIBILITY_METHOD = PopupVisibilityListener.class
+                    .getDeclaredMethod("popupVisibilityChange",
+                            new Class[] { PopupVisibilityEvent.class });
+        } catch (final java.lang.NoSuchMethodException e) {
+            // This should never happen
+            throw new java.lang.RuntimeException(
+                    "Internal error finding methods in PopupView");
+        }
+    }
+
     /* Constructors */
 
     /**
@@ -291,6 +305,7 @@ public class PopupView extends AbstractComponentContainer {
     public void changeVariables(Object source, Map variables) {
         if (variables.containsKey("popupVisibility")) {
 
+            boolean oldVisibility = popupVisible;
             popupVisible = ((Boolean) variables.get("popupVisibility"))
                     .booleanValue();
 
@@ -308,6 +323,9 @@ public class PopupView extends AbstractComponentContainer {
                 super.removeComponent(componentList.get(0));
                 componentList.clear();
             }
+            if (oldVisibility != popupVisible) {
+                fireEvent(new PopupVisibilityEvent(this));
+            }
             requestRepaint();
         }
     }
@@ -333,4 +351,89 @@ public class PopupView extends AbstractComponentContainer {
          */
         public Component getPopupComponent();
     }
+
+    /**
+     * Add a listener that is called whenever the visibility of the popup is
+     * changed.
+     * 
+     * @param listener
+     *            the listener to add
+     * @see PopupVisibilityListener
+     * @see PopupVisibilityEvent
+     * @see #removeListener(PopupVisibilityListener)
+     * 
+     */
+    public void addListener(PopupVisibilityListener listener) {
+        addListener(PopupVisibilityEvent.class, listener,
+                POPUP_VISIBILITY_METHOD);
+    }
+
+    /**
+     * Removes a previously added listener, so that it no longer receives events
+     * when the visibility of the popup changes.
+     * 
+     * @param listener
+     *            the listener to remove
+     * @see PopupVisibilityListener
+     * @see #addListener(PopupVisibilityListener)
+     */
+    public void removeListener(PopupVisibilityListener listener) {
+        removeListener(PopupVisibilityEvent.class, listener,
+                POPUP_VISIBILITY_METHOD);
+    }
+
+    /**
+     * This event is received by the PopupVisibilityListeners when the
+     * visibility of the popup changes. You can get the new visibility directly
+     * with {@link #isPopupVisible()}, or get the PopupView that produced the
+     * event with {@link #getPopupView()}.
+     * 
+     */
+    public class PopupVisibilityEvent extends Event {
+        /**
+         * Serial generated by eclipse.
+         */
+        private static final long serialVersionUID = -130167162207143457L;
+
+        public PopupVisibilityEvent(PopupView source) {
+            super(source);
+        }
+
+        /**
+         * Get the PopupView instance that is the source of this event.
+         * 
+         * @return the source PopupView
+         */
+        public PopupView getPopupView() {
+            return (PopupView) getSource();
+        }
+
+        /**
+         * Returns the current visibility of the popup.
+         * 
+         * @return true if the popup is visible
+         */
+        public boolean isPopupVisible() {
+            return getPopupView().isPopupVisible();
+        }
+    }
+
+    /**
+     * Defines a listener that can receive a PopupVisibilityEvent when the
+     * visibility of the popup changes.
+     * 
+     */
+    public interface PopupVisibilityListener {
+        /**
+         * Pass to {@link PopupView#PopupVisibilityEvent} to start listening for
+         * popup visibility changes.
+         * 
+         * @param event
+         *            the event
+         * 
+         * @see {@link PopupVisibilityEvent}
+         * @see {@link PopupView#addListener(PopupVisibilityListener)}
+         */
+        public void popupVisibilityChange(PopupVisibilityEvent event);
+    }
 }