aboutsummaryrefslogtreecommitdiffstats
path: root/client/src
diff options
context:
space:
mode:
authorArtur Signell <artur@vaadin.com>2016-09-06 21:56:19 +0300
committerArtur Signell <artur@vaadin.com>2016-09-07 11:42:20 +0300
commita18552fb252e452ce93642a946db67af6708a91d (patch)
treed063abbf8472e0a7a314927150ef4a7ccae330f1 /client/src
parenta3a84d7fb1d87e77ec2cbffdbe6978e62d5d3ab7 (diff)
downloadvaadin-framework-a18552fb252e452ce93642a946db67af6708a91d.tar.gz
vaadin-framework-a18552fb252e452ce93642a946db67af6708a91d.zip
Add AbstractTextField client side to make mapping 1:1
Change-Id: I4439b995a769d22e0eded7132e2fb09123c25a49
Diffstat (limited to 'client/src')
-rw-r--r--client/src/main/java/com/vaadin/client/ui/AbstractTextFieldWidget.java64
-rw-r--r--client/src/main/java/com/vaadin/client/ui/VTextArea.java3
-rw-r--r--client/src/main/java/com/vaadin/client/ui/VTextField.java2
-rw-r--r--client/src/main/java/com/vaadin/client/ui/textarea/TextAreaConnector.java12
-rw-r--r--client/src/main/java/com/vaadin/client/ui/textfield/AbstractTextFieldConnector.java164
-rw-r--r--client/src/main/java/com/vaadin/client/ui/textfield/TextFieldConnector.java102
6 files changed, 243 insertions, 104 deletions
diff --git a/client/src/main/java/com/vaadin/client/ui/AbstractTextFieldWidget.java b/client/src/main/java/com/vaadin/client/ui/AbstractTextFieldWidget.java
new file mode 100644
index 0000000000..256428c83a
--- /dev/null
+++ b/client/src/main/java/com/vaadin/client/ui/AbstractTextFieldWidget.java
@@ -0,0 +1,64 @@
+/*
+ * 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.client.ui;
+
+import com.vaadin.client.ui.textfield.AbstractTextFieldConnector;
+
+/**
+ * Implemented by all widgets used by a connector extending
+ * {@link AbstractTextFieldConnector}.
+ */
+public interface AbstractTextFieldWidget {
+
+ /**
+ * Sets the selection range for the field.
+ *
+ * @param start
+ * the start of the selection
+ * @param length
+ * the length of the selection
+ */
+ public void setSelectionRange(int start, int length);
+
+ /**
+ * Gets the current value of the field.
+ *
+ * @return the current text in the field
+ */
+ public String getValue();
+
+ /**
+ * Selects all text in the field.
+ */
+ public void selectAll();
+
+ /**
+ * Sets the read-only mode of the field.
+ *
+ * @param readOnly
+ * <code>true</code> to set the field to read-only,
+ * <code>false</code> otherwise
+ */
+ public void setReadOnly(boolean readOnly);
+
+ /**
+ * Gets the current cursor position inside the field.
+ *
+ * @return the current cursor position
+ */
+ public int getCursorPos();
+
+}
diff --git a/client/src/main/java/com/vaadin/client/ui/VTextArea.java b/client/src/main/java/com/vaadin/client/ui/VTextArea.java
index c63fbda38c..18727361fe 100644
--- a/client/src/main/java/com/vaadin/client/ui/VTextArea.java
+++ b/client/src/main/java/com/vaadin/client/ui/VTextArea.java
@@ -34,7 +34,8 @@ import com.vaadin.client.ui.dd.DragImageModifier;
* @author Vaadin Ltd.
*
*/
-public class VTextArea extends VTextField implements DragImageModifier {
+public class VTextArea extends VTextField
+ implements DragImageModifier, AbstractTextFieldWidget {
public static final String CLASSNAME = "v-textarea";
diff --git a/client/src/main/java/com/vaadin/client/ui/VTextField.java b/client/src/main/java/com/vaadin/client/ui/VTextField.java
index a1ef816d1d..1eb499505a 100644
--- a/client/src/main/java/com/vaadin/client/ui/VTextField.java
+++ b/client/src/main/java/com/vaadin/client/ui/VTextField.java
@@ -31,7 +31,7 @@ import com.google.gwt.user.client.ui.TextBoxBase;
*
*/
public class VTextField extends TextBoxBase
- implements Field, FocusHandler, BlurHandler {
+ implements Field, FocusHandler, BlurHandler, AbstractTextFieldWidget {
public static final String CLASSNAME = "v-textfield";
public static final String CLASSNAME_FOCUS = "focus";
diff --git a/client/src/main/java/com/vaadin/client/ui/textarea/TextAreaConnector.java b/client/src/main/java/com/vaadin/client/ui/textarea/TextAreaConnector.java
index 9ad4c8b277..1f98b04310 100644
--- a/client/src/main/java/com/vaadin/client/ui/textarea/TextAreaConnector.java
+++ b/client/src/main/java/com/vaadin/client/ui/textarea/TextAreaConnector.java
@@ -19,15 +19,17 @@ package com.vaadin.client.ui.textarea;
import com.google.gwt.dom.client.Style;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
+import com.vaadin.client.event.InputEvent;
import com.vaadin.client.ui.VTextArea;
-import com.vaadin.client.ui.textfield.TextFieldConnector;
+import com.vaadin.client.ui.textfield.AbstractTextFieldConnector;
import com.vaadin.shared.ui.Connect;
import com.vaadin.shared.ui.textarea.TextAreaServerRpc;
import com.vaadin.shared.ui.textarea.TextAreaState;
+import com.vaadin.shared.ui.textfield.ValueChangeMode;
import com.vaadin.ui.TextArea;
@Connect(TextArea.class)
-public class TextAreaConnector extends TextFieldConnector {
+public class TextAreaConnector extends AbstractTextFieldConnector {
@Override
public TextAreaState getState() {
@@ -42,6 +44,12 @@ public class TextAreaConnector extends TextFieldConnector {
@Override
protected void init() {
super.init();
+ getWidget().addChangeHandler(event -> sendValueChange());
+ getWidget().addDomHandler(event -> {
+ if (getState().valueChangeMode != ValueChangeMode.BLUR) {
+ scheduleValueChange();
+ }
+ }, InputEvent.getType());
getWidget().addMouseUpHandler(new ResizeMouseUpHandler());
}
diff --git a/client/src/main/java/com/vaadin/client/ui/textfield/AbstractTextFieldConnector.java b/client/src/main/java/com/vaadin/client/ui/textfield/AbstractTextFieldConnector.java
new file mode 100644
index 0000000000..3ee9958140
--- /dev/null
+++ b/client/src/main/java/com/vaadin/client/ui/textfield/AbstractTextFieldConnector.java
@@ -0,0 +1,164 @@
+/*
+ * 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.client.ui.textfield;
+
+import com.google.gwt.core.client.Scheduler;
+import com.google.gwt.user.client.Timer;
+import com.google.gwt.user.client.ui.Widget;
+import com.vaadin.client.annotations.OnStateChange;
+import com.vaadin.client.ui.AbstractComponentConnector;
+import com.vaadin.client.ui.AbstractTextFieldWidget;
+import com.vaadin.client.ui.ConnectorFocusAndBlurHandler;
+import com.vaadin.shared.ui.textfield.AbstractTextFieldClientRpc;
+import com.vaadin.shared.ui.textfield.AbstractTextFieldServerRpc;
+import com.vaadin.shared.ui.textfield.AbstractTextFieldState;
+import com.vaadin.shared.ui.textfield.ValueChangeMode;
+import com.vaadin.ui.AbstractTextField;
+
+/**
+ * Connector class for AbstractTextField.
+ */
+public abstract class AbstractTextFieldConnector
+ extends AbstractComponentConnector {
+
+ private class AbstractTextFieldClientRpcImpl
+ implements AbstractTextFieldClientRpc {
+ @Override
+ public void selectRange(int start, int length) {
+ int textLength = getAbstractTextField().getValue().length();
+ start = restrictTo(start, 0, textLength - 1);
+ length = restrictTo(length, 0, textLength - start);
+ getAbstractTextField().setSelectionRange(start, length);
+ }
+
+ private int restrictTo(int value, int min, int max) {
+ if (value < min) {
+ value = min;
+ }
+ if (value > max) {
+ value = max;
+ }
+
+ return value;
+ }
+
+ @Override
+ public void selectAll() {
+ getAbstractTextField().selectAll();
+ }
+ }
+
+ private int lastSentCursorPosition = -1;
+
+ private Timer valueChangeTrigger = new Timer() {
+ @Override
+ public void run() {
+ Scheduler.get().scheduleDeferred(() -> sendValueChange());
+ }
+ };
+
+ @Override
+ protected void init() {
+ registerRpc(AbstractTextFieldClientRpc.class,
+ new AbstractTextFieldClientRpcImpl());
+ ConnectorFocusAndBlurHandler.addHandlers(this);
+ }
+
+ /**
+ * Helper to cast {@link #getWidget()} to {@link AbstractTextField}. The
+ * method exists only because getWidget() must return a {@link Widget} and
+ * not an interface.
+ *
+ * @return the widget as an AbstractTextFieldWidget
+ */
+ private AbstractTextFieldWidget getAbstractTextField() {
+ return (AbstractTextFieldWidget) getWidget();
+ }
+
+ /**
+ * Called whenever a change in the value has been detected. Schedules a
+ * value change to be sent to the server, depending on the current value
+ * change mode.
+ * <p>
+ * Note that this method does not consider the {@link ValueChangeMode#BLUR}
+ * mode but assumes that {@link #sendValueChange()} is called directly for
+ * this mode.
+ */
+ protected void scheduleValueChange() {
+ switch (getState().valueChangeMode) {
+ case LAZY:
+ lazyTextChange();
+ break;
+ case TIMEOUT:
+ timeoutTextChange();
+ break;
+ case EAGER:
+ eagerTextChange();
+ break;
+ case BLUR:
+ // Nothing to schedule for this mode
+ break;
+ }
+ }
+
+ private void lazyTextChange() {
+ valueChangeTrigger.schedule(getState().valueChangeTimeout);
+ }
+
+ private void timeoutTextChange() {
+ if (valueChangeTrigger.isRunning()) {
+ return;
+ }
+ valueChangeTrigger.schedule(getState().valueChangeTimeout);
+ }
+
+ private void eagerTextChange() {
+ valueChangeTrigger.run();
+ }
+
+ @Override
+ public AbstractTextFieldState getState() {
+ return (AbstractTextFieldState) super.getState();
+ }
+
+ @OnStateChange("readOnly")
+ private void updateReadOnly() {
+ getAbstractTextField().setReadOnly(getState().readOnly);
+ }
+
+ private boolean hasStateChanged() {
+ boolean textChanged = !getAbstractTextField().getValue()
+ .equals(getState().text);
+ boolean cursorPosChanged = getAbstractTextField()
+ .getCursorPos() != lastSentCursorPosition;
+ return textChanged || cursorPosChanged;
+ }
+
+ /**
+ * Sends the updated value and cursor position to the server, if either one
+ * has changed.
+ */
+ protected void sendValueChange() {
+ if (!hasStateChanged()) {
+ return;
+ }
+ lastSentCursorPosition = getAbstractTextField().getCursorPos();
+ getRpcProxy(AbstractTextFieldServerRpc.class).setText(
+ getAbstractTextField().getValue(), lastSentCursorPosition);
+ getState().text = getAbstractTextField().getValue();
+ }
+
+}
diff --git a/client/src/main/java/com/vaadin/client/ui/textfield/TextFieldConnector.java b/client/src/main/java/com/vaadin/client/ui/textfield/TextFieldConnector.java
index 46e02c18ee..0a937de349 100644
--- a/client/src/main/java/com/vaadin/client/ui/textfield/TextFieldConnector.java
+++ b/client/src/main/java/com/vaadin/client/ui/textfield/TextFieldConnector.java
@@ -15,17 +15,10 @@
*/
package com.vaadin.client.ui.textfield;
-import com.google.gwt.core.client.Scheduler;
-import com.google.gwt.user.client.Timer;
-import com.vaadin.client.annotations.OnStateChange;
import com.vaadin.client.event.InputEvent;
-import com.vaadin.client.ui.AbstractComponentConnector;
-import com.vaadin.client.ui.ConnectorFocusAndBlurHandler;
import com.vaadin.client.ui.VTextField;
import com.vaadin.shared.ui.Connect;
import com.vaadin.shared.ui.Connect.LoadStyle;
-import com.vaadin.shared.ui.textfield.AbstractTextFieldClientRpc;
-import com.vaadin.shared.ui.textfield.AbstractTextFieldServerRpc;
import com.vaadin.shared.ui.textfield.TextFieldState;
import com.vaadin.shared.ui.textfield.ValueChangeMode;
import com.vaadin.ui.TextField;
@@ -34,49 +27,11 @@ import com.vaadin.ui.TextField;
* Connector class for TextField.
*/
@Connect(value = TextField.class, loadStyle = LoadStyle.EAGER)
-public class TextFieldConnector extends AbstractComponentConnector {
-
- private class AbstractTextFieldClientRpcImpl
- implements AbstractTextFieldClientRpc {
- @Override
- public void selectRange(int start, int length) {
- int textLength = getWidget().getText().length();
- start = restrictTo(start, 0, textLength - 1);
- length = restrictTo(length, 0, textLength - start);
- getWidget().setSelectionRange(start, length);
- }
-
- private int restrictTo(int value, int min, int max) {
- if (value < min) {
- value = min;
- }
- if (value > max) {
- value = max;
- }
-
- return value;
- }
-
- @Override
- public void selectAll() {
- getWidget().selectAll();
- }
- }
-
- private int lastSentCursorPosition = -1;
-
- private Timer valueChangeTrigger = new Timer() {
- @Override
- public void run() {
- Scheduler.get().scheduleDeferred(() -> sendValueChange());
- }
- };
+public class TextFieldConnector extends AbstractTextFieldConnector {
@Override
protected void init() {
- registerRpc(AbstractTextFieldClientRpc.class,
- new AbstractTextFieldClientRpcImpl());
- ConnectorFocusAndBlurHandler.addHandlers(this);
+ super.init();
getWidget().addChangeHandler(event -> sendValueChange());
getWidget().addDomHandler(event -> {
if (getState().valueChangeMode != ValueChangeMode.BLUR) {
@@ -85,38 +40,6 @@ public class TextFieldConnector extends AbstractComponentConnector {
}, InputEvent.getType());
}
- private void scheduleValueChange() {
- switch (getState().valueChangeMode) {
- case LAZY:
- lazyTextChange();
- break;
- case TIMEOUT:
- timeoutTextChange();
- break;
- case EAGER:
- eagerTextChange();
- break;
- case BLUR:
- // Nothing to schedule for this mode
- break;
- }
- }
-
- private void lazyTextChange() {
- valueChangeTrigger.schedule(getState().valueChangeTimeout);
- }
-
- private void timeoutTextChange() {
- if (valueChangeTrigger.isRunning()) {
- return;
- }
- valueChangeTrigger.schedule(getState().valueChangeTimeout);
- }
-
- private void eagerTextChange() {
- valueChangeTrigger.run();
- }
-
@Override
public TextFieldState getState() {
return (TextFieldState) super.getState();
@@ -127,25 +50,4 @@ public class TextFieldConnector extends AbstractComponentConnector {
return (VTextField) super.getWidget();
}
- @OnStateChange("readOnly")
- private void updateReadOnly() {
- getWidget().setReadOnly(getState().readOnly);
- }
-
- private boolean hasStateChanged() {
- boolean textChanged = !getWidget().getValue().equals(getState().text);
- boolean cursorPosChanged = getWidget()
- .getCursorPos() != lastSentCursorPosition;
- return textChanged || cursorPosChanged;
- }
-
- private void sendValueChange() {
- if (!hasStateChanged()) {
- return;
- }
- lastSentCursorPosition = getWidget().getCursorPos();
- getRpcProxy(AbstractTextFieldServerRpc.class)
- .setText(getWidget().getValue(), lastSentCursorPosition);
- getState().text = getWidget().getValue();
- }
}