From 04f30c6892c8ddf8794ed8561fa6f00beefeec28 Mon Sep 17 00:00:00 2001 From: Pekka Hyvönen Date: Thu, 15 Dec 2016 13:46:05 +0200 Subject: Allow defining a focus delegate component for CustomField (#20336) --- .../java/com/vaadin/v7/client/ui/VCustomField.java | 60 ++++++++++++++++++++++ .../ui/customfield/CustomFieldConnector.java | 35 +++++++++++-- 2 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 compatibility-client/src/main/java/com/vaadin/v7/client/ui/VCustomField.java (limited to 'compatibility-client/src') diff --git a/compatibility-client/src/main/java/com/vaadin/v7/client/ui/VCustomField.java b/compatibility-client/src/main/java/com/vaadin/v7/client/ui/VCustomField.java new file mode 100644 index 0000000000..f826e5d620 --- /dev/null +++ b/compatibility-client/src/main/java/com/vaadin/v7/client/ui/VCustomField.java @@ -0,0 +1,60 @@ +/* + * Copyright 2000-2016 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.v7.client.ui; + +import com.vaadin.client.Focusable; + +@Deprecated +public class VCustomField extends VCustomComponent implements Focusable { + + private Focusable focusDelegate; + + @Override + public void focus() { + if (focusDelegate != null) { + focusDelegate.focus(); + } + } + + /** + * Sets the focusable widget to focus instead of this custom field. + * + * @param focusDelegate + * the widget to delegate focus to + */ + public void setFocusDelegate(Focusable focusDelegate) { + this.focusDelegate = focusDelegate; + + } + + /** + * Sets the focusable widget to focus instead of this custom field. + * + * @param focusDelegate + * the widget to delegate focus to + */ + public void setFocusDelegate( + final com.google.gwt.user.client.ui.Focusable focusDelegate) { + this.focusDelegate = new Focusable() { + @Override + public void focus() { + focusDelegate.setFocus(true); + } + }; + + } + +} diff --git a/compatibility-client/src/main/java/com/vaadin/v7/client/ui/customfield/CustomFieldConnector.java b/compatibility-client/src/main/java/com/vaadin/v7/client/ui/customfield/CustomFieldConnector.java index 2d26c7c040..f72672b43d 100644 --- a/compatibility-client/src/main/java/com/vaadin/v7/client/ui/customfield/CustomFieldConnector.java +++ b/compatibility-client/src/main/java/com/vaadin/v7/client/ui/customfield/CustomFieldConnector.java @@ -17,16 +17,19 @@ package com.vaadin.v7.client.ui.customfield; import java.util.Collections; import java.util.List; +import java.util.logging.Logger; import com.google.gwt.event.shared.HandlerRegistration; import com.google.gwt.user.client.ui.Widget; import com.vaadin.client.ComponentConnector; import com.vaadin.client.ConnectorHierarchyChangeEvent; import com.vaadin.client.ConnectorHierarchyChangeEvent.ConnectorHierarchyChangeHandler; +import com.vaadin.client.Focusable; import com.vaadin.client.HasComponentsConnector; +import com.vaadin.client.communication.StateChangeEvent; import com.vaadin.shared.ui.Connect; import com.vaadin.v7.client.ui.AbstractFieldConnector; -import com.vaadin.v7.client.ui.VCustomComponent; +import com.vaadin.v7.client.ui.VCustomField; import com.vaadin.v7.ui.CustomField; @Connect(value = CustomField.class) @@ -43,8 +46,8 @@ public class CustomFieldConnector extends AbstractFieldConnector } @Override - public VCustomComponent getWidget() { - return (VCustomComponent) super.getWidget(); + public VCustomField getWidget() { + return (VCustomField) super.getWidget(); } @Override @@ -52,6 +55,32 @@ public class CustomFieldConnector extends AbstractFieldConnector // NOP, custom field does not render the caption of its content } + @Override + public void onStateChanged(StateChangeEvent stateChangeEvent) { + super.onStateChanged(stateChangeEvent); + if (getState().focusDelegate != null) { + Widget widget = ((ComponentConnector) getState().focusDelegate) + .getWidget(); + if (widget instanceof Focusable) { + getWidget().setFocusDelegate((Focusable) widget); + } else if (widget instanceof com.google.gwt.user.client.ui.Focusable) { + getWidget().setFocusDelegate( + (com.google.gwt.user.client.ui.Focusable) widget); + } else { + getLogger().warning( + "The given focus delegate does not implement Focusable: " + + widget.getClass().getName()); + } + } else { + getWidget().setFocusDelegate((Focusable) null); + } + + } + + private static Logger getLogger() { + return Logger.getLogger(CustomFieldConnector.class.getName()); + } + @Override public void onConnectorHierarchyChange( ConnectorHierarchyChangeEvent event) { -- cgit v1.2.3