1 package com.vaadin.contextmenu;
2
3 import java.io.Serializable;
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.UUID;
7
8 import com.vaadin.contextmenu.Menu.Command;
9 import com.vaadin.server.Resource;
10
11
12
13
14
15
16
17 @SuppressWarnings("serial")
18 class MenuItemImpl implements Serializable, MenuItem {
19
20
21 private final int itsId;
22 private Command itsCommand;
23 private String itsText;
24 private List<MenuItem> itsChildren;
25 private Resource itsIcon;
26 private MenuItem itsParent;
27 private boolean enabled = true;
28 private boolean visible = true;
29 private boolean isSeparator = false;
30 private String styleName;
31 private String description;
32 private boolean checkable = false;
33 private boolean checked = false;
34
35 private void markAsDirty() {
36
37
38
39 }
40
41
42
43
44
45
46
47
48
49
50
51
52 public MenuItemImpl(String caption, Resource icon, Command command) {
53 if (caption == null) {
54 throw new IllegalArgumentException("caption cannot be null");
55 }
56
57 itsId = getNextId();
58 itsText = caption;
59 itsIcon = icon;
60 itsCommand = command;
61 }
62
63 public MenuItemImpl(MenuItem parent, String trim, Resource icon,
64 Command object) {
65 this(trim, icon, object);
66 setParent(parent);
67 }
68
69 protected int getNextId() {
70
71 return UUID.randomUUID().hashCode();
72 }
73
74
75
76
77
78
79 @Override
80 public boolean hasChildren() {
81 return !isSeparator() && itsChildren != null;
82 }
83
84
85
86
87
88
89 @Override
90 public MenuItem addSeparator() {
91 MenuItem item = addItem(true, "", null, null);
92 return item;
93 }
94
95
96
97
98
99
100
101
102 @Override
103 public MenuItem addSeparatorBefore(MenuItem itemToAddBefore) {
104 MenuItem item = addItemBefore(true, "", null, null, itemToAddBefore);
105 return item;
106 }
107
108
109
110
111
112
113
114 @Override
115 public MenuItem addItem(String caption, Command command) {
116 return addItem(caption, null, command);
117 }
118
119
120
121
122
123
124
125 @Override
126 public MenuItem addItem(String caption, Resource icon, Command command)
127 throws IllegalStateException {
128 return addItem(false, caption, icon, command);
129 }
130
131 private MenuItem addItem(boolean separator, String caption, Resource icon,
132 Command command) throws IllegalStateException {
133 if (isSeparator()) {
134 throw new UnsupportedOperationException(
135 "Cannot add items to a separator");
136 }
137 if (isCheckable()) {
138 throw new IllegalStateException(
139 "A checkable item cannot have children");
140 }
141 if (caption == null) {
142 throw new IllegalArgumentException("Caption cannot be null");
143 }
144
145 if (itsChildren == null) {
146 itsChildren = new ArrayList<MenuItem>();
147 }
148
149 MenuItemImpl newItem = new MenuItemImpl(this, caption, icon, command);
150
151 newItem.setSeparator(separator);
152
153 itsChildren.add(newItem);
154
155 markAsDirty();
156
157 return newItem;
158 }
159
160
161
162
163
164
165
166
167
168 @Override
169 public MenuItem addItemBefore(String caption, Resource icon,
170 Command command, MenuItem itemToAddBefore) {
171 return addItemBefore(false, caption, icon, command, itemToAddBefore);
172 }
173
174 private MenuItem addItemBefore(boolean separator, String caption,
175 Resource icon, Command command, MenuItem itemToAddBefore)
176 throws IllegalStateException {
177 if (isCheckable()) {
178 throw new IllegalStateException(
179 "A checkable item cannot have children");
180 }
181 MenuItem newItem = null;
182
183 if (hasChildren() && itsChildren.contains(itemToAddBefore)) {
184 int index = itsChildren.indexOf(itemToAddBefore);
185 newItem = new MenuItemImpl(this, caption, icon, command);
186 itsChildren.add(index, newItem);
187 } else {
188 newItem = addItem(caption, icon, command);
189 }
190
191 markAsDirty();
192
193 return newItem;
194 }
195
196
197
198
199
200
201 @Override
202 public Command getCommand() {
203 return itsCommand;
204 }
205
206
207
208
209
210
211 @Override
212 public Resource getIcon() {
213 return itsIcon;
214 }
215
216
217
218
219
220
221 @Override
222 public MenuItem getParent() {
223 return itsParent;
224 }
225
226
227
228
229
230
231 @Override
232 public List<MenuItem> getChildren() {
233 return itsChildren;
234 }
235
236
237
238
239
240
241 @Override
242 public java.lang.String getText() {
243 return itsText;
244 }
245
246
247
248
249
250
251 @Override
252 public int getSize() {
253 if (itsChildren != null) {
254 return itsChildren.size();
255 }
256 return -1;
257 }
258
259
260
261
262
263
264 @Override
265 public int getId() {
266 return itsId;
267 }
268
269
270
271
272
273
274
275 @Override
276 public void setCommand(Command command) {
277 itsCommand = command;
278 }
279
280
281
282
283
284
285
286 @Override
287 public void setIcon(Resource icon) {
288 itsIcon = icon;
289 markAsDirty();
290 }
291
292
293
294
295
296
297 @Override
298 public void setText(java.lang.String text) {
299 if (text != null) {
300 itsText = text;
301 }
302 markAsDirty();
303 }
304
305
306
307
308
309
310
311 @Override
312 public void removeChild(MenuItem item) {
313 if (item != null && itsChildren != null) {
314 itsChildren.remove(item);
315 if (itsChildren.isEmpty()) {
316 itsChildren = null;
317 }
318 markAsDirty();
319 }
320 }
321
322
323
324
325
326
327 @Override
328 public void removeChildren() {
329 if (itsChildren != null) {
330 itsChildren.clear();
331 itsChildren = null;
332 markAsDirty();
333 }
334 }
335
336
337
338
339
340
341
342 protected void setParent(MenuItem parent) {
343 itsParent = parent;
344 }
345
346
347
348
349
350
351 @Override
352 public void setEnabled(boolean enabled) {
353 this.enabled = enabled;
354 markAsDirty();
355 }
356
357
358
359
360
361
362 @Override
363 public boolean isEnabled() {
364 return enabled;
365 }
366
367
368
369
370
371
372 @Override
373 public void setVisible(boolean visible) {
374 this.visible = visible;
375 markAsDirty();
376 }
377
378
379
380
381
382
383 @Override
384 public boolean isVisible() {
385 return visible;
386 }
387
388 protected void setSeparator(boolean isSeparator) {
389 this.isSeparator = isSeparator;
390 markAsDirty();
391 }
392
393
394
395
396
397
398 @Override
399 public boolean isSeparator() {
400 return isSeparator;
401 }
402
403
404
405
406
407
408
409 @Override
410 public void setStyleName(String styleName) {
411 this.styleName = styleName;
412 markAsDirty();
413 }
414
415
416
417
418
419
420 @Override
421 public String getStyleName() {
422 return styleName;
423 }
424
425
426
427
428
429
430
431 @Override
432 public void setDescription(String description) {
433 this.description = description;
434 markAsDirty();
435 }
436
437
438
439
440
441
442 @Override
443 public String getDescription() {
444 return description;
445 }
446
447
448
449
450
451
452 @Override
453 public boolean isCheckable() {
454 return checkable;
455 }
456
457
458
459
460
461
462 @Override
463 public void setCheckable(boolean checkable) throws IllegalStateException {
464 if (hasChildren()) {
465 throw new IllegalStateException(
466 "A menu item with children cannot be checkable");
467 }
468 this.checkable = checkable;
469 markAsDirty();
470 }
471
472
473
474
475
476
477 @Override
478 public boolean isChecked() {
479 return checked;
480 }
481
482
483
484
485
486
487 @Override
488 public void setChecked(boolean checked) {
489 this.checked = checked;
490 markAsDirty();
491 }
492
493 protected void setChildren(List<MenuItem> children) {
494 this.itsChildren = children;
495 }
496 }