Search in sources :

Example 36 with List

use of org.apache.pivot.collections.List in project pivot by apache.

the class TerraTableViewSkin method getVariableRowHeight.

protected int getVariableRowHeight(int rowIndex, ArrayList<Integer> columnWidthsArgument) {
    TableView tableView = (TableView) getComponent();
    @SuppressWarnings("unchecked") List<Object> tableData = (List<Object>) tableView.getTableData();
    TableView.ColumnSequence columns = tableView.getColumns();
    Object rowData = tableData.get(rowIndex);
    int rowHeight = 0;
    for (int i = 0, n = columns.getLength(); i < n; i++) {
        TableView.Column column = columns.get(i);
        TableView.CellRenderer cellRenderer = column.getCellRenderer();
        cellRenderer.render(rowData, rowIndex, i, tableView, column.getName(), false, false, false);
        rowHeight = Math.max(rowHeight, cellRenderer.getPreferredHeight(columnWidthsArgument.get(i).intValue()));
    }
    return rowHeight;
}
Also used : ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) TableView(org.apache.pivot.wtk.TableView)

Example 37 with List

use of org.apache.pivot.collections.List in project pivot by apache.

the class TerraTableViewSkin method getRowAt.

// Table view skin methods
@Override
public int getRowAt(int y) {
    Utils.checkNonNegative(y, "y");
    TableView tableView = (TableView) getComponent();
    int rowIndex;
    if (variableRowHeight) {
        if (y == 0) {
            rowIndex = 0;
        } else {
            rowIndex = ArrayList.binarySearch(rowBoundaries, Integer.valueOf(y));
            if (rowIndex < 0) {
                rowIndex = -(rowIndex + 1);
            }
        }
    } else {
        rowIndex = (y / (fixedRowHeight + 1));
    }
    @SuppressWarnings("unchecked") List<Object> tableData = (List<Object>) tableView.getTableData();
    if (rowIndex >= tableData.getLength()) {
        rowIndex = -1;
    }
    return rowIndex;
}
Also used : ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) TableView(org.apache.pivot.wtk.TableView)

Example 38 with List

use of org.apache.pivot.collections.List in project pivot by apache.

the class TerraTheme method load.

private void load(URL location) {
    Utils.checkNull(location, "location");
    try (InputStream inputStream = location.openStream()) {
        JSONSerializer serializer = new JSONSerializer();
        @SuppressWarnings("unchecked") Map<String, ?> properties = (Map<String, ?>) serializer.readObject(inputStream);
        font = Font.decode((String) properties.get(FONT_PROPERTY));
        String defaultStylesName = (String) properties.get(DEFAULT_STYLES_PROPERTY);
        if (defaultStylesName != null && !defaultStylesName.isEmpty()) {
            defaultStylesFile = defaultStylesName;
        }
        String namedStylesName = (String) properties.get(NAMED_STYLES_PROPERTY);
        if (namedStylesName != null && !namedStylesName.isEmpty()) {
            namedStylesFile = namedStylesName;
        }
        @SuppressWarnings("unchecked") List<String> colorCodes = (List<String>) properties.get(COLORS_PROPERTY);
        numberOfPaletteColors = colorCodes.getLength();
        int numberOfColors = numberOfPaletteColors * 3;
        colors = new ArrayList<>(numberOfColors);
        Double mult = (Double) properties.get(COLOR_MULTIPLIER_PROPERTY);
        if (mult != null) {
            colorMultiplier = mult.floatValue();
        }
        themeIsDark = properties.getBoolean(THEME_IS_DARK_PROPERTY, false);
        themeIsFlat = properties.getBoolean(THEME_IS_FLAT_PROPERTY, false);
        transitionEnabled = properties.getBoolean(TRANSITION_ENABLED_PROPERTY, true);
        for (String colorCode : colorCodes) {
            Color baseColor = GraphicsUtilities.decodeColor(colorCode, "baseColor");
            colors.add(darken(baseColor));
            colors.add(baseColor);
            colors.add(brighten(baseColor));
        }
        @SuppressWarnings("unchecked") Map<String, String> messageIconNames = (Map<String, String>) properties.get(MESSAGE_ICONS_PROPERTY);
        messageIcons = new HashMap<>();
        loadMessageIcons(messageIconNames, messageIcons);
        @SuppressWarnings("unchecked") Map<String, String> smallMessageIconNames = (Map<String, String>) properties.get(SMALL_MESSAGE_ICONS_PROPERTY);
        smallMessageIcons = new HashMap<>();
        loadMessageIcons(smallMessageIconNames, smallMessageIcons);
        if ((defaultBackgroundColor = getColorProperty(properties, DEFAULT_BACKGROUND_COLOR_PROPERTY)) == null) {
            defaultBackgroundColor = super.getDefaultBackgroundColor();
        }
        if ((defaultForegroundColor = getColorProperty(properties, DEFAULT_FOREGROUND_COLOR_PROPERTY)) == null) {
            defaultForegroundColor = super.getDefaultForegroundColor();
        }
    } catch (IOException exception) {
        throw new RuntimeException(exception);
    } catch (SerializationException exception) {
        throw new RuntimeException(exception);
    }
}
Also used : SerializationException(org.apache.pivot.serialization.SerializationException) InputStream(java.io.InputStream) Color(java.awt.Color) IOException(java.io.IOException) ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) Map(org.apache.pivot.collections.Map) HashMap(org.apache.pivot.collections.HashMap) JSONSerializer(org.apache.pivot.json.JSONSerializer)

Example 39 with List

use of org.apache.pivot.collections.List in project pivot by apache.

the class TerraVFSBrowserSkin method selectedFileRemoved.

@Override
public void selectedFileRemoved(VFSBrowser fileBrowser, FileObject file) {
    if (!updatingSelection) {
        @SuppressWarnings("unchecked") List<FileObject> files = (List<FileObject>) fileTableView.getTableData();
        int index = files.indexOf(file);
        if (index != -1) {
            updatingSelection = true;
            fileTableView.removeSelectedIndex(index);
            updatingSelection = false;
        }
    }
}
Also used : ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) FileObject(org.apache.commons.vfs2.FileObject) Point(org.apache.pivot.wtk.Point)

Example 40 with List

use of org.apache.pivot.collections.List 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)

Aggregations

List (org.apache.pivot.collections.List)47 ArrayList (org.apache.pivot.collections.ArrayList)37 TableView (org.apache.pivot.wtk.TableView)11 Span (org.apache.pivot.wtk.Span)10 Button (org.apache.pivot.wtk.Button)8 ListView (org.apache.pivot.wtk.ListView)8 Point (org.apache.pivot.wtk.Point)8 IOException (java.io.IOException)7 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)7 SerializationException (org.apache.pivot.serialization.SerializationException)6 Component (org.apache.pivot.wtk.Component)6 File (java.io.File)5 HashMap (org.apache.pivot.collections.HashMap)5 Map (org.apache.pivot.collections.Map)5 JSONSerializer (org.apache.pivot.json.JSONSerializer)5 ButtonPressListener (org.apache.pivot.wtk.ButtonPressListener)5 TableViewSortListener (org.apache.pivot.wtk.TableViewSortListener)5 Color (java.awt.Color)4 FileObject (org.apache.commons.vfs2.FileObject)4 Task (org.apache.pivot.util.concurrent.Task)4