Package com.eviware.x.impl.swing

Source Code of com.eviware.x.impl.swing.SwingFileDialogs

/*
* Copyright 2004-2014 SmartBear Software
*
* Licensed under the EUPL, Version 1.1 or - as soon as they will be approved by the European Commission - subsequent
* versions of the EUPL (the "Licence");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* http://ec.europa.eu/idabc/eupl
*
* Unless required by applicable law or agreed to in writing, software distributed under the Licence is
* distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the Licence for the specific language governing permissions and limitations
* under the Licence.
*/

package com.eviware.x.impl.swing;

import java.awt.Component;
import java.awt.Dimension;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JFileChooser;

import com.eviware.soapui.support.ExtensionFileFilter;
import com.eviware.x.dialogs.XFileDialogs;
import com.l2fprod.common.swing.JDirectoryChooser;

/**
* @author Lars
*/
public class SwingFileDialogs implements XFileDialogs {
    private static Component parent;
    private static Map<Object, JFileChooser> choosers = new HashMap<Object, JFileChooser>();

    public SwingFileDialogs(Component parent) {
        SwingFileDialogs.parent = parent;
    }

    public static synchronized JFileChooser getChooser(Object action) {
        action = null;
        JFileChooser chooser = choosers.get(action);
        if (chooser == null) {
            chooser = new JFileChooser();
            choosers.put(action, chooser);
        }

        chooser.resetChoosableFileFilters();

        return chooser;
    }

    public static Component getParent() {
        return parent;
    }

    public File saveAs(Object action, String title) {
        return saveAs(action, title, null, null, null);
    }

    public File saveAs(Object action, String title, String extension, String fileType, File defaultFile) {
        JFileChooser chooser = getChooser(action);
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.setDialogTitle(title);
        chooser.setAcceptAllFileFilterUsed(true);

        if (extension != null && fileType != null) {
            chooser.setFileFilter(new ExtensionFileFilter(extension, fileType));
        } else {
            chooser.setFileFilter(null);
        }

        if (defaultFile != null) {
            chooser.setSelectedFile(defaultFile);
        } else {
            chooser.setSelectedFile(null);
        }

        if (chooser.showSaveDialog(getParent()) != JFileChooser.APPROVE_OPTION) {
            return null;
        }

        return chooser.getSelectedFile();
    }

    public File open(Object action, String title, String extension, String fileType, String current) {
        return openFile(action, title, extension, fileType, current);
    }

    public static File openFile(Object action, String title, String extension, String fileType, String current) {
        JFileChooser chooser = getChooser(action);
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.setDialogTitle(title);
        chooser.setAcceptAllFileFilterUsed(true);
        if (current != null) {
            File file = new File(current);
            if (file.isDirectory()) {
                chooser.setCurrentDirectory(file);
            } else {
                chooser.setSelectedFile(file);
            }
        } else {
            chooser.setSelectedFile(null);
        }

        if (extension != null && fileType != null) {
            chooser.setFileFilter(new ExtensionFileFilter(extension, fileType));
        } else {
            chooser.setFileFilter(null);
        }

        if (chooser.showOpenDialog(getParent()) != JFileChooser.APPROVE_OPTION) {
            return null;
        }

        return chooser.getSelectedFile();
    }

    public File openXML(Object action, String title) {
        return open(action, title, ".xml", "XML Files (*.xml)", null);
    }

    public File openDirectory(Object action, String title, File defaultDirectory) {
        JDirectoryChooser chooser = new JDirectoryChooser(defaultDirectory);
        chooser.setDialogTitle(title);
        chooser.setShowingCreateDirectory(true);
        chooser.setPreferredSize(new Dimension(400, 400));
        if (chooser.showSaveDialog(getParent()) != JFileChooser.APPROVE_OPTION) {
            return null;
        }

        return chooser.getSelectedFile();

        // JFileChooser chooser = getChooser( action );
        // chooser.setDialogTitle( title );
        // chooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );
        //
        // if( defaultDirectory != null )
        // chooser.setCurrentDirectory( defaultDirectory );
        //
        // if( chooser.showOpenDialog( getParent() ) !=
        // JFileChooser.APPROVE_OPTION )
        // return null;
        //
        // return chooser.getSelectedFile();
    }

    public File openFileOrDirectory(Object action, String title, File defaultDirectory) {
        JFileChooser chooser = getChooser(action);
        chooser.setDialogTitle(title);
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

        if (defaultDirectory != null) {
            chooser.setCurrentDirectory(defaultDirectory);
        }

        if (chooser.showOpenDialog(getParent()) != JFileChooser.APPROVE_OPTION) {
            return null;
        }

        return chooser.getSelectedFile();
    }

    public File saveAsDirectory(Object action, String title, File defaultDirectory) {
        JDirectoryChooser chooser = new JDirectoryChooser(defaultDirectory);
        chooser.setDialogTitle(title);
        chooser.setShowingCreateDirectory(true);
        chooser.setAcceptAllFileFilterUsed(true);
        if (chooser.showSaveDialog(getParent()) != JFileChooser.APPROVE_OPTION) {
            return null;
        }

        return chooser.getSelectedFile();

        // JFileChooser chooser = getChooser( action );
        // chooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );
        // chooser.setDialogTitle( title );
        // chooser.setAcceptAllFileFilterUsed( true );
        //
        // if( defaultDirectory != null )
        // chooser.setSelectedFile( defaultDirectory );
        // else
        // chooser.setSelectedFile( null );
        //
        // if( chooser.showSaveDialog( getParent() ) !=
        // JFileChooser.APPROVE_OPTION )
        // return null;
        //
        // return chooser.getSelectedFile();
    }
}
TOP

Related Classes of com.eviware.x.impl.swing.SwingFileDialogs

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.