Search in sources :

Example 1 with SelectMode

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

the class TerraListViewSkin method mouseDown.

@Override
public boolean mouseDown(Component component, Mouse.Button button, int x, int y) {
    boolean consumed = super.mouseDown(component, button, x, y);
    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)) {
            ListView.SelectMode selectMode = listView.getSelectMode();
            if (button == Mouse.Button.LEFT) {
                Keyboard.Modifier commandModifier = Platform.getCommandModifier();
                if (Keyboard.isPressed(Keyboard.Modifier.SHIFT) && selectMode == ListView.SelectMode.MULTI) {
                    Filter<?> disabledItemFilter = listView.getDisabledItemFilter();
                    if (disabledItemFilter == null) {
                        // Select the range
                        ArrayList<Span> selectedRanges = new ArrayList<>();
                        int startIndex = listView.getFirstSelectedIndex();
                        int endIndex = listView.getLastSelectedIndex();
                        Span selectedRange = (itemIndex > startIndex) ? new Span(startIndex, itemIndex) : new Span(itemIndex, endIndex);
                        selectedRanges.add(selectedRange);
                        listView.setSelectedRanges(selectedRanges);
                    }
                } else if (Keyboard.isPressed(commandModifier) && selectMode == ListView.SelectMode.MULTI) {
                    // Toggle the item's selection state
                    if (listView.isItemSelected(itemIndex)) {
                        listView.removeSelectedIndex(itemIndex);
                    } else {
                        listView.addSelectedIndex(itemIndex);
                    }
                } else if (Keyboard.isPressed(commandModifier) && selectMode == ListView.SelectMode.SINGLE) {
                    // Toggle the item's selection state
                    if (listView.isItemSelected(itemIndex)) {
                        listView.setSelectedIndex(-1);
                    } else {
                        listView.setSelectedIndex(itemIndex);
                    }
                } else {
                    if (selectMode != ListView.SelectMode.NONE) {
                        if (listView.isItemSelected(itemIndex)) {
                            selectIndex = itemIndex;
                        } else {
                            listView.setSelectedIndex(itemIndex);
                        }
                    }
                }
            }
        }
    }
    listView.requestFocus();
    return consumed;
}
Also used : ListView(org.apache.pivot.wtk.ListView) Modifier(org.apache.pivot.wtk.Keyboard.Modifier) Keyboard(org.apache.pivot.wtk.Keyboard) ArrayList(org.apache.pivot.collections.ArrayList) SelectMode(org.apache.pivot.wtk.ListView.SelectMode) Span(org.apache.pivot.wtk.Span)

Example 2 with SelectMode

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

the class TerraListViewSkin method keyPressed.

/**
 * {@link KeyCode#UP UP} Selects the previous enabled list item when select
 * mode is not {@link SelectMode#NONE}<br> {@link KeyCode#DOWN DOWN} Selects
 * the next enabled list item when select mode is not
 * {@link SelectMode#NONE}<p> {@link Modifier#SHIFT SHIFT} +
 * {@link KeyCode#UP UP} Increases the selection size by including the
 * previous enabled list item when select mode is {@link SelectMode#MULTI}
 * <br> {@link Modifier#SHIFT SHIFT} + {@link KeyCode#DOWN DOWN} Increases
 * the selection size by including the next enabled list item when select
 * mode is {@link SelectMode#MULTI}
 */
@Override
public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
    boolean consumed = super.keyPressed(component, keyCode, keyLocation);
    ListView listView = (ListView) getComponent();
    ListView.SelectMode selectMode = listView.getSelectMode();
    switch(keyCode) {
        case Keyboard.KeyCode.UP:
            {
                if (selectMode != ListView.SelectMode.NONE) {
                    int index = listView.getFirstSelectedIndex();
                    int count = listView.getListData().getLength();
                    do {
                        index--;
                    } while (index >= 0 && listView.isItemDisabled(index));
                    if (index >= 0) {
                        if (Keyboard.isPressed(Keyboard.Modifier.SHIFT) && listView.getSelectMode() == ListView.SelectMode.MULTI) {
                            listView.addSelectedIndex(index);
                        } else {
                            listView.setSelectedIndex(index);
                        }
                    } else if (!Keyboard.isPressed(Keyboard.Modifier.SHIFT) && listView.getSelectMode() == ListView.SelectMode.MULTI && count == listView.getSelectedItems().getLength()) {
                        index = listView.getLastSelectedIndex();
                        do {
                            index--;
                        } while (index >= 0 && listView.isItemDisabled(index));
                        listView.setSelectedIndex(Math.max(0, index));
                    }
                    consumed = true;
                }
                break;
            }
        case Keyboard.KeyCode.DOWN:
            {
                if (selectMode != ListView.SelectMode.NONE) {
                    int index = listView.getLastSelectedIndex();
                    int count = listView.getListData().getLength();
                    do {
                        index++;
                    } while (index < count && listView.isItemDisabled(index));
                    if (index < count) {
                        if (Keyboard.isPressed(Keyboard.Modifier.SHIFT) && listView.getSelectMode() == ListView.SelectMode.MULTI) {
                            listView.addSelectedIndex(index);
                        } else {
                            listView.setSelectedIndex(index);
                        }
                    } else if (!Keyboard.isPressed(Keyboard.Modifier.SHIFT) && listView.getSelectMode() == ListView.SelectMode.MULTI && count == listView.getSelectedItems().getLength()) {
                        index = 0;
                        do {
                            index++;
                        } while (index < count && listView.isItemDisabled(index));
                        listView.setSelectedIndex(Math.min(count - 1, index));
                    }
                    consumed = true;
                }
                break;
            }
        default:
            {
                break;
            }
    }
    // Clear the highlight
    if (highlightIndex != -1 && listView.getSelectMode() != ListView.SelectMode.NONE && showHighlight && consumed) {
        repaintComponent(getItemBounds(highlightIndex));
    }
    highlightIndex = -1;
    return consumed;
}
Also used : ListView(org.apache.pivot.wtk.ListView) SelectMode(org.apache.pivot.wtk.ListView.SelectMode)

Aggregations

ListView (org.apache.pivot.wtk.ListView)2 SelectMode (org.apache.pivot.wtk.ListView.SelectMode)2 ArrayList (org.apache.pivot.collections.ArrayList)1 Keyboard (org.apache.pivot.wtk.Keyboard)1 Modifier (org.apache.pivot.wtk.Keyboard.Modifier)1 Span (org.apache.pivot.wtk.Span)1