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
|
/* *************************************************************************
IT Mill Toolkit
Development of Browser User Interfaces Made Easy
Copyright (C) 2000-2006 IT Mill Ltd
*************************************************************************
This product is distributed under commercial license that can be found
from the product package on license.pdf. Use of this product might
require purchasing a commercial license from IT Mill Ltd. For guidelines
on usage, see licensing-guidelines.html
*************************************************************************
For more information, contact:
IT Mill Ltd phone: +358 2 4802 7180
Ruukinkatu 2-4 fax: +358 2 4802 7181
20540, Turku email: info@itmill.com
Finland company www: www.itmill.com
Primary source for information and releases: www.itmill.com
********************************************************************** */
package com.itmill.toolkit.ui;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.itmill.toolkit.data.Container;
import com.itmill.toolkit.data.Property;
import com.itmill.toolkit.terminal.PaintException;
import com.itmill.toolkit.terminal.PaintTarget;
import com.itmill.toolkit.terminal.Resource;
/**
* <p>
* A class representing a selection of items the user has selected in a UI. The
* set of choices is presented as a set of {@link com.itmill.toolkit.data.Item}s
* in a {@link com.itmill.toolkit.data.Container}.
* </p>
*
* <p>
* A <code>Select</code> component may be in single- or multiselect mode.
* Multiselect mode means that more than one item can be selected
* simultaneously.
* </p>
*
* @author IT Mill Ltd.
* @version
* @VERSION@
* @since 3.0
*/
public class Select extends AbstractSelect implements AbstractSelect.Filtering {
/**
* Holds value of property pageLength. 0 disables paging.
*/
protected int pageLength = 10;
// current page when the user is 'paging' trough options
private int currentPage;
private int filteringMode = FILTERINGMODE_STARTSWITH;
private String filterstring;
private String prevfilterstring;
private List filteredOptions;
/* Constructors ********************************************************* */
/* Component methods **************************************************** */
public Select() {
super();
}
public Select(String caption, Collection options) {
super(caption, options);
}
public Select(String caption, Container dataSource) {
super(caption, dataSource);
}
public Select(String caption) {
super(caption);
}
/**
* Paints the content of this component.
*
* @param target
* the Paint Event.
* @throws PaintException
* if the paint operation failed.
*/
public void paintContent(PaintTarget target) throws PaintException {
// Focus control id
if (getFocusableId() > 0) {
target.addAttribute("focusid", getFocusableId());
}
// The tab ordering number
if (getTabIndex() > 0) {
target.addAttribute("tabindex", getTabIndex());
}
// If the field is modified, but not committed, set modified attribute
if (isModified()) {
target.addAttribute("modified", true);
}
// Adds the required attribute
if (isRequired()) {
target.addAttribute("required", true);
}
// Paints select attributes
if (isMultiSelect()) {
target.addAttribute("selectmode", "multi");
}
if (isNewItemsAllowed()) {
target.addAttribute("allownewitem", true);
}
boolean needNullSelectOption = false;
if (isNullSelectionAllowed()) {
target.addAttribute("nullselect", true);
needNullSelectOption = (getNullSelectionItemId() == null);
if (!needNullSelectOption) {
target.addAttribute("nullselectitem", true);
}
}
// Constructs selected keys array
String[] selectedKeys;
if (isMultiSelect()) {
selectedKeys = new String[((Set) getValue()).size()];
} else {
selectedKeys = new String[(getValue() == null
&& getNullSelectionItemId() == null ? 0 : 1)];
}
target.addAttribute("filteringmode", getFilteringMode());
// Paints the options and create array of selected id keys
// TODO Also use conventional rendering if lazy loading is not supported
// by terminal
int keyIndex = 0;
/*
* if (!isLazyLoading()) { // Support for external null selection item
* id Collection ids = getItemIds(); if (getNullSelectionItemId() !=
* null && (!ids.contains(getNullSelectionItemId()))) { // Gets the
* option attribute values Object id = getNullSelectionItemId(); String
* key = this.itemIdMapper.key(id); String caption = getItemCaption(id);
* Resource icon = getItemIcon(id); // Paints option
* target.startTag("so"); if (icon != null) {
* target.addAttribute("icon", icon); } target.addAttribute("caption",
* caption); target.addAttribute("nullselection", true);
* target.addAttribute("key", key); if (isSelected(id)) {
* target.addAttribute("selected", true); selectedKeys[keyIndex++] =
* key; } target.endTag("so"); } }
*/
/*
* Iterator i; if (this.filterstring != null) { i =
* this.optionFilter.filter(this.filterstring,
* this.lazyLoadingPageLength, this.page).iterator();
* target.addAttribute("totalMatches", this.optionFilter
* .getMatchCount()); } else { i = getItemIds().iterator(); }
*/
target.startTag("options");
boolean paintNullSelection = needNullSelectOption
&& (currentPage == 0 && (filterstring == null
|| filterstring.equals("") || filterstring.equals("-")));
if (paintNullSelection) {
target.startTag("so");
target.addAttribute("caption", "-");
target.addAttribute("key", "");
target.endTag("so");
}
List options = getFilteredOptions();
if (options.size() > pageLength) {
int first = currentPage * pageLength;
int last = first + pageLength;
if (needNullSelectOption) {
if (currentPage > 0) {
first--;
}
last--;
}
if (options.size() < last) {
last = options.size();
}
options = options.subList(first, last);
}
Iterator i = options.iterator();
// Paints the available selection options from data source
while (i.hasNext()) {
// Gets the option attribute values
Object id = i.next();
String key = itemIdMapper.key(id);
String caption = getItemCaption(id);
Resource icon = getItemIcon(id);
// Paints the option
target.startTag("so");
if (icon != null) {
target.addAttribute("icon", icon);
}
target.addAttribute("caption", caption);
if (id != null && id.equals(getNullSelectionItemId())) {
target.addAttribute("nullselection", true);
}
target.addAttribute("key", key);
if (isSelected(id) && keyIndex < selectedKeys.length) {
target.addAttribute("selected", true);
selectedKeys[keyIndex++] = key;
}
target.endTag("so");
}
target.endTag("options");
target.addAttribute("totalitems", size()
+ (needNullSelectOption ? 1 : 0));
if (filteredOptions != null) {
target.addAttribute("totalMatches", filteredOptions.size()
+ (needNullSelectOption ? 1 : 0));
}
// Paint variables
target.addVariable(this, "selected", selectedKeys);
if (isNewItemsAllowed()) {
target.addVariable(this, "newitem", "");
}
target.addVariable(this, "filter", filterstring);
target.addVariable(this, "page", currentPage);
}
protected List getFilteredOptions() {
if (filterstring == null || filterstring.equals("")
|| filteringMode == FILTERINGMODE_OFF) {
filteredOptions = new LinkedList(getItemIds());
return filteredOptions;
}
if (filterstring.equals(prevfilterstring)) {
return filteredOptions;
}
Collection items;
if (prevfilterstring != null
&& filterstring.startsWith(prevfilterstring)) {
items = filteredOptions;
} else {
items = getItemIds();
}
prevfilterstring = filterstring;
filteredOptions = new LinkedList();
for (Iterator it = items.iterator(); it.hasNext();) {
Object itemId = it.next();
String caption = getItemCaption(itemId);
if (caption == null || caption.equals("")) {
continue;
}
switch (filteringMode) {
case FILTERINGMODE_CONTAINS:
if (caption.indexOf(filterstring) > -1) {
filteredOptions.add(itemId);
}
break;
case FILTERINGMODE_STARTSWITH:
default:
if (caption.startsWith(filterstring)) {
filteredOptions.add(itemId);
}
break;
}
}
return filteredOptions;
}
/**
* Invoked when the value of a variable has changed.
*
* @see com.itmill.toolkit.ui.AbstractComponent#changeVariables(java.lang.Object,
* java.util.Map)
*/
public void changeVariables(Object source, Map variables) {
String newFilter;
if ((newFilter = (String) variables.get("filter")) != null) {
// this is a filter request
currentPage = ((Integer) variables.get("page")).intValue();
filterstring = newFilter;
requestRepaint();
return;
}
// Try to set the property value
// New option entered (and it is allowed)
String newitem = (String) variables.get("newitem");
if (newitem != null && newitem.length() > 0) {
// Checks for readonly
if (isReadOnly()) {
throw new Property.ReadOnlyException();
}
// Adds new option
if (addItem(newitem) != null) {
// Sets the caption property, if used
if (getItemCaptionPropertyId() != null) {
try {
getContainerProperty(newitem,
getItemCaptionPropertyId()).setValue(newitem);
} catch (Property.ConversionException ignored) {
// The conversion exception is safely ignored, the
// caption is
// just missing
}
}
setValue(newitem);
}
}
// Selection change
if (variables.containsKey("selected")) {
String[] ka = (String[]) variables.get("selected");
// Multiselect mode
if (isMultiSelect()) {
// TODO Optimize by adding repaintNotNeeded whan applicaple
// Converts the key-array to id-set
LinkedList s = new LinkedList();
for (int i = 0; i < ka.length; i++) {
Object id = itemIdMapper.get(ka[i]);
if (id != null && containsId(id)) {
s.add(id);
} else if (itemIdMapper.isNewIdKey(ka[i])
&& newitem != null && newitem.length() > 0) {
s.add(newitem);
}
}
// Limits the deselection to the set of visible items
// (non-visible items can not be deselected)
Collection visible = getVisibleItemIds();
if (visible != null) {
Set newsel = (Set) getValue();
if (newsel == null) {
newsel = new HashSet();
} else {
newsel = new HashSet(newsel);
}
newsel.removeAll(visible);
newsel.addAll(s);
setValue(newsel, true);
}
}
// Single select mode
else {
if (ka.length == 0) {
// Allows deselection only if the deselected item is visible
Object current = getValue();
Collection visible = getVisibleItemIds();
if (visible != null && visible.contains(current)) {
setValue(null, true);
}
} else {
Object id = itemIdMapper.get(ka[0]);
if (id != null && id.equals(getNullSelectionItemId())) {
setValue(null, true);
} else if (itemIdMapper.isNewIdKey(ka[0])) {
setValue(newitem);
} else {
setValue(id, true);
}
}
}
}
}
/**
* Gets the component UIDL tag.
*
* @return the Component UIDL tag as string.
*/
public String getTag() {
return "select";
}
public void setFilteringMode(int filteringMode) {
this.filteringMode = filteringMode;
}
public int getFilteringMode() {
return filteringMode;
}
}
|