1 package org.vaadin.tokenfield;
2
3 import java.util.Collection;
4 import java.util.HashSet;
5 import java.util.LinkedHashMap;
6 import java.util.LinkedHashSet;
7 import java.util.Set;
8
9 import com.vaadin.v7.data.Container;
10 import com.vaadin.v7.data.Property;
11 import com.vaadin.server.Resource;
12 import com.vaadin.v7.shared.ui.combobox.FilteringMode;
13 import com.vaadin.v7.ui.AbstractSelect.ItemCaptionMode;
14 import com.vaadin.v7.ui.AbstractSelect.NewItemHandler;
15 import com.vaadin.ui.Button;
16 import com.vaadin.ui.Button.ClickEvent;
17 import com.vaadin.v7.ui.ComboBox;
18 import com.vaadin.ui.Component;
19 import com.vaadin.ui.CssLayout;
20 import com.vaadin.v7.ui.CustomField;
21 import com.vaadin.v7.ui.HorizontalLayout;
22 import com.vaadin.ui.Layout;
23 import com.vaadin.v7.ui.themes.Reindeer;
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 public class TokenField extends CustomField implements Container.Editor {
225
226 private static final long serialVersionUID = -4718188396491718742L;
227
228 public enum InsertPosition {
229
230
231
232 AFTER,
233
234
235
236 BEFORE
237 }
238
239 public static final String STYLE_TOKENFIELD = "tokenfield";
240 public static final String STYLE_TOKENTEXTFIELD = "tokentextfield";
241
242 public static final String STYLE_BUTTON_EMPHAZISED = "emphasize";
243
244
245
246
247 protected Layout layout;
248
249
250
251
252 protected InsertPosition insertPosition = InsertPosition.BEFORE;
253
254
255
256
257 protected TokenComboBox cb = new TokenComboBox(insertPosition) {
258
259 private static final long serialVersionUID = -5550767105896319355L;
260
261 protected void onDelete() {
262 if (!buttons.isEmpty()) {
263 Object[] keys = buttons.keySet().toArray();
264 onTokenDelete(keys[keys.length - 1]);
265 cb.focus();
266 }
267 }
268 };
269
270
271
272
273 protected LinkedHashMap<Object, Button> buttons = new LinkedHashMap<Object, Button>();
274
275 protected boolean rememberNewTokens = true;
276
277
278
279
280
281
282
283
284
285 public TokenField(String caption, InsertPosition insertPosition) {
286 this();
287 this.insertPosition = insertPosition;
288 setCaption(caption);
289 }
290
291
292
293
294
295
296
297 public TokenField(String caption) {
298 this();
299 setCaption(caption);
300 }
301
302
303
304
305
306 public TokenField() {
307 this(new CssLayout());
308 }
309
310
311
312
313
314
315
316
317
318 public TokenField(String caption, Layout lo) {
319 this(lo);
320 setCaption(caption);
321 }
322
323
324
325
326
327
328
329
330
331
332
333
334 public TokenField(String caption, Layout lo, InsertPosition insertPosition) {
335 this(lo);
336 setCaption(caption);
337 this.insertPosition = insertPosition;
338 }
339
340
341
342
343
344
345
346
347
348
349 public TokenField(Layout lo, InsertPosition insertPosition) {
350 this(lo);
351 this.insertPosition = insertPosition;
352 }
353
354
355
356
357
358
359
360 public TokenField(Layout lo) {
361 setStyleName(STYLE_TOKENFIELD + " " + STYLE_TOKENTEXTFIELD);
362
363 cb.setImmediate(true);
364 cb.setNewItemsAllowed(true);
365 cb.setNullSelectionAllowed(false);
366 cb.addListener(new ComboBox.ValueChangeListener() {
367
368 private static final long serialVersionUID = 4370326413130922134L;
369
370 public void valueChange(
371 com.vaadin.v7.data.Property.ValueChangeEvent event) {
372 final Object tokenId = event.getProperty().getValue();
373 if (tokenId != null) {
374 onTokenInput(tokenId);
375 cb.setValue(null);
376 cb.focus();
377 }
378 }
379 });
380
381 cb.setNewItemHandler(new NewItemHandler() {
382
383 private static final long serialVersionUID = 1L;
384
385
386
387
388 public void addNewItem(String tokenId) {
389 if (isReadOnly()) {
390 throw new Property.ReadOnlyException();
391 }
392 onTokenInput(tokenId);
393 if (rememberNewTokens) {
394 rememberToken(tokenId);
395 }
396 cb.focus();
397 }
398
399 });
400
401 setLayout(lo);
402
403 }
404
405 protected void rememberToken(String tokenId) {
406 if (cb.addItem(getTokenCaption(tokenId)) != null) {
407
408 if (getTokenCaptionPropertyId() != null) {
409
410 cb.getContainerProperty(tokenId, getTokenCaptionPropertyId())
411 .setValue(tokenId);
412
413 }
414 }
415 }
416
417
418
419
420 private void rebuild() {
421 layout.removeAllComponents();
422 if (!isReadOnly() && insertPosition == InsertPosition.AFTER) {
423 layout.addComponent(cb);
424 }
425 for (Button b2 : buttons.values()) {
426 layout.addComponent(b2);
427 }
428 if (!isReadOnly() && insertPosition == InsertPosition.BEFORE) {
429 layout.addComponent(cb);
430 }
431 if (layout instanceof HorizontalLayout) {
432 ((HorizontalLayout) layout).setExpandRatio(cb, 1.0f);
433 }
434 }
435
436
437
438
439
440
441
442 protected void setInternalValue(Object newValue) {
443
444 Set<Object> vals = (Set<Object>) newValue;
445 Set<Object> old = buttons.keySet();
446
447 super.setInternalValue(newValue);
448
449 if (old == null) {
450 old = new HashSet<Object>();
451 }
452
453 if (vals == null) {
454 vals = new HashSet<Object>();
455 }
456
457 Set<Object> remove = new HashSet<Object>(old);
458 Set<Object> add = new HashSet<Object>(vals);
459 remove.removeAll(vals);
460 add.removeAll(old);
461
462 for (Object tokenId : remove) {
463 removeTokenButton(tokenId);
464 }
465 for (Object tokenId : add) {
466 addTokenButton(tokenId);
467 }
468 }
469
470
471
472
473
474
475
476
477
478
479
480
481
482 protected void onTokenInput(Object tokenId) {
483 addToken(tokenId);
484 }
485
486
487
488
489
490
491
492
493
494 protected void onTokenClick(Object tokenId) {
495 removeToken(tokenId);
496 }
497
498
499
500
501
502
503
504
505
506 protected void onTokenDelete(Object tokenId) {
507 onTokenClick(tokenId);
508 }
509
510 private void addTokenButton(final Object val) {
511 Button b = new Button();
512 configureTokenButton(val, b);
513 b.addClickListener(new Button.ClickListener() {
514 private static final long serialVersionUID = -1943432188848347317L;
515
516 public void buttonClick(ClickEvent event) {
517 onTokenClick(val);
518 }
519 });
520 buttons.put(val, b);
521
522 if (insertPosition == InsertPosition.BEFORE) {
523 layout.replaceComponent(cb, b);
524 layout.addComponent(cb);
525 } else {
526 layout.addComponent(b);
527 }
528 if (layout instanceof HorizontalLayout) {
529 ((HorizontalLayout) layout).setExpandRatio(cb, 1.0f);
530 }
531
532 }
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553 public void addToken(Object tokenId) {
554 Set<Object> set = (Set<Object>) getValue();
555 if (set == null) {
556 set = new LinkedHashSet<Object>();
557 }
558 if (set.contains(tokenId)) {
559 return;
560 }
561 HashSet<Object> newSet = new LinkedHashSet<Object>(set);
562 newSet.add(tokenId);
563 setValue(newSet);
564 }
565
566
567
568
569
570
571
572
573
574
575
576 public void removeToken(Object tokenId) {
577 Set<Object> set = (Set<Object>) getValue();
578 LinkedHashSet<Object> newSet = new LinkedHashSet<Object>(set);
579 newSet.remove(tokenId);
580
581 setValue(newSet);
582
583 }
584
585 private void removeTokenButton(Object tokenId) {
586 Button button = buttons.get(tokenId);
587 layout.removeComponent(button);
588 buttons.remove(tokenId);
589
590 }
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606 protected void configureTokenButton(Object tokenId, Button button) {
607 button.setCaption(getTokenCaption(tokenId) + " ×");
608 button.setIcon(getTokenIcon(tokenId));
609 button.setDescription("Click to remove");
610 button.setStyleName(Reindeer.BUTTON_LINK);
611 }
612
613
614
615
616
617
618 public Layout getLayout() {
619 return layout;
620 }
621
622
623
624
625
626
627
628 protected void setLayout(Layout newLayout) {
629 if (layout != null) {
630 layout.removeAllComponents();
631 }
632 layout = newLayout;
633
634
635 rebuild();
636 }
637
638
639
640
641
642
643
644
645
646
647 public InsertPosition getTokenInsertPosition() {
648 return insertPosition;
649 }
650
651
652
653
654
655
656
657
658
659 public void setTokenInsertPosition(InsertPosition insertPosition) {
660 if (this.insertPosition != insertPosition) {
661 this.insertPosition = insertPosition;
662 cb.setTokenInsertPosition(insertPosition);
663 rebuild();
664 }
665 }
666
667 public void setReadOnly(boolean readOnly) {
668 if (readOnly == isReadOnly()) {
669 return;
670 }
671 for (Button b : buttons.values()) {
672
673 }
674 super.setReadOnly(readOnly);
675 if (readOnly) {
676 layout.removeComponent(cb);
677 } else {
678 rebuild();
679 }
680 }
681
682
683
684
685
686
687
688
689
690 public void setContainerDataSource(Container c) {
691 cb.setContainerDataSource(c);
692 }
693
694
695
696
697
698
699
700
701 public Container getContainerDataSource() {
702 return cb.getContainerDataSource();
703 }
704
705
706
707
708
709
710
711
712
713
714
715 public void setNewTokensAllowed(boolean allowNewTokens) {
716 cb.setNewItemsAllowed(allowNewTokens);
717 }
718
719
720
721
722
723
724
725 public boolean isNewTokensAllowed() {
726 return cb.isNewItemsAllowed();
727 }
728
729
730
731
732
733
734
735 public boolean isRememberNewTokens() {
736 return rememberNewTokens;
737 }
738
739
740
741
742
743
744
745
746
747 public void setRememberNewTokens(boolean rememberNewTokens) {
748 this.rememberNewTokens = rememberNewTokens;
749 }
750
751
752
753
754
755
756
757
758 public void setFilteringMode(FilteringMode filteringMode) {
759 cb.setFilteringMode(filteringMode);
760 }
761
762
763
764
765
766
767
768
769 public FilteringMode getFilteringMode() {
770 return cb.getFilteringMode();
771 }
772
773
774
775
776
777
778 public void focus() {
779 cb.focus();
780 }
781
782
783
784
785
786
787
788 public String getInputPrompt() {
789 return cb.getInputPrompt();
790 }
791
792
793
794
795
796
797
798
799
800
801
802 public String getTokenCaption(Object tokenId) {
803 if (cb.containsId(tokenId)) {
804 return cb.getItemCaption(tokenId);
805 } else {
806 return "" + tokenId;
807 }
808 }
809
810
811
812
813
814 public ItemCaptionMode getTokenCaptionMode() {
815 return cb.getItemCaptionMode();
816 }
817
818
819
820
821
822 public Object getTokenCaptionPropertyId() {
823 return cb.getItemCaptionPropertyId();
824 }
825
826
827
828
829
830
831 public Resource getTokenIcon(Object tokenId) {
832 return cb.getItemIcon(tokenId);
833 }
834
835
836
837
838
839
840 public Object getTokenIconPropertyId() {
841 return cb.getItemIconPropertyId();
842 }
843
844
845
846
847
848
849 public Collection getTokenIds() {
850 return cb.getItemIds();
851 }
852
853
854
855
856
857
858 public int getTabIndex() {
859 return cb.getTabIndex();
860 }
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876 @Override
877 public void setHeight(float height, Unit unit) {
878 if (this.layout != null) {
879 this.layout.setHeight(height, unit);
880 }
881 super.setHeight(height, unit);
882 }
883
884 @Override
885 public void setWidth(float width, Unit unit) {
886 if (this.layout != null) {
887 this.layout.setWidth(width, unit);
888 }
889 super.setWidth(width, unit);
890 }
891
892 @Override
893 public void setSizeFull() {
894 if (this.layout != null) {
895 this.layout.setSizeFull();
896 }
897 super.setSizeFull();
898 }
899
900 @Override
901 public void setSizeUndefined() {
902 if (this.layout != null) {
903 this.layout.setSizeUndefined();
904 }
905 super.setSizeUndefined();
906 }
907
908 public void setInputHeight(String height) {
909 this.cb.setHeight(height);
910 }
911
912 public void setInputWidth(String width) {
913 this.cb.setWidth(width);
914 }
915
916 public void setInputHeight(float height, Unit unit) {
917 this.cb.setHeight(height, unit);
918 }
919
920 public void setInputWidth(float width, Unit unit) {
921 this.cb.setWidth(width, unit);
922 }
923
924 public void setInputSizeFull() {
925 this.cb.setSizeFull();
926 }
927
928 public void setInputSizeUndefined() {
929 this.cb.setSizeUndefined();
930 }
931
932
933
934
935
936
937
938 public void setInputPrompt(String inputPrompt) {
939 cb.setInputPrompt(inputPrompt);
940 }
941
942
943
944
945
946
947
948
949
950
951 public void setTokenCaption(Object tokenId, String caption) {
952 cb.setItemCaption(tokenId, caption);
953 }
954
955
956
957
958
959 public void setTokenCaptionMode(ItemCaptionMode mode) {
960 cb.setItemCaptionMode(mode);
961 }
962
963
964
965
966
967 public void setTokenCaptionPropertyId(Object propertyId) {
968 cb.setItemCaptionPropertyId(propertyId);
969 }
970
971
972
973
974
975
976 public void setTokenIcon(Object tokenId, Resource icon) {
977 cb.setItemIcon(tokenId, icon);
978 }
979
980
981
982
983
984 public void setTokenIconPropertyId(Object propertyId) {
985 cb.setItemIconPropertyId(propertyId);
986 }
987
988
989
990
991
992
993 public void setTabIndex(int tabIndex) {
994 cb.setTabIndex(tabIndex);
995 }
996
997
998
999
1000
1001
1002 @Override
1003 public Class<?> getType() {
1004 return Set.class;
1005 }
1006
1007 @Override
1008 protected Component initContent() {
1009 return layout;
1010 }
1011
1012 }