Search in sources :

Example 6 with Point

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

the class TerraFileBrowserSkin method install.

@Override
public void install(Component component) {
    super.install(component);
    final FileBrowser fileBrowser = (FileBrowser) component;
    BXMLSerializer bxmlSerializer = new BXMLSerializer();
    try {
        content = (Component) bxmlSerializer.readObject(TerraFileBrowserSkin.class, "terra_file_browser_skin.bxml", true);
    } catch (IOException exception) {
        throw new RuntimeException(exception);
    } catch (SerializationException exception) {
        throw new RuntimeException(exception);
    }
    fileBrowser.add(content);
    bxmlSerializer.bind(this, TerraFileBrowserSkin.class);
    driveListButton.getListButtonSelectionListeners().add(new ListButtonSelectionListener() {

        @Override
        public void selectedItemChanged(ListButton listButton, Object previousSelectedItem) {
            if (previousSelectedItem != null) {
                File drive = (File) listButton.getSelectedItem();
                if (drive != null && drive.canRead()) {
                    if (!selectingDriveFromRootDirectory) {
                        fileBrowser.setRootDirectory(drive);
                    }
                } else {
                    refreshRoots = true;
                    listButton.setSelectedItem(previousSelectedItem);
                }
            }
        }
    });
    pathListButton.getListButtonSelectionListeners().add(new ListButtonSelectionListener() {

        @Override
        public void selectedItemChanged(ListButton listButton, Object previousSelectedItem) {
            File ancestorDirectory = (File) listButton.getSelectedItem();
            if (ancestorDirectory != null) {
                fileBrowser.setRootDirectory(ancestorDirectory);
            }
        }
    });
    goUpButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            File rootDirectory = fileBrowser.getRootDirectory();
            File parentDirectory = rootDirectory.getParentFile();
            fileBrowser.setRootDirectory(parentDirectory);
        }
    });
    newFolderButton.getButtonPressListeners().add(new ButtonPressListener() {

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

        @Override
        public void buttonPressed(Button button) {
            fileBrowser.setRootDirectory(HOME_DIRECTORY);
        }
    });
    /**
     * {@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;
                for (int i = rangeStart; i <= rangeEnd; i++) {
                    @SuppressWarnings("unchecked") List<File> files = (List<File>) fileTableView.getTableData();
                    File file = files.get(i);
                    fileBrowser.addSelectedFile(file);
                }
                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<File> files = (List<File>) fileTableView.getTableData();
                    File 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<File> files = (Sequence<File>) tableView.getSelectedRows();
                for (int i = 0, n = files.getLength(); i < n; i++) {
                    File file = files.get(i);
                    files.update(i, file);
                }
                fileBrowser.setSelectedFiles(files);
                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<File> files = (List<File>) 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)) {
                    File file = (File) fileTableView.getTableData().get(indexLocal);
                    if (file.isDirectory()) {
                        fileBrowser.setRootDirectory(file);
                        consumed = true;
                    }
                }
            }
            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;
            }
            File file = (File) fileTableView.getTableData().get(row);
            // Construct and show the tooltip.
            final Tooltip tooltip = new Tooltip();
            String text = null;
            if (file != null) {
                text = file.getName();
            }
            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) 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) Component(org.apache.pivot.wtk.Component) TextInput(org.apache.pivot.wtk.TextInput) TableViewSelectionListener(org.apache.pivot.wtk.TableViewSelectionListener) 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) FileBrowser(org.apache.pivot.wtk.FileBrowser) ComponentMouseButtonListener(org.apache.pivot.wtk.ComponentMouseButtonListener) File(java.io.File) Display(org.apache.pivot.wtk.Display)

Example 7 with Point

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

the class TerraFrameSkin method mouseDown.

@Override
public boolean mouseDown(Component component, Mouse.Button button, int x, int y) {
    boolean consumed = super.mouseDown(component, button, x, y);
    Frame frame = (Frame) getComponent();
    boolean maximized = frame.isMaximized();
    if (button == Mouse.Button.LEFT && !maximized) {
        Bounds titleBarBounds = titleBarTablePane.getBounds();
        if (titleBarBounds.contains(x, y)) {
            dragOffset = new Point(x, y);
            Mouse.capture(component);
        } else {
            if (resizable && x > resizeHandle.getX() && y > resizeHandle.getY()) {
                resizeOffset = new Point(getWidth() - x, getHeight() - y);
                Mouse.capture(component);
            }
        }
    }
    return consumed;
}
Also used : Frame(org.apache.pivot.wtk.Frame) Bounds(org.apache.pivot.wtk.Bounds) Point(org.apache.pivot.wtk.Point)

Example 8 with Point

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

the class TerraSheetSkin method mouseMove.

@Override
public boolean mouseMove(Component component, int x, int y) {
    boolean consumed = super.mouseMove(component, x, y);
    if (Mouse.getCapturer() == component) {
        Sheet sheet = (Sheet) getComponent();
        Display display = sheet.getDisplay();
        Point location = sheet.mapPointToAncestor(display, x, y);
        // Pretend that the mouse can't move off screen (off the display)
        location = new Point(Math.min(Math.max(location.x, 0), display.getWidth() - 1), Math.min(Math.max(location.y, 0), display.getHeight() - 1));
        if (resizeOffset != null) {
            // Resize the frame
            int preferredWidth = -1;
            int preferredHeight = -1;
            boolean preferredWidthSet = component.isPreferredWidthSet();
            boolean preferredHeightSet = component.isPreferredHeightSet();
            boolean noPreferredSet = !(preferredWidthSet || preferredHeightSet);
            if (preferredWidthSet || noPreferredSet) {
                preferredWidth = Math.max(location.x - sheet.getX() + resizeOffset.x, 2);
                preferredWidth = Math.min(preferredWidth, sheet.getMaximumWidth());
                preferredWidth = Math.max(preferredWidth, sheet.getMinimumWidth());
            }
            if (preferredHeightSet || noPreferredSet) {
                preferredHeight = Math.max(location.y - sheet.getY() + resizeOffset.y, resizeHandle.getHeight() + 7);
                preferredHeight = Math.min(preferredHeight, sheet.getMaximumHeight());
                preferredHeight = Math.max(preferredHeight, sheet.getMinimumHeight());
            }
            sheet.setPreferredSize(preferredWidth, preferredHeight);
        }
    } else {
        Cursor cursor = null;
        Bounds resizeHandleBounds = resizeHandle.getBounds();
        if (resizable && resizeHandleBounds.contains(x, y)) {
            boolean preferredWidthSet = component.isPreferredWidthSet();
            boolean preferredHeightSet = component.isPreferredHeightSet();
            if (preferredWidthSet && preferredHeightSet) {
                cursor = Cursor.RESIZE_SOUTH_EAST;
            } else if (preferredWidthSet) {
                cursor = Cursor.RESIZE_EAST;
            } else if (preferredHeightSet) {
                cursor = Cursor.RESIZE_SOUTH;
            } else {
                cursor = Cursor.RESIZE_SOUTH_EAST;
            }
        }
        component.setCursor(cursor);
    }
    return consumed;
}
Also used : Bounds(org.apache.pivot.wtk.Bounds) Point(org.apache.pivot.wtk.Point) Cursor(org.apache.pivot.wtk.Cursor) Sheet(org.apache.pivot.wtk.Sheet) Point(org.apache.pivot.wtk.Point) Display(org.apache.pivot.wtk.Display)

Example 9 with Point

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

the class TerraSheetSkin method alignToOwner.

public void alignToOwner() {
    Sheet sheet = (Sheet) getComponent();
    Window owner = sheet.getOwner();
    if (owner != null) {
        Bounds clientArea = owner.getClientArea();
        Point location = owner.mapPointToAncestor(owner.getDisplay(), clientArea.x, clientArea.y);
        int x = location.x;
        int y = location.y;
        switch(slideSource) {
            case NORTH:
                x = location.x + (clientArea.width - getWidth()) / 2;
                y = location.y;
                break;
            case SOUTH:
                x = location.x + (clientArea.width - getWidth()) / 2;
                y = location.y + (clientArea.height - getHeight());
                break;
            case WEST:
                x = location.x;
                y = location.y + (clientArea.height - getHeight()) / 2;
                break;
            case EAST:
                x = location.x + (clientArea.width - getWidth());
                y = location.y + (clientArea.height - getHeight()) / 2;
                break;
            default:
                throw new IllegalStateException("slideSource is null or an unexpected value");
        }
        sheet.setLocation(x, y);
    }
}
Also used : Window(org.apache.pivot.wtk.Window) Bounds(org.apache.pivot.wtk.Bounds) Point(org.apache.pivot.wtk.Point) Sheet(org.apache.pivot.wtk.Sheet) Point(org.apache.pivot.wtk.Point)

Example 10 with Point

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

the class TerraSuggestionPopupSkin method windowOpened.

@Override
public void windowOpened(Window window) {
    super.windowOpened(window);
    // Adjust for list size
    SuggestionPopup suggestionPopup = (SuggestionPopup) window;
    int listSize = suggestionPopup.getListSize();
    if (listSize == -1) {
        listViewBorder.setPreferredHeight(-1);
    } else {
        if (!listViewBorder.isPreferredHeightSet()) {
            ListView.ItemRenderer itemRenderer = listView.getItemRenderer();
            int borderHeight = itemRenderer.getPreferredHeight(-1) * listSize + 2;
            if (listViewBorder.getPreferredHeight() > borderHeight) {
                listViewBorder.setPreferredHeight(borderHeight);
            } else {
                listViewBorder.setPreferredHeight(-1);
            }
        }
    }
    Display display = window.getDisplay();
    display.getContainerMouseListeners().add(displayMouseListener);
    if (dropShadowDecorator != null) {
        dropShadowDecorator.setShadowOpacity(DropShadowDecorator.DEFAULT_SHADOW_OPACITY);
    }
    returnFocusToTextInput = true;
    TextInput textInput = suggestionPopup.getTextInput();
    textInput.getComponentStateListeners().add(textInputStateListener);
    textInput.getComponentKeyListeners().add(textInputKeyListener);
    // Size and position the popup
    Point location = textInput.mapPointToAncestor(textInput.getDisplay(), 0, 0);
    window.setLocation(location.x, location.y + textInput.getHeight() - 1);
    window.setMinimumWidth(textInput.getWidth());
    window.setMaximumHeight(display.getHeight() - window.getY());
}
Also used : ListView(org.apache.pivot.wtk.ListView) SuggestionPopup(org.apache.pivot.wtk.SuggestionPopup) Point(org.apache.pivot.wtk.Point) TextInput(org.apache.pivot.wtk.TextInput) Point(org.apache.pivot.wtk.Point) Display(org.apache.pivot.wtk.Display)

Aggregations

Point (org.apache.pivot.wtk.Point)26 Bounds (org.apache.pivot.wtk.Bounds)11 Component (org.apache.pivot.wtk.Component)9 Display (org.apache.pivot.wtk.Display)9 TextInput (org.apache.pivot.wtk.TextInput)6 IOException (java.io.IOException)5 GradientPaint (java.awt.GradientPaint)4 Button (org.apache.pivot.wtk.Button)4 ButtonPressListener (org.apache.pivot.wtk.ButtonPressListener)3 Container (org.apache.pivot.wtk.Container)3 Cursor (org.apache.pivot.wtk.Cursor)3 DragSource (org.apache.pivot.wtk.DragSource)3 DropAction (org.apache.pivot.wtk.DropAction)3 DropTarget (org.apache.pivot.wtk.DropTarget)3 Label (org.apache.pivot.wtk.Label)3 Line2D (java.awt.geom.Line2D)2 File (java.io.File)2 FileObject (org.apache.commons.vfs2.FileObject)2 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)2 ArrayList (org.apache.pivot.collections.ArrayList)2