From 58853fe47bb01a0c0c2b5c380056d22ccccd6f08 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Thu, 25 Aug 2016 13:21:36 +0300 Subject: Move old TextField to compatibility package Change-Id: Ic1e8d3f9859f5e496c0e27e2fece4b5a9da74f01 --- .../main/java/com/vaadin/v7/event/FieldEvents.java | 109 +++ .../java/com/vaadin/v7/ui/AbstractTextField.java | 813 +++++++++++++++++++++ .../src/main/java/com/vaadin/v7/ui/TextField.java | 151 ++++ .../tests/server/PropertysetItemListenersTest.java | 14 - .../converter/AnyEnumToStringConverterTest.java | 129 ++++ .../tests/data/converter/ConverterFactoryTest.java | 128 ++++ .../data/converter/DateToLongConverterTest.java | 28 + .../data/converter/DateToSqlDateConverterTest.java | 28 + .../converter/DefaultConverterFactoryTest.java | 124 ++++ .../SpecificEnumToStringConverterTest.java | 125 ++++ .../converter/StringToBigDecimalConverterTest.java | 59 ++ .../converter/StringToBigIntegerConverterTest.java | 64 ++ .../converter/StringToBooleanConverterTest.java | 82 +++ .../data/converter/StringToByteConverterTest.java | 76 ++ .../converter/StringToCollectionConverterTest.java | 172 +++++ .../data/converter/StringToDateConverterTest.java | 32 + .../converter/StringToDoubleConverterTest.java | 29 + .../data/converter/StringToEnumConverterTest.java | 135 ++++ .../data/converter/StringToFloatConverterTest.java | 29 + .../converter/StringToIntegerConverterTest.java | 48 ++ .../data/converter/StringToLongConverterTest.java | 78 ++ .../data/converter/StringToShortConverterTest.java | 76 ++ .../vaadin/v7/tests/server/EventRouterTest.java | 40 + .../tests/server/PropertysetItemListenersTest.java | 14 + .../AbsFieldDataSourceLocaleChangeTest.java | 65 ++ .../abstractfield/AbsFieldValidatorsTest.java | 79 ++ .../AbsFieldValueConversionErrorTest.java | 92 +++ .../AbsFieldValueConversionsTest.java | 216 ++++++ .../AbstractFieldDeclarativeTest.java | 65 ++ .../abstractfield/AbstractFieldReadOnlyTest.java | 51 ++ .../abstractfield/DefaultConverterFactoryTest.java | 138 ++++ .../abstractfield/RemoveListenersOnDetachTest.java | 106 +++ .../AbstractTextFieldListenersTest.java | 34 + .../server/component/textfield/TextFieldTest.java | 50 ++ .../textfield/TextFieldValueChangeTest.java | 131 ++++ 35 files changed, 3596 insertions(+), 14 deletions(-) create mode 100644 compatibility-server/src/main/java/com/vaadin/v7/event/FieldEvents.java create mode 100644 compatibility-server/src/main/java/com/vaadin/v7/ui/AbstractTextField.java create mode 100644 compatibility-server/src/main/java/com/vaadin/v7/ui/TextField.java delete mode 100644 compatibility-server/src/test/java/com/vaadin/tests/server/PropertysetItemListenersTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/data/converter/AnyEnumToStringConverterTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/data/converter/ConverterFactoryTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/data/converter/DateToLongConverterTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/data/converter/DateToSqlDateConverterTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/data/converter/DefaultConverterFactoryTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/data/converter/SpecificEnumToStringConverterTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/data/converter/StringToBigDecimalConverterTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/data/converter/StringToBigIntegerConverterTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/data/converter/StringToBooleanConverterTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/data/converter/StringToByteConverterTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/data/converter/StringToCollectionConverterTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/data/converter/StringToDateConverterTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/data/converter/StringToDoubleConverterTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/data/converter/StringToEnumConverterTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/data/converter/StringToFloatConverterTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/data/converter/StringToIntegerConverterTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/data/converter/StringToLongConverterTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/data/converter/StringToShortConverterTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/server/EventRouterTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/server/PropertysetItemListenersTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/abstractfield/AbsFieldDataSourceLocaleChangeTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/abstractfield/AbsFieldValidatorsTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/abstractfield/AbsFieldValueConversionErrorTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/abstractfield/AbsFieldValueConversionsTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/abstractfield/AbstractFieldDeclarativeTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/abstractfield/AbstractFieldReadOnlyTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/abstractfield/DefaultConverterFactoryTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/abstractfield/RemoveListenersOnDetachTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/abstracttextfield/AbstractTextFieldListenersTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/textfield/TextFieldTest.java create mode 100644 compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/textfield/TextFieldValueChangeTest.java (limited to 'compatibility-server') diff --git a/compatibility-server/src/main/java/com/vaadin/v7/event/FieldEvents.java b/compatibility-server/src/main/java/com/vaadin/v7/event/FieldEvents.java new file mode 100644 index 0000000000..abd17df49b --- /dev/null +++ b/compatibility-server/src/main/java/com/vaadin/v7/event/FieldEvents.java @@ -0,0 +1,109 @@ +/* + * 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.event; + +import java.io.Serializable; +import java.lang.reflect.Method; + +import com.vaadin.event.ConnectorEventListener; +import com.vaadin.ui.Component; +import com.vaadin.util.ReflectTools; +import com.vaadin.v7.ui.Field; +import com.vaadin.v7.ui.Field.ValueChangeEvent; +import com.vaadin.v7.ui.TextField; + +/** + * Interface that serves as a wrapper for {@link Field} related events. + */ +public interface FieldEvents { + + /** + * TextChangeEvents are fired when the user is editing the text content of a + * field. Most commonly text change events are triggered by typing text with + * keyboard, but e.g. pasting content from clip board to a text field also + * triggers an event. + *

+ * TextChangeEvents differ from {@link ValueChangeEvent}s so that they are + * triggered repeatedly while the end user is filling the field. + * ValueChangeEvents are not fired until the user for example hits enter or + * focuses another field. Also note the difference that TextChangeEvents are + * only fired if the change is triggered from the user, while + * ValueChangeEvents are also fired if the field value is set by the + * application code. + *

+ * The {@link TextChangeNotifier}s implementation may decide when exactly + * TextChangeEvents are fired. TextChangeEvents are not necessary fire for + * example on each key press, but buffered with a small delay. The + * {@code TextField} component supports different modes for triggering + * TextChangeEvents. + * + * @see TextChangeListener + * @see TextChangeNotifier + * @see TextField#setTextChangeEventMode(com.vaadin.ui.TextField.TextChangeEventMode) + * @since 6.5 + */ + public static abstract class TextChangeEvent extends Component.Event { + + public TextChangeEvent(Component source) { + super(source); + } + + /** + * @return the text content of the field after the + * {@link TextChangeEvent} + */ + public abstract String getText(); + + /** + * @return the cursor position during after the {@link TextChangeEvent} + */ + public abstract int getCursorPosition(); + } + + /** + * A listener for {@link TextChangeEvent}s. + * + * @since 6.5 + */ + public interface TextChangeListener extends ConnectorEventListener { + + public static String EVENT_ID = "ie"; + public static Method EVENT_METHOD = ReflectTools.findMethod( + TextChangeListener.class, "textChange", TextChangeEvent.class); + + /** + * This method is called repeatedly while the text is edited by a user. + * + * @param event + * the event providing details of the text change + */ + public void textChange(TextChangeEvent event); + } + + /** + * An interface implemented by a {@link Field} supporting + * {@link TextChangeEvent}s. An example a {@link TextField} supports + * {@link TextChangeListener}s. + */ + public interface TextChangeNotifier extends Serializable { + public void addTextChangeListener(TextChangeListener listener); + + public void removeTextChangeListener(TextChangeListener listener); + + } + +} diff --git a/compatibility-server/src/main/java/com/vaadin/v7/ui/AbstractTextField.java b/compatibility-server/src/main/java/com/vaadin/v7/ui/AbstractTextField.java new file mode 100644 index 0000000000..f534662583 --- /dev/null +++ b/compatibility-server/src/main/java/com/vaadin/v7/ui/AbstractTextField.java @@ -0,0 +1,813 @@ +/* + * 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.ui; + +import java.util.Collection; +import java.util.Map; + +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; + +import com.vaadin.event.FieldEvents.BlurEvent; +import com.vaadin.event.FieldEvents.BlurListener; +import com.vaadin.event.FieldEvents.BlurNotifier; +import com.vaadin.event.FieldEvents.FocusEvent; +import com.vaadin.event.FieldEvents.FocusListener; +import com.vaadin.event.FieldEvents.FocusNotifier; +import com.vaadin.server.PaintException; +import com.vaadin.server.PaintTarget; +import com.vaadin.shared.v7.ui.textfield.LegacyAbstractTextFieldState; +import com.vaadin.shared.v7.ui.textfield.LegacyTextFieldConstants; +import com.vaadin.ui.LegacyComponent; +import com.vaadin.ui.declarative.DesignAttributeHandler; +import com.vaadin.ui.declarative.DesignContext; +import com.vaadin.v7.event.FieldEvents.TextChangeEvent; +import com.vaadin.v7.event.FieldEvents.TextChangeListener; +import com.vaadin.v7.event.FieldEvents.TextChangeNotifier; + +@Deprecated +public abstract class AbstractTextField extends AbstractField implements + BlurNotifier, FocusNotifier, TextChangeNotifier, LegacyComponent { + + /** + * Null representation. + */ + private String nullRepresentation = "null"; + /** + * Is setting to null from non-null value allowed by setting with null + * representation . + */ + private boolean nullSettingAllowed = false; + /** + * The text content when the last messages to the server was sent. Cleared + * when value is changed. + */ + private String lastKnownTextContent; + + /** + * The position of the cursor when the last message to the server was sent. + */ + private int lastKnownCursorPosition; + + /** + * Flag indicating that a text change event is pending to be triggered. + * Cleared by {@link #setInternalValue(Object)} and when the event is fired. + */ + private boolean textChangeEventPending; + + private boolean isFiringTextChangeEvent = false; + + private TextChangeEventMode textChangeEventMode = TextChangeEventMode.LAZY; + + private final int DEFAULT_TEXTCHANGE_TIMEOUT = 400; + + private int textChangeEventTimeout = DEFAULT_TEXTCHANGE_TIMEOUT; + + /** + * Temporarily holds the new selection position. Cleared on paint. + */ + private int selectionPosition = -1; + + /** + * Temporarily holds the new selection length. + */ + private int selectionLength; + + /** + * Flag used to determine whether we are currently handling a state change + * triggered by a user. Used to properly fire text change event before value + * change event triggered by the client side. + */ + private boolean changingVariables; + + protected AbstractTextField() { + super(); + } + + @Override + protected LegacyAbstractTextFieldState getState() { + return (LegacyAbstractTextFieldState) super.getState(); + } + + @Override + protected LegacyAbstractTextFieldState getState(boolean markAsDirty) { + return (LegacyAbstractTextFieldState) super.getState(markAsDirty); + } + + @Override + public void beforeClientResponse(boolean initial) { + super.beforeClientResponse(initial); + + String value = getValue(); + if (value == null) { + value = getNullRepresentation(); + } + getState().text = value; + } + + @Override + public void paintContent(PaintTarget target) throws PaintException { + + if (selectionPosition != -1) { + target.addAttribute("selpos", selectionPosition); + target.addAttribute("sellen", selectionLength); + selectionPosition = -1; + } + + if (hasListeners(TextChangeEvent.class)) { + target.addAttribute( + LegacyTextFieldConstants.ATTR_TEXTCHANGE_EVENTMODE, + getTextChangeEventMode().toString()); + target.addAttribute( + LegacyTextFieldConstants.ATTR_TEXTCHANGE_TIMEOUT, + getTextChangeTimeout()); + if (lastKnownTextContent != null) { + /* + * The field has be repainted for some reason (e.g. caption, + * size, stylename), but the value has not been changed since + * the last text change event. Let the client side know about + * the value the server side knows. Client side may then ignore + * the actual value, depending on its state. + */ + target.addAttribute( + LegacyTextFieldConstants.ATTR_NO_VALUE_CHANGE_BETWEEN_PAINTS, + true); + } + } + + } + + @Override + public void changeVariables(Object source, Map variables) { + changingVariables = true; + + try { + + // Sets the height set by the user when resize the