Package cz.mp.k3bg.gui.helper

Source Code of cz.mp.k3bg.gui.helper.FileChooserBuilder

/*
* FileChooserBuilder.java
*
*  created: 31.10.2011
*  charset: UTF-8
*  license: MIT (X11) (See LICENSE file for full license)
*/
package cz.mp.k3bg.gui.helper;

import cz.mp.k3bg.Application;
import cz.mp.k3bg.TextSource;
import static cz.mp.k3bg.TextSource.getLocText;
import cz.mp.k3bg.misc.DirectoryRestrictedFileSystemView;
import cz.mp.k3bg.misc.ExtFileFilter;
import cz.mp.util.FileUtils;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileSystemView;


/**
* Třída pro přípravu instance {@linkplain JFileChooser}.
*
* @author Martin Pokorný
* @version 0.1
* @see FileChooserField
*/
public class FileChooserBuilder {

    protected JFileChooser fileChooser;
    protected String rootDirPath = null;

    static {
        StandardDialogsLocaliser.localize(TextSource.MAIN);
    }
   
    // -----
   
    /**
     *
     */
    public FileChooserBuilder() {
        this(null);
    }
    /**
     *
     * @param rootDirPath
     */
    public FileChooserBuilder(String rootDirPath) {
        super();
        fileChooser = new JFileChooser();
        setDefaultChooserLabels();
        setRootImpl(rootDirPath);
    }
   
    // -----
    
    /**
     * (vytvoří nový fileChooser)
     */
    private void reinitFileChooser(FileSystemView fsv) {
        String dialogTitle = fileChooser.getDialogTitle();
        String approveButtonText = fileChooser.getApproveButtonText();
        int selectionMode = fileChooser.getFileSelectionMode();
        boolean multiSelection = fileChooser.isMultiSelectionEnabled();
        FileFilter filter = fileChooser.getFileFilter();       
        FileFilter allFilter = fileChooser.getAcceptAllFileFilter();
       
        if (fsv == null) {
            fileChooser = new JFileChooser();
        }
        else {
            fileChooser = new JFileChooser(fsv);
        }

        // nastaví nové instanci JFileChooser hodnoty z původní instance
        fileChooser.setDialogTitle(dialogTitle);
        fileChooser.setApproveButtonText(approveButtonText);
        fileChooser.setFileSelectionMode(selectionMode);
        fileChooser.setMultiSelectionEnabled(multiSelection);
        if (filter != null && ! allFilter.equals(filter)) {           
            fileChooser.setFileFilter(filter);
            fileChooser.setAcceptAllFileFilterUsed(false);
        }
       
        if (rootDirPath != null) {
            fileChooser.setCurrentDirectory(new File(rootDirPath));
        }
    }
   
    /**
     *
     * @param rootDirPath
     */
    public void setRoot(String rootDirPath) {
        setRootImpl(rootDirPath);
    }       
   
    /**
     *
     * @param rootDirPath
     */
    private void setRootImpl(String rootDirPath) {
        if (rootDirPath == null) {
            this.rootDirPath = null;
            reinitFileChooser(null);
        }       
        else {
            this.rootDirPath = FileUtils.getAbsoluteFilePath(rootDirPath);
            File dir = new File(this.rootDirPath);           
            if (dir.isDirectory()) {
                FileSystemView fsv = new DirectoryRestrictedFileSystemView(dir);       
                reinitFileChooser(fsv);
            }
            else {
                this.rootDirPath = null;
                reinitFileChooser(null);
            }
        }       
    }
   
    /**
     *
     */
    public void clear() {
        if (rootDirPath != null) {
            fileChooser.setCurrentDirectory(new File(rootDirPath));
        }
    }

    /**
     *
     * @param selectionMode  {@code JFileChooser.DIRECTORIES_ONLY}
     *      nebo {@code JFileChooser.FILES_ONLY}
     *      nebo {@code JFileChooser.FILES_AND_DIRECTORIES}
     */
    public void setSelectionMode(int selectionMode) {
        if (selectionMode != JFileChooser.DIRECTORIES_ONLY
                && selectionMode != JFileChooser.FILES_ONLY
                && selectionMode != JFileChooser.FILES_AND_DIRECTORIES) {
            throw new IllegalArgumentException("bad selectionType");
        }
        setDefaultChooserLabels();
        fileChooser.setFileSelectionMode(selectionMode);
    }   

   
  /**
   * Nastaví filtr definovaný seznamem přípon souboru a popisem filtru.
   *
   * @param baseDescription
   * @param exts
   */
  public void setFilter(String baseDescription, String... exts) {
    ExtFileFilter filter = new ExtFileFilter(baseDescription, exts);
        setFilter(filter);               
  }
   
    /**
     *
     * @param filter
     */
    public void setFilter(ExtFileFilter filter) {
    fileChooser.setFileFilter(filter);
        fileChooser.setAcceptAllFileFilterUsed(false);       
    }   
   
    /**
     *
     */
    private void setDefaultChooserLabels() {
        if (fileChooser.getFileSelectionMode() ==
                JFileChooser.DIRECTORIES_ONLY) {
            fileChooser.setDialogTitle(getLocText("gui.filechooser.choose.dir"));
            if (Application.OS_IS_WINDOWS) {
                fileChooser.setDialogTitle(getLocText("gui.filechooser.choose.dir.windows"));
            }             
        }
        else {
            fileChooser.setDialogTitle(getLocText("gui.filechooser.choose.file"));
        }
        fileChooser.setApproveButtonText(getLocText("gui.filechooser.choose"));
    }
   
    /**
     *
     * @return
     */
    public JFileChooser getFileChooser() {
        return fileChooser;
    }
   
    /**
     *
     * @return
     */
    public String getRoot() {
        return rootDirPath;
    }

}   // FileChooserBuilder.java
TOP

Related Classes of cz.mp.k3bg.gui.helper.FileChooserBuilder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.