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.

RichTextAreaConnector.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright 2000-2016 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.ui.richtextarea;
  17. import com.vaadin.client.annotations.OnStateChange;
  18. import com.vaadin.client.ui.AbstractFieldConnector;
  19. import com.vaadin.client.ui.ConnectorFocusAndBlurHandler;
  20. import com.vaadin.client.ui.SimpleManagedLayout;
  21. import com.vaadin.client.ui.VRichTextArea;
  22. import com.vaadin.client.ui.textfield.ValueChangeHandler;
  23. import com.vaadin.shared.ui.Connect;
  24. import com.vaadin.shared.ui.Connect.LoadStyle;
  25. import com.vaadin.shared.ui.richtextarea.RichTextAreaClientRpc;
  26. import com.vaadin.shared.ui.richtextarea.RichTextAreaServerRpc;
  27. import com.vaadin.shared.ui.richtextarea.RichTextAreaState;
  28. import com.vaadin.ui.RichTextArea;
  29. /**
  30. * Connector for RichTextArea.
  31. */
  32. @Connect(value = RichTextArea.class, loadStyle = LoadStyle.LAZY)
  33. public class RichTextAreaConnector extends AbstractFieldConnector
  34. implements SimpleManagedLayout, ValueChangeHandler.Owner {
  35. private class RichTextAreaClientRpcImpl implements RichTextAreaClientRpc {
  36. @Override
  37. public void selectAll() {
  38. getWidget().selectAll();
  39. }
  40. }
  41. private ValueChangeHandler valueChangeHandler;
  42. @Override
  43. protected void init() {
  44. getWidget().addBlurHandler(event -> flush());
  45. getWidget().addInputHandler(
  46. () -> valueChangeHandler.scheduleValueChange());
  47. registerRpc(RichTextAreaClientRpc.class,
  48. new RichTextAreaClientRpcImpl());
  49. ConnectorFocusAndBlurHandler.addHandlers(this);
  50. valueChangeHandler = new ValueChangeHandler(this);
  51. getLayoutManager().registerDependency(this,
  52. getWidget().formatter.getElement());
  53. }
  54. @OnStateChange("valueChangeMode")
  55. private void updateValueChangeMode() {
  56. valueChangeHandler.setValueChangeMode(getState().valueChangeMode);
  57. }
  58. @OnStateChange("valueChangeTimeout")
  59. private void updateValueChangeTimeout() {
  60. valueChangeHandler.setValueChangeTimeout(getState().valueChangeTimeout);
  61. }
  62. @OnStateChange("readOnly")
  63. private void updateReadOnly() {
  64. getWidget().setReadOnly(getState().readOnly);
  65. }
  66. @Override
  67. public void onUnregister() {
  68. super.onUnregister();
  69. getLayoutManager().unregisterDependency(this,
  70. getWidget().formatter.getElement());
  71. }
  72. @Override
  73. public VRichTextArea getWidget() {
  74. return (VRichTextArea) super.getWidget();
  75. }
  76. @Override
  77. public void layout() {
  78. if (!isUndefinedHeight()) {
  79. int rootElementInnerHeight = getLayoutManager()
  80. .getInnerHeight(getWidget().getElement());
  81. int formatterHeight = getLayoutManager()
  82. .getOuterHeight(getWidget().formatter.getElement());
  83. int editorHeight = rootElementInnerHeight - formatterHeight;
  84. if (editorHeight < 0) {
  85. editorHeight = 0;
  86. }
  87. getWidget().rta.setHeight(editorHeight + "px");
  88. }
  89. }
  90. @Override
  91. public RichTextAreaState getState() {
  92. return (RichTextAreaState) super.getState();
  93. }
  94. private boolean hasStateChanged(String widgetValue) {
  95. return !widgetValue.equals(getState().value);
  96. }
  97. @Override
  98. public void sendValueChange() {
  99. String widgetValue = getWidget().getSanitizedValue();
  100. if (!hasStateChanged(widgetValue)) {
  101. return;
  102. }
  103. getRpcProxy(RichTextAreaServerRpc.class).setText(widgetValue);
  104. getState().value = widgetValue;
  105. }
  106. @Override
  107. public void flush() {
  108. super.flush();
  109. sendValueChange();
  110. }
  111. }