View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.awt.Dimension;
17  import java.io.File;
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import javax.swing.JFileChooser;
22  
23  import com.eviware.soapui.support.ExtensionFileFilter;
24  import com.eviware.x.dialogs.XFileDialogs;
25  import com.l2fprod.common.swing.JDirectoryChooser;
26  
27  /***
28   * @author Lars
29   */
30  public class SwingFileDialogs implements XFileDialogs
31  {
32  	private static Component parent;
33  	private static Map<Object, JFileChooser> choosers = new HashMap<Object, JFileChooser>();
34  
35  	public SwingFileDialogs( Component parent )
36  	{
37  		SwingFileDialogs.parent = parent;
38  	}
39  
40  	public static synchronized JFileChooser getChooser( Object action )
41  	{
42  		action = null;
43  		JFileChooser chooser = choosers.get( action );
44  		if( chooser == null )
45  		{
46  			chooser = new JFileChooser();
47  			choosers.put( action, chooser );
48  		}
49  
50  		chooser.resetChoosableFileFilters();
51  
52  		return chooser;
53  	}
54  
55  	public static Component getParent()
56  	{
57  		return parent;
58  	}
59  
60  	public File saveAs( Object action, String title )
61  	{
62  		return saveAs( action, title, null, null, null );
63  	}
64  
65  	public File saveAs( Object action, String title, String extension, String fileType, File defaultFile )
66  	{
67  		JFileChooser chooser = getChooser( action );
68  		chooser.setFileSelectionMode( JFileChooser.FILES_ONLY );
69  		chooser.setDialogTitle( title );
70  		chooser.setAcceptAllFileFilterUsed( true );
71  
72  		if( extension != null && fileType != null )
73  		{
74  			chooser.setFileFilter( new ExtensionFileFilter( extension, fileType ) );
75  		}
76  		else
77  		{
78  			chooser.setFileFilter( null );
79  		}
80  
81  		if( defaultFile != null )
82  		{
83  			chooser.setSelectedFile( defaultFile );
84  		}
85  		else
86  		{
87  			chooser.setSelectedFile( null );
88  		}
89  
90  		if( chooser.showSaveDialog( getParent() ) != JFileChooser.APPROVE_OPTION )
91  			return null;
92  
93  		return chooser.getSelectedFile();
94  	}
95  
96  	public File open( Object action, String title, String extension, String fileType, String current )
97  	{
98  		return openFile( action, title, extension, fileType, current );
99  	}
100 
101 	public static File openFile( Object action, String title, String extension, String fileType, String current )
102 	{
103 		JFileChooser chooser = getChooser( action );
104 		chooser.setFileSelectionMode( JFileChooser.FILES_ONLY );
105 		chooser.setDialogTitle( title );
106 		chooser.setAcceptAllFileFilterUsed( true );
107 		if( current != null )
108 		{
109 			File file = new File( current );
110 			if( file.isDirectory() )
111 				chooser.setCurrentDirectory( file );
112 			else
113 				chooser.setSelectedFile( file );
114 		}
115 		else
116 		{
117 			chooser.setSelectedFile( null );
118 		}
119 
120 		if( extension != null && fileType != null )
121 		{
122 			chooser.setFileFilter( new ExtensionFileFilter( extension, fileType ) );
123 		}
124 		else
125 		{
126 			chooser.setFileFilter( null );
127 		}
128 
129 		if( chooser.showOpenDialog( getParent() ) != JFileChooser.APPROVE_OPTION )
130 			return null;
131 
132 		return chooser.getSelectedFile();
133 	}
134 
135 	public File openXML( Object action, String title )
136 	{
137 		return open( action, title, ".xml", "XML Files (*.xml)", null );
138 	}
139 
140 	public File openDirectory( Object action, String title, File defaultDirectory )
141 	{
142 		JDirectoryChooser chooser = new JDirectoryChooser( defaultDirectory );
143 		chooser.setDialogTitle( title );
144 		chooser.setShowingCreateDirectory( true );
145 		chooser.setPreferredSize( new Dimension( 400, 400 ) );
146 		if( chooser.showSaveDialog( getParent() ) != JFileChooser.APPROVE_OPTION )
147 			return null;
148 
149 		return chooser.getSelectedFile();
150 
151 		// JFileChooser chooser = getChooser( action );
152 		// chooser.setDialogTitle( title );
153 		// chooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );
154 		//
155 		// if( defaultDirectory != null )
156 		// chooser.setCurrentDirectory( defaultDirectory );
157 		//
158 		// if( chooser.showOpenDialog( getParent() ) !=
159 		// JFileChooser.APPROVE_OPTION )
160 		// return null;
161 		//
162 		// return chooser.getSelectedFile();
163 	}
164 
165 	public File openFileOrDirectory( Object action, String title, File defaultDirectory )
166 	{
167 		JFileChooser chooser = getChooser( action );
168 		chooser.setDialogTitle( title );
169 		chooser.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES );
170 
171 		if( defaultDirectory != null )
172 			chooser.setCurrentDirectory( defaultDirectory );
173 
174 		if( chooser.showOpenDialog( getParent() ) != JFileChooser.APPROVE_OPTION )
175 			return null;
176 
177 		return chooser.getSelectedFile();
178 	}
179 
180 	public File saveAsDirectory( Object action, String title, File defaultDirectory )
181 	{
182 		JDirectoryChooser chooser = new JDirectoryChooser( defaultDirectory );
183 		chooser.setDialogTitle( title );
184 		chooser.setShowingCreateDirectory( true );
185 		if( chooser.showSaveDialog( getParent() ) != JFileChooser.APPROVE_OPTION )
186 			return null;
187 
188 		return chooser.getSelectedFile();
189 
190 		// JFileChooser chooser = getChooser( action );
191 		// chooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );
192 		// chooser.setDialogTitle( title );
193 		// chooser.setAcceptAllFileFilterUsed( true );
194 		//
195 		// if( defaultDirectory != null )
196 		// chooser.setSelectedFile( defaultDirectory );
197 		// else
198 		// chooser.setSelectedFile( null );
199 		//
200 		// if( chooser.showSaveDialog( getParent() ) !=
201 		// JFileChooser.APPROVE_OPTION )
202 		// return null;
203 		//
204 		// return chooser.getSelectedFile();
205 	}
206 }