All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jaxx.runtime.swing.editor.FileEditorHandler Maven / Gradle / Ivy

There is a newer version: 3.0-alpha-1
Show newest version
/*
 * #%L
 * JAXX :: Widgets
 * %%
 * Copyright (C) 2008 - 2014 Code Lutin, Tony Chemit
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * .
 * #L%
 */
package jaxx.runtime.swing.editor;

import com.google.common.io.Files;
import org.apache.commons.lang3.StringUtils;

import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import java.io.File;

/** @author Sylvain Lletellier */
public class FileEditorHandler {

    public static final String SEPARATOR_REGEX = "\\s*,\\s*";

    public static File lastSelectedPath;

    protected FileEditor view;

    /**
     * To set the dialog (see https://forge.nuiton.org/issues/2578).
     *
     * @since 2.5.12
     */
    protected JDialog dialogOwner;

    public FileEditorHandler(FileEditor view) {
        this.view = view;
        if (lastSelectedPath == null) {
            lastSelectedPath = new File(System.getProperty("user.home"));
        }
    }

    public void openLocation() {

        // use last selected file
        File startFile = view.getSelectedFile();
        String startPath = view.getStartPath();
        if (startFile == null && StringUtils.isNotEmpty(startPath)) {

            // else filed start path
            startFile = new File(startPath);
        } else if (startFile == null) {

            // else start with user home
            startFile = lastSelectedPath;
        }
        JFileChooser fc = new JFileChooser(startFile);

        fc.setDialogTitle(view.getTitle());
        fc.setAcceptAllFileFilterUsed(view.getAcceptAllFileFilterUsed());

        // TODO sletellier 14/06/2012 : activate multi selection
//        boolean multiSelectionEnabled = view.isMultiSelectionEnabled();
//        fc.setMultiSelectionEnabled(multiSelectionEnabled);

        // used to enable directory selection
        boolean directoryEnabled = view.isDirectoryEnabled();
        if (directoryEnabled) {
            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        }

        // used to enable file selection
        boolean fileEnabled = view.isFileEnabled();
        if (fileEnabled) {

            if (directoryEnabled) {

                // both
                fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            } else {
                fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
            }
            String extsAsString = view.getExts();
            if (extsAsString != null) {

                // extentions can be separted by comma
                String[] exts = extsAsString.split(SEPARATOR_REGEX);
                String extsDescription = view.getExtsDescription();

                String[] descs = new String[0];
                if (extsDescription != null) {
                    descs = extsDescription.split(SEPARATOR_REGEX);
                }
                for (int i = 0;i i) {
                        desc = descs[i];
                    }

                    fc.addChoosableFileFilter(new ExtentionFileFiler(ext, desc));
                }
            }
        }

        // directory or/and file must be enabled
        if (!directoryEnabled && !fileEnabled) {
            throw new IllegalArgumentException("You must enable at least file or directory to open dialog");
        }

        // show dialog
        // see https://forge.nuiton.org/issues/2578
        boolean hackDialog = dialogOwner != null && dialogOwner.isAlwaysOnTop();
        if (hackDialog) {
            dialogOwner.setAlwaysOnTop(false);
        }
        int returnVal = fc.showOpenDialog(view);
        if (hackDialog) {
            dialogOwner.setAlwaysOnTop(true);
        }
        if (returnVal == JFileChooser.APPROVE_OPTION) {

            // get selected to display in ui
            File file = fc.getSelectedFile();

            setSelectedFile(file);
        }
    }

    public void setSelectedFile(String path) {
        setSelectedFile(new File(path));
    }

    public void setSelectedFile(File file) {
        view.setSelectedFile(file);
        view.setStartPath(file.getPath());
        File dir = file;
        if (dir.exists()) {
            if (!dir.isDirectory()) {
                dir = dir.getParentFile();
            }
            lastSelectedPath = dir;
        }
    }

    protected void setDialogOwner(JDialog dialogOwner) {
        this.dialogOwner = dialogOwner;
    }

    public static class ExtentionFileFiler extends FileFilter {
        protected String ext;
        protected String desciption;

        public ExtentionFileFiler(String ext, String desciption) {
            this.ext = ext;
            this.desciption = desciption;
        }

        @Override
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            }
            String fileExtension = Files.getFileExtension(file.getName());
            return ext.equals(fileExtension);
        }

        @Override
        public String getDescription() {
            return desciption;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy