Search in sources :

Example 1 with FileBrowserListener

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

the class TerraFileBrowserSheetSkin method install.

@Override
public void install(Component component) {
    super.install(component);
    final FileBrowserSheet fileBrowserSheet = (FileBrowserSheet) component;
    fileBrowserSheet.setMinimumWidth(360);
    fileBrowserSheet.setMinimumHeight(180);
    // Load the sheet content
    BXMLSerializer bxmlSerializer = new BXMLSerializer();
    Component content;
    try {
        content = (Component) bxmlSerializer.readObject(TerraFileBrowserSheetSkin.class, "terra_file_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, TerraFileBrowserSheetSkin.class);
    // set the same rootDirectory to fileBrowser
    fileBrowser.setRootDirectory(fileBrowserSheet.getRootDirectory());
    saveAsTextInput.getTextInputContentListeners().add(new TextInputContentListener() {

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

        @Override
        public void rootDirectoryChanged(FileBrowser fileBrowserArgument, File previousRootDirectory) {
            updatingSelection = true;
            fileBrowserSheet.setRootDirectory(fileBrowserArgument.getRootDirectory());
            updatingSelection = false;
            selectedDirectoryCount = 0;
            updateOKButtonState();
        }

        @Override
        public void selectedFileAdded(FileBrowser fileBrowserArgument, File file) {
            if (file.isDirectory()) {
                selectedDirectoryCount++;
            }
            updateOKButtonState();
        }

        @Override
        public void selectedFileRemoved(FileBrowser fileBrowserArgument, File file) {
            if (file.isDirectory()) {
                selectedDirectoryCount--;
            }
            updateOKButtonState();
        }

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

        private File file = null;

        @Override
        public boolean mouseClick(Component componentArgument, Mouse.Button button, int x, int y, int count) {
            boolean consumed = false;
            FileBrowserSheet.Mode mode = fileBrowserSheet.getMode();
            if (count == 1) {
                file = fileBrowser.getFileAt(x, y);
            } else if (count == 2) {
                File fileLocal = fileBrowser.getFileAt(x, y);
                if (fileLocal != null && this.file != null && fileLocal.equals(this.file) && fileBrowser.isFileSelected(fileLocal)) {
                    if (mode == FileBrowserSheet.Mode.OPEN || mode == FileBrowserSheet.Mode.OPEN_MULTIPLE) {
                        if (!fileLocal.isDirectory()) {
                            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);
    rootDirectoryChanged(fileBrowserSheet, null);
    selectedFilesChanged(fileBrowserSheet, null);
}
Also used : FileBrowserListener(org.apache.pivot.wtk.FileBrowserListener) SerializationException(org.apache.pivot.serialization.SerializationException) FileBrowserSheet(org.apache.pivot.wtk.FileBrowserSheet) IOException(java.io.IOException) Sequence(org.apache.pivot.collections.Sequence) ButtonPressListener(org.apache.pivot.wtk.ButtonPressListener) TextInputContentListener(org.apache.pivot.wtk.TextInputContentListener) Mouse(org.apache.pivot.wtk.Mouse) PushButton(org.apache.pivot.wtk.PushButton) Button(org.apache.pivot.wtk.Button) FileBrowser(org.apache.pivot.wtk.FileBrowser) ComponentMouseButtonListener(org.apache.pivot.wtk.ComponentMouseButtonListener) Component(org.apache.pivot.wtk.Component) TextInput(org.apache.pivot.wtk.TextInput) File(java.io.File) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)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 FileBrowser (org.apache.pivot.wtk.FileBrowser)1 FileBrowserListener (org.apache.pivot.wtk.FileBrowserListener)1 FileBrowserSheet (org.apache.pivot.wtk.FileBrowserSheet)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