1
2
3
4
5
6
7
8
9
10
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
152
153
154
155
156
157
158
159
160
161
162
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205 }
206 }