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.

Label.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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.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. */
  57. public Label(String text, ContentMode contentMode) {
  58. setValue(text);
  59. setContentMode(contentMode);
  60. }
  61. @Override
  62. protected LabelState getState() {
  63. return (LabelState) super.getState();
  64. }
  65. @Override
  66. protected LabelState getState(boolean markAsDirty) {
  67. return (LabelState) super.getState(markAsDirty);
  68. }
  69. /**
  70. * Gets the content mode of the label.
  71. *
  72. * @return the content mode of the label
  73. *
  74. * @see ContentMode
  75. */
  76. public ContentMode getContentMode() {
  77. return getState(false).contentMode;
  78. }
  79. /**
  80. * Sets the content mode of the label.
  81. *
  82. * @param contentMode
  83. * the content mode to set
  84. *
  85. * @see ContentMode
  86. */
  87. public void setContentMode(ContentMode contentMode) {
  88. if (contentMode == null) {
  89. throw new IllegalArgumentException("Content mode can not be null");
  90. }
  91. getState().contentMode = contentMode;
  92. }
  93. /**
  94. * Sets the text to be shown in the label.
  95. *
  96. * @param value
  97. * the text to show in the label, null is converted to an empty
  98. * string
  99. */
  100. public void setValue(String value) {
  101. if (value == null) {
  102. getState().text = "";
  103. } else {
  104. getState().text = value;
  105. }
  106. }
  107. /**
  108. * Gets the text shown in the label.
  109. *
  110. * @return the text shown in the label, not null
  111. */
  112. public String getValue() {
  113. return getState(false).text;
  114. }
  115. @Override
  116. public void readDesign(Element design, DesignContext designContext) {
  117. super.readDesign(design, designContext);
  118. String innerHtml = design.html();
  119. boolean plainText = design.hasAttr(DESIGN_ATTR_PLAIN_TEXT);
  120. if (plainText) {
  121. setContentMode(ContentMode.TEXT);
  122. } else {
  123. setContentMode(ContentMode.HTML);
  124. }
  125. if (innerHtml != null && !"".equals(innerHtml)) {
  126. if (plainText) {
  127. innerHtml = DesignFormatter.decodeFromTextNode(innerHtml);
  128. }
  129. setValue(innerHtml);
  130. }
  131. }
  132. @Override
  133. protected Collection<String> getCustomAttributes() {
  134. Collection<String> result = super.getCustomAttributes();
  135. result.add("value");
  136. result.add("content-mode");
  137. result.add("plain-text");
  138. return result;
  139. }
  140. @Override
  141. public void writeDesign(Element design, DesignContext designContext) {
  142. super.writeDesign(design, designContext);
  143. String content = getValue();
  144. if (content != null) {
  145. switch (getContentMode()) {
  146. case TEXT:
  147. case PREFORMATTED: {
  148. // FIXME This attribute is not enough to be able to restore the
  149. // content mode in readDesign. The content mode should instead
  150. // be written directly in the attribute and restored in
  151. // readDesign. See ticket #19435
  152. design.attr(DESIGN_ATTR_PLAIN_TEXT, true);
  153. String encodeForTextNode = DesignFormatter
  154. .encodeForTextNode(content);
  155. if (encodeForTextNode != null) {
  156. design.html(encodeForTextNode);
  157. }
  158. }
  159. break;
  160. case HTML:
  161. design.html(content);
  162. break;
  163. }
  164. }
  165. }
  166. }