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.

AbstractLegacyComponent.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright 2000-2018 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.v7.ui;
  17. import org.jsoup.nodes.Attributes;
  18. import org.jsoup.nodes.Element;
  19. import com.vaadin.ui.AbstractComponent;
  20. import com.vaadin.ui.declarative.DesignAttributeHandler;
  21. import com.vaadin.ui.declarative.DesignContext;
  22. import com.vaadin.v7.shared.AbstractLegacyComponentState;
  23. /**
  24. * An abstract base class for compatibility components.
  25. * <p>
  26. * Used since immediate and read-only properties has been removed in Vaadin 8
  27. * from {@link AbstractComponent}.
  28. *
  29. * @author Vaadin Ltd
  30. * @since 8.0
  31. * @deprecated only used for Vaadin 7 compatiblity components
  32. */
  33. @Deprecated
  34. public class AbstractLegacyComponent extends AbstractComponent {
  35. private Boolean explicitImmediateValue;
  36. /**
  37. * Returns the explicitly set immediate value.
  38. *
  39. * @return the explicitly set immediate value or null if
  40. * {@link #setImmediate(boolean)} has not been explicitly invoked
  41. */
  42. protected Boolean getExplicitImmediateValue() {
  43. return explicitImmediateValue;
  44. }
  45. /**
  46. * Returns the immediate mode of the component.
  47. * <p>
  48. * Since Vaadin 8, the default mode is immediate.
  49. *
  50. * @return true if the component is in immediate mode (explicitly or
  51. * implicitly set), false if the component if not in immediate mode
  52. */
  53. public boolean isImmediate() {
  54. if (explicitImmediateValue != null) {
  55. return explicitImmediateValue;
  56. } else {
  57. return true;
  58. }
  59. }
  60. /**
  61. * Sets the component's immediate mode to the specified status.
  62. * <p>
  63. * Since Vaadin 8, the default mode is immediate.
  64. *
  65. * @param immediate
  66. * the boolean value specifying if the component should be in the
  67. * immediate mode after the call.
  68. */
  69. public void setImmediate(boolean immediate) {
  70. explicitImmediateValue = immediate;
  71. getState().immediate = immediate;
  72. }
  73. /**
  74. * Tests whether the component is in the read-only mode. The user can not
  75. * change the value of a read-only component. As only {@code AbstractField}
  76. * or {@code LegacyField} components normally have a value that can be input
  77. * or changed by the user, this is mostly relevant only to field components,
  78. * though not restricted to them.
  79. *
  80. * <p>
  81. * Notice that the read-only mode only affects whether the user can change
  82. * the <i>value</i> of the component; it is possible to, for example, scroll
  83. * a read-only table.
  84. * </p>
  85. *
  86. * <p>
  87. * The method will return {@code true} if the component or any of its
  88. * parents is in the read-only mode.
  89. * </p>
  90. *
  91. * @return <code>true</code> if the component or any of its parents is in
  92. * read-only mode, <code>false</code> if not.
  93. * @see #setReadOnly(boolean)
  94. */
  95. @Override
  96. public boolean isReadOnly() {
  97. return getState(false).readOnly;
  98. }
  99. /**
  100. * Sets the read-only mode of the component to the specified mode. The user
  101. * can not change the value of a read-only component.
  102. *
  103. * <p>
  104. * As only {@code AbstractField} or {@code LegacyField} components normally
  105. * have a value that can be input or changed by the user, this is mostly
  106. * relevant only to field components, though not restricted to them.
  107. * </p>
  108. *
  109. * <p>
  110. * Notice that the read-only mode only affects whether the user can change
  111. * the <i>value</i> of the component; it is possible to, for example, scroll
  112. * a read-only table.
  113. * </p>
  114. *
  115. * <p>
  116. * In Vaadin 8 the read-only property is part of {@link HasValue} API.
  117. * </p>
  118. *
  119. * @param readOnly
  120. * a boolean value specifying whether the component is put
  121. * read-only mode or not
  122. */
  123. @Override
  124. public void setReadOnly(boolean readOnly) {
  125. getState().readOnly = readOnly;
  126. }
  127. @Override
  128. public void readDesign(Element design, DesignContext designContext) {
  129. super.readDesign(design, designContext);
  130. Attributes attr = design.attributes();
  131. // handle immediate
  132. if (attr.hasKey("immediate")) {
  133. setImmediate(DesignAttributeHandler.getFormatter()
  134. .parse(attr.get("immediate"), Boolean.class));
  135. }
  136. }
  137. @Override
  138. public void writeDesign(Element design, DesignContext designContext) {
  139. super.writeDesign(design, designContext);
  140. AbstractLegacyComponent def = designContext.getDefaultInstance(this);
  141. Attributes attr = design.attributes();
  142. // handle immediate
  143. if (explicitImmediateValue != null) {
  144. DesignAttributeHandler.writeAttribute("immediate", attr,
  145. explicitImmediateValue, def.isImmediate(), Boolean.class,
  146. designContext);
  147. }
  148. }
  149. @Override
  150. public void beforeClientResponse(boolean initial) {
  151. super.beforeClientResponse(initial);
  152. getState().immediate = isImmediate();
  153. }
  154. @Override
  155. protected AbstractLegacyComponentState getState() {
  156. return (AbstractLegacyComponentState) super.getState();
  157. }
  158. @Override
  159. protected AbstractLegacyComponentState getState(boolean markAsDirty) {
  160. return (AbstractLegacyComponentState) super.getState(markAsDirty);
  161. }
  162. }