]> source.dussan.org Git - vaadin-framework.git/commitdiff
Send button click events via RPC (#8278).
authorHenri Sara <hesara@vaadin.com>
Wed, 25 Jan 2012 11:18:50 +0000 (13:18 +0200)
committerHenri Sara <hesara@vaadin.com>
Wed, 25 Jan 2012 11:50:57 +0000 (13:50 +0200)
src/com/vaadin/terminal/gwt/client/ui/VButton.java
src/com/vaadin/terminal/gwt/client/ui/VNativeButton.java
src/com/vaadin/ui/Button.java

index 074df531a8fcdd7dfad79973aa4f4372adbd30ff..b638304657bfb14aecd5be67a9c9aa0a5e29d06a 100644 (file)
@@ -4,6 +4,7 @@
 
 package com.vaadin.terminal.gwt.client.ui;
 
+import com.google.gwt.core.client.GWT;
 import com.google.gwt.dom.client.Document;
 import com.google.gwt.dom.client.Element;
 import com.google.gwt.dom.client.NativeEvent;
@@ -29,6 +30,8 @@ import com.vaadin.terminal.gwt.client.VPaintableWidget;
 import com.vaadin.terminal.gwt.client.UIDL;
 import com.vaadin.terminal.gwt.client.Util;
 import com.vaadin.terminal.gwt.client.VTooltip;
+import com.vaadin.terminal.gwt.client.communication.ClientToServerRpc;
+import com.vaadin.terminal.gwt.client.communication.ClientToServerRpc.InitializableClientToServerRpc;
 
 public class VButton extends FocusWidget implements VPaintableWidget,
         ClickHandler, FocusHandler, BlurHandler {
@@ -36,6 +39,27 @@ public class VButton extends FocusWidget implements VPaintableWidget,
     public static final String CLASSNAME = "v-button";
     private static final String CLASSNAME_PRESSED = "v-pressed";
 
+    /**
+     * RPC interface for calls from client to server.
+     * 
+     * @since 7.0
+     */
+    public interface ButtonClientToServerRpc extends ClientToServerRpc {
+        /**
+         * Button click event.
+         * 
+         * @param mouseEventDetails
+         *            serialized mouse event details
+         */
+        public void click(String mouseEventDetails);
+
+        /**
+         * Indicate to the server that the client has disabled the button as a
+         * result of a click.
+         */
+        public void disableOnClick();
+    }
+
     public static final String ATTR_DISABLE_ON_CLICK = "dc";
 
     // mouse movement is checked before synthesizing click event on mouseout
@@ -93,6 +117,7 @@ public class VButton extends FocusWidget implements VPaintableWidget,
     private HandlerRegistration blurHandlerRegistration;
 
     private int clickShortcut = 0;
+    private ButtonClientToServerRpc buttonRpcProxy;
 
     public VButton() {
         super(DOM.createDiv());
@@ -357,19 +382,26 @@ public class VButton extends FocusWidget implements VPaintableWidget,
         }
         if (disableOnClick) {
             setEnabled(false);
-            client.updateVariable(id, "disabledOnClick", true, false);
+            getButtonRpcProxy().disableOnClick();
         }
 
-        client.updateVariable(id, "state", true, false);
-
         // Add mouse details
         MouseEventDetails details = new MouseEventDetails(
                 event.getNativeEvent(), getElement());
-        client.updateVariable(id, "mousedetails", details.serialize(), true);
+        getButtonRpcProxy().click(details.serialize());
 
         clickPending = false;
     }
 
+    protected ButtonClientToServerRpc getButtonRpcProxy() {
+        if (null == buttonRpcProxy) {
+            buttonRpcProxy = GWT.create(ButtonClientToServerRpc.class);
+            ((InitializableClientToServerRpc) buttonRpcProxy).initRpc(id,
+                    client);
+        }
+        return buttonRpcProxy;
+    }
+
     /*
      * ALL BELOW COPY-PASTED FROM GWT CustomButton
      */
index 97d0747496d2805811bc731cdf3616ed8ff753e9..8385130a420257e4d32cfea96c7cadccb287bcc2 100644 (file)
@@ -4,6 +4,7 @@
 
 package com.vaadin.terminal.gwt.client.ui;
 
+import com.google.gwt.core.client.GWT;
 import com.google.gwt.dom.client.Element;
 import com.google.gwt.event.dom.client.BlurEvent;
 import com.google.gwt.event.dom.client.BlurHandler;
@@ -25,6 +26,8 @@ import com.vaadin.terminal.gwt.client.VPaintableWidget;
 import com.vaadin.terminal.gwt.client.UIDL;
 import com.vaadin.terminal.gwt.client.Util;
 import com.vaadin.terminal.gwt.client.VTooltip;
+import com.vaadin.terminal.gwt.client.communication.ClientToServerRpc.InitializableClientToServerRpc;
+import com.vaadin.terminal.gwt.client.ui.VButton.ButtonClientToServerRpc;
 
 public class VNativeButton extends Button implements VPaintableWidget,
         ClickHandler, FocusHandler, BlurHandler {
@@ -37,6 +40,8 @@ public class VNativeButton extends Button implements VPaintableWidget,
 
     protected ApplicationConnection client;
 
+    private ButtonClientToServerRpc buttonRpcProxy;
+
     protected Element errorIndicatorElement;
 
     protected final Element captionElement = DOM.createSpan();
@@ -170,18 +175,26 @@ public class VNativeButton extends Button implements VPaintableWidget,
         }
         if (disableOnClick) {
             setEnabled(false);
-            client.updateVariable(id, "disabledOnClick", true, false);
+            getButtonRpcProxy().disableOnClick();
         }
 
         // Add mouse details
         MouseEventDetails details = new MouseEventDetails(
                 event.getNativeEvent(), getElement());
-        client.updateVariable(id, "mousedetails", details.serialize(), false);
+        getButtonRpcProxy().click(details.serialize());
 
-        client.updateVariable(id, "state", true, true);
         clickPending = false;
     }
 
+    protected ButtonClientToServerRpc getButtonRpcProxy() {
+        if (null == buttonRpcProxy) {
+            buttonRpcProxy = GWT.create(ButtonClientToServerRpc.class);
+            ((InitializableClientToServerRpc) buttonRpcProxy).initRpc(id,
+                    client);
+        }
+        return buttonRpcProxy;
+    }
+
     public void onFocus(FocusEvent arg0) {
         client.updateVariable(id, EventId.FOCUS, "", true);
     }
index 6d9ef0b59da393288083e21efab5d65948101d7e..7c0811008a32b41772b3e588066c9fc64f42e1b1 100644 (file)
@@ -23,6 +23,8 @@ import com.vaadin.terminal.PaintException;
 import com.vaadin.terminal.PaintTarget;
 import com.vaadin.terminal.gwt.client.MouseEventDetails;
 import com.vaadin.terminal.gwt.client.ui.VButton;
+import com.vaadin.terminal.gwt.client.ui.VButton.ButtonClientToServerRpc;
+import com.vaadin.terminal.gwt.server.RpcTarget;
 import com.vaadin.tools.ReflectTools;
 import com.vaadin.ui.ClientWidget.LoadStyle;
 import com.vaadin.ui.Component.Focusable;
@@ -39,7 +41,7 @@ import com.vaadin.ui.Component.Focusable;
 @ClientWidget(value = VButton.class, loadStyle = LoadStyle.EAGER)
 public class Button extends AbstractComponent implements
         FieldEvents.BlurNotifier, FieldEvents.FocusNotifier, Focusable,
-        Action.ShortcutNotifier {
+        Action.ShortcutNotifier, RpcTarget {
 
     /* Private members */
 
@@ -49,6 +51,18 @@ public class Button extends AbstractComponent implements
      * Creates a new push button.
      */
     public Button() {
+        // TODO take the implementation out of an anonymous class?
+        registerRpcImplementation(new ButtonClientToServerRpc() {
+            public void click(String mouseEventDetails) {
+                fireClick(MouseEventDetails.deSerialize(mouseEventDetails));
+            }
+
+            public void disableOnClick() {
+                // Could be optimized so the button is not repainted because of
+                // this (client side has already disabled the button)
+                setEnabled(false);
+            }
+        }, ButtonClientToServerRpc.class);
     }
 
     /**
@@ -108,24 +122,6 @@ public class Button extends AbstractComponent implements
     public void changeVariables(Object source, Map<String, Object> variables) {
         super.changeVariables(source, variables);
 
-        if (variables.containsKey("disabledOnClick")) {
-            // Could be optimized so the button is not repainted because of this
-            // (client side has already disabled the button)
-            setEnabled(false);
-        }
-
-        if (!isReadOnly() && variables.containsKey("state")) {
-            // Send click events when the button is pushed
-            if (variables.containsKey("mousedetails")) {
-                fireClick(MouseEventDetails.deSerialize((String) variables
-                        .get("mousedetails")));
-            } else {
-                // for compatibility with custom implementations which
-                // don't send mouse details
-                fireClick();
-            }
-        }
-
         if (variables.containsKey(FocusEvent.EVENT_ID)) {
             fireEvent(new FocusEvent(this));
         }