Search in sources :

Example 1 with Visual

use of org.apache.pivot.wtk.Visual 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)

Example 2 with Visual

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

the class NativeDragDropTest method startup.

@Override
public void startup(final Display display, Map<String, String> properties) throws Exception {
    final Label label = new Label("http://pivot.apache.org/");
    label.getStyles().put(Style.font, new Font("Arial", Font.PLAIN, 24));
    label.getStyles().put(Style.horizontalAlignment, HorizontalAlignment.CENTER);
    label.getStyles().put(Style.verticalAlignment, VerticalAlignment.CENTER);
    label.setDragSource(new DragSource() {

        private LocalManifest content = null;

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

        @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()) {
                frame.getStyles().put(Style.backgroundColor, "#ffcccc");
                dropAction = DropAction.COPY;
            }
            return dropAction;
        }

        @Override
        public void dragExit(Component component) {
            frame.getStyles().put(Style.backgroundColor, "#ffffff");
        }

        @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()) {
                Label labelLocal = (Label) component;
                try {
                    labelLocal.setText(dragContent.getText());
                } catch (IOException exception) {
                    System.err.println(exception);
                }
            }
            dragExit(component);
            return dropAction;
        }
    });
    frame = new Frame(label);
    frame.open(display);
}
Also used : Frame(org.apache.pivot.wtk.Frame) Label(org.apache.pivot.wtk.Label) 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) Font(java.awt.Font) LocalManifest(org.apache.pivot.wtk.LocalManifest) Point(org.apache.pivot.wtk.Point) DropTarget(org.apache.pivot.wtk.DropTarget) Component(org.apache.pivot.wtk.Component) Visual(org.apache.pivot.wtk.Visual)

Aggregations

IOException (java.io.IOException)2 Component (org.apache.pivot.wtk.Component)2 DragSource (org.apache.pivot.wtk.DragSource)2 DropAction (org.apache.pivot.wtk.DropAction)2 DropTarget (org.apache.pivot.wtk.DropTarget)2 LocalManifest (org.apache.pivot.wtk.LocalManifest)2 Manifest (org.apache.pivot.wtk.Manifest)2 Point (org.apache.pivot.wtk.Point)2 Visual (org.apache.pivot.wtk.Visual)2 Font (java.awt.Font)1 FileList (org.apache.pivot.io.FileList)1 Button (org.apache.pivot.wtk.Button)1 ButtonPressListener (org.apache.pivot.wtk.ButtonPressListener)1 Frame (org.apache.pivot.wtk.Frame)1 Label (org.apache.pivot.wtk.Label)1 ListView (org.apache.pivot.wtk.ListView)1 PushButton (org.apache.pivot.wtk.PushButton)1 Image (org.apache.pivot.wtk.media.Image)1