Search in sources :

Example 1 with ListButtonSelectionListener

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

the class TextPaneDemo method startup.

@Override
public void startup(Display display, Map<String, String> properties) throws Exception {
    System.out.println("startup(...)");
    System.out.println("\n" + "In this test application as a sample for setting the display scale on startup, use startup argument \"--scale=n\" property; \n" + "for instance, using \"--scale=2.0\" will set double scale on the whole application.\n" + "\n" + "Anyway, using Ctrl-Shift-MouseWheel will scale the display up and down as well, for the user of your application.\n");
    BXMLSerializer bxmlSerializer = new BXMLSerializer();
    window = (Window) bxmlSerializer.readObject(TextPaneDemo.class, "text_pane_demo.bxml");
    bxmlSerializer.bind(this, TextPaneDemo.class);
    window.setTitle("Apache Pivot Rich Text Editor Demo");
    // make the text on the "bold" button bold
    Font boldButtonFont = boldButton.getStyles().getFont(Style.font);
    boldButton.getStyles().put(Style.font, boldButtonFont.deriveFont(Font.BOLD));
    // make the text on the "italic" button italic
    Font italicButtonFont = italicButton.getStyles().getFont(Style.font);
    italicButton.getStyles().put(Style.font, italicButtonFont.deriveFont(Font.ITALIC));
    fontFamilyListButton.setListData(new ArrayList<>(GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames()));
    fontSizeListButton.setSelectedItem(fontFamilyListButton.getListData().get(0));
    fontFamilyListButton.setItemRenderer(new ListViewItemRenderer() {

        @Override
        public void render(Object item, int index, ListView listView, boolean selected, Button.State state, boolean highlighted, boolean disabled) {
            super.render(item, index, listView, selected, state, highlighted, disabled);
            if (item != null) {
                String fontFamilyName = (String) item;
                label.getStyles().put(Style.font, Font.decode(fontFamilyName + "-12"));
            }
        }
    });
    fontFamilyListButton.setDataRenderer(new ListButtonDataRenderer() {

        @Override
        public void render(Object data, Button button, boolean highlight) {
            super.render(data, button, highlight);
            if (data != null) {
                String fontFamilyName = (String) data;
                label.getStyles().put(Style.font, Font.decode(fontFamilyName + "-12"));
            }
        }
    });
    fontSizeListButton.setListData(new NumericSpinnerData(12, 30, 1));
    fontSizeListButton.setSelectedItem(12);
    openFileButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            final FileBrowserSheet fileBrowserSheet = new FileBrowserSheet();
            fileBrowserSheet.setMode(FileBrowserSheet.Mode.OPEN);
            fileBrowserSheet.open(window, new SheetCloseListener() {

                @Override
                public void sheetClosed(Sheet sheet) {
                    if (sheet.getResult()) {
                        loadedFile = fileBrowserSheet.getSelectedFile();
                        try {
                            BufferedReader reader = new BufferedReader(new FileReader(loadedFile));
                            PlainTextSerializer serializer = new PlainTextSerializer();
                            textPane.setDocument(serializer.readObject(reader));
                            reader.close();
                            window.setTitle(loadedFile.getCanonicalPath());
                        } catch (IOException ex) {
                            ex.printStackTrace();
                            Alert.alert(ex.getMessage(), window);
                        }
                    }
                }
            });
        }
    });
    saveFileButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            final FileBrowserSheet fileBrowserSheet = new FileBrowserSheet();
            if (loadedFile != null) {
                fileBrowserSheet.setSelectedFile(loadedFile);
            }
            fileBrowserSheet.setMode(FileBrowserSheet.Mode.SAVE_AS);
            fileBrowserSheet.open(window, new SheetCloseListener() {

                @Override
                public void sheetClosed(Sheet sheet) {
                    if (sheet.getResult()) {
                        File selectedFile = fileBrowserSheet.getSelectedFile();
                        try {
                            FileWriter writer = new FileWriter(selectedFile);
                            PlainTextSerializer serializer = new PlainTextSerializer();
                            serializer.writeObject(textPane.getDocument(), writer);
                            writer.close();
                            loadedFile = selectedFile;
                            window.setTitle(loadedFile.getCanonicalPath());
                        } catch (IOException ex) {
                            ex.printStackTrace();
                            Alert.alert(ex.getMessage(), window);
                        }
                    }
                }
            });
        }
    });
    boldButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            applyStyleToSelection(new StyleApplicator() {

                @Override
                public void apply(TextSpan span) {
                    if (span.getFont() != null) {
                        Font font = span.getFont();
                        if (font.getStyle() == Font.PLAIN) {
                            font = font.deriveFont(Font.BOLD);
                        } else if (font.getStyle() == Font.BOLD) {
                            font = font.deriveFont(Font.PLAIN);
                        } else {
                            // the font is BOLD+ITALIC
                            font = font.deriveFont(Font.ITALIC);
                        }
                        span.setFont(font);
                    } else {
                        span.setFont("Arial BOLD 12");
                    }
                }
            });
            requestTextPaneFocus();
        }
    });
    italicButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            applyStyleToSelection(new StyleApplicator() {

                @Override
                public void apply(TextSpan span) {
                    if (span.getFont() != null) {
                        Font font = span.getFont();
                        if (font.getStyle() == Font.PLAIN) {
                            font = font.deriveFont(Font.ITALIC);
                        } else if (font.getStyle() == Font.ITALIC) {
                            font = font.deriveFont(Font.PLAIN);
                        } else {
                            // the font is BOLD+ITALIC
                            font = font.deriveFont(Font.BOLD);
                        }
                        span.setFont(font);
                    } else {
                        span.setFont("Arial ITALIC 12");
                    }
                }
            });
            requestTextPaneFocus();
        }
    });
    underlineButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            applyStyleToSelection(new StyleApplicator() {

                @Override
                public void apply(TextSpan span) {
                    span.setUnderline(!span.isUnderline());
                }
            });
            requestTextPaneFocus();
        }
    });
    strikethroughButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            applyStyleToSelection(new StyleApplicator() {

                @Override
                public void apply(TextSpan span) {
                    span.setStrikethrough(!span.isStrikethrough());
                }
            });
            requestTextPaneFocus();
        }
    });
    foregroundColorChooserButton.getColorChooserButtonSelectionListeners().add(new ColorChooserButtonSelectionListener() {

        @Override
        public void selectedColorChanged(ColorChooserButton colorChooserButton, Color previousSelectedColor) {
            applyStyleToSelection(new StyleApplicator() {

                @Override
                public void apply(TextSpan span) {
                    span.setForegroundColor(foregroundColorChooserButton.getSelectedColor());
                }
            });
            requestTextPaneFocus();
        }
    });
    backgroundColorChooserButton.getColorChooserButtonSelectionListeners().add(new ColorChooserButtonSelectionListener() {

        @Override
        public void selectedColorChanged(ColorChooserButton colorChooserButton, Color previousSelectedColor) {
            applyStyleToSelection(new StyleApplicator() {

                @Override
                public void apply(TextSpan span) {
                    span.setBackgroundColor(backgroundColorChooserButton.getSelectedColor());
                }
            });
            requestTextPaneFocus();
        }
    });
    ListButtonSelectionListener fontButtonPressListener = new ListButtonSelectionListener() {

        @Override
        public void selectedItemChanged(ListButton listButton, Object previousSelectedItem) {
            int selectedFontSize = ((Integer) fontSizeListButton.getSelectedItem()).intValue();
            String selectedFontFamily = (String) fontFamilyListButton.getSelectedItem();
            final Font derivedFont = Font.decode(selectedFontFamily + " " + selectedFontSize);
            applyStyleToSelection(new StyleApplicator() {

                @Override
                public void apply(TextSpan span) {
                    span.setFont(derivedFont);
                }
            });
            requestTextPaneFocus();
        }
    };
    fontFamilyListButton.getListButtonSelectionListeners().add(fontButtonPressListener);
    fontSizeListButton.getListButtonSelectionListeners().add(fontButtonPressListener);
    wrapTextCheckbox.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            textPane.getStyles().put(Style.wrapText, wrapTextCheckbox.isSelected());
            requestTextPaneFocus();
        }
    });
    alignLeftButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            applyAlignmentStyle(HorizontalAlignment.LEFT);
            requestTextPaneFocus();
        }
    });
    alignCentreButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            applyAlignmentStyle(HorizontalAlignment.CENTER);
            requestTextPaneFocus();
        }
    });
    alignRightButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            applyAlignmentStyle(HorizontalAlignment.RIGHT);
            requestTextPaneFocus();
        }
    });
    String scaleProperty = properties.get("scale");
    if (scaleProperty != null && !scaleProperty.isEmpty()) {
        try {
            double scaleFactor = Double.parseDouble(scaleProperty);
            System.out.println("Got scaling factor \"" + scaleProperty + "\" from command line arguments, now applying to display");
            display.getDisplayHost().setScale(scaleFactor);
        } catch (NumberFormatException nfe) {
            System.err.println("(NumberFormatException: " + nfe.getMessage());
        }
    }
    window.open(display);
    requestTextPaneFocus();
}
Also used : ListViewItemRenderer(org.apache.pivot.wtk.content.ListViewItemRenderer) ListButtonSelectionListener(org.apache.pivot.wtk.ListButtonSelectionListener) ListButtonDataRenderer(org.apache.pivot.wtk.content.ListButtonDataRenderer) FileWriter(java.io.FileWriter) NumericSpinnerData(org.apache.pivot.wtk.content.NumericSpinnerData) Font(java.awt.Font) ButtonPressListener(org.apache.pivot.wtk.ButtonPressListener) ListView(org.apache.pivot.wtk.ListView) ColorChooserButton(org.apache.pivot.wtk.ColorChooserButton) PushButton(org.apache.pivot.wtk.PushButton) Button(org.apache.pivot.wtk.Button) ListButton(org.apache.pivot.wtk.ListButton) ColorChooserButton(org.apache.pivot.wtk.ColorChooserButton) FileReader(java.io.FileReader) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer) FileBrowserSheet(org.apache.pivot.wtk.FileBrowserSheet) PlainTextSerializer(org.apache.pivot.wtk.text.PlainTextSerializer) Color(java.awt.Color) SheetCloseListener(org.apache.pivot.wtk.SheetCloseListener) IOException(java.io.IOException) TextSpan(org.apache.pivot.wtk.text.TextSpan) ListButton(org.apache.pivot.wtk.ListButton) ColorChooserButtonSelectionListener(org.apache.pivot.wtk.ColorChooserButtonSelectionListener) BufferedReader(java.io.BufferedReader) Sheet(org.apache.pivot.wtk.Sheet) FileBrowserSheet(org.apache.pivot.wtk.FileBrowserSheet) File(java.io.File)

Example 2 with ListButtonSelectionListener

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

the class LargeData method startup.

@Override
public void startup(Display display, Map<String, String> properties) throws Exception {
    basePath = properties.get(BASE_PATH_KEY);
    if (basePath == null) {
        throw new IllegalArgumentException(BASE_PATH_KEY + " is required.");
    }
    origin = ApplicationContext.getOrigin();
    if (origin == null) {
        System.out.println("Running as a Standalone Java Application, with user home: \"" + USER_HOME + "\"");
        if (USER_HOME != null) {
            System.out.println("Set as origin the user home");
            origin = (new File(USER_HOME).toURI()).toURL();
        }
    }
    BXMLSerializer bxmlSerializer = new BXMLSerializer();
    window = (Window) bxmlSerializer.readObject(LargeData.class, "large_data.bxml");
    fileListButton = (ListButton) bxmlSerializer.getNamespace().get("fileListButton");
    loadDataButton = (PushButton) bxmlSerializer.getNamespace().get("loadDataButton");
    cancelButton = (PushButton) bxmlSerializer.getNamespace().get("cancelButton");
    clearButton = (PushButton) bxmlSerializer.getNamespace().get("clearButton");
    statusLabel = (Label) bxmlSerializer.getNamespace().get("statusLabel");
    tableView = (TableView) bxmlSerializer.getNamespace().get("tableView");
    fileListButton.getListButtonSelectionListeners().add(new ListButtonSelectionListener() {

        @Override
        public void selectedItemChanged(ListButton listButtonArgument, Object previousSelectedItem) {
            Object selectedItem = listButtonArgument.getSelectedItem();
            System.out.println("Selected: " + selectedItem.toString() + ", now clear table data ...");
            // empty the table
            tableView.getTableData().clear();
        }
    });
    loadDataButton.getButtonPressListeners().add((button) -> {
        loadDataButton.setEnabled(false);
        cancelButton.setEnabled(true);
        loadData();
    });
    cancelButton.getButtonPressListeners().add((button) -> {
        if (loadDataTask != null) {
            loadDataTask.abort();
        }
        loadDataButton.setEnabled(true);
        cancelButton.setEnabled(false);
    });
    clearButton.getButtonPressListeners().add((button) -> {
        if (loadDataTask != null) {
            loadDataTask.abort();
        }
        // empty the table
        tableView.getTableData().clear();
        statusLabel.setText("");
    });
    tableView.getTableViewSortListeners().add(new TableViewSortListener() {

        @Override
        public void sortChanged(TableView tableViewArgument) {
            @SuppressWarnings("unchecked") List<Object> tableData = (List<Object>) tableViewArgument.getTableData();
            long startTime = System.currentTimeMillis();
            tableData.setComparator(new TableViewRowComparator(tableViewArgument));
            long endTime = System.currentTimeMillis();
            statusLabel.setText("Data sorted in " + (endTime - startTime) + " ms.");
        }
    });
    window.open(display);
}
Also used : ListButton(org.apache.pivot.wtk.ListButton) TableViewRowComparator(org.apache.pivot.wtk.content.TableViewRowComparator) ListButtonSelectionListener(org.apache.pivot.wtk.ListButtonSelectionListener) TableViewSortListener(org.apache.pivot.wtk.TableViewSortListener) ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) File(java.io.File) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer) TableView(org.apache.pivot.wtk.TableView)

Example 3 with ListButtonSelectionListener

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

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

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

the class ComponentInspectorSkin method addEnumControl.

private Component addEnumControl(final Dictionary<String, Object> dictionary, final String key, Class<? extends Enum<?>> type, Form.Section section) {
    Enum<?> value = (Enum<?>) dictionary.get(key);
    ArrayList<Object> listData = new ArrayList<>();
    listData.add(null);
    Enum<?>[] enumConstants = type.getEnumConstants();
    for (int i = 0; i < enumConstants.length; i++) {
        listData.add(enumConstants[i]);
    }
    ListButton listButton = new ListButton();
    listButton.setListData(listData);
    listButton.setSelectedItem(value);
    section.add(listButton);
    Form.setLabel(listButton, key);
    listButton.getListButtonSelectionListeners().add(new ListButtonSelectionListener() {

        private boolean updating = false;

        @Override
        public void selectedIndexChanged(ListButton listButtonArgument, int previousSelectedIndex) {
            if (!updating) {
                updating = true;
                try {
                    dictionary.put(key, listButtonArgument.getSelectedItem());
                } catch (Exception exception) {
                    displayErrorMessage(exception, listButtonArgument.getWindow());
                    listButtonArgument.setSelectedIndex(previousSelectedIndex);
                } finally {
                    updating = false;
                }
            }
        }
    });
    return listButton;
}
Also used : ListButton(org.apache.pivot.wtk.ListButton) ListButtonSelectionListener(org.apache.pivot.wtk.ListButtonSelectionListener) ArrayList(org.apache.pivot.collections.ArrayList) Point(org.apache.pivot.wtk.Point)

Aggregations

ListButton (org.apache.pivot.wtk.ListButton)6 ListButtonSelectionListener (org.apache.pivot.wtk.ListButtonSelectionListener)6 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)4 ArrayList (org.apache.pivot.collections.ArrayList)4 File (java.io.File)3 IOException (java.io.IOException)3 List (org.apache.pivot.collections.List)3 Button (org.apache.pivot.wtk.Button)3 ButtonPressListener (org.apache.pivot.wtk.ButtonPressListener)3 Point (org.apache.pivot.wtk.Point)3 PushButton (org.apache.pivot.wtk.PushButton)3 TableView (org.apache.pivot.wtk.TableView)3 TableViewSortListener (org.apache.pivot.wtk.TableViewSortListener)3 Sequence (org.apache.pivot.collections.Sequence)2 SerializationException (org.apache.pivot.serialization.SerializationException)2 Component (org.apache.pivot.wtk.Component)2 ComponentKeyListener (org.apache.pivot.wtk.ComponentKeyListener)2 ComponentMouseButtonListener (org.apache.pivot.wtk.ComponentMouseButtonListener)2 ComponentTooltipListener (org.apache.pivot.wtk.ComponentTooltipListener)2 Container (org.apache.pivot.wtk.Container)2