Search in sources :

Example 1 with ListViewSelectionListener

use of org.apache.pivot.wtk.ListViewSelectionListener in project pivot by apache.

the class Pivot811 method startup.

@Override
public void startup(final Display displayArgument, Map<String, String> properties) throws Exception {
    this.display = displayArgument;
    Frame listFrame = new Frame();
    listFrame.setTitle("List Frame");
    listFrame.setPreferredSize(400, 300);
    listFrame.setLocation(20, 20);
    listFrame.getStyles().put(Style.padding, Insets.NONE);
    BoxPane boxPane = new BoxPane();
    boxPane.getStyles().put(Style.fill, true);
    boxPane.setOrientation(Orientation.VERTICAL);
    listFrame.setContent(boxPane);
    Label infoLabel = new Label("Double click on a list item to open a detail frame");
    boxPane.add(infoLabel);
    ScrollPane scrollPane = new ScrollPane();
    scrollPane.setHorizontalScrollBarPolicy(ScrollBarPolicy.FILL);
    scrollPane.setVerticalScrollBarPolicy(ScrollBarPolicy.FILL_TO_CAPACITY);
    // workaround for pivot-738,
    scrollPane.setRepaintAllViewport(true);
    // needed only in in some cases
    boxPane.add(scrollPane);
    final ListView listView = new ListView();
    List<String> listData = new ArrayList<>();
    for (int i = 0; i < 50; ++i) {
        listData.add("List Item " + i);
    }
    listView.setListData(listData);
    scrollPane.setView(listView);
    listView.getListViewSelectionListeners().add(new ListViewSelectionListener() {

        @Override
        public void selectedItemChanged(ListView listViewArgument, Object previousSelectedItem) {
            System.out.println("selectedItemChanged : " + listViewArgument.getSelectedItem());
        }
    });
    listView.getComponentMouseButtonListeners().add(new ComponentMouseButtonListener() {

        @Override
        public boolean mouseClick(Component component, Mouse.Button button, int x, int y, int count) {
            System.out.println("mouseClick : " + count);
            if (count == 2) {
                System.out.println("double click, now open a detail frame");
                Frame detailFrame = new Frame();
                detailFrame.setTitle("Detail Frame");
                detailFrame.setPreferredSize(400, 300);
                int selectedIndex = listView.getSelectedIndex();
                detailFrame.setLocation(80 + (selectedIndex * 10), 80 + (selectedIndex * 10));
                detailFrame.getStyles().put(Style.padding, Insets.NONE);
                BoxPane boxPaneLocal = new BoxPane();
                boxPaneLocal.getStyles().put(Style.fill, true);
                boxPaneLocal.setOrientation(Orientation.VERTICAL);
                detailFrame.setContent(boxPaneLocal);
                String selectedItem = listView.getSelectedItem().toString();
                Label label = new Label("Selected Item is \"" + selectedItem + "\"");
                boxPaneLocal.add(label);
                // spacer
                boxPaneLocal.add(new Label(""));
                boxPaneLocal.add(new Label("Click inside the text input to focus it"));
                TextInput textInput = new TextInput();
                textInput.setText("Focusable component");
                // workaround for pivot-811:
                boxPaneLocal.add(textInput);
                // add a focusable element
                // inside the frame
                detailFrame.open(displayArgument);
                // workaround for pivot-811: force the focus on the first
                // focusable element inside the frame
                detailFrame.requestFocus();
            // textInput.requestFocus(); // or use this ...
            }
            return true;
        }
    });
    listFrame.open(displayArgument);
    listView.setSelectedIndex(0);
    listView.requestFocus();
}
Also used : Frame(org.apache.pivot.wtk.Frame) Label(org.apache.pivot.wtk.Label) ArrayList(org.apache.pivot.collections.ArrayList) ListView(org.apache.pivot.wtk.ListView) Mouse(org.apache.pivot.wtk.Mouse) BoxPane(org.apache.pivot.wtk.BoxPane) ScrollPane(org.apache.pivot.wtk.ScrollPane) ComponentMouseButtonListener(org.apache.pivot.wtk.ComponentMouseButtonListener) ListViewSelectionListener(org.apache.pivot.wtk.ListViewSelectionListener) Component(org.apache.pivot.wtk.Component) TextInput(org.apache.pivot.wtk.TextInput)

Example 2 with ListViewSelectionListener

use of org.apache.pivot.wtk.ListViewSelectionListener 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)

Example 3 with ListViewSelectionListener

use of org.apache.pivot.wtk.ListViewSelectionListener in project pivot by apache.

the class Pivot718 method controlList.

private void controlList(BXMLSerializer bxmlSerializer) {
    listDelButton = (PushButton) bxmlSerializer.getNamespace().get("listDelButton");
    list = (ListView) bxmlSerializer.getNamespace().get("list");
    list.getListViewSelectionListeners().add(new ListViewSelectionListener() {

        @Override
        public void selectedRangeAdded(ListView listView, int rangeStart, int rangeEnd) {
            System.out.println("selectedRangeAdded");
        }

        @Override
        public void selectedRangeRemoved(ListView listView, int rangeStart, int rangeEnd) {
            System.out.println("selectedRangeRemoved");
        }

        @Override
        public void selectedRangesChanged(ListView listView, Sequence<Span> previousSelectedRanges) {
            System.out.println("selectedRangesChanged");
        }

        @Override
        public void selectedItemChanged(ListView listView, Object previousSelectedItem) {
            System.out.println("selectedItemChanged :::" + listView.getSelectedItem());
        }
    });
    listDelButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            Object x = list.getSelectedItem();
            System.out.println("delete :: " + x);
            // List data = list.getListData();
            @SuppressWarnings("unchecked") List<Object> data = (List<Object>) list.getListData();
            data.remove(x);
        }
    });
}
Also used : ButtonPressListener(org.apache.pivot.wtk.ButtonPressListener) ListView(org.apache.pivot.wtk.ListView) PushButton(org.apache.pivot.wtk.PushButton) Button(org.apache.pivot.wtk.Button) List(org.apache.pivot.collections.List) ListViewSelectionListener(org.apache.pivot.wtk.ListViewSelectionListener) Span(org.apache.pivot.wtk.Span)

Example 4 with ListViewSelectionListener

use of org.apache.pivot.wtk.ListViewSelectionListener in project pivot by apache.

the class ListViewSelectionTest method startup.

@Override
public void startup(Display display, Map<String, String> properties) {
    ArrayList<Span> selectedRanges = new ArrayList<>();
    selectedRanges.add(new Span(0, 0));
    listView.setSelectedRanges(selectedRanges);
    dumpSelection();
    listView.addSelectedRange(new Span(4, 4));
    dumpSelection();
    listView.addSelectedRange(new Span(2, 2));
    dumpSelection();
    listView.addSelectedRange(new Span(0, 4));
    dumpSelection();
    selectedRanges.clear();
    selectedRanges.add(new Span(1, 1));
    selectedRanges.add(new Span(3, 3));
    listView.setSelectedRanges(selectedRanges);
    dumpSelection();
    listView.addSelectedRange(new Span(0, 4));
    dumpSelection();
    listView.removeSelectedRange(new Span(2, 2));
    dumpSelection();
    listView.removeSelectedRange(new Span(4, 4));
    dumpSelection();
    listView.removeSelectedRange(new Span(0, 0));
    dumpSelection();
    listView.removeSelectedRange(new Span(1, 3));
    dumpSelection();
    selectedRanges.clear();
    selectedRanges.add(new Span(4, 6));
    listView.setSelectedRanges(selectedRanges);
    dumpSelection();
    listView.addSelectedRange(new Span(2, 5));
    dumpSelection();
    listView.addSelectedRange(new Span(4, 8));
    dumpSelection();
    verifySelection(0);
    verifySelection(4);
    verifySelection(6);
    verifySelection(8);
    listView.removeSelectedRange(new Span(8, 12));
    dumpSelection();
    verifySelection(8);
    listView.removeSelectedRange(new Span(0, 4));
    dumpSelection();
    verifySelection(4);
    listView.getListViewSelectionListeners().add(new ListViewSelectionListener() {

        @Override
        public void selectedRangesChanged(ListView listViewArgument, Sequence<Span> previousSelectedRanges) {
            System.out.println("Selection changed");
        }
    });
    listView.setSelectedIndex(2);
    listView.getListData().remove(2, 1);
}
Also used : ListView(org.apache.pivot.wtk.ListView) ArrayList(org.apache.pivot.collections.ArrayList) ListViewSelectionListener(org.apache.pivot.wtk.ListViewSelectionListener) Span(org.apache.pivot.wtk.Span)

Aggregations

ListView (org.apache.pivot.wtk.ListView)4 ListViewSelectionListener (org.apache.pivot.wtk.ListViewSelectionListener)4 Span (org.apache.pivot.wtk.Span)3 ArrayList (org.apache.pivot.collections.ArrayList)2 List (org.apache.pivot.collections.List)1 Sequence (org.apache.pivot.collections.Sequence)1 BoxPane (org.apache.pivot.wtk.BoxPane)1 Button (org.apache.pivot.wtk.Button)1 ButtonPressListener (org.apache.pivot.wtk.ButtonPressListener)1 Component (org.apache.pivot.wtk.Component)1 ComponentMouseButtonListener (org.apache.pivot.wtk.ComponentMouseButtonListener)1 Frame (org.apache.pivot.wtk.Frame)1 Label (org.apache.pivot.wtk.Label)1 Mouse (org.apache.pivot.wtk.Mouse)1 PushButton (org.apache.pivot.wtk.PushButton)1 ScrollPane (org.apache.pivot.wtk.ScrollPane)1 TextInput (org.apache.pivot.wtk.TextInput)1 ListItem (org.apache.pivot.wtk.content.ListItem)1