Search in sources :

Example 16 with Span

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

Example 17 with Span

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

the class TerraTableViewSkin method mouseDown.

@Override
public boolean mouseDown(Component component, Mouse.Button button, int x, int y) {
    boolean consumed = super.mouseDown(component, button, x, y);
    TableView tableView = (TableView) getComponent();
    int rowIndex = getRowAt(y);
    if (rowIndex >= 0 && !tableView.isRowDisabled(rowIndex)) {
        TableView.SelectMode selectMode = tableView.getSelectMode();
        if (button == Mouse.Button.LEFT) {
            Keyboard.Modifier commandModifier = Platform.getCommandModifier();
            if (Keyboard.isPressed(Keyboard.Modifier.SHIFT) && selectMode == TableView.SelectMode.MULTI) {
                Filter<?> disabledRowFilter = tableView.getDisabledRowFilter();
                if (disabledRowFilter == null) {
                    // Select the range
                    int startIndex = tableView.getFirstSelectedIndex();
                    int endIndex = tableView.getLastSelectedIndex();
                    // indicated row
                    if (startIndex == -1) {
                        tableView.addSelectedIndex(rowIndex);
                    } else {
                        // otherwise select the range of rows
                        Span selectedRange = (rowIndex > startIndex) ? new Span(startIndex, rowIndex) : new Span(rowIndex, endIndex);
                        ArrayList<Span> selectedRanges = new ArrayList<>();
                        selectedRanges.add(selectedRange);
                        tableView.setSelectedRanges(selectedRanges);
                    }
                }
            } else if (Keyboard.isPressed(commandModifier) && selectMode == TableView.SelectMode.MULTI) {
                // Toggle the item's selection state
                if (tableView.isRowSelected(rowIndex)) {
                    tableView.removeSelectedIndex(rowIndex);
                } else {
                    tableView.addSelectedIndex(rowIndex);
                }
            } else if (Keyboard.isPressed(commandModifier) && selectMode == TableView.SelectMode.SINGLE) {
                // Toggle the item's selection state
                if (tableView.isRowSelected(rowIndex)) {
                    tableView.setSelectedIndex(-1);
                } else {
                    tableView.setSelectedIndex(rowIndex);
                }
            } else {
                if (selectMode != TableView.SelectMode.NONE) {
                    if (!tableView.isRowSelected(rowIndex)) {
                        tableView.setSelectedIndex(rowIndex);
                    }
                    selectIndex = rowIndex;
                }
            }
        }
    }
    tableView.requestFocus();
    if (editOnMouseDown) {
        if (selectIndex != -1 && button == Mouse.Button.LEFT) {
            TableView.RowEditor rowEditor = tableView.getRowEditor();
            if (rowEditor != null) {
                if (rowEditor.isEditing()) {
                    rowEditor.endEdit(true);
                }
                rowEditor.beginEdit(tableView, selectIndex, getColumnAt(x));
            }
        }
    }
    return consumed;
}
Also used : Modifier(org.apache.pivot.wtk.Keyboard.Modifier) SelectMode(org.apache.pivot.wtk.TableView.SelectMode) Keyboard(org.apache.pivot.wtk.Keyboard) ArrayList(org.apache.pivot.collections.ArrayList) Span(org.apache.pivot.wtk.Span) TableView(org.apache.pivot.wtk.TableView)

Example 18 with Span

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

the class TerraTableViewSkin method selectedRangesChanged.

@Override
public void selectedRangesChanged(TableView tableView, Sequence<Span> previousSelectedRanges) {
    if (previousSelectedRanges != null && previousSelectedRanges != tableView.getSelectedRanges()) {
        if (tableView.isValid()) {
            // Repaint the area occupied by the previous selection
            if (previousSelectedRanges.getLength() > 0) {
                int rangeStart = previousSelectedRanges.get(0).start;
                int rangeEnd = previousSelectedRanges.get(previousSelectedRanges.getLength() - 1).end;
                Bounds previousSelectionBounds = getRowBounds(rangeStart);
                previousSelectionBounds = previousSelectionBounds.union(getRowBounds(rangeEnd));
                repaintComponent(previousSelectionBounds);
            }
            // Repaint the area occupied by the current selection
            Sequence<Span> selectedRanges = tableView.getSelectedRanges();
            if (selectedRanges.getLength() > 0) {
                int rangeStart = selectedRanges.get(0).start;
                int rangeEnd = selectedRanges.get(selectedRanges.getLength() - 1).end;
                Bounds selectionBounds = getRowBounds(rangeStart);
                selectionBounds = selectionBounds.union(getRowBounds(rangeEnd));
                repaintComponent(selectionBounds);
                // Ensure that the selection is visible
                Bounds visibleSelectionBounds = tableView.getVisibleArea(selectionBounds);
                if (visibleSelectionBounds != null && visibleSelectionBounds.height < selectionBounds.height) {
                    // TODO Repainting the entire component is a workaround
                    // for PIVOT-490
                    repaintComponent();
                    tableView.scrollAreaToVisible(selectionBounds);
                }
            }
        } else {
            validateSelection = true;
        }
    }
}
Also used : Bounds(org.apache.pivot.wtk.Bounds) Span(org.apache.pivot.wtk.Span)

Example 19 with Span

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

the class TerraVFSBrowserSkin method install.

@Override
public void install(Component component) {
    super.install(component);
    final VFSBrowser fileBrowser = (VFSBrowser) component;
    BXMLSerializer bxmlSerializer = new BXMLSerializer();
    try {
        content = (Component) bxmlSerializer.readObject(TerraVFSBrowserSkin.class, "terra_vfs_browser_skin.bxml", true);
    } catch (IOException exception) {
        throw new RuntimeException(exception);
    } catch (SerializationException exception) {
        throw new RuntimeException(exception);
    }
    fileBrowser.add(content);
    bxmlSerializer.bind(this, TerraVFSBrowserSkin.class);
    // Notify all the renderers of which component they are dealing with
    ((FileRenderer) pathListButton.getDataRenderer()).setFileBrowser(fileBrowser);
    ((FileRenderer) pathListButton.getItemRenderer()).setFileBrowser(fileBrowser);
    for (TableView.Column col : fileTableView.getColumns()) {
        ((FileRenderer) col.getCellRenderer()).setFileBrowser(fileBrowser);
    }
    homeDirectory = fileBrowser.getHomeDirectory();
    driveListButton.getListButtonSelectionListeners().add(new ListButtonSelectionListener() {

        @Override
        public void selectedItemChanged(ListButton listButton, Object previousSelectedItem) {
            if (previousSelectedItem != null) {
                FileObject drive = (FileObject) listButton.getSelectedItem();
                try {
                    if (drive.isReadable()) {
                        fileBrowser.setRootDirectory(drive);
                    } else {
                        refreshRoots = true;
                        listButton.setSelectedItem(previousSelectedItem);
                    }
                } catch (FileSystemException fse) {
                    throw new RuntimeException(fse);
                }
            }
        }
    });
    pathListButton.getListButtonSelectionListeners().add(new ListButtonSelectionListener() {

        @Override
        public void selectedItemChanged(ListButton listButton, Object previousSelectedItem) {
            FileObject ancestorDirectory = (FileObject) listButton.getSelectedItem();
            if (ancestorDirectory != null) {
                try {
                    fileBrowser.setRootDirectory(ancestorDirectory);
                } catch (FileSystemException fse) {
                    throw new RuntimeException(fse);
                }
            }
        }
    });
    goUpButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            try {
                FileObject rootDirectory = fileBrowser.getRootDirectory();
                FileObject parentDirectory = rootDirectory.getParent();
                fileBrowser.setRootDirectory(parentDirectory);
            } catch (FileSystemException fse) {
                throw new RuntimeException(fse);
            }
        }
    });
    newFolderButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
        // TODO
        }
    });
    goHomeButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            try {
                fileBrowser.setRootDirectory(fileBrowser.getHomeDirectory());
            } catch (FileSystemException fse) {
                throw new RuntimeException(fse);
            }
        }
    });
    /**
     * {@link KeyCode#DOWN DOWN} Transfer focus to the file list and select
     * the first item.<br> {@link KeyCode#ESCAPE ESCAPE} Clear the search
     * field.
     */
    searchTextInput.getComponentKeyListeners().add(new ComponentKeyListener() {

        @Override
        public boolean keyPressed(Component componentArgument, int keyCode, Keyboard.KeyLocation keyLocation) {
            boolean consumed = false;
            if (keyCode == Keyboard.KeyCode.ESCAPE) {
                searchTextInput.setText("");
                consumed = true;
            } else if (keyCode == Keyboard.KeyCode.DOWN) {
                if (fileTableView.getTableData().getLength() > 0) {
                    fileTableView.setSelectedIndex(0);
                    fileTableView.requestFocus();
                }
            }
            return consumed;
        }
    });
    searchTextInput.getTextInputContentListeners().add(new TextInputContentListener() {

        @Override
        public void textChanged(TextInput textInput) {
            refreshFileList();
        }
    });
    fileTableView.getTableViewSelectionListeners().add(new TableViewSelectionListener() {

        @Override
        public void selectedRangeAdded(TableView tableView, int rangeStart, int rangeEnd) {
            if (!updatingSelection) {
                updatingSelection = true;
                try {
                    for (int i = rangeStart; i <= rangeEnd; i++) {
                        @SuppressWarnings("unchecked") List<FileObject> files = (List<FileObject>) fileTableView.getTableData();
                        FileObject file = files.get(i);
                        fileBrowser.addSelectedFile(file);
                    }
                } catch (FileSystemException fse) {
                    throw new RuntimeException(fse);
                }
                updatingSelection = false;
            }
        }

        @Override
        public void selectedRangeRemoved(TableView tableView, int rangeStart, int rangeEnd) {
            if (!updatingSelection) {
                updatingSelection = true;
                for (int i = rangeStart; i <= rangeEnd; i++) {
                    @SuppressWarnings("unchecked") List<FileObject> files = (List<FileObject>) fileTableView.getTableData();
                    FileObject file = files.get(i);
                    fileBrowser.removeSelectedFile(file);
                }
                updatingSelection = false;
            }
        }

        @Override
        public void selectedRangesChanged(TableView tableView, Sequence<Span> previousSelectedRanges) {
            if (!updatingSelection && previousSelectedRanges != null) {
                updatingSelection = true;
                @SuppressWarnings("unchecked") Sequence<FileObject> files = (Sequence<FileObject>) tableView.getSelectedRows();
                for (int i = 0, n = files.getLength(); i < n; i++) {
                    FileObject file = files.get(i);
                    files.update(i, file);
                }
                try {
                    fileBrowser.setSelectedFiles(files);
                } catch (FileSystemException fse) {
                    throw new RuntimeException(fse);
                }
                updatingSelection = false;
            }
        }

        @Override
        public void selectedRowChanged(TableView tableView, Object previousSelectedRow) {
        // No-op
        }
    });
    fileTableView.getTableViewSortListeners().add(new TableViewSortListener() {

        @Override
        public void sortChanged(TableView tableView) {
            TableView.SortDictionary sort = fileTableView.getSort();
            if (!sort.isEmpty()) {
                Dictionary.Pair<String, SortDirection> pair = fileTableView.getSort().get(0);
                @SuppressWarnings("unchecked") List<FileObject> files = (List<FileObject>) fileTableView.getTableData();
                files.setComparator(getFileComparator(pair.key, pair.value));
            }
        }
    });
    fileTableView.getComponentMouseButtonListeners().add(new ComponentMouseButtonListener() {

        private int index = -1;

        @Override
        public boolean mouseClick(Component componentArgument, Mouse.Button button, int x, int y, int count) {
            boolean consumed = false;
            if (count == 1) {
                index = fileTableView.getRowAt(y);
            } else if (count == 2) {
                int indexLocal = fileTableView.getRowAt(y);
                if (indexLocal != -1 && indexLocal == this.index && fileTableView.isRowSelected(indexLocal)) {
                    FileObject file = (FileObject) fileTableView.getTableData().get(indexLocal);
                    try {
                        if (file.getName().getType() == FileType.FOLDER) {
                            fileBrowser.setRootDirectory(file);
                            consumed = true;
                        }
                    } catch (FileSystemException fse) {
                        throw new RuntimeException(fse);
                    }
                }
            }
            return consumed;
        }
    });
    fileBrowser.setFocusTraversalPolicy(new IndexFocusTraversalPolicy() {

        @Override
        public Component getNextComponent(Container container, Component componentArgument, FocusTraversalDirection direction) {
            Component nextComponent;
            if (componentArgument == null) {
                nextComponent = fileTableView;
            } else {
                nextComponent = super.getNextComponent(container, componentArgument, direction);
            }
            return nextComponent;
        }
    });
    fileTableView.setSort(TableViewFileRenderer.NAME_KEY, SortDirection.ASCENDING);
    fileTableView.getComponentTooltipListeners().add(new ComponentTooltipListener() {

        @Override
        public void tooltipTriggered(Component comp, int x, int y) {
            // Check that we are on the first column.
            if (fileTableView.getColumnAt(x) != 0) {
                return;
            }
            // Gets the underlying file
            int row = fileTableView.getRowAt(y);
            if (row < 0) {
                return;
            }
            FileObject file = (FileObject) fileTableView.getTableData().get(row);
            // Construct and show the tooltip.
            final Tooltip tooltip = new Tooltip();
            String text = null;
            if (file != null) {
                text = file.getName().getBaseName();
            }
            if (text == null || text.isEmpty()) {
                return;
            }
            TextArea toolTipTextArea = new TextArea();
            toolTipTextArea.setText(text);
            toolTipTextArea.getStyles().put(Style.wrapText, true);
            toolTipTextArea.getStyles().put(Style.backgroundColor, null);
            tooltip.setContent(toolTipTextArea);
            Point location = comp.getDisplay().getMouseLocation();
            x = location.x;
            y = location.y;
            // Ensure that the tooltip stays on screen
            Display display = comp.getDisplay();
            int tooltipHeight = tooltip.getPreferredHeight();
            if (y + tooltipHeight > display.getHeight()) {
                y -= tooltipHeight;
            }
            int tooltipXOffset = 16;
            int padding = 15;
            toolTipTextArea.setMaximumWidth(display.getWidth() - (x + tooltipXOffset + padding));
            tooltip.setLocation(x + tooltipXOffset, y);
            tooltip.open(comp.getWindow());
        }
    });
    rootDirectoryChanged(fileBrowser, null);
    selectedFilesChanged(fileBrowser, null);
}
Also used : FocusTraversalDirection(org.apache.pivot.wtk.FocusTraversalDirection) ComponentTooltipListener(org.apache.pivot.wtk.ComponentTooltipListener) TextArea(org.apache.pivot.wtk.TextArea) ListButtonSelectionListener(org.apache.pivot.wtk.ListButtonSelectionListener) TableViewSortListener(org.apache.pivot.wtk.TableViewSortListener) Span(org.apache.pivot.wtk.Span) ButtonPressListener(org.apache.pivot.wtk.ButtonPressListener) FileSystemException(org.apache.commons.vfs2.FileSystemException) TextInputContentListener(org.apache.pivot.wtk.TextInputContentListener) Container(org.apache.pivot.wtk.Container) PushButton(org.apache.pivot.wtk.PushButton) ListButton(org.apache.pivot.wtk.ListButton) Button(org.apache.pivot.wtk.Button) ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) FileObject(org.apache.commons.vfs2.FileObject) Component(org.apache.pivot.wtk.Component) TextInput(org.apache.pivot.wtk.TextInput) TableViewSelectionListener(org.apache.pivot.wtk.TableViewSelectionListener) VFSBrowser(org.apache.pivot.wtk.VFSBrowser) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer) TableView(org.apache.pivot.wtk.TableView) SerializationException(org.apache.pivot.serialization.SerializationException) Keyboard(org.apache.pivot.wtk.Keyboard) Tooltip(org.apache.pivot.wtk.Tooltip) IOException(java.io.IOException) Sequence(org.apache.pivot.collections.Sequence) Point(org.apache.pivot.wtk.Point) Point(org.apache.pivot.wtk.Point) ListButton(org.apache.pivot.wtk.ListButton) ComponentKeyListener(org.apache.pivot.wtk.ComponentKeyListener) Mouse(org.apache.pivot.wtk.Mouse) FileObject(org.apache.commons.vfs2.FileObject) ComponentMouseButtonListener(org.apache.pivot.wtk.ComponentMouseButtonListener) Display(org.apache.pivot.wtk.Display)

Example 20 with Span

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

Aggregations

Span (org.apache.pivot.wtk.Span)25 ArrayList (org.apache.pivot.collections.ArrayList)11 List (org.apache.pivot.collections.List)10 Button (org.apache.pivot.wtk.Button)7 Keyboard (org.apache.pivot.wtk.Keyboard)7 ButtonPressListener (org.apache.pivot.wtk.ButtonPressListener)6 Component (org.apache.pivot.wtk.Component)6 ListView (org.apache.pivot.wtk.ListView)6 TableView (org.apache.pivot.wtk.TableView)6 Sequence (org.apache.pivot.collections.Sequence)5 Bounds (org.apache.pivot.wtk.Bounds)5 ComponentKeyListener (org.apache.pivot.wtk.ComponentKeyListener)5 IOException (java.io.IOException)4 PushButton (org.apache.pivot.wtk.PushButton)4 TableViewSelectionListener (org.apache.pivot.wtk.TableViewSelectionListener)4 TableViewSortListener (org.apache.pivot.wtk.TableViewSortListener)4 TextInput (org.apache.pivot.wtk.TextInput)4 TextSpan (org.apache.pivot.wtk.text.TextSpan)4 File (java.io.File)3 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)3