Mercurial > hg > ccmieditor
comparison java/src/uk/ac/qmul/eecs/ccmi/gui/FileService.java @ 0:9418ab7b7f3f
Initial import
author | Fiore Martin <fiore@eecs.qmul.ac.uk> |
---|---|
date | Fri, 16 Dec 2011 17:35:51 +0000 |
parents | |
children | 71ff0735df5a |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:9418ab7b7f3f |
---|---|
1 package uk.ac.qmul.eecs.ccmi.gui; | |
2 | |
3 import java.awt.Frame; | |
4 import java.io.BufferedInputStream; | |
5 import java.io.BufferedOutputStream; | |
6 import java.io.File; | |
7 import java.io.FileInputStream; | |
8 import java.io.FileNotFoundException; | |
9 import java.io.FileOutputStream; | |
10 import java.io.IOException; | |
11 import java.io.InputStream; | |
12 import java.io.OutputStream; | |
13 import java.util.ResourceBundle; | |
14 | |
15 import javax.swing.JFileChooser; | |
16 import javax.swing.JOptionPane; | |
17 | |
18 import uk.ac.qmul.eecs.ccmi.gui.filechooser.FileChooser; | |
19 import uk.ac.qmul.eecs.ccmi.gui.filechooser.FileChooserFactory; | |
20 import uk.ac.qmul.eecs.ccmi.sound.SoundEvent; | |
21 import uk.ac.qmul.eecs.ccmi.sound.SoundFactory; | |
22 import uk.ac.qmul.eecs.ccmi.utils.PreferencesService; | |
23 | |
24 public abstract class FileService | |
25 { | |
26 | |
27 /** | |
28 * An Open object encapsulates the stream, name and path of the file that the user selected for opening. | |
29 */ | |
30 public interface Open | |
31 { | |
32 /** | |
33 * Gets the input stream corresponding to the user selection. | |
34 * @return the input stream, or null if the user cancels the file selection task | |
35 */ | |
36 InputStream getInputStream() throws IOException ; | |
37 /** | |
38 * Gets the name of the file that the user selected. | |
39 * @return the file name, or null if the user cancels the file selection task | |
40 */ | |
41 String getName() throws IOException ; | |
42 | |
43 /** | |
44 * Gets the path of the file that the user selected. | |
45 * @return the file path , or null if the user cancels the file selection task | |
46 */ | |
47 String getPath() throws IOException; | |
48 | |
49 } | |
50 | |
51 /** | |
52 * A Save object encapsulates the stream and name of the file that the user selected for saving. | |
53 */ | |
54 public interface Save | |
55 { | |
56 /** | |
57 * Gets the output stream corresponding to the user selection. | |
58 * @return the output stream, or null if the user cancels the file selection task | |
59 */ | |
60 OutputStream getOutputStream(); | |
61 /** | |
62 * Gets the name of the file that the user selected. | |
63 * @return the file name, or null if the user cancels the file selection task | |
64 */ | |
65 String getName(); | |
66 /** | |
67 * Gets the path of the file that the user selected. | |
68 * @return the file path, or null if the user cancels the file selection task | |
69 */ | |
70 String getPath(); | |
71 } | |
72 | |
73 /** | |
74 * This class implements a FileService with a JFileChooser | |
75 */ | |
76 public static class ChooserService | |
77 { | |
78 public ChooserService(File initialDirectory){ | |
79 useAccessible = Boolean.parseBoolean(PreferencesService.getInstance().get("use_accessible_filechooser", "true")); | |
80 fileChooser = FileChooserFactory.getFileChooser(useAccessible); | |
81 fileChooser.setCurrentDirectory(initialDirectory); | |
82 } | |
83 | |
84 /* If the user cancels the task (presses cancel button or the X at the top left) * | |
85 * the CANCEl sound is played (together with the registered playerListeenr if any) */ | |
86 public FileService.Open open(String defaultDirectory, String defaultFile, | |
87 ExtensionFilter filter, Frame frame) throws FileNotFoundException { | |
88 fileChooser.resetChoosableFileFilters(); | |
89 fileChooser.setFileFilter(filter); | |
90 if (defaultDirectory != null) | |
91 fileChooser.setCurrentDirectory(new File(defaultDirectory)); | |
92 if (defaultFile == null) | |
93 fileChooser.setSelectedFile(null); | |
94 else | |
95 fileChooser.setSelectedFile(new File(defaultFile)); | |
96 int response = fileChooser.showOpenDialog(frame); | |
97 if (response == JFileChooser.APPROVE_OPTION) | |
98 return new OpenImpl(fileChooser.getSelectedFile()); | |
99 else{ | |
100 if(useAccessible) | |
101 SoundFactory.getInstance().play(SoundEvent.CANCEL); | |
102 return new OpenImpl(null); | |
103 } | |
104 } | |
105 | |
106 | |
107 /* If the user cancels the task (presses cancel button or the X at the top left) * | |
108 * the CANCEl sound is played (together with the registered playerListeenr if any) */ | |
109 public FileService.Save save(String defaultDirectory, String defaultFile, | |
110 ExtensionFilter filter, String removeExtension, String addExtension) throws FileNotFoundException { | |
111 fileChooser.resetChoosableFileFilters(); | |
112 fileChooser.setFileFilter(filter); | |
113 if (defaultDirectory == null) | |
114 fileChooser.setCurrentDirectory(new File(".")); | |
115 else | |
116 fileChooser.setCurrentDirectory(new File(defaultDirectory)); | |
117 if (defaultFile != null){ | |
118 File f = new File(editExtension(defaultFile, removeExtension, addExtension)); | |
119 if(f.exists()) | |
120 fileChooser.setSelectedFile(f); | |
121 else | |
122 fileChooser.setSelectedFile(null); | |
123 }else | |
124 fileChooser.setSelectedFile(null); | |
125 int response = fileChooser.showSaveDialog(null); | |
126 if (response == JFileChooser.APPROVE_OPTION){ | |
127 File f = fileChooser.getSelectedFile(); | |
128 if (addExtension != null && f.getName().indexOf(".") < 0) // no extension supplied | |
129 f = new File(f.getPath() + addExtension); | |
130 if (!f.exists()) return new SaveImpl(f); | |
131 | |
132 /* file with this name already exists, we must ask the user to confirm */ | |
133 ResourceBundle resources = ResourceBundle.getBundle(EditorFrame.class.getName()); | |
134 if(useAccessible){ | |
135 int result = SpeechOptionPane.showConfirmDialog( | |
136 null, | |
137 resources.getString("dialog.overwrite"), | |
138 SpeechOptionPane.YES_NO_OPTION); | |
139 if (result == SpeechOptionPane.YES_OPTION) | |
140 return new SaveImpl(f); | |
141 }else{ | |
142 int result = JOptionPane.showConfirmDialog( | |
143 null, | |
144 resources.getString("dialog.overwrite"), | |
145 null, | |
146 JOptionPane.YES_NO_OPTION); | |
147 if (result == JOptionPane.YES_OPTION) | |
148 return new SaveImpl(f); | |
149 } | |
150 } | |
151 if(useAccessible) | |
152 SoundFactory.getInstance().play(SoundEvent.CANCEL); | |
153 return new SaveImpl(null); | |
154 } | |
155 | |
156 private FileChooser fileChooser; | |
157 private boolean useAccessible; | |
158 } | |
159 | |
160 public static class DirectService { | |
161 public Open open(File file) throws IOException{ | |
162 return new OpenImpl(file); | |
163 } | |
164 | |
165 public Save save(File file) throws IOException{ | |
166 return new SaveImpl(file); | |
167 } | |
168 } | |
169 | |
170 private static class SaveImpl implements FileService.Save{ | |
171 public SaveImpl(File f) throws FileNotFoundException{ | |
172 if (f != null){ | |
173 path = f.getPath(); | |
174 name = getFileNameFromPath(path,false); | |
175 out = new BufferedOutputStream(new FileOutputStream(f)); | |
176 } | |
177 } | |
178 | |
179 @Override | |
180 public String getName() { return name; } | |
181 @Override | |
182 public String getPath() {return path; } | |
183 @Override | |
184 public OutputStream getOutputStream() { return out; } | |
185 | |
186 private String name; | |
187 private String path; | |
188 private OutputStream out; | |
189 } | |
190 | |
191 private static class OpenImpl implements FileService.Open | |
192 { | |
193 public OpenImpl(File f) throws FileNotFoundException{ | |
194 if (f != null){ | |
195 path = f.getPath(); | |
196 name = getFileNameFromPath(path,false); | |
197 in = new BufferedInputStream(new FileInputStream(f)); | |
198 } | |
199 } | |
200 | |
201 @Override | |
202 public String getName() { return name; } | |
203 @Override | |
204 public String getPath() { return path; } | |
205 @Override | |
206 public InputStream getInputStream() { return in; } | |
207 | |
208 private String path; | |
209 private String name; | |
210 private InputStream in; | |
211 } | |
212 | |
213 /** | |
214 Edits the file path so that it ends in the desired | |
215 extension. | |
216 @param original the file to use as a starting point | |
217 @param toBeRemoved the extension that is to be | |
218 removed before adding the desired extension. Use | |
219 null if nothing needs to be removed. | |
220 @param desired the desired extension (e.g. ".png"), | |
221 or a | separated list of extensions | |
222 @return original if it already has the desired | |
223 extension, or a new file with the edited file path | |
224 */ | |
225 public static String editExtension(String original, | |
226 String toBeRemoved, String desired){ | |
227 if (original == null) return null; | |
228 int n = desired.indexOf('|'); | |
229 if (n >= 0) desired = desired.substring(0, n); | |
230 String path = original; | |
231 if (!path.toLowerCase().endsWith(desired.toLowerCase())){ | |
232 if (toBeRemoved != null && path.toLowerCase().endsWith( | |
233 toBeRemoved.toLowerCase())) | |
234 path = path.substring(0, path.length() - toBeRemoved.length()); | |
235 path = path + desired; | |
236 } | |
237 return path; | |
238 } | |
239 | |
240 public static String getFileNameFromPath(String path,boolean keepExtension){ | |
241 int index = path.lastIndexOf(System.getProperty("file.separator")); | |
242 String name; | |
243 if(index == -1) | |
244 name = path; | |
245 else | |
246 name = path.substring(index+1); | |
247 if(!keepExtension){ | |
248 index = name.lastIndexOf('.'); | |
249 if(index != -1) | |
250 name = name.substring(0, index); | |
251 } | |
252 return name; | |
253 } | |
254 } |