Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Label.java 17KB

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