Search in sources :

Example 6 with ListView

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

the class CheckedListViewTest method startup.

@Override
public void startup(Display display, Map<String, String> properties) throws Exception {
    BXMLSerializer serializer = new BXMLSerializer();
    mainWindow = (Window) serializer.readObject(CheckedListViewTest.class, "checked_list_view_test.bxml");
    serializer.bind(this);
    allowMixedStateCheckbox.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            listView.setAllowTriStateCheckmarks(button.isSelected());
            // Not sure why, but changing this setting clears all the checks but doesn't
            // trigger the item state listener (it's documented, but ...)
            selectedItemsLabel.setText("");
        }
    });
    showMixedAsSelectedCheckbox.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            listView.setCheckmarksMixedAsChecked(button.isSelected());
        }
    });
    listView.getComponentKeyListeners().add(new ComponentKeyListener() {

        @Override
        public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
            if (keyCode == Keyboard.KeyCode.DELETE) {
                @SuppressWarnings("unchecked") List<Object> listData = (List<Object>) listView.getListData();
                Sequence<Span> selectedRanges = listView.getSelectedRanges();
                for (int i = selectedRanges.getLength() - 1; i >= 0; i--) {
                    Span selectedRange = selectedRanges.get(i);
                    listData.remove(selectedRange.start, selectedRange.end - selectedRange.start + 1);
                }
            }
            return false;
        }
    });
    listView.getListViewItemStateListeners().add(new ListViewItemStateListener() {

        private void displayCheckedItems(ListView listView) {
            List<?> listData = listView.getListData();
            StringBuffer buf = new StringBuffer();
            for (Integer i : listView.getCheckedIndexes()) {
                if (buf.length() > 0) {
                    buf.append(",");
                }
                Object item = listData.get(i);
                buf.append(item.toString());
            }
            selectedItemsLabel.setText(buf.toString());
        }

        @Override
        public void itemCheckedChanged(ListView listView, int index) {
            displayCheckedItems(listView);
        }

        @Override
        public void itemCheckedStateChanged(ListView listView, int index) {
            displayCheckedItems(listView);
        }
    });
    listView.setItemChecked(0, true);
    listView.setItemChecked(2, true);
    mainWindow.open(display);
}
Also used : ListViewItemStateListener(org.apache.pivot.wtk.ListViewItemStateListener) Keyboard(org.apache.pivot.wtk.Keyboard) Sequence(org.apache.pivot.collections.Sequence) Span(org.apache.pivot.wtk.Span) ButtonPressListener(org.apache.pivot.wtk.ButtonPressListener) ComponentKeyListener(org.apache.pivot.wtk.ComponentKeyListener) ListView(org.apache.pivot.wtk.ListView) Button(org.apache.pivot.wtk.Button) List(org.apache.pivot.collections.List) Component(org.apache.pivot.wtk.Component) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer)

Example 7 with ListView

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

the class TerraListViewSkin method getPreferredHeight.

@Override
public int getPreferredHeight(int width) {
    int preferredHeight = 0;
    ListView listView = (ListView) getComponent();
    @SuppressWarnings("unchecked") List<Object> listData = (List<Object>) listView.getListData();
    ListView.ItemRenderer itemRenderer = listView.getItemRenderer();
    if (variableItemHeight) {
        int clientWidth = width;
        if (listView.getCheckmarksEnabled()) {
            clientWidth = Math.max(clientWidth - (CHECKBOX.getWidth() + checkboxPadding.getWidth()), 0);
        }
        int index = 0;
        for (Object item : listData) {
            itemRenderer.render(item, index++, listView, false, Button.State.UNSELECTED, false, false);
            preferredHeight += itemRenderer.getPreferredHeight(clientWidth);
        }
    } else {
        itemRenderer.render(null, -1, listView, false, Button.State.UNSELECTED, false, false);
        int fixedItemHeightLocal = itemRenderer.getPreferredHeight(-1);
        if (listView.getCheckmarksEnabled()) {
            fixedItemHeightLocal = Math.max(CHECKBOX.getHeight() + checkboxPadding.getHeight(), fixedItemHeightLocal);
        }
        preferredHeight = listData.getLength() * fixedItemHeightLocal;
    }
    return preferredHeight;
}
Also used : ListView(org.apache.pivot.wtk.ListView) ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List)

Example 8 with ListView

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

the class TerraListViewSkin method keyTyped.

/**
 * Select the next enabled list item where the first character of the
 * rendered text matches the typed key (case insensitive).
 */
@Override
public boolean keyTyped(Component component, char character) {
    boolean consumed = super.keyTyped(component, character);
    ListView listView = (ListView) getComponent();
    List<?> listData = listView.getListData();
    ListView.ItemRenderer itemRenderer = listView.getItemRenderer();
    char characterUpper = Character.toUpperCase(character);
    for (int i = listView.getLastSelectedIndex() + 1, n = listData.getLength(); i < n; i++) {
        if (!listView.isItemDisabled(i)) {
            String string = itemRenderer.toString(listData.get(i));
            if (string != null && string.length() > 0) {
                char first = Character.toUpperCase(string.charAt(0));
                if (first == characterUpper) {
                    listView.setSelectedIndex(i);
                    consumed = true;
                    break;
                }
            }
        }
    }
    if (!consumed && wrapSelectNext) {
        for (int i = 0, n = listData.getLength(); i < n; i++) {
            if (!listView.isItemDisabled(i)) {
                String string = itemRenderer.toString(listData.get(i));
                if (string != null && string.length() > 0) {
                    char first = Character.toUpperCase(string.charAt(0));
                    if (first == characterUpper) {
                        listView.setSelectedIndex(i);
                        consumed = true;
                        break;
                    }
                }
            }
        }
    }
    return consumed;
}
Also used : ListView(org.apache.pivot.wtk.ListView)

Example 9 with ListView

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

the class TerraListViewSkin method mouseClick.

@Override
public boolean mouseClick(Component component, Mouse.Button button, int x, int y, int count) {
    boolean consumed = super.mouseClick(component, button, x, y, count);
    ListView listView = (ListView) getComponent();
    int itemIndex = getItemAt(y);
    if (itemIndex != -1 && !listView.isItemDisabled(itemIndex)) {
        if (listView.getCheckmarksEnabled() && !listView.isCheckmarkDisabled(itemIndex) && getCheckboxBounds(itemIndex).contains(x, y)) {
            if (listView.getAllowTriStateCheckmarks()) {
                Button.State currentState = listView.getItemCheckmarkState(itemIndex);
                Button.State nextState = currentState;
                switch(currentState) {
                    case UNSELECTED:
                        if (listView.getCheckmarksMixedAsChecked()) {
                            nextState = Button.State.SELECTED;
                        } else {
                            nextState = Button.State.MIXED;
                        }
                        break;
                    case MIXED:
                        nextState = Button.State.SELECTED;
                        break;
                    case SELECTED:
                        nextState = Button.State.UNSELECTED;
                        break;
                }
                listView.setItemCheckmarkState(itemIndex, nextState);
            } else {
                listView.setItemChecked(itemIndex, !listView.isItemChecked(itemIndex));
            }
        } else {
            if (selectIndex != -1 && count == 1 && button == Mouse.Button.LEFT) {
                ListView.ItemEditor itemEditor = listView.getItemEditor();
                if (itemEditor != null) {
                    if (itemEditor.isEditing()) {
                        itemEditor.endEdit(true);
                    }
                    itemEditor.beginEdit(listView, selectIndex);
                }
            }
        }
    }
    selectIndex = -1;
    return consumed;
}
Also used : ListView(org.apache.pivot.wtk.ListView) Button(org.apache.pivot.wtk.Button)

Example 10 with ListView

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

the class TerraListViewSkin method getPreferredWidth.

@Override
public int getPreferredWidth(int height) {
    int preferredWidth = 0;
    ListView listView = (ListView) getComponent();
    @SuppressWarnings("unchecked") List<Object> listData = (List<Object>) listView.getListData();
    ListView.ItemRenderer itemRenderer = listView.getItemRenderer();
    int index = 0;
    for (Object item : listData) {
        itemRenderer.render(item, index++, listView, false, Button.State.UNSELECTED, false, false);
        preferredWidth = Math.max(preferredWidth, itemRenderer.getPreferredWidth(-1));
    }
    if (listView.getCheckmarksEnabled()) {
        preferredWidth += CHECKBOX.getWidth() + checkboxPadding.getWidth();
    }
    return preferredWidth;
}
Also used : ListView(org.apache.pivot.wtk.ListView) ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List)

Aggregations

ListView (org.apache.pivot.wtk.ListView)28 ArrayList (org.apache.pivot.collections.ArrayList)10 Button (org.apache.pivot.wtk.Button)10 List (org.apache.pivot.collections.List)8 ButtonPressListener (org.apache.pivot.wtk.ButtonPressListener)7 PushButton (org.apache.pivot.wtk.PushButton)6 Span (org.apache.pivot.wtk.Span)6 ListViewSelectionListener (org.apache.pivot.wtk.ListViewSelectionListener)4 Sheet (org.apache.pivot.wtk.Sheet)4 SheetCloseListener (org.apache.pivot.wtk.SheetCloseListener)4 File (java.io.File)3 Bounds (org.apache.pivot.wtk.Bounds)3 BoxPane (org.apache.pivot.wtk.BoxPane)3 Component (org.apache.pivot.wtk.Component)3 FileBrowserSheet (org.apache.pivot.wtk.FileBrowserSheet)3 Frame (org.apache.pivot.wtk.Frame)3 Color (java.awt.Color)2 IOException (java.io.IOException)2 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)2 Sequence (org.apache.pivot.collections.Sequence)2