use of org.apache.pivot.wtk.content.ListItem in project pivot by apache.
the class ListViews method initialize.
@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
selectionLabel = (Label) namespace.get("selectionLabel");
listView = (ListView) namespace.get("listView");
listView.getListViewSelectionListeners().add(new ListViewSelectionListener() {
@Override
public void selectedRangeAdded(ListView listViewArgument, int rangeStart, int rangeEnd) {
updateSelection(listViewArgument);
}
@Override
public void selectedRangeRemoved(ListView listViewArgument, int rangeStart, int rangeEnd) {
updateSelection(listViewArgument);
}
@Override
public void selectedRangesChanged(ListView listViewArgument, Sequence<Span> previousSelectedRanges) {
if (previousSelectedRanges != null && previousSelectedRanges != listViewArgument.getSelectedRanges()) {
updateSelection(listViewArgument);
}
}
@Override
public void selectedItemChanged(ListView listViewArgument, Object previousSelectedItem) {
// No-op
}
private void updateSelection(ListView listViewArgument) {
// TODO: in future use StringBuffer instead ...
String selectionText = "";
Sequence<Span> selectedRanges = listViewArgument.getSelectedRanges();
for (int i = 0, n = selectedRanges.getLength(); i < n; i++) {
Span selectedRange = selectedRanges.get(i);
for (int j = selectedRange.start; j <= selectedRange.end; j++) {
if (selectionText.length() > 0) {
selectionText += ", ";
}
Object item = listViewArgument.getListData().get(j);
String text;
if (item instanceof ListItem) {
// item is a listItem
// (for example because
// it has an image)
text = ((ListItem) item).getText();
} else {
// item is a standard item for listData
text = item.toString();
}
selectionText += text;
}
}
selectionLabel.setText(selectionText);
}
});
}
Aggregations