Search in sources :

Example 1 with ListItem

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);
        }
    });
}
Also used : ListView(org.apache.pivot.wtk.ListView) ListViewSelectionListener(org.apache.pivot.wtk.ListViewSelectionListener) Sequence(org.apache.pivot.collections.Sequence) ListItem(org.apache.pivot.wtk.content.ListItem) Span(org.apache.pivot.wtk.Span)

Aggregations

Sequence (org.apache.pivot.collections.Sequence)1 ListView (org.apache.pivot.wtk.ListView)1 ListViewSelectionListener (org.apache.pivot.wtk.ListViewSelectionListener)1 Span (org.apache.pivot.wtk.Span)1 ListItem (org.apache.pivot.wtk.content.ListItem)1