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 Component parent;
32     private static Map<Object, JFileChooser> choosers = new HashMap<Object, JFileChooser>();
33    
34     public SwingFileDialogs(Component parent)
35     {
36        this.parent = parent;
37     }
38     
39     public 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        return chooser;
50     }
51     
52     public Component getParent()
53     {
54        return parent;
55     }
56     
57     public File saveAs(Object action, String title)
58     {
59        return saveAs(action, title, null, null, null);
60     }
61     
62     public File saveAs(Object action, String title, String extension, String fileType, File defaultFile)
63     {
64        JFileChooser chooser = getChooser(action);
65        chooser.setDialogTitle(title);
66        chooser.setAcceptAllFileFilterUsed( true );
67        if(extension != null && fileType != null)
68           chooser.setFileFilter( new ExtensionFileFilter( extension, fileType ));
69        
70        if(defaultFile != null)
71           chooser.setSelectedFile(defaultFile);
72        
73        if (chooser.showSaveDialog(getParent()) != JFileChooser.APPROVE_OPTION)
74           return null;
75           
76        return chooser.getSelectedFile();
77     }
78     
79     public File open(Object action, String title, String extension, String fileType)
80     {
81        JFileChooser chooser = getChooser(action);
82        chooser.setDialogTitle(title);
83        chooser.setAcceptAllFileFilterUsed( true );
84        if(extension != null && fileType != null)
85           chooser.setFileFilter( new ExtensionFileFilter( extension, fileType ));
86        
87        if (chooser.showOpenDialog(getParent()) != JFileChooser.APPROVE_OPTION)
88           return null;
89           
90        return chooser.getSelectedFile();      
91     }
92     
93     public File openXML(Object action, String title)
94     {
95        return open(action, title, ".xml", "XML Files (*.xml)");
96     }
97     
98     public File openDirectory(Object action, String title, File defaultDirectory)
99     {
100       JFileChooser chooser = getChooser(action);
101       chooser.setDialogTitle(title);
102       chooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );
103       if(defaultDirectory != null)
104          chooser.setCurrentDirectory( defaultDirectory );
105       
106       if (chooser.showOpenDialog(getParent()) != JFileChooser.APPROVE_OPTION)
107          return null;
108          
109       return chooser.getSelectedFile();
110    }
111 }