View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 eviware.com
    3    *
    4    *  soapUI is free software; you can redistribute it and/or modify it under the
    5    *  terms of version 2.1 of the GNU Lesser General Public License as published by
    6    *  the Free Software Foundation.
    7    *
    8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
    9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    10   *  See the GNU Lesser General Public License for more details at gnu.org.
    11   */
    12
    13  package com.eviware.x.impl.swing;
    14
    15  import java.awt.Component;
    16  import java.io.File;
    17  import java.util.HashMap;
    18  import java.util.Map;
    19
    20  import javax.swing.JFileChooser;
    21
    22  import com.eviware.soapui.support.ExtensionFileFilter;
    23  import com.eviware.x.dialogs.XFileDialogs;
    24
    25  /***
    26   *
    27   * @author Lars
    28   */
    29  public class SwingFileDialogs implements XFileDialogs
    30  {
    31     private static Component parent;
    32     private static Map<Object, JFileChooser> choosers = new HashMap<Object, JFileChooser>();
    33
    34     public SwingFileDialogs(Component parent)
    35     {
    36        SwingFileDialogs.parent = parent;
    37     }
    38
    39     public static synchronized JFileChooser getChooser(Object action)
    40     {
    41     	action = null;
    42        JFileChooser chooser = choosers.get(action);
    43        if( chooser == null )
    44        {
    45           chooser = new JFileChooser();
    46           choosers.put(action, chooser);
    47        }
    48
    49        chooser.resetChoosableFileFilters();
    50
    51        return chooser;
    52     }
    53
    54     public static Component getParent()
    55     {
    56        return parent;
    57     }
    58
    59     public File saveAs(Object action, String title)
    60     {
    61        return saveAs(action, title, null, null, null);
    62     }
    63
    64     public File saveAs(Object action, String title, String extension, String fileType, File defaultFile)
    65     {
    66        JFileChooser chooser = getChooser(action);
    67        chooser.setDialogTitle(title);
    68        chooser.setAcceptAllFileFilterUsed( true );
    69
    70        if(extension != null && fileType != null)
    71           chooser.setFileFilter( new ExtensionFileFilter( extension, fileType ));
    72
    73        if(defaultFile != null)
    74           chooser.setSelectedFile(defaultFile);
    75
    76        if (chooser.showSaveDialog(getParent()) != JFileChooser.APPROVE_OPTION)
    77           return null;
    78
    79        return chooser.getSelectedFile();
    80     }
    81
    82     public File open(Object action, String title, String extension, String fileType, String current)
    83     {
    84     	return openFile( action, title, extension, fileType, current );
    85     }
    86
    87     public static File openFile(Object action, String title, String extension, String fileType, String current)
    88     {
    89        JFileChooser chooser = getChooser(action);
    90        chooser.setDialogTitle(title);
    91        chooser.setAcceptAllFileFilterUsed( true );
    92        if( current != null )
    93        	chooser.setSelectedFile( new File( current) );
    94
    95        if(extension != null && fileType != null)
    96           chooser.setFileFilter( new ExtensionFileFilter( extension, fileType ));
    97
    98        if (chooser.showOpenDialog(getParent()) != JFileChooser.APPROVE_OPTION)
    99           return null;
    100
    101       return chooser.getSelectedFile();
    102    }
    103
    104    public File openXML(Object action, String title)
    105    {
    106       return open(action, title, ".xml", "XML Files (*.xml)", null);
    107    }
    108
    109    public File openDirectory(Object action, String title, File defaultDirectory)
    110    {
    111       JFileChooser chooser = getChooser(action);
    112       chooser.setDialogTitle(title);
    113       chooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );
    114
    115       if(defaultDirectory != null)
    116          chooser.setCurrentDirectory( defaultDirectory );
    117
    118       if (chooser.showOpenDialog(getParent()) != JFileChooser.APPROVE_OPTION)
    119          return null;
    120
    121       return chooser.getSelectedFile();
    122    }
    123 }