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.

ComponentState.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.shared;
  5. import java.util.HashSet;
  6. import java.util.List;
  7. import java.util.Set;
  8. import com.vaadin.shared.communication.SharedState;
  9. import com.vaadin.shared.communication.URLReference;
  10. /**
  11. * Default shared state implementation for UI components.
  12. *
  13. * State classes of concrete components should extend this class.
  14. *
  15. * @since 7.0
  16. */
  17. public class ComponentState extends SharedState {
  18. private String height = "";
  19. private String width = "";
  20. private boolean readOnly = false;
  21. private boolean immediate = false;
  22. private String description = "";
  23. // Note: for the caption, there is a difference between null and an empty
  24. // string!
  25. private String caption = null;
  26. private boolean visible = true;
  27. private URLReference icon = null;
  28. private List<String> styles = null;
  29. private String debugId = null;
  30. /**
  31. * A set of event identifiers with registered listeners.
  32. */
  33. private Set<String> registeredEventListeners = null;
  34. // HTML formatted error message for the component
  35. // TODO this could be an object with more information, but currently the UI
  36. // only uses the message
  37. private String errorMessage = null;
  38. /**
  39. * Returns the component height as set by the server.
  40. *
  41. * Can be relative (containing the percent sign) or absolute, or empty
  42. * string for undefined height.
  43. *
  44. * @return component height as defined by the server, not null
  45. */
  46. public String getHeight() {
  47. if (height == null) {
  48. return "";
  49. }
  50. return height;
  51. }
  52. /**
  53. * Sets the height of the component in the server format.
  54. *
  55. * Can be relative (containing the percent sign) or absolute, or null or
  56. * empty string for undefined height.
  57. *
  58. * @param height
  59. * component height
  60. */
  61. public void setHeight(String height) {
  62. this.height = height;
  63. }
  64. /**
  65. * Returns true if the component height is undefined, false if defined
  66. * (absolute or relative).
  67. *
  68. * @return true if component height is undefined
  69. */
  70. public boolean isUndefinedHeight() {
  71. return "".equals(getHeight());
  72. }
  73. /**
  74. * Returns the component width as set by the server.
  75. *
  76. * Can be relative (containing the percent sign) or absolute, or empty
  77. * string for undefined height.
  78. *
  79. * @return component width as defined by the server, not null
  80. */
  81. public String getWidth() {
  82. if (width == null) {
  83. return "";
  84. }
  85. return width;
  86. }
  87. /**
  88. * Sets the width of the component in the server format.
  89. *
  90. * Can be relative (containing the percent sign) or absolute, or null or
  91. * empty string for undefined width.
  92. *
  93. * @param width
  94. * component width
  95. */
  96. public void setWidth(String width) {
  97. this.width = width;
  98. }
  99. /**
  100. * Returns true if the component width is undefined, false if defined
  101. * (absolute or relative).
  102. *
  103. * @return true if component width is undefined
  104. */
  105. public boolean isUndefinedWidth() {
  106. return "".equals(getWidth());
  107. }
  108. /**
  109. * Returns true if the component is in read-only mode.
  110. *
  111. * @see com.vaadin.ui.Component#isReadOnly()
  112. *
  113. * @return true if the component is in read-only mode
  114. */
  115. public boolean isReadOnly() {
  116. return readOnly;
  117. }
  118. /**
  119. * Sets or resets the read-only mode for a component.
  120. *
  121. * @see com.vaadin.ui.Component#setReadOnly()
  122. *
  123. * @param readOnly
  124. * new mode for the component
  125. */
  126. public void setReadOnly(boolean readOnly) {
  127. this.readOnly = readOnly;
  128. }
  129. /**
  130. * Returns true if the component is in immediate mode.
  131. *
  132. * @see com.vaadin.terminal.VariableOwner#isImmediate()
  133. *
  134. * @return true if the component is in immediate mode
  135. */
  136. public boolean isImmediate() {
  137. return immediate;
  138. }
  139. /**
  140. * Sets or resets the immediate mode for a component.
  141. *
  142. * @see com.vaadin.terminal.VariableOwner#setImmediate()
  143. *
  144. * @param immediate
  145. * new mode for the component
  146. */
  147. public void setImmediate(boolean immediate) {
  148. this.immediate = immediate;
  149. }
  150. /**
  151. * Returns true if the component has user-defined styles.
  152. *
  153. * @return true if the component has user-defined styles
  154. */
  155. public boolean hasStyles() {
  156. return styles != null && !styles.isEmpty();
  157. }
  158. /**
  159. * Gets the description of the component (typically shown as tooltip).
  160. *
  161. * @see com.vaadin.ui.AbstractComponent#getDescription()
  162. *
  163. * @return component description (not null, can be empty string)
  164. */
  165. public String getDescription() {
  166. return description;
  167. }
  168. /**
  169. * Sets the description of the component (typically shown as tooltip).
  170. *
  171. * @see com.vaadin.ui.AbstractComponent#setDescription(String)
  172. *
  173. * @param description
  174. * new component description (can be null)
  175. */
  176. public void setDescription(String description) {
  177. this.description = description;
  178. }
  179. /**
  180. * Returns true if the component has a description.
  181. *
  182. * @return true if the component has a description
  183. */
  184. public boolean hasDescription() {
  185. return getDescription() != null && !"".equals(getDescription());
  186. }
  187. /**
  188. * Gets the caption of the component (typically shown by the containing
  189. * layout).
  190. *
  191. * @see com.vaadin.ui.Component#getCaption()
  192. *
  193. * @return component caption - can be null (no caption) or empty string
  194. * (reserve space for an empty caption)
  195. */
  196. public String getCaption() {
  197. return caption;
  198. }
  199. /**
  200. * Sets the caption of the component (typically shown by the containing
  201. * layout).
  202. *
  203. * @see com.vaadin.ui.Component#setCaption(String)
  204. *
  205. * @param caption
  206. * new component caption - can be null (no caption) or empty
  207. * string (reserve space for an empty caption)
  208. */
  209. public void setCaption(String caption) {
  210. this.caption = caption;
  211. }
  212. /**
  213. * Returns the visibility state of the component. Note that this state is
  214. * related to the component only, not its parent. This might differ from
  215. * what {@link com.vaadin.ui.Component#isVisible()} returns as this takes
  216. * the hierarchy into account.
  217. *
  218. * @return The visibility state.
  219. */
  220. public boolean isVisible() {
  221. return visible;
  222. }
  223. /**
  224. * Sets the visibility state of the component.
  225. *
  226. * @param visible
  227. * The new visibility state.
  228. */
  229. public void setVisible(boolean visible) {
  230. this.visible = visible;
  231. }
  232. public URLReference getIcon() {
  233. return icon;
  234. }
  235. public void setIcon(URLReference icon) {
  236. this.icon = icon;
  237. }
  238. /**
  239. * Gets the style names for the component.
  240. *
  241. * @return A List of style names or null if no styles have been set.
  242. */
  243. public List<String> getStyles() {
  244. return styles;
  245. }
  246. /**
  247. * Sets the style names for the component.
  248. *
  249. * @param styles
  250. * A list containing style names
  251. */
  252. public void setStyles(List<String> styles) {
  253. this.styles = styles;
  254. }
  255. /**
  256. * Gets the debug id for the component. The debugId is added as DOM id for
  257. * the component.
  258. *
  259. * @return The debug id for the component or null if not set
  260. */
  261. public String getDebugId() {
  262. return debugId;
  263. }
  264. /**
  265. * Sets the debug id for the component. The debugId is added as DOM id for
  266. * the component.
  267. *
  268. * @param debugId
  269. * The new debugId for the component or null for no debug id
  270. *
  271. */
  272. public void setDebugId(String debugId) {
  273. this.debugId = debugId;
  274. }
  275. /**
  276. * Gets the identifiers for the event listeners that have been registered
  277. * for the component (using an event id)
  278. *
  279. * @return A set of event identifiers or null if no identifiers have been
  280. * registered
  281. */
  282. public Set<String> getRegisteredEventListeners() {
  283. return registeredEventListeners;
  284. }
  285. /**
  286. * Sets the identifiers for the event listeners that have been registered
  287. * for the component (using an event id)
  288. *
  289. * @param registeredEventListeners
  290. * The new set of identifiers or null if no identifiers have been
  291. * registered
  292. */
  293. public void setRegisteredEventListeners(Set<String> registeredEventListeners) {
  294. this.registeredEventListeners = registeredEventListeners;
  295. }
  296. /**
  297. * Adds an event listener id.
  298. *
  299. * @param eventListenerId
  300. * The event identifier to add
  301. */
  302. public void addRegisteredEventListener(String eventListenerId) {
  303. if (registeredEventListeners == null) {
  304. registeredEventListeners = new HashSet<String>();
  305. }
  306. registeredEventListeners.add(eventListenerId);
  307. }
  308. /**
  309. * Removes an event listener id.
  310. *
  311. * @param eventListenerId
  312. * The event identifier to remove
  313. */
  314. public void removeRegisteredEventListener(String eventIdentifier) {
  315. if (registeredEventListeners == null) {
  316. return;
  317. }
  318. registeredEventListeners.remove(eventIdentifier);
  319. if (registeredEventListeners.size() == 0) {
  320. registeredEventListeners = null;
  321. }
  322. }
  323. /**
  324. * Returns the current error message for the component.
  325. *
  326. * @return HTML formatted error message to show for the component or null if
  327. * none
  328. */
  329. public String getErrorMessage() {
  330. return errorMessage;
  331. }
  332. /**
  333. * Sets the current error message for the component.
  334. *
  335. * TODO this could use an object with more details about the error
  336. *
  337. * @param errorMessage
  338. * HTML formatted error message to show for the component or null
  339. * for none
  340. */
  341. public void setErrorMessage(String errorMessage) {
  342. this.errorMessage = errorMessage;
  343. }
  344. }