From 8b3e927419e61ff2eee43a4dcb4b832a3e22746b Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Wed, 11 Apr 2012 17:32:47 +0300 Subject: [PATCH] Button, NativeButton and CheckBox are no longer Vaadin6Components Added FocusAndBlurServerRpc for sending focus and blur events to any component. --- src/com/vaadin/event/FieldEvents.java | 23 ++++ .../terminal/gwt/client/EventHelper.java | 70 +++++++----- .../gwt/client/ui/ButtonConnector.java | 61 ++++++---- .../gwt/client/ui/CheckBoxConnector.java | 105 +++++++++++++++--- .../gwt/client/ui/NativeButtonConnector.java | 54 ++++++--- .../terminal/gwt/client/ui/VButton.java | 19 +--- .../terminal/gwt/client/ui/VCheckBox.java | 51 +-------- .../terminal/gwt/client/ui/VNativeButton.java | 20 +--- src/com/vaadin/ui/AbstractComponent.java | 15 ++- src/com/vaadin/ui/Button.java | 35 ++---- src/com/vaadin/ui/CheckBox.java | 75 +++++++------ 11 files changed, 296 insertions(+), 232 deletions(-) diff --git a/src/com/vaadin/event/FieldEvents.java b/src/com/vaadin/event/FieldEvents.java index 28fd6bb4f7..20e9fabb36 100644 --- a/src/com/vaadin/event/FieldEvents.java +++ b/src/com/vaadin/event/FieldEvents.java @@ -8,8 +8,10 @@ import java.io.Serializable; import java.lang.reflect.Method; import com.vaadin.terminal.gwt.client.EventId; +import com.vaadin.terminal.gwt.client.communication.FieldRpc.FocusAndBlurServerRpc; import com.vaadin.tools.ReflectTools; import com.vaadin.ui.Component; +import com.vaadin.ui.Component.Event; import com.vaadin.ui.Field; import com.vaadin.ui.Field.ValueChangeEvent; import com.vaadin.ui.TextField; @@ -247,4 +249,25 @@ public interface FieldEvents { public void removeListener(TextChangeListener listener); } + + public static abstract class FocusAndBlurServerRpcImpl implements + FocusAndBlurServerRpc { + + private Component component; + + public FocusAndBlurServerRpcImpl(Component component) { + this.component = component; + } + + protected abstract void fireEvent(Event event); + + public void blur() { + fireEvent(new BlurEvent(component)); + } + + public void focus() { + fireEvent(new FocusEvent(component)); + } + }; + } diff --git a/src/com/vaadin/terminal/gwt/client/EventHelper.java b/src/com/vaadin/terminal/gwt/client/EventHelper.java index 06b9042eb4..95f5125f1b 100644 --- a/src/com/vaadin/terminal/gwt/client/EventHelper.java +++ b/src/com/vaadin/terminal/gwt/client/EventHelper.java @@ -6,10 +6,12 @@ package com.vaadin.terminal.gwt.client; import static com.vaadin.terminal.gwt.client.EventId.BLUR; import static com.vaadin.terminal.gwt.client.EventId.FOCUS; +import com.google.gwt.event.dom.client.BlurEvent; import com.google.gwt.event.dom.client.BlurHandler; +import com.google.gwt.event.dom.client.DomEvent.Type; +import com.google.gwt.event.dom.client.FocusEvent; import com.google.gwt.event.dom.client.FocusHandler; -import com.google.gwt.event.dom.client.HasBlurHandlers; -import com.google.gwt.event.dom.client.HasFocusHandlers; +import com.google.gwt.event.shared.EventHandler; import com.google.gwt.event.shared.HandlerRegistration; /** @@ -40,38 +42,56 @@ import com.google.gwt.event.shared.HandlerRegistration; */ public class EventHelper { - public static HandlerRegistration updateFocusHandler( - ComponentConnector paintable, ApplicationConnection client, - HandlerRegistration handlerRegistration) { - if (client.hasEventListeners(paintable, FOCUS)) { - if (handlerRegistration == null) { - handlerRegistration = ((HasFocusHandlers) paintable) - .addFocusHandler((FocusHandler) paintable); - } - return handlerRegistration; - } else if (handlerRegistration != null) { - handlerRegistration.removeHandler(); - handlerRegistration = null; + /** + * Adds or removes a focus handler depending on if the connector has focus + * listeners on the server side or not. + * + * @param connector + * The connector to update. Must implement focusHandler. + * @param handlerRegistration + * The old registration reference or null no handler has been + * registered previously + * @return a new registration handler that can be used to unregister the + * handler later + */ + public static HandlerRegistration updateFocusHandler( + T connector, HandlerRegistration handlerRegistration) { + return updateHandler(connector, FOCUS, handlerRegistration, + FocusEvent.getType()); + } - } - return null; + /** + * Adds or removes a blur handler depending on if the connector has blur + * listeners on the server side or not. + * + * @param connector + * The connector to update. Must implement BlurHandler. + * @param handlerRegistration + * The old registration reference or null no handler has been + * registered previously + * @return a new registration handler that can be used to unregister the + * handler later + */ + public static HandlerRegistration updateBlurHandler( + T connector, HandlerRegistration handlerRegistration) { + return updateHandler(connector, BLUR, handlerRegistration, + BlurEvent.getType()); } - public static HandlerRegistration updateBlurHandler( - ComponentConnector paintable, ApplicationConnection client, - HandlerRegistration handlerRegistration) { - if (client.hasEventListeners(paintable, BLUR)) { + private static HandlerRegistration updateHandler( + ComponentConnector connector, String eventIdentifier, + HandlerRegistration handlerRegistration, Type type) { + if (connector.hasEventListener(eventIdentifier)) { if (handlerRegistration == null) { - handlerRegistration = ((HasBlurHandlers) paintable) - .addBlurHandler((BlurHandler) paintable); + handlerRegistration = connector.getWidget().addDomHandler( + (H) connector, type); } - return handlerRegistration; } else if (handlerRegistration != null) { handlerRegistration.removeHandler(); handlerRegistration = null; - } - return null; + return handlerRegistration; + } } diff --git a/src/com/vaadin/terminal/gwt/client/ui/ButtonConnector.java b/src/com/vaadin/terminal/gwt/client/ui/ButtonConnector.java index ddc9b59430..9d8838751f 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/ButtonConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/ButtonConnector.java @@ -5,21 +5,25 @@ package com.vaadin.terminal.gwt.client.ui; import com.google.gwt.core.client.GWT; +import com.google.gwt.event.dom.client.BlurEvent; +import com.google.gwt.event.dom.client.BlurHandler; +import com.google.gwt.event.dom.client.FocusEvent; +import com.google.gwt.event.dom.client.FocusHandler; +import com.google.gwt.event.shared.HandlerRegistration; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.ui.Widget; -import com.vaadin.terminal.gwt.client.ApplicationConnection; import com.vaadin.terminal.gwt.client.EventHelper; import com.vaadin.terminal.gwt.client.MouseEventDetails; -import com.vaadin.terminal.gwt.client.Paintable; -import com.vaadin.terminal.gwt.client.UIDL; +import com.vaadin.terminal.gwt.client.communication.FieldRpc.FocusAndBlurServerRpc; import com.vaadin.terminal.gwt.client.communication.RpcProxy; import com.vaadin.terminal.gwt.client.communication.ServerRpc; +import com.vaadin.terminal.gwt.client.communication.StateChangeEvent; import com.vaadin.terminal.gwt.client.ui.Component.LoadStyle; import com.vaadin.ui.Button; @Component(value = Button.class, loadStyle = LoadStyle.EAGER) public class ButtonConnector extends AbstractComponentConnector implements - Paintable { + BlurHandler, FocusHandler { /** * RPC interface for calls from client to server. @@ -42,6 +46,13 @@ public class ButtonConnector extends AbstractComponentConnector implements public void disableOnClick(); } + private ButtonServerRpc rpc = RpcProxy.create(ButtonServerRpc.class, this); + private FocusAndBlurServerRpc focusBlurProxy = RpcProxy.create( + FocusAndBlurServerRpc.class, this); + + private HandlerRegistration focusHandlerRegistration = null; + private HandlerRegistration blurHandlerRegistration = null; + @Override public boolean delegateCaptionHandling() { return false; @@ -50,27 +61,18 @@ public class ButtonConnector extends AbstractComponentConnector implements @Override public void init() { super.init(); - getWidget().buttonRpcProxy = RpcProxy.create(ButtonServerRpc.class, - this); + getWidget().buttonRpcProxy = rpc; + getWidget().client = getConnection(); + getWidget().paintableId = getConnectorId(); } - public void updateFromUIDL(UIDL uidl, ApplicationConnection client) { - - // Ensure correct implementation, - // but don't let container manage caption etc. - if (!isRealUpdate(uidl)) { - return; - } - - getWidget().focusHandlerRegistration = EventHelper.updateFocusHandler( - this, client, getWidget().focusHandlerRegistration); - getWidget().blurHandlerRegistration = EventHelper.updateBlurHandler( - this, client, getWidget().blurHandlerRegistration); - - // Save details - getWidget().client = client; - getWidget().paintableId = uidl.getId(); - + @Override + public void onStateChanged(StateChangeEvent stateChangeEvent) { + super.onStateChanged(stateChangeEvent); + focusHandlerRegistration = EventHelper.updateFocusHandler(this, + focusHandlerRegistration); + blurHandlerRegistration = EventHelper.updateBlurHandler(this, + blurHandlerRegistration); // Set text getWidget().setText(getState().getCaption()); @@ -93,7 +95,7 @@ public class ButtonConnector extends AbstractComponentConnector implements if (getState().getIcon() != null) { if (getWidget().icon == null) { - getWidget().icon = new Icon(client); + getWidget().icon = new Icon(getConnection()); getWidget().wrapper.insertBefore(getWidget().icon.getElement(), getWidget().captionElement); } @@ -123,4 +125,15 @@ public class ButtonConnector extends AbstractComponentConnector implements return (ButtonState) super.getState(); } + public void onFocus(FocusEvent event) { + // EventHelper.updateFocusHandler ensures that this is called only when + // there is a listener on server side + focusBlurProxy.focus(); + } + + public void onBlur(BlurEvent event) { + // EventHelper.updateFocusHandler ensures that this is called only when + // there is a listener on server side + focusBlurProxy.blur(); + } } diff --git a/src/com/vaadin/terminal/gwt/client/ui/CheckBoxConnector.java b/src/com/vaadin/terminal/gwt/client/ui/CheckBoxConnector.java index 5c9193f08b..f32980c636 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/CheckBoxConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/CheckBoxConnector.java @@ -4,38 +4,79 @@ package com.vaadin.terminal.gwt.client.ui; import com.google.gwt.core.client.GWT; +import com.google.gwt.event.dom.client.BlurEvent; +import com.google.gwt.event.dom.client.BlurHandler; +import com.google.gwt.event.dom.client.ClickEvent; +import com.google.gwt.event.dom.client.ClickHandler; +import com.google.gwt.event.dom.client.FocusEvent; +import com.google.gwt.event.dom.client.FocusHandler; +import com.google.gwt.event.shared.HandlerRegistration; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.ui.Widget; -import com.vaadin.terminal.gwt.client.ApplicationConnection; +import com.vaadin.terminal.gwt.client.AbstractFieldState; import com.vaadin.terminal.gwt.client.EventHelper; -import com.vaadin.terminal.gwt.client.Paintable; -import com.vaadin.terminal.gwt.client.UIDL; +import com.vaadin.terminal.gwt.client.MouseEventDetails; +import com.vaadin.terminal.gwt.client.MouseEventDetailsBuilder; import com.vaadin.terminal.gwt.client.VTooltip; +import com.vaadin.terminal.gwt.client.communication.FieldRpc.FocusAndBlurServerRpc; +import com.vaadin.terminal.gwt.client.communication.RpcProxy; +import com.vaadin.terminal.gwt.client.communication.ServerRpc; +import com.vaadin.terminal.gwt.client.communication.StateChangeEvent; import com.vaadin.ui.CheckBox; @Component(CheckBox.class) public class CheckBoxConnector extends AbstractFieldConnector implements - Paintable { + FocusHandler, BlurHandler, ClickHandler { + + public interface CheckBoxServerRpc extends ServerRpc { + public void setChecked(boolean checked, + MouseEventDetails mouseEventDetails); + } + + public static class CheckBoxState extends AbstractFieldState { + private boolean checked = false; + + public boolean isChecked() { + return checked; + } + + public void setChecked(boolean checked) { + this.checked = checked; + } + + } + + private HandlerRegistration focusHandlerRegistration; + private HandlerRegistration blurHandlerRegistration; + + private CheckBoxServerRpc rpc = RpcProxy.create(CheckBoxServerRpc.class, + this); + private FocusAndBlurServerRpc focusBlurRpc = RpcProxy.create( + FocusAndBlurServerRpc.class, this); @Override public boolean delegateCaptionHandling() { return false; } - public void updateFromUIDL(UIDL uidl, ApplicationConnection client) { - // Save details - getWidget().client = client; - getWidget().id = uidl.getId(); + @Override + protected void init() { + super.init(); + getWidget().addClickHandler(this); + getWidget().client = getConnection(); + getWidget().id = getConnectorId(); - if (!isRealUpdate(uidl)) { - return; - } + } - getWidget().focusHandlerRegistration = EventHelper.updateFocusHandler( - this, client, getWidget().focusHandlerRegistration); - getWidget().blurHandlerRegistration = EventHelper.updateBlurHandler( - this, client, getWidget().blurHandlerRegistration); + @Override + public void onStateChanged(StateChangeEvent stateChangeEvent) { + super.onStateChanged(stateChangeEvent); + + focusHandlerRegistration = EventHelper.updateFocusHandler(this, + focusHandlerRegistration); + blurHandlerRegistration = EventHelper.updateBlurHandler(this, + blurHandlerRegistration); if (null != getState().getErrorMessage()) { if (getWidget().errorIndicatorElement == null) { @@ -62,7 +103,7 @@ public class CheckBoxConnector extends AbstractFieldConnector implements if (getState().getIcon() != null) { if (getWidget().icon == null) { - getWidget().icon = new Icon(client); + getWidget().icon = new Icon(getConnection()); DOM.insertChild(getWidget().getElement(), getWidget().icon.getElement(), 1); getWidget().icon.sinkEvents(VTooltip.TOOLTIP_EVENTS); @@ -78,11 +119,15 @@ public class CheckBoxConnector extends AbstractFieldConnector implements // Set text getWidget().setText(getState().getCaption()); - getWidget().setValue( - uidl.getBooleanVariable(getWidget().VARIABLE_STATE)); + getWidget().setValue(getState().isChecked()); getWidget().immediate = getState().isImmediate(); } + @Override + public CheckBoxState getState() { + return (CheckBoxState) super.getState(); + } + @Override public VCheckBox getWidget() { return (VCheckBox) super.getWidget(); @@ -93,4 +138,28 @@ public class CheckBoxConnector extends AbstractFieldConnector implements return GWT.create(VCheckBox.class); } + public void onFocus(FocusEvent event) { + // EventHelper.updateFocusHandler ensures that this is called only when + // there is a listener on server side + focusBlurRpc.focus(); + } + + public void onBlur(BlurEvent event) { + // EventHelper.updateFocusHandler ensures that this is called only when + // there is a listener on server side + focusBlurRpc.blur(); + } + + public void onClick(ClickEvent event) { + if (!isEnabled()) { + return; + } + + // Add mouse details + MouseEventDetails details = MouseEventDetailsBuilder + .buildMouseEventDetails(event.getNativeEvent(), getWidget() + .getElement()); + rpc.setChecked(getWidget().getValue(), details); + + } } diff --git a/src/com/vaadin/terminal/gwt/client/ui/NativeButtonConnector.java b/src/com/vaadin/terminal/gwt/client/ui/NativeButtonConnector.java index bf1299ed27..6dd7cd0eac 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/NativeButtonConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/NativeButtonConnector.java @@ -4,19 +4,29 @@ package com.vaadin.terminal.gwt.client.ui; import com.google.gwt.core.client.GWT; +import com.google.gwt.event.dom.client.BlurEvent; +import com.google.gwt.event.dom.client.BlurHandler; +import com.google.gwt.event.dom.client.FocusEvent; +import com.google.gwt.event.dom.client.FocusHandler; +import com.google.gwt.event.shared.HandlerRegistration; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.ui.Widget; -import com.vaadin.terminal.gwt.client.ApplicationConnection; import com.vaadin.terminal.gwt.client.EventHelper; -import com.vaadin.terminal.gwt.client.Paintable; -import com.vaadin.terminal.gwt.client.UIDL; +import com.vaadin.terminal.gwt.client.communication.FieldRpc.FocusAndBlurServerRpc; import com.vaadin.terminal.gwt.client.communication.RpcProxy; +import com.vaadin.terminal.gwt.client.communication.StateChangeEvent; import com.vaadin.terminal.gwt.client.ui.ButtonConnector.ButtonServerRpc; import com.vaadin.ui.NativeButton; @Component(NativeButton.class) public class NativeButtonConnector extends AbstractComponentConnector implements - Paintable { + BlurHandler, FocusHandler { + + private HandlerRegistration focusHandlerRegistration; + private HandlerRegistration blurHandlerRegistration; + + private FocusAndBlurServerRpc focusBlurRpc = RpcProxy.create( + FocusAndBlurServerRpc.class, this); @Override public void init() { @@ -24,6 +34,8 @@ public class NativeButtonConnector extends AbstractComponentConnector implements getWidget().buttonRpcProxy = RpcProxy.create(ButtonServerRpc.class, this); + getWidget().client = getConnection(); + getWidget().paintableId = getConnectorId(); } @Override @@ -31,21 +43,15 @@ public class NativeButtonConnector extends AbstractComponentConnector implements return false; } - public void updateFromUIDL(UIDL uidl, ApplicationConnection client) { - - if (!isRealUpdate(uidl)) { - return; - } + @Override + public void onStateChanged(StateChangeEvent stateChangeEvent) { + super.onStateChanged(stateChangeEvent); getWidget().disableOnClick = getState().isDisableOnClick(); - getWidget().focusHandlerRegistration = EventHelper.updateFocusHandler( - this, client, getWidget().focusHandlerRegistration); - getWidget().blurHandlerRegistration = EventHelper.updateBlurHandler( - this, client, getWidget().blurHandlerRegistration); - - // Save details - getWidget().client = client; - getWidget().paintableId = uidl.getId(); + focusHandlerRegistration = EventHelper.updateFocusHandler(this, + focusHandlerRegistration); + blurHandlerRegistration = EventHelper.updateBlurHandler(this, + blurHandlerRegistration); // Set text getWidget().setText(getState().getCaption()); @@ -69,7 +75,7 @@ public class NativeButtonConnector extends AbstractComponentConnector implements if (getState().getIcon() != null) { if (getWidget().icon == null) { - getWidget().icon = new Icon(client); + getWidget().icon = new Icon(getConnection()); getWidget().getElement().insertBefore( getWidget().icon.getElement(), getWidget().captionElement); @@ -100,4 +106,16 @@ public class NativeButtonConnector extends AbstractComponentConnector implements return (ButtonState) super.getState(); } + public void onFocus(FocusEvent event) { + // EventHelper.updateFocusHandler ensures that this is called only when + // there is a listener on server side + focusBlurRpc.focus(); + } + + public void onBlur(BlurEvent event) { + // EventHelper.updateFocusHandler ensures that this is called only when + // there is a listener on server side + focusBlurRpc.blur(); + } + } diff --git a/src/com/vaadin/terminal/gwt/client/ui/VButton.java b/src/com/vaadin/terminal/gwt/client/ui/VButton.java index a8c034c557..454ca79320 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VButton.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VButton.java @@ -7,29 +7,22 @@ package com.vaadin.terminal.gwt.client.ui; import com.google.gwt.dom.client.Document; import com.google.gwt.dom.client.Element; import com.google.gwt.dom.client.NativeEvent; -import com.google.gwt.event.dom.client.BlurEvent; -import com.google.gwt.event.dom.client.BlurHandler; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; -import com.google.gwt.event.dom.client.FocusEvent; -import com.google.gwt.event.dom.client.FocusHandler; import com.google.gwt.event.dom.client.KeyCodes; -import com.google.gwt.event.shared.HandlerRegistration; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.ui.Accessibility; import com.google.gwt.user.client.ui.FocusWidget; import com.vaadin.terminal.gwt.client.ApplicationConnection; import com.vaadin.terminal.gwt.client.BrowserInfo; -import com.vaadin.terminal.gwt.client.EventId; import com.vaadin.terminal.gwt.client.MouseEventDetails; import com.vaadin.terminal.gwt.client.MouseEventDetailsBuilder; import com.vaadin.terminal.gwt.client.Util; import com.vaadin.terminal.gwt.client.VTooltip; import com.vaadin.terminal.gwt.client.ui.ButtonConnector.ButtonServerRpc; -public class VButton extends FocusWidget implements ClickHandler, FocusHandler, - BlurHandler { +public class VButton extends FocusWidget implements ClickHandler { public static final String CLASSNAME = "v-button"; private static final String CLASSNAME_PRESSED = "v-pressed"; @@ -85,9 +78,6 @@ public class VButton extends FocusWidget implements ClickHandler, FocusHandler, private boolean disallowNextClick = false; private boolean isHovering; - protected HandlerRegistration focusHandlerRegistration; - protected HandlerRegistration blurHandlerRegistration; - protected int clickShortcut = 0; // TODO Move this to VButtonPaintable ButtonServerRpc buttonRpcProxy; @@ -446,11 +436,4 @@ public class VButton extends FocusWidget implements ClickHandler, FocusHandler, return ret; }-*/; - public void onFocus(FocusEvent arg0) { - client.updateVariable(paintableId, EventId.FOCUS, "", true); - } - - public void onBlur(BlurEvent arg0) { - client.updateVariable(paintableId, EventId.BLUR, "", true); - } } diff --git a/src/com/vaadin/terminal/gwt/client/ui/VCheckBox.java b/src/com/vaadin/terminal/gwt/client/ui/VCheckBox.java index d7be367f97..f2ac958d24 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VCheckBox.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VCheckBox.java @@ -4,27 +4,15 @@ package com.vaadin.terminal.gwt.client.ui; -import com.google.gwt.event.dom.client.BlurEvent; -import com.google.gwt.event.dom.client.BlurHandler; -import com.google.gwt.event.dom.client.ClickEvent; -import com.google.gwt.event.dom.client.ClickHandler; -import com.google.gwt.event.dom.client.FocusEvent; -import com.google.gwt.event.dom.client.FocusHandler; -import com.google.gwt.event.shared.HandlerRegistration; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Element; import com.google.gwt.user.client.Event; import com.vaadin.terminal.gwt.client.ApplicationConnection; -import com.vaadin.terminal.gwt.client.EventId; -import com.vaadin.terminal.gwt.client.MouseEventDetails; -import com.vaadin.terminal.gwt.client.MouseEventDetailsBuilder; import com.vaadin.terminal.gwt.client.Util; import com.vaadin.terminal.gwt.client.VTooltip; public class VCheckBox extends com.google.gwt.user.client.ui.CheckBox implements - Field, FocusHandler, BlurHandler { - - public static final String VARIABLE_STATE = "state"; + Field { public static final String CLASSNAME = "v-checkbox"; @@ -38,28 +26,9 @@ public class VCheckBox extends com.google.gwt.user.client.ui.CheckBox implements Icon icon; - HandlerRegistration focusHandlerRegistration; - HandlerRegistration blurHandlerRegistration; - public VCheckBox() { setStyleName(CLASSNAME); - addClickHandler(new ClickHandler() { - - public void onClick(ClickEvent event) { - if (id == null || client == null || !isEnabled()) { - return; - } - - // Add mouse details - MouseEventDetails details = MouseEventDetailsBuilder - .buildMouseEventDetails(event.getNativeEvent(), - getElement()); - client.updateVariable(id, "mousedetails", details.serialize(), - false); - client.updateVariable(id, VARIABLE_STATE, getValue(), immediate); - } - }); sinkEvents(VTooltip.TOOLTIP_EVENTS); Element el = DOM.getFirstChild(getElement()); while (el != null) { @@ -87,22 +56,4 @@ public class VCheckBox extends com.google.gwt.user.client.ui.CheckBox implements } } - @Override - public void setWidth(String width) { - super.setWidth(width); - } - - @Override - public void setHeight(String height) { - super.setHeight(height); - } - - public void onFocus(FocusEvent arg0) { - client.updateVariable(id, EventId.FOCUS, "", true); - } - - public void onBlur(BlurEvent arg0) { - client.updateVariable(id, EventId.BLUR, "", true); - } - } diff --git a/src/com/vaadin/terminal/gwt/client/ui/VNativeButton.java b/src/com/vaadin/terminal/gwt/client/ui/VNativeButton.java index 7866d326ab..b3f60c91ad 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VNativeButton.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VNativeButton.java @@ -5,27 +5,20 @@ package com.vaadin.terminal.gwt.client.ui; import com.google.gwt.dom.client.Element; -import com.google.gwt.event.dom.client.BlurEvent; -import com.google.gwt.event.dom.client.BlurHandler; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; -import com.google.gwt.event.dom.client.FocusEvent; -import com.google.gwt.event.dom.client.FocusHandler; -import com.google.gwt.event.shared.HandlerRegistration; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.ui.Button; import com.vaadin.terminal.gwt.client.ApplicationConnection; import com.vaadin.terminal.gwt.client.BrowserInfo; -import com.vaadin.terminal.gwt.client.EventId; import com.vaadin.terminal.gwt.client.MouseEventDetails; import com.vaadin.terminal.gwt.client.MouseEventDetailsBuilder; import com.vaadin.terminal.gwt.client.Util; import com.vaadin.terminal.gwt.client.VTooltip; import com.vaadin.terminal.gwt.client.ui.ButtonConnector.ButtonServerRpc; -public class VNativeButton extends Button implements ClickHandler, - FocusHandler, BlurHandler { +public class VNativeButton extends Button implements ClickHandler { public static final String CLASSNAME = "v-nativebutton"; @@ -50,9 +43,6 @@ public class VNativeButton extends Button implements ClickHandler, */ private boolean clickPending; - protected HandlerRegistration focusHandlerRegistration; - protected HandlerRegistration blurHandlerRegistration; - protected boolean disableOnClick = false; public VNativeButton() { @@ -131,14 +121,6 @@ public class VNativeButton extends Button implements ClickHandler, clickPending = false; } - public void onFocus(FocusEvent arg0) { - client.updateVariable(paintableId, EventId.FOCUS, "", true); - } - - public void onBlur(BlurEvent arg0) { - client.updateVariable(paintableId, EventId.BLUR, "", true); - } - @Override public void setEnabled(boolean enabled) { if (isEnabled() != enabled) { diff --git a/src/com/vaadin/ui/AbstractComponent.java b/src/com/vaadin/ui/AbstractComponent.java index b54f5ee948..83e6f54ad3 100644 --- a/src/com/vaadin/ui/AbstractComponent.java +++ b/src/com/vaadin/ui/AbstractComponent.java @@ -33,6 +33,7 @@ import com.vaadin.terminal.Resource; import com.vaadin.terminal.Terminal; import com.vaadin.terminal.gwt.client.ComponentState; import com.vaadin.terminal.gwt.client.communication.ClientRpc; +import com.vaadin.terminal.gwt.client.communication.ServerRpc; import com.vaadin.terminal.gwt.server.ClientMethodInvocation; import com.vaadin.terminal.gwt.server.ComponentSizeValidator; import com.vaadin.terminal.gwt.server.ResourceReference; @@ -1536,9 +1537,17 @@ public abstract class AbstractComponent implements Component, MethodEventSource * @param implementation * RPC interface implementation. Also used to deduce the type. */ - protected void registerRpc(T implementation) { - Class[] interfaces = implementation.getClass().getInterfaces(); - if (interfaces.length != 1) { + protected void registerRpc(T implementation) { + Class cls = implementation.getClass(); + Class[] interfaces = cls.getInterfaces(); + while (interfaces.length == 0) { + // Search upwards until an interface is found. It must be found as T + // extends ServerRpc + cls = cls.getSuperclass(); + interfaces = cls.getInterfaces(); + } + if (interfaces.length != 1 + || !(ServerRpc.class.isAssignableFrom(interfaces[0]))) { throw new RuntimeException( "Use registerRpc(T implementation, Class rpcInterfaceType) if the Rpc implementation implements more than one interface"); } diff --git a/src/com/vaadin/ui/Button.java b/src/com/vaadin/ui/Button.java index 68e75b2c69..63106945b2 100644 --- a/src/com/vaadin/ui/Button.java +++ b/src/com/vaadin/ui/Button.java @@ -6,21 +6,18 @@ package com.vaadin.ui; import java.io.Serializable; import java.lang.reflect.Method; -import java.util.Map; import com.vaadin.event.Action; import com.vaadin.event.FieldEvents; import com.vaadin.event.FieldEvents.BlurEvent; import com.vaadin.event.FieldEvents.BlurListener; +import com.vaadin.event.FieldEvents.FocusAndBlurServerRpcImpl; import com.vaadin.event.FieldEvents.FocusEvent; import com.vaadin.event.FieldEvents.FocusListener; import com.vaadin.event.ShortcutAction; import com.vaadin.event.ShortcutAction.KeyCode; import com.vaadin.event.ShortcutAction.ModifierKey; import com.vaadin.event.ShortcutListener; -import com.vaadin.terminal.PaintException; -import com.vaadin.terminal.PaintTarget; -import com.vaadin.terminal.Vaadin6Component; import com.vaadin.terminal.gwt.client.MouseEventDetails; import com.vaadin.terminal.gwt.client.ui.ButtonConnector.ButtonServerRpc; import com.vaadin.terminal.gwt.client.ui.ButtonState; @@ -38,7 +35,7 @@ import com.vaadin.ui.Component.Focusable; @SuppressWarnings("serial") public class Button extends AbstractComponent implements FieldEvents.BlurNotifier, FieldEvents.FocusNotifier, Focusable, - Action.ShortcutNotifier, Vaadin6Component { + Action.ShortcutNotifier { private ButtonServerRpc rpc = new ButtonServerRpc() { public void click(MouseEventDetails mouseEventDetails) { @@ -52,11 +49,19 @@ public class Button extends AbstractComponent implements } }; + FocusAndBlurServerRpcImpl focusBlurRpc = new FocusAndBlurServerRpcImpl(this) { + @Override + protected void fireEvent(Event event) { + Button.this.fireEvent(event); + } + }; + /** * Creates a new push button. */ public Button() { registerRpc(rpc); + registerRpc(focusBlurRpc); } /** @@ -83,26 +88,6 @@ public class Button extends AbstractComponent implements addListener(listener); } - /** - * Invoked when the value of a variable has changed. Button listeners are - * notified if the button is clicked. - * - * @param source - * @param variables - */ - public void changeVariables(Object source, Map variables) { - if (variables.containsKey(FocusEvent.EVENT_ID)) { - fireEvent(new FocusEvent(this)); - } - if (variables.containsKey(BlurEvent.EVENT_ID)) { - fireEvent(new BlurEvent(this)); - } - } - - public void paintContent(PaintTarget target) throws PaintException { - // TODO Remove once Vaadin6Component is no longer implemented - } - /** * Click event. This event is thrown, when the button is clicked. * diff --git a/src/com/vaadin/ui/CheckBox.java b/src/com/vaadin/ui/CheckBox.java index e53bb5a3b7..e98cf3638f 100644 --- a/src/com/vaadin/ui/CheckBox.java +++ b/src/com/vaadin/ui/CheckBox.java @@ -4,24 +4,50 @@ package com.vaadin.ui; -import java.util.Map; - import com.vaadin.data.Property; import com.vaadin.event.FieldEvents.BlurEvent; import com.vaadin.event.FieldEvents.BlurListener; +import com.vaadin.event.FieldEvents.FocusAndBlurServerRpcImpl; import com.vaadin.event.FieldEvents.FocusEvent; import com.vaadin.event.FieldEvents.FocusListener; -import com.vaadin.terminal.PaintException; -import com.vaadin.terminal.PaintTarget; -import com.vaadin.terminal.Vaadin6Component; -import com.vaadin.terminal.gwt.client.ui.VCheckBox; +import com.vaadin.terminal.gwt.client.MouseEventDetails; +import com.vaadin.terminal.gwt.client.ui.CheckBoxConnector.CheckBoxServerRpc; +import com.vaadin.terminal.gwt.client.ui.CheckBoxConnector.CheckBoxState; + +public class CheckBox extends AbstractField { + + private CheckBoxServerRpc rpc = new CheckBoxServerRpc() { + + public void setChecked(boolean checked, + MouseEventDetails mouseEventDetails) { + if (isReadOnly()) { + return; + } + + final Boolean oldValue = getValue(); + final Boolean newValue = checked; + + if (!newValue.equals(oldValue)) { + // The event is only sent if the switch state is changed + setValue(newValue); + } + + } + }; + + FocusAndBlurServerRpcImpl focusBlurRpc = new FocusAndBlurServerRpcImpl(this) { + @Override + protected void fireEvent(Event event) { + CheckBox.this.fireEvent(event); + } + }; -public class CheckBox extends AbstractField implements - Vaadin6Component { /** * Creates a new checkbox. */ public CheckBox() { + registerRpc(rpc); + registerRpc(focusBlurRpc); setValue(Boolean.FALSE); } @@ -66,32 +92,18 @@ public class CheckBox extends AbstractField implements return Boolean.class; } - public void paintContent(PaintTarget target) throws PaintException { - Boolean value = getValue(); - boolean booleanValue = (value != null) ? value : false; - target.addVariable(this, VCheckBox.VARIABLE_STATE, booleanValue); + @Override + public CheckBoxState getState() { + return (CheckBoxState) super.getState(); } - public void changeVariables(Object source, Map variables) { - - if (!isReadOnly() && variables.containsKey(VCheckBox.VARIABLE_STATE)) { - // Gets the new and old states - final Boolean newValue = (Boolean) variables - .get(VCheckBox.VARIABLE_STATE); - final Boolean oldValue = getValue(); - - // The event is only sent if the switch state is changed - if (newValue != null && !newValue.equals(oldValue)) { - setValue(newValue); - } - } - - if (variables.containsKey(FocusEvent.EVENT_ID)) { - fireEvent(new FocusEvent(this)); - } - if (variables.containsKey(BlurEvent.EVENT_ID)) { - fireEvent(new BlurEvent(this)); + @Override + protected void setInternalValue(Boolean newValue) { + super.setInternalValue(newValue); + if (newValue == null) { + newValue = false; } + getState().setChecked(newValue); } public void addListener(BlurListener listener) { @@ -110,7 +122,6 @@ public class CheckBox extends AbstractField implements public void removeListener(FocusListener listener) { removeListener(FocusEvent.EVENT_ID, FocusEvent.class, listener); - } /** -- 2.39.5