Search in sources :

Example 1 with VFSBrowserSheet

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

the class TerraVFSBrowserSheetSkin method previewSheetClose.

@Override
public Vote previewSheetClose(final Sheet sheet, final boolean result) {
    Vote vote = null;
    if (result && !okButton.isEnabled()) {
        vote = Vote.DENY;
    } else {
        if (result) {
            updatingSelection = true;
            VFSBrowserSheet fileBrowserSheet = (VFSBrowserSheet) sheet;
            VFSBrowserSheet.Mode mode = fileBrowserSheet.getMode();
            FileSystemManager manager = fileBrowserSheet.getManager();
            FileName baseFileName = fileBrowserSheet.getBaseFileName();
            switch(mode) {
                case OPEN:
                case OPEN_MULTIPLE:
                case SAVE_TO:
                    {
                        try {
                            fileBrowserSheet.setSelectedFiles(fileBrowser.getSelectedFiles());
                        } catch (FileSystemException fse) {
                            throw new RuntimeException(fse);
                        }
                        break;
                    }
                case SAVE_AS:
                    {
                        String fileName = saveAsTextInput.getText();
                        // current location and let the chips fall ...
                        try {
                            FileObject selectedFile = manager.resolveFile(fileBrowser.getRootDirectory(), fileName);
                            // !fileName.startsWith(File.separator)) {
                            if (selectedFile.exists() && selectedFile.getType() == FileType.FOLDER) {
                                try {
                                    // TODO: what to do about canonical file representations?
                                    FileObject root = /* selectedFile.getCanonicalFile(); */
                                    selectedFile;
                                    fileBrowserSheet.setRootDirectory(root);
                                    fileBrowser.setRootDirectory(root);
                                    setHostLabel(root);
                                    saveAsTextInput.setText("");
                                } catch (IOException ioe) {
                                    Form.setFlag(saveAsBoxPane, new Form.Flag());
                                }
                                selectedFile = null;
                                vote = Vote.DENY;
                            } else {
                                FileObject root = selectedFile.getParent();
                                if (root != null && root.exists() && root.getType() == FileType.FOLDER) {
                                    try {
                                        // TODO: canonical file again
                                        // fileBrowserSheet.setRootDirectory(root.getCanonicalFile());
                                        fileBrowserSheet.setRootDirectory(root);
                                        setHostLabel(root);
                                        selectedFile = manager.resolveFile(selectedFile.getName().getURI());
                                    } catch (IOException ioe) {
                                        Form.setFlag(saveAsBoxPane, new Form.Flag());
                                        selectedFile = null;
                                        vote = Vote.DENY;
                                    }
                                } else {
                                    // Could be an error message here
                                    // ("Directory does not exist")
                                    Form.setFlag(saveAsBoxPane, new Form.Flag());
                                    selectedFile = null;
                                    vote = Vote.DENY;
                                }
                            }
                            if (selectedFile != null) {
                                fileBrowserSheet.setSelectedFiles(new ArrayList<>(selectedFile));
                            }
                        } catch (FileSystemException fse) {
                            Form.setFlag(saveAsBoxPane, new Form.Flag());
                            vote = Vote.DENY;
                        }
                        break;
                    }
                default:
                    break;
            }
            updatingSelection = false;
        }
        if (vote == null) {
            vote = super.previewSheetClose(sheet, result);
        }
    }
    return vote;
}
Also used : Vote(org.apache.pivot.util.Vote) Form(org.apache.pivot.wtk.Form) FileName(org.apache.commons.vfs2.FileName) ArrayList(org.apache.pivot.collections.ArrayList) IOException(java.io.IOException) FileSystemManager(org.apache.commons.vfs2.FileSystemManager) FileSystemException(org.apache.commons.vfs2.FileSystemException) VFSBrowserSheet(org.apache.pivot.wtk.VFSBrowserSheet) FileObject(org.apache.commons.vfs2.FileObject)

Example 2 with VFSBrowserSheet

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

the class TerraVFSBrowserSheetSkin method updateOKButtonState.

private void updateOKButtonState() {
    VFSBrowserSheet fileBrowserSheet = (VFSBrowserSheet) getComponent();
    VFSBrowserSheet.Mode mode = fileBrowserSheet.getMode();
    Sequence<FileObject> selectedFiles = fileBrowser.getSelectedFiles();
    switch(mode) {
        case OPEN:
        case OPEN_MULTIPLE:
            {
                okButton.setEnabled(selectedFiles.getLength() > 0 && selectedDirectoryCount == 0);
                break;
            }
        case SAVE_AS:
            {
                okButton.setEnabled(saveAsTextInput.getCharacterCount() > 0);
                break;
            }
        case SAVE_TO:
            {
                okButton.setEnabled(selectedDirectoryCount > 0);
                break;
            }
        default:
            break;
    }
}
Also used : VFSBrowserSheet(org.apache.pivot.wtk.VFSBrowserSheet) FileObject(org.apache.commons.vfs2.FileObject)

Example 3 with VFSBrowserSheet

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

the class VFSBrowserTest method startup.

@Override
public void startup(final Display display, final Map<String, String> properties) throws Exception {
    BoxPane windowContent = new BoxPane();
    windowContent.getStyles().put(Style.verticalAlignment, VerticalAlignment.CENTER);
    final Checkbox showHiddenCheckbox = new Checkbox("Show hidden files");
    windowContent.add(showHiddenCheckbox);
    PushButton button = new PushButton("Open File Browser");
    button.getStyles().put(Style.padding, "[2, 4, 2, 4]");
    button.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(final Button buttonArgument) {
            try {
                final VFSBrowserSheet vfsBrowserSheet = new VFSBrowserSheet(VFSBrowserSheet.Mode.OPEN);
                vfsBrowserSheet.getStyles().put(Style.showHiddenFiles, showHiddenCheckbox.isSelected());
                vfsBrowserSheet.open(frame, new SheetCloseListener() {

                    @Override
                    public void sheetClosed(final Sheet sheet) {
                        if (sheet.getResult()) {
                            Sequence<FileObject> selectedFiles = vfsBrowserSheet.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, frame);
                        } else {
                            Alert.alert(MessageType.INFO, "You didn't select anything.", frame);
                        }
                    }
                });
            } catch (FileSystemException fse) {
                Alert.alert(MessageType.ERROR, String.format("File System Exception: %1$s", fse.getMessage()), frame);
            }
        }
    });
    windowContent.add(button);
    frame = new Frame(windowContent);
    frame.setMaximized(true);
    frame.open(display);
}
Also used : Frame(org.apache.pivot.wtk.Frame) SheetCloseListener(org.apache.pivot.wtk.SheetCloseListener) ButtonPressListener(org.apache.pivot.wtk.ButtonPressListener) FileSystemException(org.apache.commons.vfs2.FileSystemException) VFSBrowserSheet(org.apache.pivot.wtk.VFSBrowserSheet) ListView(org.apache.pivot.wtk.ListView) BoxPane(org.apache.pivot.wtk.BoxPane) PushButton(org.apache.pivot.wtk.PushButton) Button(org.apache.pivot.wtk.Button) Checkbox(org.apache.pivot.wtk.Checkbox) FileObject(org.apache.commons.vfs2.FileObject) PushButton(org.apache.pivot.wtk.PushButton) Sheet(org.apache.pivot.wtk.Sheet) VFSBrowserSheet(org.apache.pivot.wtk.VFSBrowserSheet)

Example 4 with VFSBrowserSheet

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

the class TerraVFSBrowserSheetSkin method install.

@Override
public void install(Component component) {
    super.install(component);
    final VFSBrowserSheet fileBrowserSheet = (VFSBrowserSheet) component;
    fileBrowserSheet.setMinimumWidth(360);
    fileBrowserSheet.setMinimumHeight(180);
    // Load the sheet content
    BXMLSerializer bxmlSerializer = new BXMLSerializer();
    Component content;
    try {
        content = (Component) bxmlSerializer.readObject(TerraVFSBrowserSheetSkin.class, "terra_vfs_browser_sheet_skin.bxml", true);
    } catch (IOException exception) {
        throw new RuntimeException(exception);
    } catch (SerializationException exception) {
        throw new RuntimeException(exception);
    }
    fileBrowserSheet.setContent(content);
    bxmlSerializer.bind(this, TerraVFSBrowserSheetSkin.class);
    // set the same rootDirectory as the component
    try {
        FileObject rootDirectory = fileBrowserSheet.getRootDirectory();
        fileBrowser.setRootDirectory(rootDirectory);
        setHostLabel(rootDirectory);
    } catch (FileSystemException fse) {
        throw new RuntimeException(fse);
    }
    // set the same homeDirectory as the component
    try {
        fileBrowser.setHomeDirectory(fileBrowserSheet.getHomeDirectory());
    } catch (FileSystemException fse) {
        throw new RuntimeException(fse);
    }
    saveAsTextInput.getTextInputContentListeners().add(new TextInputContentListener() {

        @Override
        public void textChanged(TextInput textInput) {
            Form.clearFlag(saveAsBoxPane);
            updateOKButtonState();
        }
    });
    fileBrowser.getFileBrowserListeners().add(new VFSBrowserListener() {

        @Override
        public void rootDirectoryChanged(VFSBrowser fileBrowserArgument, FileObject previousRootDirectory) {
            updatingSelection = true;
            try {
                FileObject rootDirectory = fileBrowserArgument.getRootDirectory();
                fileBrowserSheet.setRootDirectory(rootDirectory);
                setHostLabel(rootDirectory);
            } catch (FileSystemException fse) {
                throw new RuntimeException(fse);
            }
            updatingSelection = false;
            selectedDirectoryCount = 0;
            updateOKButtonState();
        }

        @Override
        public void homeDirectoryChanged(VFSBrowser fileBrowserArgument, FileObject previousHomeDirectory) {
            updatingSelection = true;
            try {
                fileBrowserSheet.setHomeDirectory(fileBrowserArgument.getHomeDirectory());
            } catch (FileSystemException fse) {
                throw new RuntimeException(fse);
            }
            updatingSelection = false;
        }

        @Override
        public void selectedFileAdded(VFSBrowser fileBrowserArgument, FileObject file) {
            if (file.getName().getType() == FileType.FOLDER) {
                selectedDirectoryCount++;
            }
            updateOKButtonState();
        }

        @Override
        public void selectedFileRemoved(VFSBrowser fileBrowserArgument, FileObject file) {
            if (file.getName().getType() == FileType.FOLDER) {
                selectedDirectoryCount--;
            }
            updateOKButtonState();
        }

        @Override
        public void selectedFilesChanged(VFSBrowser fileBrowserArgument, Sequence<FileObject> previousSelectedFiles) {
            selectedDirectoryCount = 0;
            Sequence<FileObject> selectedFiles = fileBrowserArgument.getSelectedFiles();
            for (int i = 0, n = selectedFiles.getLength(); i < n; i++) {
                FileObject selectedFile = selectedFiles.get(i);
                if (selectedFile.getName().getType() == FileType.FOLDER) {
                    selectedDirectoryCount++;
                }
            }
            if (!fileBrowserArgument.isMultiSelect()) {
                FileObject selectedFile = fileBrowserArgument.getSelectedFile();
                if (selectedFile != null && selectedFile.getName().getType() != FileType.FOLDER) {
                    saveAsTextInput.setText(selectedFile.getName().getPath());
                }
            }
            updateOKButtonState();
        }
    });
    fileBrowser.getComponentMouseButtonListeners().add(new ComponentMouseButtonListener() {

        private FileObject file = null;

        @Override
        public boolean mouseClick(Component componentArgument, Mouse.Button button, int x, int y, int count) {
            boolean consumed = false;
            VFSBrowserSheet.Mode mode = fileBrowserSheet.getMode();
            if (count == 1) {
                file = fileBrowser.getFileAt(x, y);
            } else if (count == 2) {
                FileObject fileLocal = fileBrowser.getFileAt(x, y);
                if (fileLocal != null && this.file != null && fileLocal.equals(this.file) && fileBrowser.isFileSelected(fileLocal)) {
                    if (mode == VFSBrowserSheet.Mode.OPEN || mode == VFSBrowserSheet.Mode.OPEN_MULTIPLE) {
                        if (fileLocal.getName().getType() != FileType.FOLDER) {
                            fileBrowserSheet.close(true);
                            consumed = true;
                        }
                    }
                }
            }
            return consumed;
        }
    });
    okButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            fileBrowserSheet.close(true);
        }
    });
    cancelButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            fileBrowserSheet.close(false);
        }
    });
    // Add this as a file browser sheet listener
    fileBrowserSheet.getFileBrowserSheetListeners().add(this);
    modeChanged(fileBrowserSheet, null);
    homeDirectoryChanged(fileBrowserSheet, null);
    rootDirectoryChanged(fileBrowserSheet, null);
    selectedFilesChanged(fileBrowserSheet, null);
}
Also used : SerializationException(org.apache.pivot.serialization.SerializationException) VFSBrowserListener(org.apache.pivot.wtk.VFSBrowserListener) IOException(java.io.IOException) Sequence(org.apache.pivot.collections.Sequence) ButtonPressListener(org.apache.pivot.wtk.ButtonPressListener) FileSystemException(org.apache.commons.vfs2.FileSystemException) TextInputContentListener(org.apache.pivot.wtk.TextInputContentListener) VFSBrowserSheet(org.apache.pivot.wtk.VFSBrowserSheet) Mouse(org.apache.pivot.wtk.Mouse) PushButton(org.apache.pivot.wtk.PushButton) Button(org.apache.pivot.wtk.Button) ComponentMouseButtonListener(org.apache.pivot.wtk.ComponentMouseButtonListener) FileObject(org.apache.commons.vfs2.FileObject) Component(org.apache.pivot.wtk.Component) TextInput(org.apache.pivot.wtk.TextInput) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer) VFSBrowser(org.apache.pivot.wtk.VFSBrowser)

Example 5 with VFSBrowserSheet

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

the class TerraVFSBrowserSheetSkin method updateDisabledFileFilter.

private void updateDisabledFileFilter() {
    VFSBrowserSheet fileBrowserSheet = (VFSBrowserSheet) getComponent();
    Filter<FileObject> disabledFileFilter = fileBrowserSheet.getDisabledFileFilter();
    VFSBrowserSheet.Mode mode = fileBrowserSheet.getMode();
    if (mode == VFSBrowserSheet.Mode.SAVE_TO) {
        disabledFileFilter = new SaveToFileFilter(disabledFileFilter);
    }
    fileBrowser.setDisabledFileFilter(disabledFileFilter);
}
Also used : VFSBrowserSheet(org.apache.pivot.wtk.VFSBrowserSheet) FileObject(org.apache.commons.vfs2.FileObject)

Aggregations

FileObject (org.apache.commons.vfs2.FileObject)5 VFSBrowserSheet (org.apache.pivot.wtk.VFSBrowserSheet)5 FileSystemException (org.apache.commons.vfs2.FileSystemException)3 IOException (java.io.IOException)2 Button (org.apache.pivot.wtk.Button)2 ButtonPressListener (org.apache.pivot.wtk.ButtonPressListener)2 PushButton (org.apache.pivot.wtk.PushButton)2 FileName (org.apache.commons.vfs2.FileName)1 FileSystemManager (org.apache.commons.vfs2.FileSystemManager)1 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)1 ArrayList (org.apache.pivot.collections.ArrayList)1 Sequence (org.apache.pivot.collections.Sequence)1 SerializationException (org.apache.pivot.serialization.SerializationException)1 Vote (org.apache.pivot.util.Vote)1 BoxPane (org.apache.pivot.wtk.BoxPane)1 Checkbox (org.apache.pivot.wtk.Checkbox)1 Component (org.apache.pivot.wtk.Component)1 ComponentMouseButtonListener (org.apache.pivot.wtk.ComponentMouseButtonListener)1 Form (org.apache.pivot.wtk.Form)1 Frame (org.apache.pivot.wtk.Frame)1