Author: [EMAIL PROTECTED]
Date: Thu Oct 23 09:05:12 2008
New Revision: 3826

Added:
     
branches/1_6_events/user/src/com/google/gwt/user/client/HistoryChangeEvent.java 
   
(contents, props changed)
     
branches/1_6_events/user/src/com/google/gwt/user/client/HistoryChangeHandler.java
    
(contents, props changed)
    branches/1_6_events/user/src/com/google/gwt/user/client/L.java    
(contents, props changed)
Modified:
    branches/1_6_events/user/src/com/google/gwt/user/client/History.java
    branches/1_6_events/user/src/com/google/gwt/user/client/Window.java
     
branches/1_6_events/user/src/com/google/gwt/user/client/impl/HistoryImpl.java
     
branches/1_6_events/user/test/com/google/gwt/user/client/ui/CompositeTest.java

Log:
Added HistoryChangeHandler to History to replace HistoryListener.  Also  
moved the Listener to Handler logic to an L class, like we do with widgets.

Patch by: jlabanca



Modified:  
branches/1_6_events/user/src/com/google/gwt/user/client/History.java
==============================================================================
--- branches/1_6_events/user/src/com/google/gwt/user/client/History.java        
 
(original)
+++ branches/1_6_events/user/src/com/google/gwt/user/client/History.java        
 
Thu Oct 23 09:05:12 2008
@@ -16,6 +16,7 @@
  package com.google.gwt.user.client;

  import com.google.gwt.core.client.GWT;
+import com.google.gwt.event.shared.HandlerRegistration;
  import com.google.gwt.user.client.impl.HistoryImpl;

  /**
@@ -72,12 +73,23 @@
    }

    /**
+   * Adds a [EMAIL PROTECTED] HistoryChangeEvent} handler to be informed of 
changes  
to the
+   * browser's history stack.
+   *
+   * @param handler the handler
+   */
+  public static HandlerRegistration addHistoryChangeHandler(
+      HistoryChangeHandler handler) {
+    return impl.addHistoryChangeHandler(handler);
+  }
+
+  /**
     * Adds a listener to be informed of changes to the browser's history  
stack.
     *
     * @param listener the listener to be added
     */
    public static void addHistoryListener(HistoryListener listener) {
-    HistoryImpl.addHistoryListener(listener);
+    L.HistoryChange.add(listener);
    }

    /**
@@ -172,6 +184,6 @@
     * @param listener the listener to be removed
     */
    public static void removeHistoryListener(HistoryListener listener) {
-    HistoryImpl.removeHistoryListener(listener);
+    L.HistoryChange.remove(impl.getHandlers(), listener);
    }
  }

Added:  
branches/1_6_events/user/src/com/google/gwt/user/client/HistoryChangeEvent.java
==============================================================================
--- (empty file)
+++  
branches/1_6_events/user/src/com/google/gwt/user/client/HistoryChangeEvent.java 
 
Thu Oct 23 09:05:12 2008
@@ -0,0 +1,62 @@
+/*
+ * 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.user.client;
+
+import com.google.gwt.event.shared.AbstractEvent;
+
+/**
+ * Fired when the user clicks the browser's 'back' or 'forward' buttons.
+ */
+public class HistoryChangeEvent extends AbstractEvent {
+  /**
+   * The event type.
+   */
+  public static final Type<HistoryChangeEvent, HistoryChangeHandler> TYPE  
= new Type<HistoryChangeEvent, HistoryChangeHandler>() {
+    @Override
+    protected void fire(HistoryChangeHandler handler, HistoryChangeEvent  
event) {
+      handler.onHistoryChanged(event);
+    }
+  };
+
+  /**
+   * The token representing the current history state.
+   */
+  private String historyToken = null;
+
+  /**
+   * Construct a new [EMAIL PROTECTED] HistoryChangeEvent}.
+   *
+   * @param historyToken the token representing the current history state.
+   */
+  public HistoryChangeEvent(String historyToken) {
+    this.historyToken = historyToken;
+  }
+
+  /**
+   * Get the token representing the current history state.
+   *
+   * @return the history token.
+   */
+  public String getHistoryToken() {
+    return historyToken;
+  }
+
+  @Override
+  protected Type getType() {
+    return TYPE;
+  }
+}

Added:  
branches/1_6_events/user/src/com/google/gwt/user/client/HistoryChangeHandler.java
==============================================================================
--- (empty file)
+++  
branches/1_6_events/user/src/com/google/gwt/user/client/HistoryChangeHandler.java
        
Thu Oct 23 09:05:12 2008
@@ -0,0 +1,32 @@
+/*
+ * 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.user.client;
+
+import com.google.gwt.event.shared.EventHandler;
+
+/**
+ * Implement this interface to receive notification of changes to the  
browser
+ * history state. It is used with [EMAIL PROTECTED]  
com.google.gwt.user.client.History}.
+ */
+public interface HistoryChangeHandler extends EventHandler {
+  /**
+   * Fired when the user clicks the browser's 'back' or 'forward' buttons.
+   *
+   * @param event the event
+   */
+  void onHistoryChanged(HistoryChangeEvent event);
+}

Added: branches/1_6_events/user/src/com/google/gwt/user/client/L.java
==============================================================================
--- (empty file)
+++ branches/1_6_events/user/src/com/google/gwt/user/client/L.java      Thu Oct 
 
23 09:05:12 2008
@@ -0,0 +1,151 @@
+/*
+ * 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.user.client;
+
+import com.google.gwt.event.logical.shared.CloseEvent;
+import com.google.gwt.event.logical.shared.CloseHandler;
+import com.google.gwt.event.logical.shared.ResizeEvent;
+import com.google.gwt.event.logical.shared.ResizeHandler;
+import com.google.gwt.event.shared.EventHandler;
+import com.google.gwt.event.shared.HandlerManager;
+import com.google.gwt.event.shared.AbstractEvent.Type;
+
+import java.util.EventListener;
+
+/**
+ * Root of legacy listener support hierarchy.
+ *
+ * @param <ListenerType> listener type
+ */
[EMAIL PROTECTED]
+abstract class L<ListenerType> implements EventHandler {
+  public static class HistoryChange extends L<HistoryListener> implements
+      HistoryChangeHandler {
+    @Deprecated
+    public static void add(HistoryListener listener) {
+      History.addHistoryChangeHandler(new HistoryChange(listener));
+    }
+
+    public static void remove(HandlerManager manager, HistoryListener  
listener) {
+      baseRemove(manager, listener, HistoryChangeEvent.TYPE);
+    }
+
+    protected HistoryChange(HistoryListener listener) {
+      super(listener);
+    }
+
+    public void onHistoryChanged(HistoryChangeEvent event) {
+      listener.onHistoryChanged(event.getHistoryToken());
+    }
+  }
+
+  public static class WindowClose extends L<WindowCloseListener> implements
+      WindowClosingHandler, CloseHandler<Window> {
+    @Deprecated
+    public static void add(WindowCloseListener listener) {
+      WindowClose handler = new WindowClose(listener);
+      Window.addWindowClosingHandler(handler);
+      Window.addCloseHandler(handler);
+    }
+
+    public static void remove(HandlerManager manager,
+        WindowCloseListener listener) {
+      baseRemove(manager, listener, WindowClosingEvent.TYPE,  
CloseEvent.TYPE);
+    }
+
+    protected WindowClose(WindowCloseListener listener) {
+      super(listener);
+    }
+
+    public void onClose(CloseEvent<Window> event) {
+      listener.onWindowClosed();
+    }
+
+    public void onWindowClosing(WindowClosingEvent event) {
+      String message = listener.onWindowClosing();
+      if (event.getMessage() == null) {
+        event.setMessage(message);
+      }
+    }
+  }
+
+  public static class WindowResize extends L<WindowResizeListener>  
implements
+      ResizeHandler {
+    @Deprecated
+    public static void add(WindowResizeListener listener) {
+      Window.addResizeHandler(new WindowResize(listener));
+    }
+
+    public static void remove(HandlerManager manager,
+        WindowResizeListener listener) {
+      baseRemove(manager, listener, ResizeEvent.TYPE);
+    }
+
+    protected WindowResize(WindowResizeListener listener) {
+      super(listener);
+    }
+
+    public void onResize(ResizeEvent event) {
+      listener.onWindowResized(event.getWidth(), event.getHeight());
+    }
+  }
+
+  public static class WindowScroll extends L<WindowScrollListener>  
implements
+      WindowScrollHandler {
+    @Deprecated
+    public static void add(WindowScrollListener listener) {
+      Window.addWindowScrollHandler(new WindowScroll(listener));
+    }
+
+    public static void remove(HandlerManager manager,
+        WindowScrollListener listener) {
+      baseRemove(manager, listener, WindowScrollEvent.TYPE);
+    }
+
+    protected WindowScroll(WindowScrollListener listener) {
+      super(listener);
+    }
+
+    public void onWindowScroll(WindowScrollEvent event) {
+      listener.onWindowScrolled(event.getScrollLeft(),  
event.getScrollTop());
+    }
+  }
+
+  static void baseRemove(HandlerManager manager, EventListener listener,
+      Type... keys) {
+    if (manager != null) {
+      for (Type key : keys) {
+        int handlerCount = manager.getHandlerCount(key);
+        for (int i = 0; i < handlerCount; i++) {
+          EventHandler handler = manager.getHandler(key, i);
+          if (handler instanceof L && ((L)  
handler).listener.equals(listener)) {
+            manager.removeHandler(key, handler);
+          }
+        }
+      }
+    }
+  }
+
+  /**
+   * Listener being wrapped.
+   */
+  protected final ListenerType listener;
+
+  protected L(ListenerType listener) {
+    this.listener = listener;
+  }
+}

Modified:  
branches/1_6_events/user/src/com/google/gwt/user/client/Window.java
==============================================================================
--- branches/1_6_events/user/src/com/google/gwt/user/client/Window.java  
(original)
+++ branches/1_6_events/user/src/com/google/gwt/user/client/Window.java Thu  
Oct 23 09:05:12 2008
@@ -25,13 +25,11 @@
  import com.google.gwt.event.shared.EventHandler;
  import com.google.gwt.event.shared.HandlerManager;
  import com.google.gwt.event.shared.HandlerRegistration;
-import com.google.gwt.event.shared.AbstractEvent.Type;
  import com.google.gwt.http.client.URL;
  import com.google.gwt.user.client.impl.WindowImpl;

  import java.util.ArrayList;
  import java.util.Collections;
-import java.util.EventListener;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
@@ -236,89 +234,6 @@
      }
    }

-  /**
-   * Root of legacy window listener support hierarchy.
-   *
-   * @param <ListenerType> listener type
-   */
-  @Deprecated
-  private static class ListenerDelagate<ListenerType> implements  
EventHandler {
-    static void baseRemove(EventListener listener, Type... keys) {
-      HandlerManager manager = Window.getHandlers();
-      if (manager != null) {
-        for (Type key : keys) {
-          int handlerCount = manager.getHandlerCount(key);
-          for (int i = 0; i < handlerCount; i++) {
-            EventHandler handler = manager.getHandler(key, i);
-            if (handler instanceof ListenerDelagate
-                && ((ListenerDelagate) handler).listener.equals(listener))  
{
-              manager.removeHandler(key, handler);
-            }
-          }
-        }
-      }
-    }
-
-    protected final ListenerType listener;
-
-    public ListenerDelagate(ListenerType listener) {
-      this.listener = listener;
-    }
-  }
-
-  /**
-   * A delegate to a [EMAIL PROTECTED] WindowCloseListener}.
-   */
-  @Deprecated
-  private static class WindowCloseListenerDelagate extends
-      ListenerDelagate<WindowCloseListener> implements  
WindowClosingHandler,
-      CloseHandler<Window> {
-    public WindowCloseListenerDelagate(WindowCloseListener listener) {
-      super(listener);
-    }
-
-    public void onClose(CloseEvent<Window> event) {
-      listener.onWindowClosed();
-    }
-
-    public void onWindowClosing(WindowClosingEvent event) {
-      String message = listener.onWindowClosing();
-      if (event.getMessage() == null) {
-        event.setMessage(message);
-      }
-    }
-  }
-
-  /**
-   * A delegate to a [EMAIL PROTECTED] WindowResizeListener}.
-   */
-  @Deprecated
-  private static class WindowResizeListenerDelagate extends
-      ListenerDelagate<WindowResizeListener> implements ResizeHandler {
-    public WindowResizeListenerDelagate(WindowResizeListener listener) {
-      super(listener);
-    }
-
-    public void onResize(ResizeEvent event) {
-      listener.onWindowResized(event.getWidth(), event.getHeight());
-    }
-  }
-
-  /**
-   * A delegate to a [EMAIL PROTECTED] WindowScrollListener}.
-   */
-  @Deprecated
-  private static class WindowScrollListenerDelagate extends
-      ListenerDelagate<WindowScrollListener> implements  
WindowScrollHandler {
-    public WindowScrollListenerDelagate(WindowScrollListener listener) {
-      super(listener);
-    }
-
-    public void onWindowScroll(WindowScrollEvent event) {
-      listener.onWindowScrolled(event.getScrollLeft(),  
event.getScrollTop());
-    }
-  }
-
    private static boolean closeHandlersInitialized;
    private static boolean scrollHandlersInitialized;
    private static boolean resizeHandlersInitialized;
@@ -354,10 +269,7 @@
     */
    @Deprecated
    public static void addWindowCloseListener(WindowCloseListener listener) {
-    WindowCloseListenerDelagate delegate = new WindowCloseListenerDelagate(
-        listener);
-    addCloseHandler(delegate);
-    addWindowClosingHandler(delegate);
+    L.WindowClose.add(listener);
    }

    /**
@@ -378,7 +290,7 @@
     */
    @Deprecated
    public static void addWindowResizeListener(WindowResizeListener  
listener) {
-    addResizeHandler(new WindowResizeListenerDelagate(listener));
+    L.WindowResize.add(listener);
    }

    /**
@@ -400,7 +312,7 @@
     */
    @Deprecated
    public static void addWindowScrollListener(WindowScrollListener  
listener) {
-    addWindowScrollHandler(new WindowScrollListenerDelagate(listener));
+    L.WindowScroll.add(listener);
    }

    /**
@@ -523,8 +435,7 @@
     */
    @Deprecated
    public static void removeWindowCloseListener(WindowCloseListener  
listener) {
-    ListenerDelagate.baseRemove(listener, WindowClosingEvent.TYPE,
-        CloseEvent.TYPE);
+    L.WindowClose.remove(getHandlers(), listener);
    }

    /**
@@ -534,7 +445,7 @@
     */
    @Deprecated
    public static void removeWindowResizeListener(WindowResizeListener  
listener) {
-    ListenerDelagate.baseRemove(listener, ResizeEvent.TYPE);
+    L.WindowResize.remove(getHandlers(), listener);
    }

    /**
@@ -544,7 +455,7 @@
     */
    @Deprecated
    public static void removeWindowScrollListener(WindowScrollListener  
listener) {
-    ListenerDelagate.baseRemove(listener, WindowScrollEvent.TYPE);
+    L.WindowScroll.remove(getHandlers(), listener);
    }

    /**

Modified:  
branches/1_6_events/user/src/com/google/gwt/user/client/impl/HistoryImpl.java
==============================================================================
---  
branches/1_6_events/user/src/com/google/gwt/user/client/impl/HistoryImpl.java   
 
(original)
+++  
branches/1_6_events/user/src/com/google/gwt/user/client/impl/HistoryImpl.java   
 
Thu Oct 23 09:05:12 2008
@@ -17,9 +17,12 @@

  import com.google.gwt.core.client.GWT;
  import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
-import com.google.gwt.user.client.HistoryListener;
-
-import java.util.ArrayList;
+import com.google.gwt.event.shared.AbstractEvent;
+import com.google.gwt.event.shared.EventHandler;
+import com.google.gwt.event.shared.HandlerManager;
+import com.google.gwt.event.shared.HandlerRegistration;
+import com.google.gwt.user.client.HistoryChangeEvent;
+import com.google.gwt.user.client.HistoryChangeHandler;

  /**
   * Native implementation associated with
@@ -29,50 +32,78 @@
   */
  public abstract class HistoryImpl {

-  private static ArrayList<HistoryListener> historyListeners = new  
ArrayList<HistoryListener>();
+  private static HandlerManager handlerManager;

    /**
-   * Adds a listener to be informed of changes to the browser's history  
stack.
+   * Adds a [EMAIL PROTECTED] HistoryChangeEvent} handler to be informed of 
changes  
to the
+   * browser's history stack.
     *
-   * @param listener the listener to be added
+   * @param handler the handler
     */
-  public static void addHistoryListener(HistoryListener listener) {
-    historyListeners.add(listener);
+  public static HandlerRegistration addHistoryChangeHandler(
+      HistoryChangeHandler handler) {
+    return addHandler(HistoryChangeEvent.TYPE, handler);
    }

    /**
-   * Fires the [EMAIL PROTECTED] HistoryListener#onHistoryChanged(String)} 
event to  
all
-   * listeners with the given token.
+   * Fires the [EMAIL PROTECTED] HistoryChangeEvent} to all handlers with the 
given  
token.
     */
    public static void fireHistoryChangedImpl(String historyToken) {
-    // TODO: replace this copy when a more general solution to event  
handlers
-    // wanting to remove themselves from the listener list is implemented.
+    fireEvent(new HistoryChangeEvent(historyToken));
+  }

-    // This is necessary to avoid a CurrentModificationException in hosted
-    // mode, as the listeners may try to remove themselves from the list  
while
-    // it is being iterated, such as in HistoryTest.
-    HistoryListener[] listenersToInvoke = historyListeners.toArray(new  
HistoryListener[historyListeners.size()]);
-    for (HistoryListener listener : listenersToInvoke) {
-      listener.onHistoryChanged(historyToken);
-    }
+  /**
+   * Returns the [EMAIL PROTECTED] HandlerManager} used for event management.
+   *
+   * @return the handler manager
+   */
+  public static HandlerManager getHandlers() {
+    return handlerManager;
    }

    public static native String getToken() /*-{
      return $wnd.__gwt_historyToken || "";
    }-*/;

+  protected static native void setToken(String token) /*-{
+    $wnd.__gwt_historyToken = token;
+  }-*/;
+
    /**
-   * Removes a history listener.
+   * Adds this handler to the History.
     *
-   * @param listener the listener to be removed
+   * @param <HandlerType> the type of handler to add
+   * @param type the event type
+   * @param handler the handler
+   * @return [EMAIL PROTECTED] HandlerRegistration} used to remove the handler
     */
-  public static void removeHistoryListener(HistoryListener listener) {
-    historyListeners.remove(listener);
+  private static <HandlerType extends EventHandler> HandlerRegistration  
addHandler(
+      AbstractEvent.Type<?, HandlerType> type, final HandlerType handler) {
+    return ensureHandlers().addHandler(type, handler);
    }

-  protected static native void setToken(String token) /*-{
-    $wnd.__gwt_historyToken = token;
-  }-*/;
+  /**
+   * Returns the [EMAIL PROTECTED] HandlerManager}, ensuring it exists.
+   *
+   * @return the handler manager
+   */
+  private static HandlerManager ensureHandlers() {
+    if (handlerManager == null) {
+      handlerManager = new HandlerManager(null);
+    }
+    return handlerManager;
+  }
+
+  /**
+   * Fires an event.
+   *
+   * @param event the event
+   */
+  private static void fireEvent(AbstractEvent event) {
+    if (handlerManager != null) {
+      handlerManager.fireEvent(event);
+    }
+  }

    private static void fireHistoryChanged(String historyToken) {
      UncaughtExceptionHandler handler = GWT.getUncaughtExceptionHandler();

Modified:  
branches/1_6_events/user/test/com/google/gwt/user/client/ui/CompositeTest.java
==============================================================================
---  
branches/1_6_events/user/test/com/google/gwt/user/client/ui/CompositeTest.java  
 
(original)
+++  
branches/1_6_events/user/test/com/google/gwt/user/client/ui/CompositeTest.java  
 
Thu Oct 23 09:05:12 2008
@@ -123,4 +123,12 @@

      c.tb.setFocus(true);
    }
+
+  /**
+   * This test is here to prevent a "No tests found" warning in Junit.
+   *
+   * TODO: Remove this when testBrowserEvents is enabled
+   */
+  public void testNothing() {
+  }
  }

--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/Google-Web-Toolkit-Contributors
-~----------~----~----~----~------~----~------~--~---

Reply via email to