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

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