選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Label.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.ui;
  17. import java.util.Collection;
  18. import org.jsoup.nodes.Element;
  19. import com.vaadin.shared.ui.ContentMode;
  20. import com.vaadin.shared.ui.label.LabelState;
  21. import com.vaadin.ui.declarative.DesignContext;
  22. import com.vaadin.ui.declarative.DesignFormatter;
  23. /**
  24. * Label component for showing non-editable short texts.
  25. * <p>
  26. * The label content can be set to the modes specified by {@link ContentMode}.
  27. * If content mode is set to HTML, any HTML content is allowed.
  28. *
  29. * @author Vaadin Ltd.
  30. * @since 3.0
  31. */
  32. @SuppressWarnings("serial")
  33. public class Label extends AbstractComponent {
  34. /**
  35. * Creates an empty Label.
  36. */
  37. public Label() {
  38. this("");
  39. }
  40. /**
  41. * Creates a new instance with text content mode and the given text.
  42. *
  43. * @param text
  44. * the text to set
  45. */
  46. public Label(String text) {
  47. this(text, ContentMode.TEXT);
  48. }
  49. /**
  50. * Creates a new instance with the given text and content mode.
  51. *
  52. * @param text
  53. * the text to set
  54. * @param contentMode
  55. * the content mode to use
  56. * @since 8.0
  57. */
  58. public Label(String text, ContentMode contentMode) {
  59. setValue(text);
  60. setContentMode(contentMode);
  61. }
  62. @Override
  63. protected LabelState getState() {
  64. return (LabelState) super.getState();
  65. }
  66. @Override
  67. protected LabelState getState(boolean markAsDirty) {
  68. return (LabelState) super.getState(markAsDirty);
  69. }
  70. /**
  71. * Gets the content mode of the label.
  72. *
  73. * @return the content mode of the label
  74. *
  75. * @see ContentMode
  76. * @since 8.0
  77. */
  78. public ContentMode getContentMode() {
  79. return getState(false).contentMode;
  80. }
  81. /**
  82. * Sets the content mode of the label.
  83. *
  84. * @param contentMode
  85. * the content mode to set
  86. *
  87. * @see ContentMode
  88. * @since 8.0
  89. */
  90. public void setContentMode(ContentMode contentMode) {
  91. if (contentMode == null) {
  92. throw new IllegalArgumentException("Content mode can not be null");
  93. }
  94. getState().contentMode = contentMode;
  95. }
  96. /**
  97. * Sets the text to be shown in the label.
  98. *
  99. * @param value
  100. * the text to show in the label, null is converted to an empty
  101. * string
  102. */
  103. public void setValue(String value) {
  104. if (value == null) {
  105. getState().text = "";
  106. } else {
  107. getState().text = value;
  108. }
  109. }
  110. /**
  111. * Gets the text shown in the label.
  112. *
  113. * @return the text shown in the label, not null
  114. */
  115. public String getValue() {
  116. return getState(false).text;
  117. }
  118. @Override
  119. public void readDesign(Element design, DesignContext designContext) {
  120. super.readDesign(design, designContext);
  121. String innerHtml = design.html();
  122. boolean plainText = design.hasAttr(DESIGN_ATTR_PLAIN_TEXT);
  123. if (plainText) {
  124. setContentMode(ContentMode.TEXT);
  125. } else {
  126. setContentMode(ContentMode.HTML);
  127. }
  128. if (innerHtml != null && !innerHtml.isEmpty()) {
  129. if (plainText) {
  130. innerHtml = DesignFormatter.decodeFromTextNode(innerHtml);
  131. }
  132. setValue(innerHtml);
  133. }
  134. }
  135. @Override
  136. protected Collection<String> getCustomAttributes() {
  137. Collection<String> result = super.getCustomAttributes();
  138. result.add("value");
  139. result.add("content-mode");
  140. result.add("plain-text");
  141. return result;
  142. }
  143. @Override
  144. public void writeDesign(Element design, DesignContext designContext) {
  145. super.writeDesign(design, designContext);
  146. String content = getValue();
  147. if (content != null) {
  148. switch (getContentMode()) {
  149. case TEXT:
  150. case PREFORMATTED: {
  151. // FIXME This attribute is not enough to be able to restore the
  152. // content mode in readDesign. The content mode should instead
  153. // be written directly in the attribute and restored in
  154. // readDesign. See ticket #19435
  155. design.attr(DESIGN_ATTR_PLAIN_TEXT, true);
  156. String encodeForTextNode = DesignFormatter
  157. .encodeForTextNode(content);
  158. if (encodeForTextNode != null) {
  159. design.html(encodeForTextNode);
  160. }
  161. }
  162. break;
  163. case HTML:
  164. design.html(content);
  165. break;
  166. }
  167. }
  168. }
  169. }