Search in sources :

Example 1 with VFSBrowserListener

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

Aggregations

IOException (java.io.IOException)1 FileObject (org.apache.commons.vfs2.FileObject)1 FileSystemException (org.apache.commons.vfs2.FileSystemException)1 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)1 Sequence (org.apache.pivot.collections.Sequence)1 SerializationException (org.apache.pivot.serialization.SerializationException)1 Button (org.apache.pivot.wtk.Button)1 ButtonPressListener (org.apache.pivot.wtk.ButtonPressListener)1 Component (org.apache.pivot.wtk.Component)1 ComponentMouseButtonListener (org.apache.pivot.wtk.ComponentMouseButtonListener)1 Mouse (org.apache.pivot.wtk.Mouse)1 PushButton (org.apache.pivot.wtk.PushButton)1 TextInput (org.apache.pivot.wtk.TextInput)1 TextInputContentListener (org.apache.pivot.wtk.TextInputContentListener)1 VFSBrowser (org.apache.pivot.wtk.VFSBrowser)1 VFSBrowserListener (org.apache.pivot.wtk.VFSBrowserListener)1 VFSBrowserSheet (org.apache.pivot.wtk.VFSBrowserSheet)1