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 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * Copyright 2000-2013 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.lang.reflect.Method;
  18. import java.util.Locale;
  19. import java.util.logging.Logger;
  20. import com.vaadin.data.Property;
  21. import com.vaadin.data.util.converter.Converter;
  22. import com.vaadin.data.util.converter.ConverterUtil;
  23. import com.vaadin.shared.ui.label.ContentMode;
  24. import com.vaadin.shared.ui.label.LabelState;
  25. import com.vaadin.shared.util.SharedUtil;
  26. /**
  27. * Label component for showing non-editable short texts.
  28. *
  29. * The label content can be set to the modes specified by {@link ContentMode}
  30. *
  31. * <p>
  32. * The contents of the label may contain simple formatting:
  33. * <ul>
  34. * <li><b>&lt;b></b> Bold
  35. * <li><b>&lt;i></b> Italic
  36. * <li><b>&lt;u></b> Underlined
  37. * <li><b>&lt;br/></b> Linebreak
  38. * <li><b>&lt;ul>&lt;li>item 1&lt;/li>&lt;li>item 2&lt;/li>&lt;/ul></b> List of
  39. * items
  40. * </ul>
  41. * The <b>b</b>,<b>i</b>,<b>u</b> and <b>li</b> tags can contain all the tags in
  42. * the list recursively.
  43. * </p>
  44. *
  45. * @author Vaadin Ltd.
  46. * @since 3.0
  47. */
  48. @SuppressWarnings("serial")
  49. public class Label extends AbstractComponent implements Property<String>,
  50. Property.Viewer, Property.ValueChangeListener,
  51. Property.ValueChangeNotifier, Comparable<Label> {
  52. private static final Logger logger = Logger
  53. .getLogger(Label.class.getName());
  54. /**
  55. * @deprecated As of 7.0, use {@link ContentMode#TEXT} instead
  56. */
  57. @Deprecated
  58. public static final ContentMode CONTENT_TEXT = ContentMode.TEXT;
  59. /**
  60. * @deprecated As of 7.0, use {@link ContentMode#PREFORMATTED} instead
  61. */
  62. @Deprecated
  63. public static final ContentMode CONTENT_PREFORMATTED = ContentMode.PREFORMATTED;
  64. /**
  65. * @deprecated As of 7.0, use {@link ContentMode#HTML} instead
  66. */
  67. @Deprecated
  68. public static final ContentMode CONTENT_XHTML = ContentMode.HTML;
  69. /**
  70. * @deprecated As of 7.0, use {@link ContentMode#XML} instead
  71. */
  72. @Deprecated
  73. public static final ContentMode CONTENT_XML = ContentMode.XML;
  74. /**
  75. * @deprecated As of 7.0, use {@link ContentMode#RAW} instead
  76. */
  77. @Deprecated
  78. public static final ContentMode CONTENT_RAW = ContentMode.RAW;
  79. /**
  80. * @deprecated As of 7.0, use {@link ContentMode#TEXT} instead
  81. */
  82. @Deprecated
  83. public static final ContentMode CONTENT_DEFAULT = ContentMode.TEXT;
  84. /**
  85. * A converter used to convert from the data model type to the field type
  86. * and vice versa. Label type is always String.
  87. */
  88. private Converter<String, Object> converter = null;
  89. private Property<String> dataSource = null;
  90. /**
  91. * Creates an empty Label.
  92. */
  93. public Label() {
  94. this("");
  95. }
  96. /**
  97. * Creates a new instance of Label with text-contents.
  98. *
  99. * @param content
  100. */
  101. public Label(String content) {
  102. this(content, ContentMode.TEXT);
  103. }
  104. /**
  105. * Creates a new instance of Label with text-contents read from given
  106. * datasource.
  107. *
  108. * @param contentSource
  109. */
  110. public Label(Property contentSource) {
  111. this(contentSource, ContentMode.TEXT);
  112. }
  113. /**
  114. * Creates a new instance of Label with text-contents.
  115. *
  116. * @param content
  117. * @param contentMode
  118. */
  119. public Label(String content, ContentMode contentMode) {
  120. setValue(content);
  121. setContentMode(contentMode);
  122. setWidth(100, Unit.PERCENTAGE);
  123. }
  124. /**
  125. * Creates a new instance of Label with text-contents read from given
  126. * datasource.
  127. *
  128. * @param contentSource
  129. * @param contentMode
  130. */
  131. public Label(Property contentSource, ContentMode contentMode) {
  132. setPropertyDataSource(contentSource);
  133. setContentMode(contentMode);
  134. setWidth(100, Unit.PERCENTAGE);
  135. }
  136. @Override
  137. protected LabelState getState() {
  138. return (LabelState) super.getState();
  139. }
  140. /**
  141. * Gets the value of the label.
  142. * <p>
  143. * The value of the label is the text that is shown to the end user.
  144. * Depending on the {@link ContentMode} it is plain text or markup.
  145. * </p>
  146. *
  147. * @return the value of the label.
  148. */
  149. @Override
  150. public String getValue() {
  151. if (getPropertyDataSource() == null) {
  152. // Use internal value if we are running without a data source
  153. return getState().text;
  154. }
  155. return getDataSourceValue();
  156. }
  157. /**
  158. * Returns the current value of the data source converted using the current
  159. * locale.
  160. *
  161. * @return
  162. */
  163. private String getDataSourceValue() {
  164. return ConverterUtil.convertFromModel(getPropertyDataSource()
  165. .getValue(), String.class, getConverter(), getLocale());
  166. }
  167. /**
  168. * Set the value of the label. Value of the label is the XML contents of the
  169. * label.
  170. *
  171. * @param newStringValue
  172. * the New value of the label.
  173. */
  174. @Override
  175. public void setValue(String newStringValue) {
  176. if (getPropertyDataSource() == null) {
  177. getState().text = newStringValue;
  178. } else {
  179. throw new IllegalStateException(
  180. "Label is only a Property.Viewer and cannot update its data source");
  181. }
  182. }
  183. /**
  184. * Gets the type of the Property.
  185. *
  186. * @see com.vaadin.data.Property#getType()
  187. */
  188. @Override
  189. public Class<String> getType() {
  190. return String.class;
  191. }
  192. /**
  193. * Gets the viewing data-source property.
  194. *
  195. * @return the data source property.
  196. * @see com.vaadin.data.Property.Viewer#getPropertyDataSource()
  197. */
  198. @Override
  199. public Property getPropertyDataSource() {
  200. return dataSource;
  201. }
  202. /**
  203. * Sets the property as data-source for viewing.
  204. *
  205. * @param newDataSource
  206. * the new data source Property
  207. * @see com.vaadin.data.Property.Viewer#setPropertyDataSource(com.vaadin.data.Property)
  208. */
  209. @Override
  210. public void setPropertyDataSource(Property newDataSource) {
  211. // Stops listening the old data source changes
  212. if (dataSource != null
  213. && Property.ValueChangeNotifier.class
  214. .isAssignableFrom(dataSource.getClass())) {
  215. ((Property.ValueChangeNotifier) dataSource).removeListener(this);
  216. }
  217. if (newDataSource != null
  218. && !ConverterUtil.canConverterHandle(getConverter(),
  219. String.class, newDataSource.getType())) {
  220. // Try to find a converter
  221. Converter<String, ?> c = ConverterUtil.getConverter(String.class,
  222. newDataSource.getType(), getSession());
  223. setConverter(c);
  224. }
  225. dataSource = newDataSource;
  226. if (dataSource != null) {
  227. // Update the value from the data source. If data source was set to
  228. // null, retain the old value
  229. getState().text = getDataSourceValue();
  230. }
  231. // Listens the new data source if possible
  232. if (dataSource != null
  233. && Property.ValueChangeNotifier.class
  234. .isAssignableFrom(dataSource.getClass())) {
  235. ((Property.ValueChangeNotifier) dataSource).addListener(this);
  236. }
  237. markAsDirty();
  238. }
  239. /**
  240. * Gets the content mode of the Label.
  241. *
  242. * @return the Content mode of the label.
  243. *
  244. * @see ContentMode
  245. */
  246. public ContentMode getContentMode() {
  247. return getState().contentMode;
  248. }
  249. /**
  250. * Sets the content mode of the Label.
  251. *
  252. * @param contentMode
  253. * the New content mode of the label.
  254. *
  255. * @see ContentMode
  256. */
  257. public void setContentMode(ContentMode contentMode) {
  258. if (contentMode == null) {
  259. throw new IllegalArgumentException("Content mode can not be null");
  260. }
  261. getState().contentMode = contentMode;
  262. }
  263. /* Value change events */
  264. private static final Method VALUE_CHANGE_METHOD;
  265. static {
  266. try {
  267. VALUE_CHANGE_METHOD = Property.ValueChangeListener.class
  268. .getDeclaredMethod("valueChange",
  269. new Class[] { Property.ValueChangeEvent.class });
  270. } catch (final java.lang.NoSuchMethodException e) {
  271. // This should never happen
  272. throw new java.lang.RuntimeException(
  273. "Internal error finding methods in Label");
  274. }
  275. }
  276. /**
  277. * Value change event
  278. *
  279. * @author Vaadin Ltd.
  280. * @since 3.0
  281. */
  282. public static class ValueChangeEvent extends Component.Event implements
  283. Property.ValueChangeEvent {
  284. /**
  285. * New instance of text change event
  286. *
  287. * @param source
  288. * the Source of the event.
  289. */
  290. public ValueChangeEvent(Label source) {
  291. super(source);
  292. }
  293. /**
  294. * Gets the Property that has been modified.
  295. *
  296. * @see com.vaadin.data.Property.ValueChangeEvent#getProperty()
  297. */
  298. @Override
  299. public Property getProperty() {
  300. return (Property) getSource();
  301. }
  302. }
  303. /**
  304. * Adds the value change listener.
  305. *
  306. * @param listener
  307. * the Listener to be added.
  308. * @see com.vaadin.data.Property.ValueChangeNotifier#addListener(com.vaadin.data.Property.ValueChangeListener)
  309. */
  310. @Override
  311. public void addValueChangeListener(Property.ValueChangeListener listener) {
  312. addListener(Label.ValueChangeEvent.class, listener, VALUE_CHANGE_METHOD);
  313. }
  314. /**
  315. * @deprecated As of 7.0, replaced by
  316. * {@link #addValueChangeListener(com.vaadin.data.Property.ValueChangeListener)}
  317. **/
  318. @Override
  319. @Deprecated
  320. public void addListener(Property.ValueChangeListener listener) {
  321. addValueChangeListener(listener);
  322. }
  323. /**
  324. * Removes the value change listener.
  325. *
  326. * @param listener
  327. * the Listener to be removed.
  328. * @see com.vaadin.data.Property.ValueChangeNotifier#removeListener(com.vaadin.data.Property.ValueChangeListener)
  329. */
  330. @Override
  331. public void removeValueChangeListener(Property.ValueChangeListener listener) {
  332. removeListener(Label.ValueChangeEvent.class, listener,
  333. VALUE_CHANGE_METHOD);
  334. }
  335. /**
  336. * @deprecated As of 7.0, replaced by
  337. * {@link #removeValueChangeListener(com.vaadin.data.Property.ValueChangeListener)}
  338. **/
  339. @Override
  340. @Deprecated
  341. public void removeListener(Property.ValueChangeListener listener) {
  342. removeValueChangeListener(listener);
  343. }
  344. /**
  345. * Emits the options change event.
  346. */
  347. protected void fireValueChange() {
  348. // Set the error message
  349. fireEvent(new Label.ValueChangeEvent(this));
  350. }
  351. /**
  352. * Listens the value change events from data source.
  353. *
  354. * @see com.vaadin.data.Property.ValueChangeListener#valueChange(Property.ValueChangeEvent)
  355. */
  356. @Override
  357. public void valueChange(Property.ValueChangeEvent event) {
  358. updateValueFromDataSource();
  359. }
  360. private void updateValueFromDataSource() {
  361. // Update the internal value from the data source
  362. String newConvertedValue = getDataSourceValue();
  363. if (!SharedUtil.equals(newConvertedValue, getState().text)) {
  364. getState().text = newConvertedValue;
  365. fireValueChange();
  366. }
  367. }
  368. @Override
  369. public void attach() {
  370. super.attach();
  371. localeMightHaveChanged();
  372. }
  373. @Override
  374. public void setLocale(Locale locale) {
  375. super.setLocale(locale);
  376. localeMightHaveChanged();
  377. }
  378. private void localeMightHaveChanged() {
  379. if (getPropertyDataSource() != null) {
  380. updateValueFromDataSource();
  381. }
  382. }
  383. private String getComparableValue() {
  384. String stringValue = getValue();
  385. if (stringValue == null) {
  386. stringValue = "";
  387. }
  388. if (getContentMode() == ContentMode.HTML
  389. || getContentMode() == ContentMode.XML) {
  390. return stripTags(stringValue);
  391. } else {
  392. return stringValue;
  393. }
  394. }
  395. /**
  396. * Compares the Label to other objects.
  397. *
  398. * <p>
  399. * Labels can be compared to other labels for sorting label contents. This
  400. * is especially handy for sorting table columns.
  401. * </p>
  402. *
  403. * <p>
  404. * In RAW, PREFORMATTED and TEXT modes, the label contents are compared as
  405. * is. In XML, UIDL and HTML modes, only CDATA is compared and tags ignored.
  406. * If the other object is not a Label, its toString() return value is used
  407. * in comparison.
  408. * </p>
  409. *
  410. * @param other
  411. * the Other object to compare to.
  412. * @return a negative integer, zero, or a positive integer as this object is
  413. * less than, equal to, or greater than the specified object.
  414. * @see java.lang.Comparable#compareTo(java.lang.Object)
  415. */
  416. @Override
  417. public int compareTo(Label other) {
  418. String thisValue = getComparableValue();
  419. String otherValue = other.getComparableValue();
  420. return thisValue.compareTo(otherValue);
  421. }
  422. /**
  423. * Strips the tags from the XML.
  424. *
  425. * @param xml
  426. * the String containing a XML snippet.
  427. * @return the original XML without tags.
  428. */
  429. private String stripTags(String xml) {
  430. final StringBuffer res = new StringBuffer();
  431. int processed = 0;
  432. final int xmlLen = xml.length();
  433. while (processed < xmlLen) {
  434. int next = xml.indexOf('<', processed);
  435. if (next < 0) {
  436. next = xmlLen;
  437. }
  438. res.append(xml.substring(processed, next));
  439. if (processed < xmlLen) {
  440. next = xml.indexOf('>', processed);
  441. if (next < 0) {
  442. next = xmlLen;
  443. }
  444. processed = next + 1;
  445. }
  446. }
  447. return res.toString();
  448. }
  449. /**
  450. * Gets the converter used to convert the property data source value to the
  451. * label value.
  452. *
  453. * @return The converter or null if none is set.
  454. */
  455. public Converter<String, Object> getConverter() {
  456. return converter;
  457. }
  458. /**
  459. * Sets the converter used to convert the label value to the property data
  460. * source type. The converter must have a presentation type of String.
  461. *
  462. * @param converter
  463. * The new converter to use.
  464. */
  465. public void setConverter(Converter<String, ?> converter) {
  466. this.converter = (Converter<String, Object>) converter;
  467. markAsDirty();
  468. }
  469. }