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

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