From 9586a30b64bfd1e8645574d9d7b6568d52dc9e25 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Mon, 25 Mar 2013 21:54:36 +0200 Subject: [PATCH] Made is possible to configure tooltip on the server (#8065) Change-Id: I35af6df1dfa75ef1de1268eb630fc0f4b9306170 --- client/src/com/vaadin/client/VTooltip.java | 138 +++++++++- .../com/vaadin/client/ui/ui/UIConnector.java | 17 ++ server/src/com/vaadin/ui/Tooltip.java | 240 ++++++++++++++++++ server/src/com/vaadin/ui/UI.java | 11 + .../src/com/vaadin/shared/ui/ui/UIState.java | 12 + .../components/ui/TooltipConfiguration.html | 135 ++++++++++ .../components/ui/TooltipConfiguration.java | 107 ++++++++ 7 files changed, 650 insertions(+), 10 deletions(-) create mode 100644 server/src/com/vaadin/ui/Tooltip.java create mode 100644 uitest/src/com/vaadin/tests/components/ui/TooltipConfiguration.html create mode 100644 uitest/src/com/vaadin/tests/components/ui/TooltipConfiguration.java diff --git a/client/src/com/vaadin/client/VTooltip.java b/client/src/com/vaadin/client/VTooltip.java index e6d9a79a5b..53770f4201 100644 --- a/client/src/com/vaadin/client/VTooltip.java +++ b/client/src/com/vaadin/client/VTooltip.java @@ -48,11 +48,6 @@ public class VTooltip extends VOverlay { public static final int TOOLTIP_EVENTS = Event.ONKEYDOWN | Event.ONMOUSEOVER | Event.ONMOUSEOUT | Event.ONMOUSEMOVE | Event.ONCLICK; - protected static final int MAX_WIDTH = 500; - private static final int QUICK_OPEN_TIMEOUT = 1000; - private static final int CLOSE_TIMEOUT = 300; - private static final int OPEN_DELAY = 750; - private static final int QUICK_OPEN_DELAY = 100; VErrorMessage em = new VErrorMessage(); Element description = DOM.createDiv(); @@ -64,6 +59,13 @@ public class VTooltip extends VOverlay { private String uniqueId = DOM.createUniqueId(); private Element layoutElement; + private int maxWidth; + + // Delays for the tooltip, configurable on the server side + private int openDelay; + private int quickOpenDelay; + private int quickOpenTimeout; + private int closeTimeout; /** * Used to show tooltips; usually used via the singleton in @@ -126,8 +128,8 @@ public class VTooltip extends VOverlay { @Override public void setPosition(int offsetWidth, int offsetHeight) { - if (offsetWidth > MAX_WIDTH) { - setWidth(MAX_WIDTH + "px"); + if (offsetWidth > getMaxWidth()) { + setWidth(getMaxWidth() + "px"); // Check new height and width with reflowed content offsetWidth = getOffsetWidth(); @@ -172,7 +174,7 @@ public class VTooltip extends VOverlay { // Schedule timer for showing the tooltip according to if it was // recently closed or not. - int timeout = justClosed ? QUICK_OPEN_DELAY : OPEN_DELAY; + int timeout = justClosed ? getQuickOpenDelay() : getOpenDelay(); showTimer.schedule(timeout); opening = true; } @@ -222,10 +224,10 @@ public class VTooltip extends VOverlay { // already about to close return; } - closeTimer.schedule(CLOSE_TIMEOUT); + closeTimer.schedule(getCloseTimeout()); closing = true; justClosed = true; - justClosedTimer.schedule(QUICK_OPEN_TIMEOUT); + justClosedTimer.schedule(getQuickOpenTimeout()); } @Override @@ -466,4 +468,120 @@ public class VTooltip extends VOverlay { Roles.getTooltipRole().setAriaHiddenState(layoutElement, false); } + + /** + * Returns the time (in ms) the tooltip should be displayed after an event + * that will cause it to be closed (e.g. mouse click outside the component, + * key down). + * + * @return The close timeout (in ms) + */ + public int getCloseTimeout() { + return closeTimeout; + } + + /** + * Sets the time (in ms) the tooltip should be displayed after an event that + * will cause it to be closed (e.g. mouse click outside the component, key + * down). + * + * @param closeTimeout + * The close timeout (in ms) + */ + public void setCloseTimeout(int closeTimeout) { + this.closeTimeout = closeTimeout; + } + + /** + * Returns the time (in ms) during which {@link #getQuickOpenDelay()} should + * be used instead of {@link #getOpenDelay()}. The quick open delay is used + * when the tooltip has very recently been shown, is currently hidden but + * about to be shown again. + * + * @return The quick open timeout (in ms) + */ + public int getQuickOpenTimeout() { + return quickOpenTimeout; + } + + /** + * Sets the time (in ms) that determines when {@link #getQuickOpenDelay()} + * should be used instead of {@link #getOpenDelay()}. The quick open delay + * is used when the tooltip has very recently been shown, is currently + * hidden but about to be shown again. + * + * @param quickOpenTimeout + * The quick open timeout (in ms) + */ + public void setQuickOpenTimeout(int quickOpenTimeout) { + this.quickOpenTimeout = quickOpenTimeout; + } + + /** + * Returns the time (in ms) that should elapse before a tooltip will be + * shown, in the situation when a tooltip has very recently been shown + * (within {@link #getQuickOpenDelay()} ms). + * + * @return The quick open delay (in ms) + */ + public int getQuickOpenDelay() { + return quickOpenDelay; + } + + /** + * Sets the time (in ms) that should elapse before a tooltip will be shown, + * in the situation when a tooltip has very recently been shown (within + * {@link #getQuickOpenDelay()} ms). + * + * @param quickOpenDelay + * The quick open delay (in ms) + */ + public void setQuickOpenDelay(int quickOpenDelay) { + this.quickOpenDelay = quickOpenDelay; + } + + /** + * Returns the time (in ms) that should elapse after an event triggering + * tooltip showing has occurred (e.g. mouse over) before the tooltip is + * shown. If a tooltip has recently been shown, then + * {@link #getQuickOpenDelay()} is used instead of this. + * + * @return The open delay (in ms) + */ + public int getOpenDelay() { + return openDelay; + } + + /** + * Sets the time (in ms) that should elapse after an event triggering + * tooltip showing has occurred (e.g. mouse over) before the tooltip is + * shown. If a tooltip has recently been shown, then + * {@link #getQuickOpenDelay()} is used instead of this. + * + * @param openDelay + * The open delay (in ms) + */ + public void setOpenDelay(int openDelay) { + this.openDelay = openDelay; + } + + /** + * Sets the maximum width of the tooltip popup. + * + * @param maxWidth + * The maximum width the tooltip popup (in pixels) + */ + public void setMaxWidth(int maxWidth) { + this.maxWidth = maxWidth; + } + + /** + * Returns the maximum width of the tooltip popup. + * + * @return The maximum width the tooltip popup (in pixels) + */ + public int getMaxWidth() { + return maxWidth; + } + } diff --git a/client/src/com/vaadin/client/ui/ui/UIConnector.java b/client/src/com/vaadin/client/ui/ui/UIConnector.java index 1df74afd2e..b8b7786d21 100644 --- a/client/src/com/vaadin/client/ui/ui/UIConnector.java +++ b/client/src/com/vaadin/client/ui/ui/UIConnector.java @@ -599,4 +599,21 @@ public class UIConnector extends AbstractSingleComponentContainerConnector } }); } + + @Override + public void onStateChanged(StateChangeEvent stateChangeEvent) { + super.onStateChanged(stateChangeEvent); + if (stateChangeEvent.hasPropertyChanged("tooltipConfiguration")) { + getConnection().getVTooltip().setCloseTimeout( + getState().tooltipConfiguration.closeTimeout); + getConnection().getVTooltip().setOpenDelay( + getState().tooltipConfiguration.openDelay); + getConnection().getVTooltip().setQuickOpenDelay( + getState().tooltipConfiguration.quickOpenDelay); + getConnection().getVTooltip().setQuickOpenTimeout( + getState().tooltipConfiguration.quickOpenTimeout); + getConnection().getVTooltip().setMaxWidth( + getState().tooltipConfiguration.maxWidth); + } + } } diff --git a/server/src/com/vaadin/ui/Tooltip.java b/server/src/com/vaadin/ui/Tooltip.java new file mode 100644 index 0000000000..093bb33b51 --- /dev/null +++ b/server/src/com/vaadin/ui/Tooltip.java @@ -0,0 +1,240 @@ +/* + * Copyright 2000-2013 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.ui; + +import java.io.Serializable; + +import com.vaadin.shared.ui.ui.UIState.TooltipConfiguration; + +/** + * Provides method for configuring the tooltip. + * + * @author Vaadin Ltd + * @since 7.1 + */ +public interface Tooltip extends Serializable { + /** + * Returns the time (in ms) the tooltip should be displayed after an event + * that will cause it to be closed (e.g. mouse click outside the component, + * key down). + * + * @return The close timeout + */ + public int getCloseTimeout(); + + /** + * Sets the time (in ms) the tooltip should be displayed after an event that + * will cause it to be closed (e.g. mouse click outside the component, key + * down). + * + * @param closeTimeout + * The close timeout + */ + public void setCloseTimeout(int closeTimeout); + + /** + * Returns the time (in ms) during which {@link #getQuickOpenDelay()} should + * be used instead of {@link #getOpenDelay()}. The quick open delay is used + * when the tooltip has very recently been shown, is currently hidden but + * about to be shown again. + * + * @return The quick open timeout + */ + public int getQuickOpenTimeout(); + + /** + * Sets the time (in ms) that determines when {@link #getQuickOpenDelay()} + * should be used instead of {@link #getOpenDelay()}. The quick open delay + * is used when the tooltip has very recently been shown, is currently + * hidden but about to be shown again. + * + * @param quickOpenTimeout + * The quick open timeout + */ + public void setQuickOpenTimeout(int quickOpenTimeout); + + /** + * Returns the time (in ms) that should elapse before a tooltip will be + * shown, in the situation when a tooltip has very recently been shown + * (within {@link #getQuickOpenDelay()} ms). + * + * @return The quick open delay + */ + public int getQuickOpenDelay(); + + /** + * Sets the time (in ms) that should elapse before a tooltip will be shown, + * in the situation when a tooltip has very recently been shown (within + * {@link #getQuickOpenDelay()} ms). + * + * @param quickOpenDelay + * The quick open delay + */ + public void setQuickOpenDelay(int quickOpenDelay); + + /** + * Returns the time (in ms) that should elapse after an event triggering + * tooltip showing has occurred (e.g. mouse over) before the tooltip is + * shown. If a tooltip has recently been shown, then + * {@link #getQuickOpenDelay()} is used instead of this. + * + * @return The open delay + */ + public int getOpenDelay(); + + /** + * Sets the time (in ms) that should elapse after an event triggering + * tooltip showing has occurred (e.g. mouse over) before the tooltip is + * shown. If a tooltip has recently been shown, then + * {@link #getQuickOpenDelay()} is used instead of this. + * + * @param openDelay + * The open delay + */ + public void setOpenDelay(int openDelay); + + /** + * Returns the maximum width of the tooltip popup. + * + * @return The maximum width the tooltip popup + */ + public int getMaxWidth(); + + /** + * Sets the maximum width of the tooltip popup. + * + * @param maxWidth + * The maximum width the tooltip popup + */ + public void setMaxWidth(int maxWidth); +} + +class TooltipImpl implements Tooltip { + private UI ui; + + public TooltipImpl(UI ui) { + this.ui = ui; + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.ui.UI.Tooltip#getCloseTimeout() + */ + @Override + public int getCloseTimeout() { + return getState(false).closeTimeout; + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.ui.Tooltip#setCloseTimeout(int) + */ + @Override + public void setCloseTimeout(int closeTimeout) { + getState().closeTimeout = closeTimeout; + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.ui.Tooltip#getQuickOpenTimeout() + */ + @Override + public int getQuickOpenTimeout() { + return getState(false).quickOpenTimeout; + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.ui.Tooltip#setQuickOpenTimeout(int) + */ + @Override + public void setQuickOpenTimeout(int quickOpenTimeout) { + getState().quickOpenTimeout = quickOpenTimeout; + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.ui.Tooltip#getQuickOpenDelay() + */ + @Override + public int getQuickOpenDelay() { + return getState(false).quickOpenDelay; + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.ui.Tooltip#setQuickOpenDelay(int) + */ + @Override + public void setQuickOpenDelay(int quickOpenDelay) { + getState().quickOpenDelay = quickOpenDelay; + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.ui.Tooltip#getOpenDelay() + */ + @Override + public int getOpenDelay() { + return getState(false).openDelay; + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.ui.Tooltip#setOpenDelay(int) + */ + @Override + public void setOpenDelay(int openDelay) { + getState().openDelay = openDelay; + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.ui.Tooltip#getMaxWidth() + */ + @Override + public int getMaxWidth() { + return getState(false).maxWidth; + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.ui.Tooltip#setMaxWidth(int) + */ + @Override + public void setMaxWidth(int maxWidth) { + getState().maxWidth = maxWidth; + } + + private TooltipConfiguration getState() { + return ui.getState().tooltipConfiguration; + } + + private TooltipConfiguration getState(boolean markAsDirty) { + return ui.getState(markAsDirty).tooltipConfiguration; + } + +} diff --git a/server/src/com/vaadin/ui/UI.java b/server/src/com/vaadin/ui/UI.java index 47cfd471cd..e9499da7f3 100644 --- a/server/src/com/vaadin/ui/UI.java +++ b/server/src/com/vaadin/ui/UI.java @@ -155,6 +155,8 @@ public abstract class UI extends AbstractSingleComponentContainer implements private boolean closing = false; + private Tooltip tooltip = new TooltipImpl(this); + /** * Creates a new empty UI without a caption. The content of the UI must be * set by calling {@link #setContent(Component)} before using the UI. @@ -1095,4 +1097,13 @@ public abstract class UI extends AbstractSingleComponentContainer implements } } + + /** + * Retrieves the object used for configuring tooltips. + * + * @return The instance used for tooltip configuration + */ + public Tooltip getTooltip() { + return tooltip; + } } diff --git a/shared/src/com/vaadin/shared/ui/ui/UIState.java b/shared/src/com/vaadin/shared/ui/ui/UIState.java index 9abaf47f4b..cc6897dd0d 100644 --- a/shared/src/com/vaadin/shared/ui/ui/UIState.java +++ b/shared/src/com/vaadin/shared/ui/ui/UIState.java @@ -15,9 +15,21 @@ */ package com.vaadin.shared.ui.ui; +import java.io.Serializable; + import com.vaadin.shared.ui.TabIndexState; public class UIState extends TabIndexState { + public TooltipConfiguration tooltipConfiguration = new TooltipConfiguration(); + + public static class TooltipConfiguration implements Serializable { + public int openDelay = 750; + public int quickOpenDelay = 100; + public int quickOpenTimeout = 1000; + public int closeTimeout = 300; + public int maxWidth = 500; + } + { primaryStyleName = "v-ui"; // Default is 1 for legacy reasons diff --git a/uitest/src/com/vaadin/tests/components/ui/TooltipConfiguration.html b/uitest/src/com/vaadin/tests/components/ui/TooltipConfiguration.html new file mode 100644 index 0000000000..5170d4408f --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/ui/TooltipConfiguration.html @@ -0,0 +1,135 @@ + + + + + + +New Test + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
New Test
open/run/com.vaadin.tests.components.ui.TooltipConfiguration?restartApplication
typevaadin=runcomvaadintestscomponentsuiTooltipConfiguration::PID_SClose timeout0
mouseMoveAtvaadin=runcomvaadintestscomponentsuiTooltipConfiguration::PID_SshortTooltip0,0
waitForElementPresentid=gwt-uid-3
assertTextid=gwt-uid-3This is a short tooltip
mouseClickvaadin=runcomvaadintestscomponentsuiTooltipConfiguration::
assertElementNotPresentid=gwt-uid-3
typevaadin=runcomvaadintestscomponentsuiTooltipConfiguration::PID_SClose timeout3000
mouseMoveAtvaadin=runcomvaadintestscomponentsuiTooltipConfiguration::PID_SshortTooltip0,0
waitForElementPresentid=gwt-uid-3
assertTextid=gwt-uid-3This is a short tooltip
mouseClickvaadin=runcomvaadintestscomponentsuiTooltipConfiguration::
assertElementPresentid=gwt-uid-3
mouseClickvaadin=runcomvaadintestscomponentsuiTooltipConfiguration::PID_SClose timeout60,9
typevaadin=runcomvaadintestscomponentsuiTooltipConfiguration::PID_SClose timeout0
mouseMoveAtvaadin=runcomvaadintestscomponentsuiTooltipConfiguration::PID_SlongTooltip0,0
waitForElementPresentid=gwt-uid-3
assertElementWidthid=gwt-uid-3500
mouseClickvaadin=runcomvaadintestscomponentsuiTooltipConfiguration::
typevaadin=runcomvaadintestscomponentsuiTooltipConfiguration::PID_SMax width100
mouseMoveAtvaadin=runcomvaadintestscomponentsuiTooltipConfiguration::PID_SlongTooltip0,0
waitForElementPresentid=gwt-uid-3
assertElementWidthid=gwt-uid-3100
+ + diff --git a/uitest/src/com/vaadin/tests/components/ui/TooltipConfiguration.java b/uitest/src/com/vaadin/tests/components/ui/TooltipConfiguration.java new file mode 100644 index 0000000000..b8998ff32e --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/ui/TooltipConfiguration.java @@ -0,0 +1,107 @@ +package com.vaadin.tests.components.ui; + +import com.vaadin.data.Property; +import com.vaadin.data.Property.ValueChangeEvent; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUIWithLog; +import com.vaadin.tests.util.LoremIpsum; +import com.vaadin.ui.NativeButton; +import com.vaadin.ui.TextField; + +public class TooltipConfiguration extends AbstractTestUIWithLog { + + private TextField closeTimeout; + private TextField quickOpenTimeout; + private TextField maxWidth; + private TextField openDelay; + private TextField quickOpenDelay; + + @Override + protected void setup(VaadinRequest request) { + NativeButton componentWithShortTooltip = new NativeButton( + "Short tooltip"); + componentWithShortTooltip.setDescription("This is a short tooltip"); + componentWithShortTooltip.setId("shortTooltip"); + + NativeButton componentWithLongTooltip = new NativeButton("Long tooltip"); + componentWithLongTooltip.setId("longTooltip"); + componentWithLongTooltip.setDescription(LoremIpsum.get(5000)); + + closeTimeout = createIntegerTextField("Close timeout", + getState().tooltipConfiguration.closeTimeout); + closeTimeout.addValueChangeListener(new Property.ValueChangeListener() { + @Override + public void valueChange(ValueChangeEvent event) { + getTooltip().setCloseTimeout( + (Integer) closeTimeout.getConvertedValue()); + } + }); + maxWidth = createIntegerTextField("Max width", + getState().tooltipConfiguration.maxWidth); + maxWidth.addValueChangeListener(new Property.ValueChangeListener() { + @Override + public void valueChange(ValueChangeEvent event) { + getTooltip() + .setMaxWidth((Integer) maxWidth.getConvertedValue()); + } + }); + openDelay = createIntegerTextField("Open delay", + getState().tooltipConfiguration.openDelay); + openDelay.addValueChangeListener(new Property.ValueChangeListener() { + @Override + public void valueChange(ValueChangeEvent event) { + getTooltip().setOpenDelay( + (Integer) openDelay.getConvertedValue()); + } + }); + + quickOpenDelay = createIntegerTextField("Quick open delay", + getState().tooltipConfiguration.quickOpenDelay); + quickOpenDelay + .addValueChangeListener(new Property.ValueChangeListener() { + @Override + public void valueChange(ValueChangeEvent event) { + getTooltip().setQuickOpenDelay( + (Integer) quickOpenDelay.getConvertedValue()); + } + }); + + quickOpenTimeout = createIntegerTextField("Quick open timeout", + getState().tooltipConfiguration.quickOpenTimeout); + quickOpenTimeout + .addValueChangeListener(new Property.ValueChangeListener() { + @Override + public void valueChange(ValueChangeEvent event) { + getTooltip().setQuickOpenTimeout( + (Integer) quickOpenTimeout.getConvertedValue()); + } + }); + + getLayout().addComponents(closeTimeout, openDelay, quickOpenDelay, + quickOpenTimeout, maxWidth); + + getLayout().addComponents(componentWithShortTooltip, + componentWithLongTooltip); + + } + + private TextField createIntegerTextField(String caption, int initialValue) { + TextField tf = new TextField(caption); + tf.setId(caption); + tf.setConverter(Integer.class); + tf.setImmediate(true); + tf.setConvertedValue(initialValue); + return tf; + } + + @Override + protected String getTestDescription() { + return "Tests that tooltip delays can be configured"; + } + + @Override + protected Integer getTicketNumber() { + return 8065; + } + +} -- 2.39.5