Search in sources :

Example 61 with Component

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

the class FlowPaneSkin method getBaseline.

@Override
public int getBaseline(int width, int height) {
    FlowPane flowPane = (FlowPane) getComponent();
    int baseline = -1;
    if (alignToBaseline) {
        int contentWidth = Math.max(width - padding.getWidth(), 0);
        // Break the components into multiple rows, and calculate the
        // baseline of the first row
        int rowWidth = 0;
        for (int i = 0, n = flowPane.getLength(); i < n; i++) {
            Component component = flowPane.get(i);
            if (component.isVisible()) {
                Dimensions size = component.getPreferredSize();
                if (rowWidth + size.width > contentWidth && rowWidth > 0) {
                    // and it is not the only component in this row; wrap
                    break;
                }
                baseline = Math.max(baseline, component.getBaseline(size.width, size.height));
                rowWidth += size.width + horizontalSpacing;
            }
        }
        // Include top padding value
        baseline += padding.top;
    }
    return baseline;
}
Also used : FlowPane(org.apache.pivot.wtk.FlowPane) Dimensions(org.apache.pivot.wtk.Dimensions) Component(org.apache.pivot.wtk.Component)

Example 62 with Component

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

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

the class ScaleDecoratorDemo method startup.

@Override
public void startup(Display display, Map<String, String> properties) throws Exception {
    BXMLSerializer bxmlSerializer = new BXMLSerializer();
    scaleWindow = (Window) bxmlSerializer.readObject(DecoratorDemo.class, "scale_window.bxml");
    bxmlSerializer.bind(this);
    imageView.getComponentMouseWheelListeners().add(new ComponentMouseWheelListener() {

        @Override
        public boolean mouseWheel(Component component, Mouse.ScrollType scrollType, int scrollAmount, int wheelRotation, int x, int y) {
            // Note: both scale values are the same
            float currentScale = scaleDecorator.getScaleX();
            if (wheelRotation < 0) {
                // UP == zoom in, make scale larger
                scaleDecorator.setScale(currentScale * 2.0f);
            } else {
                // DOWN == zoom out, make scale smaller
                scaleDecorator.setScale(currentScale / 2.0f);
            }
            component.repaint();
            return true;
        }
    });
    scaleWindow.open(display);
}
Also used : Mouse(org.apache.pivot.wtk.Mouse) ComponentMouseWheelListener(org.apache.pivot.wtk.ComponentMouseWheelListener) Component(org.apache.pivot.wtk.Component) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer)

Example 64 with Component

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

the class FileDropTargetDemo method initialize.

@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
    fileList = new FileList();
    fileTableView.setTableData(fileList);
    fileList.getListListeners().add(new ListListener<File>() {

        @Override
        public void itemInserted(List<File> list, int index) {
            uploadButton.setEnabled(list.getLength() > 0);
        }

        @Override
        public void itemsRemoved(List<File> list, int index, Sequence<File> files) {
            uploadButton.setEnabled(list.getLength() > 0);
            if (fileTableView.isFocused() && index < list.getLength()) {
                fileTableView.setSelectedIndex(index);
            }
        }
    });
    fileTableView.getComponentKeyListeners().add(new ComponentKeyListener() {

        @Override
        public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
            if (keyCode == Keyboard.KeyCode.DELETE || keyCode == Keyboard.KeyCode.BACKSPACE) {
                Sequence<Span> selectedRanges = fileTableView.getSelectedRanges();
                for (int i = selectedRanges.getLength() - 1; i >= 0; i--) {
                    Span range = selectedRanges.get(i);
                    int index = range.start;
                    int count = range.end - index + 1;
                    fileList.remove(index, count);
                }
            }
            return false;
        }
    });
    fileTableView.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 {
                    FileList tableData = (FileList) fileTableView.getTableData();
                    FileList fileListLocal = dragContent.getFileList();
                    for (File file : fileListLocal) {
                        if (file.isDirectory()) {
                        // TODO Expand recursively
                        }
                        tableData.add(file);
                    }
                    dropAction = DropAction.COPY;
                } catch (IOException exception) {
                    System.err.println(exception);
                }
            }
            dragExit(component);
            return dropAction;
        }
    });
    uploadButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            Prompt.prompt(MessageType.INFO, "Pretending to upload...", FileDropTargetDemo.this);
        }
    });
}
Also used : FileList(org.apache.pivot.io.FileList) Keyboard(org.apache.pivot.wtk.Keyboard) DropAction(org.apache.pivot.wtk.DropAction) Sequence(org.apache.pivot.collections.Sequence) IOException(java.io.IOException) Manifest(org.apache.pivot.wtk.Manifest) Span(org.apache.pivot.wtk.Span) ComponentKeyListener(org.apache.pivot.wtk.ComponentKeyListener) ButtonPressListener(org.apache.pivot.wtk.ButtonPressListener) PushButton(org.apache.pivot.wtk.PushButton) Button(org.apache.pivot.wtk.Button) DropTarget(org.apache.pivot.wtk.DropTarget) Component(org.apache.pivot.wtk.Component) File(java.io.File)

Example 65 with Component

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

the class BorderTest method startup.

@Override
public void startup(Display display, Map<String, String> properties) throws Exception {
    BXMLSerializer bxmlSerializer = new BXMLSerializer();
    frame = new Frame((Component) bxmlSerializer.readObject(getClass().getResource("border_test.bxml")));
    frame.setTitle("Border Test");
    frame.setPreferredSize(480, 360);
    frame.open(display);
}
Also used : Frame(org.apache.pivot.wtk.Frame) Component(org.apache.pivot.wtk.Component) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer)

Aggregations

Component (org.apache.pivot.wtk.Component)209 Dimensions (org.apache.pivot.wtk.Dimensions)40 Point (org.apache.pivot.wtk.Point)38 GradientPaint (java.awt.GradientPaint)33 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)24 TextInput (org.apache.pivot.wtk.TextInput)21 Label (org.apache.pivot.wtk.Label)20 BoxPane (org.apache.pivot.wtk.BoxPane)18 Paint (java.awt.Paint)17 Button (org.apache.pivot.wtk.Button)15 PushButton (org.apache.pivot.wtk.PushButton)14 ScrollPane (org.apache.pivot.wtk.ScrollPane)14 TablePane (org.apache.pivot.wtk.TablePane)14 Window (org.apache.pivot.wtk.Window)14 IOException (java.io.IOException)13 ButtonPressListener (org.apache.pivot.wtk.ButtonPressListener)13 Frame (org.apache.pivot.wtk.Frame)13 FlowPane (org.apache.pivot.wtk.FlowPane)12 ComponentStateListener (org.apache.pivot.wtk.ComponentStateListener)11 ComponentMouseButtonListener (org.apache.pivot.wtk.ComponentMouseButtonListener)10