Search in sources :

Example 21 with Point

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

the class TerraFrameSkin method mouseMove.

@Override
public boolean mouseMove(Component component, int x, int y) {
    boolean consumed = super.mouseMove(component, x, y);
    if (Mouse.getCapturer() == component) {
        Frame frame = (Frame) getComponent();
        Display display = frame.getDisplay();
        Point location = frame.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 (dragOffset != null) {
            // Move the frame
            frame.setLocation(location.x - dragOffset.x, location.y - dragOffset.y);
        } else {
            if (resizeOffset != null) {
                // Resize the frame
                int preferredWidth = -1;
                int preferredHeight = -1;
                if (frame.isPreferredWidthSet()) {
                    preferredWidth = Math.max(location.x - frame.getX() + resizeOffset.x, titleBarTablePane.getPreferredWidth(-1) + 2);
                    preferredWidth = Math.min(preferredWidth, frame.getMaximumWidth());
                    preferredWidth = Math.max(preferredWidth, frame.getMinimumWidth());
                }
                if (frame.isPreferredHeightSet()) {
                    preferredHeight = Math.max(location.y - frame.getY() + resizeOffset.y, titleBarTablePane.getHeight() + resizeHandle.getHeight() + (showContentBevel ? 1 : 0) + 6);
                    preferredHeight = Math.min(preferredHeight, frame.getMaximumHeight());
                    preferredHeight = Math.max(preferredHeight, frame.getMinimumHeight());
                }
                frame.setPreferredSize(preferredWidth, preferredHeight);
            }
        }
    } else {
        Cursor cursor = null;
        if (resizeHandle.isVisible() && x > resizeHandle.getX() && y > resizeHandle.getY()) {
            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;
            }
        }
        component.setCursor(cursor);
    }
    return consumed;
}
Also used : Frame(org.apache.pivot.wtk.Frame) Point(org.apache.pivot.wtk.Point) Cursor(org.apache.pivot.wtk.Cursor) Point(org.apache.pivot.wtk.Point) GradientPaint(java.awt.GradientPaint) Display(org.apache.pivot.wtk.Display)

Example 22 with Point

use of org.apache.pivot.wtk.Point 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 23 with Point

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

the class TerraSheetSkin method mouseDown.

@Override
public boolean mouseDown(Container container, Mouse.Button button, int x, int y) {
    Sheet sheet = (Sheet) container;
    if (!sheet.isTopMost()) {
        Window owner = sheet.getOwner();
        owner.moveToFront();
    }
    boolean consumed = super.mouseDown(container, button, x, y);
    if (resizable && button == Mouse.Button.LEFT) {
        Bounds resizeHandleBounds = resizeHandle.getBounds();
        if (resizeHandleBounds.contains(x, y)) {
            resizeOffset = new Point(getWidth() - x, getHeight() - y);
            Mouse.capture(container);
        }
    }
    return consumed;
}
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)

Example 24 with Point

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

the class TerraTabPaneSkin method paint.

@Override
public void paint(Graphics2D graphics) {
    TabPane tabPane = (TabPane) getComponent();
    Bounds tabPaneBounds = tabPane.getBounds();
    // Call the base class to paint the background
    super.paint(graphics);
    // Paint the content background and border
    int x = 0;
    int y = 0;
    int width = 0;
    int height = 0;
    switch(tabOrientation) {
        case HORIZONTAL:
            {
                x = 0;
                y = Math.max(tabButtonPanorama.getY() + tabButtonPanorama.getHeight() - 1, 0);
                width = tabPaneBounds.width;
                height = Math.max(tabPaneBounds.height - y, 0);
                break;
            }
        case VERTICAL:
            {
                x = Math.max(tabButtonPanorama.getX() + tabButtonPanorama.getWidth() - 1, 0);
                y = 0;
                width = Math.max(tabPaneBounds.width - x, 0);
                height = tabPaneBounds.height;
                break;
            }
        default:
            {
                break;
            }
    }
    TabButton activeTabButton;
    if (selectionChangeTransition == null) {
        activeTabButton = (TabButton) tabButtonGroup.getSelection();
    } else {
        activeTabButton = (TabButton) tabButtonBoxPane.get(selectionChangeTransition.index);
    }
    if (activeTabButton != null) {
        Bounds contentBounds = new Bounds(x, y, width, height);
        GraphicsUtilities.setAntialiasingOn(graphics);
        // Paint the background
        graphics.setPaint(activeTabColor);
        graphics.fillRect(contentBounds.x, contentBounds.y, contentBounds.width, contentBounds.height);
        if (!themeIsFlat()) {
            // Draw the border
            double top = contentBounds.y + 0.5;
            double left = contentBounds.x + 0.5;
            double bottom = top + contentBounds.height - 1;
            double right = left + contentBounds.width - 1;
            graphics.setPaint(borderColor);
            // Draw the right and bottom borders
            graphics.draw(new Line2D.Double(right, top, right, bottom));
            graphics.draw(new Line2D.Double(left, bottom, right, bottom));
            // Draw the left and top borders
            switch(tabOrientation) {
                case HORIZONTAL:
                    {
                        graphics.draw(new Line2D.Double(left, top, left, bottom));
                        Point selectedTabButtonLocation = activeTabButton.mapPointToAncestor(tabPane, 0, 0);
                        graphics.draw(new Line2D.Double(left, top, selectedTabButtonLocation.x + 0.5, top));
                        graphics.draw(new Line2D.Double(selectedTabButtonLocation.x + activeTabButton.getWidth() - 0.5, top, right, top));
                        break;
                    }
                case VERTICAL:
                    {
                        graphics.draw(new Line2D.Double(left, top, right, top));
                        Point selectedTabButtonLocation = activeTabButton.mapPointToAncestor(tabPane, 0, 0);
                        graphics.draw(new Line2D.Double(left, top, left, selectedTabButtonLocation.y + 0.5));
                        graphics.draw(new Line2D.Double(left, selectedTabButtonLocation.y + activeTabButton.getHeight() - 0.5, left, bottom));
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
        }
    }
}
Also used : TabPane(org.apache.pivot.wtk.TabPane) Bounds(org.apache.pivot.wtk.Bounds) Point(org.apache.pivot.wtk.Point) Line2D(java.awt.geom.Line2D) Point(org.apache.pivot.wtk.Point) GradientPaint(java.awt.GradientPaint)

Example 25 with Point

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

the class ComponentInspectorSkin method addPointControl.

private static Component addPointControl(final Dictionary<String, Object> dictionary, final String key, Form.Section section) {
    Point point = (Point) dictionary.get(key);
    BoxPane boxPane = new BoxPane(Orientation.VERTICAL);
    section.add(boxPane);
    Form.setLabel(boxPane, key);
    FlowPane flowPane = new FlowPane();
    flowPane.getStyles().put(Style.alignToBaseline, true);
    flowPane.getStyles().put(Style.horizontalSpacing, 5);
    boxPane.add(flowPane);
    TextInput textInput = new TextInput();
    textInput.setTextSize(3);
    textInput.setMaximumLength(4);
    textInput.setValidator(new IntValidator());
    textInput.setText(String.valueOf(point.x));
    flowPane.add(textInput);
    textInput.getComponentStateListeners().add(new ComponentStateListener() {

        @Override
        public void focusedChanged(Component component, Component obverseComponent) {
            if (!component.isFocused()) {
                TextInput textInputLocal = (TextInput) component;
                Point pointLocal = (Point) dictionary.get(key);
                try {
                    int x = Integer.parseInt(textInputLocal.getText());
                    dictionary.put(key, new Point(x, pointLocal.y));
                } catch (Exception exception) {
                    displayErrorMessage(exception, component.getWindow());
                    textInputLocal.setText(String.valueOf(pointLocal.x));
                }
            }
        }
    });
    Label label = new Label("x");
    label.getStyles().put(Style.font, "{italic:true}");
    flowPane.add(label);
    flowPane = new FlowPane();
    flowPane.getStyles().put(Style.alignToBaseline, true);
    flowPane.getStyles().put(Style.horizontalSpacing, 5);
    boxPane.add(flowPane);
    textInput = new TextInput();
    textInput.setTextSize(3);
    textInput.setMaximumLength(4);
    textInput.setValidator(new IntValidator());
    textInput.setText(String.valueOf(point.y));
    flowPane.add(textInput);
    textInput.getComponentStateListeners().add(new ComponentStateListener() {

        @Override
        public void focusedChanged(Component component, Component obverseComponent) {
            if (!component.isFocused()) {
                TextInput textInputLocal = (TextInput) component;
                Point pointLocal = (Point) dictionary.get(key);
                try {
                    int y = Integer.parseInt(textInputLocal.getText());
                    dictionary.put(key, new Point(pointLocal.x, y));
                } catch (Exception exception) {
                    displayErrorMessage(exception, component.getWindow());
                    textInputLocal.setText(String.valueOf(pointLocal.y));
                }
            }
        }
    });
    label = new Label("y");
    label.getStyles().put(Style.font, "{italic:true}");
    flowPane.add(label);
    return boxPane;
}
Also used : IntValidator(org.apache.pivot.wtk.validation.IntValidator) BoxPane(org.apache.pivot.wtk.BoxPane) Label(org.apache.pivot.wtk.Label) FlowPane(org.apache.pivot.wtk.FlowPane) Point(org.apache.pivot.wtk.Point) TextInput(org.apache.pivot.wtk.TextInput) Component(org.apache.pivot.wtk.Component) ComponentStateListener(org.apache.pivot.wtk.ComponentStateListener)

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