Search in sources :

Example 1 with ListView

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

the class Pivot811 method startup.

@Override
public void startup(final Display displayArgument, Map<String, String> properties) throws Exception {
    this.display = displayArgument;
    Frame listFrame = new Frame();
    listFrame.setTitle("List Frame");
    listFrame.setPreferredSize(400, 300);
    listFrame.setLocation(20, 20);
    listFrame.getStyles().put(Style.padding, Insets.NONE);
    BoxPane boxPane = new BoxPane();
    boxPane.getStyles().put(Style.fill, true);
    boxPane.setOrientation(Orientation.VERTICAL);
    listFrame.setContent(boxPane);
    Label infoLabel = new Label("Double click on a list item to open a detail frame");
    boxPane.add(infoLabel);
    ScrollPane scrollPane = new ScrollPane();
    scrollPane.setHorizontalScrollBarPolicy(ScrollBarPolicy.FILL);
    scrollPane.setVerticalScrollBarPolicy(ScrollBarPolicy.FILL_TO_CAPACITY);
    // workaround for pivot-738,
    scrollPane.setRepaintAllViewport(true);
    // needed only in in some cases
    boxPane.add(scrollPane);
    final ListView listView = new ListView();
    List<String> listData = new ArrayList<>();
    for (int i = 0; i < 50; ++i) {
        listData.add("List Item " + i);
    }
    listView.setListData(listData);
    scrollPane.setView(listView);
    listView.getListViewSelectionListeners().add(new ListViewSelectionListener() {

        @Override
        public void selectedItemChanged(ListView listViewArgument, Object previousSelectedItem) {
            System.out.println("selectedItemChanged : " + listViewArgument.getSelectedItem());
        }
    });
    listView.getComponentMouseButtonListeners().add(new ComponentMouseButtonListener() {

        @Override
        public boolean mouseClick(Component component, Mouse.Button button, int x, int y, int count) {
            System.out.println("mouseClick : " + count);
            if (count == 2) {
                System.out.println("double click, now open a detail frame");
                Frame detailFrame = new Frame();
                detailFrame.setTitle("Detail Frame");
                detailFrame.setPreferredSize(400, 300);
                int selectedIndex = listView.getSelectedIndex();
                detailFrame.setLocation(80 + (selectedIndex * 10), 80 + (selectedIndex * 10));
                detailFrame.getStyles().put(Style.padding, Insets.NONE);
                BoxPane boxPaneLocal = new BoxPane();
                boxPaneLocal.getStyles().put(Style.fill, true);
                boxPaneLocal.setOrientation(Orientation.VERTICAL);
                detailFrame.setContent(boxPaneLocal);
                String selectedItem = listView.getSelectedItem().toString();
                Label label = new Label("Selected Item is \"" + selectedItem + "\"");
                boxPaneLocal.add(label);
                // spacer
                boxPaneLocal.add(new Label(""));
                boxPaneLocal.add(new Label("Click inside the text input to focus it"));
                TextInput textInput = new TextInput();
                textInput.setText("Focusable component");
                // workaround for pivot-811:
                boxPaneLocal.add(textInput);
                // add a focusable element
                // inside the frame
                detailFrame.open(displayArgument);
                // workaround for pivot-811: force the focus on the first
                // focusable element inside the frame
                detailFrame.requestFocus();
            // textInput.requestFocus(); // or use this ...
            }
            return true;
        }
    });
    listFrame.open(displayArgument);
    listView.setSelectedIndex(0);
    listView.requestFocus();
}
Also used : Frame(org.apache.pivot.wtk.Frame) Label(org.apache.pivot.wtk.Label) ArrayList(org.apache.pivot.collections.ArrayList) ListView(org.apache.pivot.wtk.ListView) Mouse(org.apache.pivot.wtk.Mouse) BoxPane(org.apache.pivot.wtk.BoxPane) ScrollPane(org.apache.pivot.wtk.ScrollPane) ComponentMouseButtonListener(org.apache.pivot.wtk.ComponentMouseButtonListener) ListViewSelectionListener(org.apache.pivot.wtk.ListViewSelectionListener) Component(org.apache.pivot.wtk.Component) TextInput(org.apache.pivot.wtk.TextInput)

Example 2 with ListView

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

the class FileBrowsing method initialize.

@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
    openSheetButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            Button selection = fileBrowserSheetModeGroup.getSelection();
            String mode = (String) selection.getUserData().get("mode");
            FileBrowserSheet.Mode fileBrowserSheetMode = FileBrowserSheet.Mode.valueOf(mode.toUpperCase());
            final FileBrowserSheet fileBrowserSheet = new FileBrowserSheet();
            if (fileBrowserSheetMode == FileBrowserSheet.Mode.SAVE_AS) {
                fileBrowserSheet.setSelectedFile(new File(fileBrowserSheet.getRootDirectory(), "New File"));
            }
            fileBrowserSheet.setMode(fileBrowserSheetMode);
            fileBrowserSheet.open(FileBrowsing.this, new SheetCloseListener() {

                @Override
                public void sheetClosed(Sheet sheet) {
                    if (sheet.getResult()) {
                        Sequence<File> selectedFiles = fileBrowserSheet.getSelectedFiles();
                        ListView listView = new ListView();
                        listView.setListData(new ArrayList<>(selectedFiles));
                        listView.setSelectMode(ListView.SelectMode.NONE);
                        listView.getStyles().put(Style.backgroundColor, null);
                        Alert.alert(MessageType.INFO, "You selected:", listView, FileBrowsing.this);
                    } else {
                        Alert.alert(MessageType.INFO, "You didn't select anything.", FileBrowsing.this);
                    }
                }
            });
        }
    });
}
Also used : ButtonPressListener(org.apache.pivot.wtk.ButtonPressListener) ListView(org.apache.pivot.wtk.ListView) FileBrowserSheet(org.apache.pivot.wtk.FileBrowserSheet) PushButton(org.apache.pivot.wtk.PushButton) Button(org.apache.pivot.wtk.Button) SheetCloseListener(org.apache.pivot.wtk.SheetCloseListener) File(java.io.File) Sheet(org.apache.pivot.wtk.Sheet) FileBrowserSheet(org.apache.pivot.wtk.FileBrowserSheet)

Example 3 with ListView

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

the class ListViews method initialize.

@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
    selectionLabel = (Label) namespace.get("selectionLabel");
    listView = (ListView) namespace.get("listView");
    listView.getListViewSelectionListeners().add(new ListViewSelectionListener() {

        @Override
        public void selectedRangeAdded(ListView listViewArgument, int rangeStart, int rangeEnd) {
            updateSelection(listViewArgument);
        }

        @Override
        public void selectedRangeRemoved(ListView listViewArgument, int rangeStart, int rangeEnd) {
            updateSelection(listViewArgument);
        }

        @Override
        public void selectedRangesChanged(ListView listViewArgument, Sequence<Span> previousSelectedRanges) {
            if (previousSelectedRanges != null && previousSelectedRanges != listViewArgument.getSelectedRanges()) {
                updateSelection(listViewArgument);
            }
        }

        @Override
        public void selectedItemChanged(ListView listViewArgument, Object previousSelectedItem) {
        // No-op
        }

        private void updateSelection(ListView listViewArgument) {
            // TODO: in future use StringBuffer instead ...
            String selectionText = "";
            Sequence<Span> selectedRanges = listViewArgument.getSelectedRanges();
            for (int i = 0, n = selectedRanges.getLength(); i < n; i++) {
                Span selectedRange = selectedRanges.get(i);
                for (int j = selectedRange.start; j <= selectedRange.end; j++) {
                    if (selectionText.length() > 0) {
                        selectionText += ", ";
                    }
                    Object item = listViewArgument.getListData().get(j);
                    String text;
                    if (item instanceof ListItem) {
                        // item is a listItem
                        // (for example because
                        // it has an image)
                        text = ((ListItem) item).getText();
                    } else {
                        // item is a standard item for listData
                        text = item.toString();
                    }
                    selectionText += text;
                }
            }
            selectionLabel.setText(selectionText);
        }
    });
}
Also used : ListView(org.apache.pivot.wtk.ListView) ListViewSelectionListener(org.apache.pivot.wtk.ListViewSelectionListener) Sequence(org.apache.pivot.collections.Sequence) ListItem(org.apache.pivot.wtk.content.ListItem) Span(org.apache.pivot.wtk.Span)

Example 4 with ListView

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

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

the class DragAndDropDemo method initialize.

@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
    // Text
    label.setDragSource(new DragSource() {

        private LocalManifest content = null;

        @Override
        public boolean beginDrag(Component component, int x, int y) {
            String text = label.getText();
            if (text != null) {
                content = new LocalManifest();
                content.putText(label.getText());
            }
            return (content != null);
        }

        @Override
        public void endDrag(Component component, DropAction dropAction) {
            content = null;
        }

        @Override
        public boolean isNative() {
            return true;
        }

        @Override
        public LocalManifest getContent() {
            return content;
        }

        @Override
        public Visual getRepresentation() {
            return null;
        }

        @Override
        public Point getOffset() {
            return null;
        }

        @Override
        public int getSupportedDropActions() {
            return DropAction.COPY.getMask();
        }
    });
    label.setDropTarget(new DropTarget() {

        @Override
        public DropAction dragEnter(Component component, Manifest dragContent, int supportedDropActions, DropAction userDropAction) {
            DropAction dropAction = null;
            if (dragContent.containsText() && DropAction.COPY.isSelected(supportedDropActions)) {
                dropAction = DropAction.COPY;
            }
            return dropAction;
        }

        @Override
        public void dragExit(Component component) {
        // empty block
        }

        @Override
        public DropAction dragMove(Component component, Manifest dragContent, int supportedDropActions, int x, int y, DropAction userDropAction) {
            return (dragContent.containsText() ? DropAction.COPY : null);
        }

        @Override
        public DropAction userDropActionChange(Component component, Manifest dragContent, int supportedDropActions, int x, int y, DropAction userDropAction) {
            return (dragContent.containsText() ? DropAction.COPY : null);
        }

        @Override
        public DropAction drop(Component component, Manifest dragContent, int supportedDropActions, int x, int y, DropAction userDropAction) {
            DropAction dropAction = null;
            if (dragContent.containsText()) {
                try {
                    label.setText(dragContent.getText());
                    dropAction = DropAction.COPY;
                } catch (IOException exception) {
                    System.err.println(exception);
                }
            }
            dragExit(component);
            return dropAction;
        }
    });
    copyTextButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            String text = label.getText();
            LocalManifest clipboardContent = new LocalManifest();
            clipboardContent.putText(text);
            Clipboard.setContent(clipboardContent);
        }
    });
    pasteTextButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            Manifest clipboardContent = Clipboard.getContent();
            if (clipboardContent != null && clipboardContent.containsText()) {
                try {
                    label.setText(clipboardContent.getText());
                } catch (IOException exception) {
                    System.err.println(exception);
                }
            }
        }
    });
    // Images
    imageView.setDragSource(new DragSource() {

        private LocalManifest content = null;

        @Override
        public boolean beginDrag(Component component, int x, int y) {
            Image image = imageView.getImage();
            if (image != null) {
                content = new LocalManifest();
                content.putImage(image);
            }
            return (content != null);
        }

        @Override
        public void endDrag(Component component, DropAction dropAction) {
            content = null;
        }

        @Override
        public boolean isNative() {
            return true;
        }

        @Override
        public LocalManifest getContent() {
            return content;
        }

        @Override
        public Visual getRepresentation() {
            return null;
        }

        @Override
        public Point getOffset() {
            return null;
        }

        @Override
        public int getSupportedDropActions() {
            return DropAction.COPY.getMask();
        }
    });
    imageView.setDropTarget(new DropTarget() {

        @Override
        public DropAction dragEnter(Component component, Manifest dragContent, int supportedDropActions, DropAction userDropAction) {
            DropAction dropAction = null;
            if (dragContent.containsImage() && DropAction.COPY.isSelected(supportedDropActions)) {
                dropAction = DropAction.COPY;
            }
            return dropAction;
        }

        @Override
        public void dragExit(Component component) {
        // empty block
        }

        @Override
        public DropAction dragMove(Component component, Manifest dragContent, int supportedDropActions, int x, int y, DropAction userDropAction) {
            return (dragContent.containsImage() ? DropAction.COPY : null);
        }

        @Override
        public DropAction userDropActionChange(Component component, Manifest dragContent, int supportedDropActions, int x, int y, DropAction userDropAction) {
            return (dragContent.containsImage() ? DropAction.COPY : null);
        }

        @Override
        public DropAction drop(Component component, Manifest dragContent, int supportedDropActions, int x, int y, DropAction userDropAction) {
            DropAction dropAction = null;
            if (dragContent.containsImage()) {
                try {
                    imageView.setImage(dragContent.getImage());
                    dropAction = DropAction.COPY;
                } catch (IOException exception) {
                    System.err.println(exception);
                }
            }
            dragExit(component);
            return dropAction;
        }
    });
    copyImageButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            Image image = imageView.getImage();
            if (image != null) {
                LocalManifest clipboardContent = new LocalManifest();
                clipboardContent.putImage(image);
                Clipboard.setContent(clipboardContent);
            }
        }
    });
    pasteImageButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            Manifest clipboardContent = Clipboard.getContent();
            if (clipboardContent != null && clipboardContent.containsImage()) {
                try {
                    imageView.setImage(clipboardContent.getImage());
                } catch (IOException exception) {
                    System.err.println(exception);
                }
            }
        }
    });
    // Files
    listView.setListData(new FileList());
    listView.setDragSource(new DragSource() {

        private LocalManifest content = null;

        @Override
        public boolean beginDrag(Component component, int x, int y) {
            ListView listViewLocal = (ListView) component;
            FileList fileList = (FileList) listViewLocal.getListData();
            if (fileList.getLength() > 0) {
                content = new LocalManifest();
                content.putFileList(fileList);
            }
            return (content != null);
        }

        @Override
        public void endDrag(Component component, DropAction dropAction) {
            content = null;
        }

        @Override
        public boolean isNative() {
            return true;
        }

        @Override
        public LocalManifest getContent() {
            return content;
        }

        @Override
        public Visual getRepresentation() {
            return null;
        }

        @Override
        public Point getOffset() {
            return null;
        }

        @Override
        public int getSupportedDropActions() {
            return DropAction.COPY.getMask();
        }
    });
    listView.setDropTarget(new DropTarget() {

        @Override
        public DropAction dragEnter(Component component, Manifest dragContent, int supportedDropActions, DropAction userDropAction) {
            DropAction dropAction = null;
            if (dragContent.containsFileList() && DropAction.COPY.isSelected(supportedDropActions)) {
                dropAction = DropAction.COPY;
            }
            return dropAction;
        }

        @Override
        public void dragExit(Component component) {
        // empty block
        }

        @Override
        public DropAction dragMove(Component component, Manifest dragContent, int supportedDropActions, int x, int y, DropAction userDropAction) {
            return (dragContent.containsFileList() ? DropAction.COPY : null);
        }

        @Override
        public DropAction userDropActionChange(Component component, Manifest dragContent, int supportedDropActions, int x, int y, DropAction userDropAction) {
            return (dragContent.containsFileList() ? DropAction.COPY : null);
        }

        @Override
        public DropAction drop(Component component, Manifest dragContent, int supportedDropActions, int x, int y, DropAction userDropAction) {
            DropAction dropAction = null;
            if (dragContent.containsFileList()) {
                try {
                    listView.setListData(dragContent.getFileList());
                    dropAction = DropAction.COPY;
                } catch (IOException exception) {
                    System.err.println(exception);
                }
            }
            dragExit(component);
            return dropAction;
        }
    });
    copyFilesButton.getButtonPressListeners().add(new ButtonPressListener() {

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

        @Override
        public void buttonPressed(Button button) {
        // TODO
        }
    });
}
Also used : FileList(org.apache.pivot.io.FileList) DropAction(org.apache.pivot.wtk.DropAction) DragSource(org.apache.pivot.wtk.DragSource) Point(org.apache.pivot.wtk.Point) IOException(java.io.IOException) LocalManifest(org.apache.pivot.wtk.LocalManifest) Manifest(org.apache.pivot.wtk.Manifest) Image(org.apache.pivot.wtk.media.Image) LocalManifest(org.apache.pivot.wtk.LocalManifest) Point(org.apache.pivot.wtk.Point) ButtonPressListener(org.apache.pivot.wtk.ButtonPressListener) ListView(org.apache.pivot.wtk.ListView) PushButton(org.apache.pivot.wtk.PushButton) Button(org.apache.pivot.wtk.Button) DropTarget(org.apache.pivot.wtk.DropTarget) Component(org.apache.pivot.wtk.Component) Visual(org.apache.pivot.wtk.Visual)

Aggregations

ListView (org.apache.pivot.wtk.ListView)28 ArrayList (org.apache.pivot.collections.ArrayList)10 Button (org.apache.pivot.wtk.Button)10 List (org.apache.pivot.collections.List)8 ButtonPressListener (org.apache.pivot.wtk.ButtonPressListener)7 PushButton (org.apache.pivot.wtk.PushButton)6 Span (org.apache.pivot.wtk.Span)6 ListViewSelectionListener (org.apache.pivot.wtk.ListViewSelectionListener)4 Sheet (org.apache.pivot.wtk.Sheet)4 SheetCloseListener (org.apache.pivot.wtk.SheetCloseListener)4 File (java.io.File)3 Bounds (org.apache.pivot.wtk.Bounds)3 BoxPane (org.apache.pivot.wtk.BoxPane)3 Component (org.apache.pivot.wtk.Component)3 FileBrowserSheet (org.apache.pivot.wtk.FileBrowserSheet)3 Frame (org.apache.pivot.wtk.Frame)3 Color (java.awt.Color)2 IOException (java.io.IOException)2 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)2 Sequence (org.apache.pivot.collections.Sequence)2