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