Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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