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.setFileSelectionMode( JFileChooser.FILES_ONLY );
68        chooser.setDialogTitle(title);
69        chooser.setAcceptAllFileFilterUsed( true );
70  
71        if(extension != null && fileType != null)
72           chooser.setFileFilter( new ExtensionFileFilter( extension, fileType ));
73        
74        if(defaultFile != null)
75           chooser.setSelectedFile(defaultFile);
76        
77        if (chooser.showSaveDialog(getParent()) != JFileChooser.APPROVE_OPTION)
78           return null;
79           
80        return chooser.getSelectedFile();
81     }
82     
83     public File open(Object action, String title, String extension, String fileType, String current)
84     {
85     	return openFile( action, title, extension, fileType, current );
86     }
87     
88     public static File openFile(Object action, String title, String extension, String fileType, String current)
89     {
90        JFileChooser chooser = getChooser(action);
91        chooser.setFileSelectionMode( JFileChooser.FILES_ONLY );
92        chooser.setDialogTitle(title);
93        chooser.setAcceptAllFileFilterUsed( true );
94        if( current != null )
95        	chooser.setSelectedFile( new File( current) );
96        
97        if(extension != null && fileType != null)
98           chooser.setFileFilter( new ExtensionFileFilter( extension, fileType ));
99        
100       if (chooser.showOpenDialog(getParent()) != JFileChooser.APPROVE_OPTION)
101          return null;
102          
103       return chooser.getSelectedFile();      
104    }
105    
106    public File openXML(Object action, String title)
107    {
108       return open(action, title, ".xml", "XML Files (*.xml)", null);
109    }
110    
111    public File openDirectory(Object action, String title, File defaultDirectory)
112    {
113       JFileChooser chooser = getChooser(action);
114       chooser.setDialogTitle(title);
115       chooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );
116 
117       if(defaultDirectory != null)
118          chooser.setCurrentDirectory( defaultDirectory );
119       
120       if (chooser.showOpenDialog(getParent()) != JFileChooser.APPROVE_OPTION)
121          return null;
122          
123       return chooser.getSelectedFile();
124    }
125 }