My favorites
▼
|
Sign in
tokenfield
A Vaadin component for selecting multiple tokens (e.g tags, email addresses...)
Project Home
Wiki
Issues
Source
READ-ONLY: This project has been
archived
. For more information see
this post
.
Search
Search within:
All issues
Open issues
New issues
Issues to verify
for
Advanced search
Search tips
Subscriptions
Issue
10
attachment: SortableTokenField.java
(4.2 KB)
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
package org.vaadin.tokenfield;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import org.vaadin.tokenfield.TokenField;
import com.vaadin.event.dd.DragAndDropEvent;
import com.vaadin.event.dd.DropHandler;
import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
import com.vaadin.event.dd.acceptcriteria.SourceIsTarget;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.Layout;
import fi.jasoft.dragdroplayouts.DDCssLayout;
import fi.jasoft.dragdroplayouts.client.ui.LayoutDragMode;
import fi.jasoft.dragdroplayouts.drophandlers.DefaultCssLayoutDropHandler;
import fi.jasoft.dragdroplayouts.interfaces.DragFilter;
import fi.jasoft.dragdroplayouts.interfaces.LayoutDragSource;
public class SortableTokenField extends TokenField {
private static final long serialVersionUID = 1L;
public SortableTokenField(String caption, InsertPosition insertPosition) {
this();
this.insertPosition = insertPosition;
setCaption(caption);
}
/**
* Create a new SortableTokenField with a caption.
*
* @param caption
* the desired caption
*/
public SortableTokenField(String caption) {
this();
setCaption(caption);
}
/**
* Create a new SortableTokenField.
*
*/
public SortableTokenField() {
super(new DDCssLayout());
DDCssLayout lo = (DDCssLayout) getLayout();
lo.setDropHandler(new DefaultCssLayoutDropHandler() {
@Override
protected void handleComponentReordering(DragAndDropEvent event) {
super.handleComponentReordering(event);
SortableTokenField.this.handleComponentReordering();
}
@Override
public AcceptCriterion getAcceptCriterion() {
return SourceIsTarget.get();
}
});
}
/**
* Create a new SortableTokenField with a caption and a given layout.
*
* @param caption
* the desired caption
* @param lo
* the desired layout
*/
public SortableTokenField(String caption, Layout lo) {
this(lo);
setCaption(caption);
}
/**
* Create a new SortableTokenField with a caption, a given layout, and the specified token insert position.
*
*
* @param caption
* the desired caption
* @param lo
* the desired layout
* @param insertPosition
* the desired token insert position
*/
public SortableTokenField(String caption, Layout lo, InsertPosition insertPosition) {
this(lo);
setCaption(caption);
this.insertPosition = insertPosition;
}
/**
* Create a new SortableTokenField with the given layout, and the specified token insert position.
*
* @param lo
* the desired layout
* @param insertPosition
* the desired token insert position
*/
public SortableTokenField(Layout lo, InsertPosition insertPosition) {
this(lo);
this.insertPosition = insertPosition;
}
/**
* Create a new SortableTokenField with the given layout.
*
* @param lo
* the desired layout
*/
public SortableTokenField(Layout lo) {
super(lo);
}
/**
* Sets the value of the field corresponding to the Ordering in the Layout. Should be called in a custom
* {@link DropHandler} after reordering.
*/
public void handleComponentReordering() {
LinkedHashSet<Object> set = new LinkedHashSet<Object>();
HashMap<Component, Object> reverseButtonsMap = new HashMap<Component, Object>();
for (Object o : buttons.keySet())
reverseButtonsMap.put(buttons.get(o), o);
Iterator<Component> itr = layout.getComponentIterator();
while (itr.hasNext()) {
Object o = reverseButtonsMap.get(itr.next());
if (o != null)
set.add(o);
}
// TODO find a better way to set a Value to a set that contains the same
// elements but is linked in a different order
setInternalValue(new LinkedHashSet<Object>());
setValue(set);
}
/**
* If the Layout implements {@link LayoutDragSource} the dragMode will be set to CLONE. The {@link DropHandler} must
* be set manually.
*/
@Override
public void setLayout(Layout newLayout) {
if (newLayout instanceof LayoutDragSource) {
((LayoutDragSource) newLayout).setDragMode(LayoutDragMode.CLONE);
((LayoutDragSource) newLayout).setDragFilter(new DragFilter() {
@Override
public boolean isDraggable(Component component) {
return component instanceof Button;
}
});
}
super.setLayout(newLayout);
}
}
Powered by
Google Project Hosting