You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TextFieldConnector.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.tokka.connectors.fields;
  17. import com.google.gwt.core.client.Scheduler;
  18. import com.google.gwt.event.dom.client.ChangeEvent;
  19. import com.google.gwt.event.dom.client.ChangeHandler;
  20. import com.google.gwt.user.client.Command;
  21. import com.google.gwt.user.client.ui.TextBox;
  22. import com.vaadin.client.communication.StateChangeEvent;
  23. import com.vaadin.client.ui.AbstractComponentConnector;
  24. import com.vaadin.shared.ui.Connect;
  25. import com.vaadin.shared.ui.Connect.LoadStyle;
  26. import com.vaadin.shared.ui.components.fields.TextFieldServerRpc;
  27. import com.vaadin.shared.ui.components.fields.TextFieldState;
  28. @Connect(value = com.vaadin.tokka.ui.components.fields.TextField.class, loadStyle = LoadStyle.EAGER)
  29. public class TextFieldConnector extends AbstractComponentConnector {
  30. @Override
  31. protected void init() {
  32. getWidget().addChangeHandler(new ChangeHandler() {
  33. @Override
  34. public void onChange(ChangeEvent event) {
  35. getRpcProxy(TextFieldServerRpc.class)
  36. .setText(getWidget().getValue());
  37. }
  38. });
  39. }
  40. @Override
  41. public TextFieldState getState() {
  42. return (TextFieldState) super.getState();
  43. }
  44. @Override
  45. public TextBox getWidget() {
  46. return (TextBox) super.getWidget();
  47. }
  48. @Override
  49. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  50. // TODO Split into separate @OnStateChanged methods
  51. super.onStateChanged(stateChangeEvent);
  52. final TextBox w = getWidget();
  53. final TextFieldState state = getState();
  54. w.setReadOnly(state.readOnly);
  55. if (state.placeholder != null) {
  56. w.getElement().setAttribute("placeholder", state.placeholder);
  57. } else {
  58. w.getElement().removeAttribute("placeholder");
  59. }
  60. if (state.maxLength >= 0) {
  61. w.setMaxLength(state.maxLength);
  62. } else {
  63. w.getElement().removeAttribute("maxlength");
  64. }
  65. w.setValue(state.text);
  66. if (state.selectionStart != -1) {
  67. /*
  68. * Gecko defers setting the text so we need to defer the selection.
  69. */
  70. Scheduler.get().scheduleDeferred(new Command() {
  71. @Override
  72. public void execute() {
  73. w.setSelectionRange(state.selectionStart,
  74. state.selectionLength);
  75. }
  76. });
  77. }
  78. }
  79. }