Skip to content

Commit

Permalink
Refactor handling of focus and blur RPC in Connectors (#17917)
Browse files Browse the repository at this point in the history
Change-Id: I2e4fb3fb941fda2aa4cbc7154fa9a3f7e8e9ce02
  • Loading branch information
jdahlstrom committed May 20, 2015
1 parent 26b06bd commit 8ba56b9
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 181 deletions.
11 changes: 5 additions & 6 deletions client/src/com/vaadin/client/ApplicationConnection.java
Expand Up @@ -63,6 +63,7 @@
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.client.ApplicationConfiguration.ErrorMessage;
import com.vaadin.client.ApplicationConnection.ApplicationStoppedEvent;
import com.vaadin.client.ResourceLoader.ResourceLoadEvent;
import com.vaadin.client.ResourceLoader.ResourceLoadListener;
import com.vaadin.client.communication.HasJavaScriptConnectorHelper;
Expand Down Expand Up @@ -94,7 +95,6 @@
import com.vaadin.client.ui.dd.VDragAndDropManager;
import com.vaadin.client.ui.ui.UIConnector;
import com.vaadin.client.ui.window.WindowConnector;
import com.vaadin.shared.AbstractComponentState;
import com.vaadin.shared.ApplicationConstants;
import com.vaadin.shared.JsonConstants;
import com.vaadin.shared.VaadinUriResolver;
Expand Down Expand Up @@ -3414,20 +3414,19 @@ public ApplicationConfiguration getConfiguration() {
* before the component is updated so the value is correct if called from
* updatedFromUIDL.
*
* @param paintable
* @param connector
* The connector to register event listeners for
* @param eventIdentifier
* The identifier for the event
* @return true if at least one listener has been registered on server side
* for the event identified by eventIdentifier.
* @deprecated As of 7.0. Use
* {@link AbstractComponentState#hasEventListener(String)}
* instead
* {@link AbstractConnector#hasEventListener(String)} instead
*/
@Deprecated
public boolean hasEventListeners(ComponentConnector paintable,
public boolean hasEventListeners(ComponentConnector connector,
String eventIdentifier) {
return paintable.hasEventListener(eventIdentifier);
return connector.hasEventListener(eventIdentifier);
}

/**
Expand Down
17 changes: 7 additions & 10 deletions client/src/com/vaadin/client/EventHelper.java
Expand Up @@ -51,7 +51,6 @@
*
*
* </pre>
*
*/
public class EventHelper {

Expand All @@ -69,7 +68,7 @@ public class EventHelper {
*/
public static <T extends ComponentConnector & FocusHandler> HandlerRegistration updateFocusHandler(
T connector, HandlerRegistration handlerRegistration) {
return updateHandler(connector, FOCUS, handlerRegistration,
return updateHandler(connector, connector, FOCUS, handlerRegistration,
FocusEvent.getType(), connector.getWidget());
}

Expand All @@ -89,7 +88,7 @@ public static <T extends ComponentConnector & FocusHandler> HandlerRegistration
*/
public static <T extends ComponentConnector & FocusHandler> HandlerRegistration updateFocusHandler(
T connector, HandlerRegistration handlerRegistration, Widget widget) {
return updateHandler(connector, FOCUS, handlerRegistration,
return updateHandler(connector, connector, FOCUS, handlerRegistration,
FocusEvent.getType(), widget);
}

Expand All @@ -107,7 +106,7 @@ public static <T extends ComponentConnector & FocusHandler> HandlerRegistration
*/
public static <T extends ComponentConnector & BlurHandler> HandlerRegistration updateBlurHandler(
T connector, HandlerRegistration handlerRegistration) {
return updateHandler(connector, BLUR, handlerRegistration,
return updateHandler(connector, connector, BLUR, handlerRegistration,
BlurEvent.getType(), connector.getWidget());
}

Expand All @@ -128,23 +127,21 @@ public static <T extends ComponentConnector & BlurHandler> HandlerRegistration u
*/
public static <T extends ComponentConnector & BlurHandler> HandlerRegistration updateBlurHandler(
T connector, HandlerRegistration handlerRegistration, Widget widget) {
return updateHandler(connector, BLUR, handlerRegistration,
return updateHandler(connector, connector, BLUR, handlerRegistration,
BlurEvent.getType(), widget);
}

private static <H extends EventHandler> HandlerRegistration updateHandler(
ComponentConnector connector, String eventIdentifier,
public static <H extends EventHandler> HandlerRegistration updateHandler(
ComponentConnector connector, H handler, String eventIdentifier,
HandlerRegistration handlerRegistration, Type<H> type, Widget widget) {
if (connector.hasEventListener(eventIdentifier)) {
if (handlerRegistration == null) {
handlerRegistration = widget.addDomHandler((H) connector, type);
handlerRegistration = widget.addDomHandler(handler, type);
}
} else if (handlerRegistration != null) {
handlerRegistration.removeHandler();
handlerRegistration = null;
}
return handlerRegistration;

}

}
36 changes: 5 additions & 31 deletions client/src/com/vaadin/client/connectors/GridConnector.java
Expand Up @@ -32,17 +32,11 @@
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
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.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.client.ComponentConnector;
import com.vaadin.client.ConnectorHierarchyChangeEvent;
import com.vaadin.client.DeferredWorker;
import com.vaadin.client.EventHelper;
import com.vaadin.client.MouseEventDetailsBuilder;
import com.vaadin.client.annotations.OnStateChange;
import com.vaadin.client.communication.StateChangeEvent;
Expand All @@ -52,6 +46,7 @@
import com.vaadin.client.renderers.Renderer;
import com.vaadin.client.ui.AbstractFieldConnector;
import com.vaadin.client.ui.AbstractHasComponentsConnector;
import com.vaadin.client.ui.ConnectorFocusAndBlurHandler;
import com.vaadin.client.ui.SimpleManagedLayout;
import com.vaadin.client.widget.grid.CellReference;
import com.vaadin.client.widget.grid.CellStyleGenerator;
Expand Down Expand Up @@ -86,7 +81,6 @@
import com.vaadin.client.widgets.Grid.HeaderCell;
import com.vaadin.client.widgets.Grid.HeaderRow;
import com.vaadin.shared.Connector;
import com.vaadin.shared.communication.FieldRpc.FocusAndBlurServerRpc;
import com.vaadin.shared.data.sort.SortDirection;
import com.vaadin.shared.ui.Connect;
import com.vaadin.shared.ui.grid.DetailsConnectorChange;
Expand Down Expand Up @@ -119,7 +113,7 @@
*/
@Connect(com.vaadin.ui.Grid.class)
public class GridConnector extends AbstractHasComponentsConnector implements
SimpleManagedLayout, DeferredWorker, FocusHandler, BlurHandler {
SimpleManagedLayout, DeferredWorker {

private static final class CustomCellStyleGenerator implements
CellStyleGenerator<JsonObject> {
Expand Down Expand Up @@ -764,9 +758,6 @@ public void closeDetails(int rowIndex) {

private final LazyDetailsScrollAdjuster lazyDetailsScrollAdjuster = new LazyDetailsScrollAdjuster();

private HandlerRegistration focusHandlerRegistration = null;
private HandlerRegistration blurHandlerRegistration = null;

@Override
@SuppressWarnings("unchecked")
public Grid<JsonObject> getWidget() {
Expand Down Expand Up @@ -915,6 +906,9 @@ public void onSelectAll(SelectAllEvent<JsonObject> event) {
getWidget().addColumnReorderHandler(columnReorderHandler);
getWidget().addColumnVisibilityChangeHandler(
columnVisibilityChangeHandler);

ConnectorFocusAndBlurHandler.addHandlers(this);

getWidget().setDetailsGenerator(customDetailsGenerator);
getLayoutManager().registerDependency(this, getWidget().getElement());

Expand Down Expand Up @@ -992,12 +986,6 @@ public void onStateChanged(final StateChangeEvent stateChangeEvent) {
getWidget().resetSizesFromDom();
lastKnownTheme = activeTheme;
}

// Focus and blur events
focusHandlerRegistration = EventHelper.updateFocusHandler(this,
focusHandlerRegistration);
blurHandlerRegistration = EventHelper.updateBlurHandler(this,
blurHandlerRegistration);
}

private void updateSelectDeselectAllowed() {
Expand Down Expand Up @@ -1434,18 +1422,4 @@ public boolean isWorkPending() {
public DetailsListener getDetailsListener() {
return detailsListener;
}

@Override
public void onFocus(FocusEvent event) {
// EventHelper.updateFocusHandler ensures that this is called only when
// there is a listener on server side
getRpcProxy(FocusAndBlurServerRpc.class).focus();
}

@Override
public void onBlur(BlurEvent event) {
// EventHelper.updateFocusHandler ensures that this is called only when
// there is a listener on server side
getRpcProxy(FocusAndBlurServerRpc.class).blur();
}
}
87 changes: 87 additions & 0 deletions client/src/com/vaadin/client/ui/ConnectorFocusAndBlurHandler.java
@@ -0,0 +1,87 @@
/*
* Copyright 2000-2014 Vaadin Ltd.
*
* 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.vaadin.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.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.client.EventHelper;
import com.vaadin.client.communication.StateChangeEvent;
import com.vaadin.client.communication.StateChangeEvent.StateChangeHandler;
import com.vaadin.shared.EventId;
import com.vaadin.shared.communication.FieldRpc.FocusAndBlurServerRpc;

/**
* A handler for focus and blur events which uses {@link FocusAndBlurServerRpc}
* to transmit received events to the server. Events are only handled if there
* is a corresponding listener on the server side.
*
* @since
* @author Vaadin Ltd
*/
public class ConnectorFocusAndBlurHandler implements StateChangeHandler,
FocusHandler, BlurHandler {

private final AbstractComponentConnector connector;
private final Widget widget;
private HandlerRegistration focusRegistration = null;
private HandlerRegistration blurRegistration = null;

public static void addHandlers(AbstractComponentConnector connector) {
addHandlers(connector, connector.getWidget());
}

public static void addHandlers(AbstractComponentConnector connector,
Widget widget) {
connector.addStateChangeHandler("registeredEventListeners",
new ConnectorFocusAndBlurHandler(connector, widget));
}

private ConnectorFocusAndBlurHandler(AbstractComponentConnector connector,
Widget widget) {
this.connector = connector;
this.widget = widget;
}

@Override
public void onStateChanged(StateChangeEvent stateChangeEvent) {
focusRegistration = EventHelper.updateHandler(connector, this,
EventId.FOCUS, focusRegistration, FocusEvent.getType(), widget);
blurRegistration = EventHelper.updateHandler(connector, this,
EventId.BLUR, blurRegistration, BlurEvent.getType(), widget);
}

@Override
public void onFocus(FocusEvent event) {
// updateHandler ensures that this is called only when
// there is a listener on the server side
getRpc().focus();
}

@Override
public void onBlur(BlurEvent event) {
// updateHandler ensures that this is called only when
// there is a listener on the server side
getRpc().blur();
}

private FocusAndBlurServerRpc getRpc() {
return connector.getRpcProxy(FocusAndBlurServerRpc.class);
}
}
38 changes: 3 additions & 35 deletions client/src/com/vaadin/client/ui/button/ButtonConnector.java
Expand Up @@ -16,24 +16,17 @@

package com.vaadin.client.ui.button;

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.vaadin.client.EventHelper;
import com.vaadin.client.MouseEventDetailsBuilder;
import com.vaadin.client.VCaption;
import com.vaadin.client.annotations.OnStateChange;
import com.vaadin.client.communication.StateChangeEvent;
import com.vaadin.client.ui.AbstractComponentConnector;
import com.vaadin.client.ui.ConnectorFocusAndBlurHandler;
import com.vaadin.client.ui.Icon;
import com.vaadin.client.ui.VButton;
import com.vaadin.shared.MouseEventDetails;
import com.vaadin.shared.communication.FieldRpc.FocusAndBlurServerRpc;
import com.vaadin.shared.ui.Connect;
import com.vaadin.shared.ui.Connect.LoadStyle;
import com.vaadin.shared.ui.button.ButtonServerRpc;
Expand All @@ -42,10 +35,7 @@

@Connect(value = Button.class, loadStyle = LoadStyle.EAGER)
public class ButtonConnector extends AbstractComponentConnector implements
BlurHandler, FocusHandler, ClickHandler {

private HandlerRegistration focusHandlerRegistration = null;
private HandlerRegistration blurHandlerRegistration = null;
ClickHandler {

@Override
public boolean delegateCaptionHandling() {
Expand All @@ -57,6 +47,7 @@ public void init() {
super.init();
getWidget().addClickHandler(this);
getWidget().client = getConnection();
ConnectorFocusAndBlurHandler.addHandlers(this);
}

@OnStateChange("errorMessage")
Expand Down Expand Up @@ -90,15 +81,6 @@ void onResourceChange() {
}
}

@Override
public void onStateChanged(StateChangeEvent stateChangeEvent) {
super.onStateChanged(stateChangeEvent);
focusHandlerRegistration = EventHelper.updateFocusHandler(this,
focusHandlerRegistration);
blurHandlerRegistration = EventHelper.updateBlurHandler(this,
blurHandlerRegistration);
}

@OnStateChange({ "caption", "captionAsHtml" })
void setCaption() {
VCaption.setCaptionText(getWidget().captionElement, getState());
Expand Down Expand Up @@ -126,20 +108,6 @@ public ButtonState getState() {
return (ButtonState) super.getState();
}

@Override
public void onFocus(FocusEvent event) {
// EventHelper.updateFocusHandler ensures that this is called only when
// there is a listener on server side
getRpcProxy(FocusAndBlurServerRpc.class).focus();
}

@Override
public void onBlur(BlurEvent event) {
// EventHelper.updateFocusHandler ensures that this is called only when
// there is a listener on server side
getRpcProxy(FocusAndBlurServerRpc.class).blur();
}

@Override
public void onClick(ClickEvent event) {
if (getState().disableOnClick) {
Expand Down

0 comments on commit 8ba56b9

Please sign in to comment.