Mercurial > hg > ccmieditor
comparison java/src/uk/ac/qmul/eecs/ccmi/gui/FileService.java @ 3:9e67171477bc
PHANTOM Omni Heptic device release
author | Fiore Martin <fiore@eecs.qmul.ac.uk> |
---|---|
date | Wed, 25 Apr 2012 17:09:09 +0100 |
parents | 71ff0735df5a |
children | d66dd5880081 |
comparison
equal
deleted
inserted
replaced
2:4b2f975e35fa | 3:9e67171477bc |
---|---|
1 /* | |
2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool | |
3 | |
4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/) | |
5 | |
6 This program is free software: you can redistribute it and/or modify | |
7 it under the terms of the GNU General Public License as published by | |
8 the Free Software Foundation, either version 3 of the License, or | |
9 (at your option) any later version. | |
10 | |
11 This program is distributed in the hope that it will be useful, | |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 GNU General Public License for more details. | |
15 | |
16 You should have received a copy of the GNU General Public License | |
17 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 */ | |
1 package uk.ac.qmul.eecs.ccmi.gui; | 19 package uk.ac.qmul.eecs.ccmi.gui; |
2 | 20 |
3 import java.awt.Frame; | 21 import java.awt.Frame; |
4 import java.io.BufferedInputStream; | 22 import java.io.BufferedInputStream; |
5 import java.io.BufferedOutputStream; | 23 import java.io.BufferedOutputStream; |
22 import uk.ac.qmul.eecs.ccmi.utils.PreferencesService; | 40 import uk.ac.qmul.eecs.ccmi.utils.PreferencesService; |
23 | 41 |
24 public abstract class FileService | 42 public abstract class FileService |
25 { | 43 { |
26 | 44 |
27 /** | 45 /** |
28 * An Open object encapsulates the stream, name and path of the file that the user selected for opening. | 46 * An Open object encapsulates the stream, name and path of the file that the user selected for opening. |
29 */ | 47 */ |
30 public interface Open | 48 public interface Open |
31 { | 49 { |
32 /** | 50 /** |
33 * Gets the input stream corresponding to the user selection. | 51 * Gets the input stream corresponding to the user selection. |
34 * @return the input stream, or null if the user cancels the file selection task | 52 * @return the input stream, or null if the user cancels the file selection task |
35 */ | 53 */ |
36 InputStream getInputStream() throws IOException ; | 54 InputStream getInputStream() throws IOException ; |
37 /** | 55 /** |
38 * Gets the name of the file that the user selected. | 56 * 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 | 57 * @return the file name, or null if the user cancels the file selection task |
40 */ | 58 */ |
41 String getName() throws IOException ; | 59 String getName() throws IOException ; |
42 | 60 |
43 /** | 61 /** |
44 * Gets the path of the file that the user selected. | 62 * 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 | 63 * @return the file path , or null if the user cancels the file selection task |
46 */ | 64 */ |
47 String getPath() throws IOException; | 65 String getPath() throws IOException; |
48 | 66 |
49 } | 67 } |
50 | 68 |
51 /** | 69 /** |
52 * A Save object encapsulates the stream and name of the file that the user selected for saving. | 70 * A Save object encapsulates the stream and name of the file that the user selected for saving. |
53 */ | 71 */ |
54 public interface Save | 72 public interface Save |
55 { | 73 { |
56 /** | 74 /** |
57 * Gets the output stream corresponding to the user selection. | 75 * Gets the output stream corresponding to the user selection. |
58 * @return the output stream, or null if the user cancels the file selection task | 76 * @return the output stream, or null if the user cancels the file selection task |
59 */ | 77 */ |
60 OutputStream getOutputStream(); | 78 OutputStream getOutputStream(); |
61 /** | 79 /** |
62 * Gets the name of the file that the user selected. | 80 * 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 | 81 * @return the file name, or null if the user cancels the file selection task |
64 */ | 82 */ |
65 String getName(); | 83 String getName(); |
66 /** | 84 /** |
67 * Gets the path of the file that the user selected. | 85 * 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 | 86 * @return the file path, or null if the user cancels the file selection task |
69 */ | 87 */ |
70 String getPath(); | 88 String getPath(); |
71 } | 89 } |
72 | 90 |
73 /** | 91 /** |
74 * This class implements a FileService with a JFileChooser | 92 * This class implements a FileService with a JFileChooser |
75 */ | 93 */ |
76 public static class ChooserService | 94 public static class ChooserService |
77 { | 95 { |
78 public ChooserService(File initialDirectory){ | 96 public ChooserService(File initialDirectory){ |
79 useAccessible = Boolean.parseBoolean(PreferencesService.getInstance().get("use_accessible_filechooser", "true")); | 97 useAccessible = Boolean.parseBoolean(PreferencesService.getInstance().get("use_accessible_filechooser", "true")); |
80 fileChooser = FileChooserFactory.getFileChooser(useAccessible); | 98 fileChooser = FileChooserFactory.getFileChooser(useAccessible); |
81 fileChooser.setCurrentDirectory(initialDirectory); | 99 fileChooser.setCurrentDirectory(initialDirectory); |
82 } | 100 } |
83 | 101 |
84 /* If the user cancels the task (presses cancel button or the X at the top left) * | 102 public FileService.Open open(String defaultDirectory, String defaultFile, |
85 * the CANCEl sound is played (together with the registered playerListeenr if any) */ | 103 ExtensionFilter filter, Frame frame) throws FileNotFoundException { |
86 public FileService.Open open(String defaultDirectory, String defaultFile, | 104 checkChangedOption(); |
87 ExtensionFilter filter, Frame frame) throws FileNotFoundException { | 105 fileChooser.resetChoosableFileFilters(); |
88 fileChooser.resetChoosableFileFilters(); | 106 fileChooser.setFileFilter(filter); |
89 fileChooser.setFileFilter(filter); | 107 if (defaultDirectory != null) |
90 if (defaultDirectory != null) | 108 fileChooser.setCurrentDirectory(new File(defaultDirectory)); |
91 fileChooser.setCurrentDirectory(new File(defaultDirectory)); | 109 if (defaultFile == null) |
92 if (defaultFile == null) | 110 fileChooser.setSelectedFile(null); |
93 fileChooser.setSelectedFile(null); | 111 else |
94 else | 112 fileChooser.setSelectedFile(new File(defaultFile)); |
95 fileChooser.setSelectedFile(new File(defaultFile)); | 113 int response = fileChooser.showOpenDialog(frame); |
96 int response = fileChooser.showOpenDialog(frame); | 114 if (response == JFileChooser.APPROVE_OPTION) |
97 if (response == JFileChooser.APPROVE_OPTION) | 115 return new OpenImpl(fileChooser.getSelectedFile()); |
98 return new OpenImpl(fileChooser.getSelectedFile()); | 116 else{ |
99 else{ | 117 /* If the user cancels the task (presses cancel button or the X at the top left corner) * |
100 if(useAccessible) | 118 * the CANCEl sound is played (together with the registered playerListeenr if any) */ |
101 SoundFactory.getInstance().play(SoundEvent.CANCEL); | 119 if(useAccessible) |
102 return new OpenImpl(null); | 120 SoundFactory.getInstance().play(SoundEvent.CANCEL); |
103 } | 121 return new OpenImpl(null); |
104 } | 122 } |
105 | 123 } |
106 | 124 |
107 /* If the user cancels the task (presses cancel button or the X at the top left) * | 125 |
108 * the CANCEl sound is played (together with the registered playerListeenr if any) */ | 126 /* If the user cancels the task (presses cancel button or the X at the top left) * |
109 public FileService.Save save(String defaultDirectory, String defaultFile, | 127 * the CANCEl sound is played (together with the registered playerListeenr if any) */ |
110 ExtensionFilter filter, String removeExtension, String addExtension) throws FileNotFoundException { | 128 public FileService.Save save(String defaultDirectory, String defaultFile, |
111 fileChooser.resetChoosableFileFilters(); | 129 ExtensionFilter filter, String removeExtension, String addExtension, String[] currentTabs) throws IOException { |
112 fileChooser.setFileFilter(filter); | 130 checkChangedOption(); |
113 if (defaultDirectory == null) | 131 fileChooser.resetChoosableFileFilters(); |
114 fileChooser.setCurrentDirectory(new File(".")); | 132 fileChooser.setFileFilter(filter); |
115 else | 133 if (defaultDirectory == null) |
116 fileChooser.setCurrentDirectory(new File(defaultDirectory)); | 134 fileChooser.setCurrentDirectory(new File(".")); |
117 if (defaultFile != null){ | 135 else |
118 File f = new File(editExtension(defaultFile, removeExtension, addExtension)); | 136 fileChooser.setCurrentDirectory(new File(defaultDirectory)); |
119 if(f.exists()) | 137 if (defaultFile != null){ |
120 fileChooser.setSelectedFile(f); | 138 File f = new File(editExtension(defaultFile, removeExtension, addExtension)); |
121 else | 139 if(f.exists()) |
122 fileChooser.setSelectedFile(null); | 140 fileChooser.setSelectedFile(f); |
123 }else | 141 else |
124 fileChooser.setSelectedFile(null); | 142 fileChooser.setSelectedFile(null); |
125 int response = fileChooser.showSaveDialog(null); | 143 }else |
126 if (response == JFileChooser.APPROVE_OPTION){ | 144 fileChooser.setSelectedFile(null); |
127 File f = fileChooser.getSelectedFile(); | 145 int response = fileChooser.showSaveDialog(null); |
128 if (addExtension != null && f.getName().indexOf(".") < 0) // no extension supplied | 146 if (response == JFileChooser.APPROVE_OPTION){ |
129 f = new File(f.getPath() + addExtension); | 147 ResourceBundle resources = ResourceBundle.getBundle(EditorFrame.class.getName()); |
130 if (!f.exists()) return new SaveImpl(f); | 148 File f = fileChooser.getSelectedFile(); |
131 | 149 if (addExtension != null && f.getName().indexOf(".") < 0) // no extension supplied |
132 /* file with this name already exists, we must ask the user to confirm */ | 150 f = new File(f.getPath() + addExtension); |
133 ResourceBundle resources = ResourceBundle.getBundle(EditorFrame.class.getName()); | 151 |
134 if(useAccessible){ | 152 String fileName = getFileNameFromPath(f.getAbsolutePath(),false); |
135 int result = SpeechOptionPane.showConfirmDialog( | 153 for(String tab : currentTabs){ |
136 null, | 154 if(fileName.equals(tab)) |
137 resources.getString("dialog.overwrite"), | 155 throw new IOException(resources.getString("dialog.error.same_file_name")); |
138 SpeechOptionPane.YES_NO_OPTION); | 156 } |
139 if (result == SpeechOptionPane.YES_OPTION) | 157 |
140 return new SaveImpl(f); | 158 if (!f.exists()) // file doesn't exits return the new SaveImpl with no problems |
141 }else{ | 159 return new SaveImpl(f); |
142 int result = JOptionPane.showConfirmDialog( | 160 |
143 null, | 161 /* file with this name already exists, we must ask the user to confirm */ |
144 resources.getString("dialog.overwrite"), | 162 if(useAccessible){ |
145 null, | 163 int result = SpeechOptionPane.showConfirmDialog( |
146 JOptionPane.YES_NO_OPTION); | 164 null, |
147 if (result == JOptionPane.YES_OPTION) | 165 resources.getString("dialog.overwrite"), |
148 return new SaveImpl(f); | 166 SpeechOptionPane.YES_NO_OPTION); |
149 } | 167 if (result == SpeechOptionPane.YES_OPTION) |
150 } | 168 return new SaveImpl(f); |
151 if(useAccessible) | 169 }else{ |
152 SoundFactory.getInstance().play(SoundEvent.CANCEL); | 170 int result = JOptionPane.showConfirmDialog( |
153 return new SaveImpl(null); | 171 null, |
154 } | 172 resources.getString("dialog.overwrite"), |
155 | 173 null, |
156 private FileChooser fileChooser; | 174 JOptionPane.YES_NO_OPTION); |
157 private boolean useAccessible; | 175 if (result == JOptionPane.YES_OPTION) |
158 } | 176 return new SaveImpl(f); |
159 | 177 } |
160 public static class DirectService { | 178 } |
161 public Open open(File file) throws IOException{ | 179 if(useAccessible) |
162 return new OpenImpl(file); | 180 SoundFactory.getInstance().play(SoundEvent.CANCEL); |
163 } | 181 /* returned if the user doesn't want to overwrite the file */ |
164 | 182 return new SaveImpl(null); |
165 public Save save(File file) throws IOException{ | 183 } |
166 return new SaveImpl(file); | 184 |
167 } | 185 /* check if the user has changed the configuration since the last time a the fileChooser was shown */ |
168 } | 186 private void checkChangedOption(){ |
169 | 187 boolean useAccessible = Boolean.parseBoolean(PreferencesService.getInstance().get("use_accessible_filechooser", "true")); |
170 private static class SaveImpl implements FileService.Save{ | 188 if(this.useAccessible != useAccessible){ |
171 public SaveImpl(File f) throws FileNotFoundException{ | 189 this.useAccessible = useAccessible; |
172 if (f != null){ | 190 File currentDir = fileChooser.getCurrentDirectory(); |
173 path = f.getPath(); | 191 fileChooser = FileChooserFactory.getFileChooser(useAccessible); |
174 name = getFileNameFromPath(path,false); | 192 fileChooser.setCurrentDirectory(currentDir); |
175 out = new BufferedOutputStream(new FileOutputStream(f)); | 193 } |
176 } | 194 } |
177 } | 195 |
178 | 196 private FileChooser fileChooser; |
179 @Override | 197 private boolean useAccessible; |
180 public String getName() { return name; } | 198 } |
181 @Override | 199 |
182 public String getPath() {return path; } | 200 public static class DirectService { |
183 @Override | 201 public Open open(File file) throws IOException{ |
184 public OutputStream getOutputStream() { return out; } | 202 return new OpenImpl(file); |
185 | 203 } |
186 private String name; | 204 |
187 private String path; | 205 public Save save(File file) throws IOException{ |
188 private OutputStream out; | 206 return new SaveImpl(file); |
189 } | 207 } |
190 | 208 } |
191 private static class OpenImpl implements FileService.Open | 209 |
192 { | 210 private static class SaveImpl implements FileService.Save{ |
193 public OpenImpl(File f) throws FileNotFoundException{ | 211 public SaveImpl(File f) throws FileNotFoundException{ |
194 if (f != null){ | 212 if (f != null){ |
195 path = f.getPath(); | 213 path = f.getPath(); |
196 name = getFileNameFromPath(path,false); | 214 name = getFileNameFromPath(path,false); |
197 in = new BufferedInputStream(new FileInputStream(f)); | 215 out = new BufferedOutputStream(new FileOutputStream(f)); |
198 } | 216 } |
199 } | 217 } |
200 | 218 |
201 @Override | 219 @Override |
202 public String getName() { return name; } | 220 public String getName() { return name; } |
203 @Override | 221 @Override |
204 public String getPath() { return path; } | 222 public String getPath() {return path; } |
205 @Override | 223 @Override |
206 public InputStream getInputStream() { return in; } | 224 public OutputStream getOutputStream() { return out; } |
207 | 225 |
208 private String path; | 226 private String name; |
209 private String name; | 227 private String path; |
210 private InputStream in; | 228 private OutputStream out; |
211 } | 229 } |
212 | 230 |
213 /** | 231 private static class OpenImpl implements FileService.Open |
232 { | |
233 public OpenImpl(File f) throws FileNotFoundException{ | |
234 if (f != null){ | |
235 path = f.getPath(); | |
236 name = getFileNameFromPath(path,false); | |
237 in = new BufferedInputStream(new FileInputStream(f)); | |
238 } | |
239 } | |
240 | |
241 @Override | |
242 public String getName() { return name; } | |
243 @Override | |
244 public String getPath() { return path; } | |
245 @Override | |
246 public InputStream getInputStream() { return in; } | |
247 | |
248 private String path; | |
249 private String name; | |
250 private InputStream in; | |
251 } | |
252 | |
253 /** | |
214 Edits the file path so that it ends in the desired | 254 Edits the file path so that it ends in the desired |
215 extension. | 255 extension. |
216 @param original the file to use as a starting point | 256 @param original the file to use as a starting point |
217 @param toBeRemoved the extension that is to be | 257 @param toBeRemoved the extension that is to be |
218 removed before adding the desired extension. Use | 258 removed before adding the desired extension. Use |
219 null if nothing needs to be removed. | 259 null if nothing needs to be removed. |
220 @param desired the desired extension (e.g. ".png"), | 260 @param desired the desired extension (e.g. ".png"), |
221 or a | separated list of extensions | 261 or a | separated list of extensions |
222 @return original if it already has the desired | 262 @return original if it already has the desired |
223 extension, or a new file with the edited file path | 263 extension, or a new file with the edited file path |
224 */ | 264 */ |
225 public static String editExtension(String original, | 265 public static String editExtension(String original, |
226 String toBeRemoved, String desired){ | 266 String toBeRemoved, String desired){ |
227 if (original == null) return null; | 267 if (original == null) return null; |
228 int n = desired.indexOf('|'); | 268 int n = desired.indexOf('|'); |
229 if (n >= 0) desired = desired.substring(0, n); | 269 if (n >= 0) desired = desired.substring(0, n); |
230 String path = original; | 270 String path = original; |
231 if (!path.toLowerCase().endsWith(desired.toLowerCase())){ | 271 if (!path.toLowerCase().endsWith(desired.toLowerCase())){ |
232 if (toBeRemoved != null && path.toLowerCase().endsWith( | 272 if (toBeRemoved != null && path.toLowerCase().endsWith( |
233 toBeRemoved.toLowerCase())) | 273 toBeRemoved.toLowerCase())) |
234 path = path.substring(0, path.length() - toBeRemoved.length()); | 274 path = path.substring(0, path.length() - toBeRemoved.length()); |
235 path = path + desired; | 275 path = path + desired; |
236 } | 276 } |
237 return path; | 277 return path; |
238 } | 278 } |
239 | 279 |
240 public static String getFileNameFromPath(String path,boolean keepExtension){ | 280 public static String getFileNameFromPath(String path,boolean keepExtension){ |
241 int index = path.lastIndexOf(System.getProperty("file.separator")); | 281 int index = path.lastIndexOf(System.getProperty("file.separator")); |
242 String name; | 282 String name; |
243 if(index == -1) | 283 if(index == -1) |
244 name = path; | 284 name = path; |
245 else | 285 else |
246 name = path.substring(index+1); | 286 name = path.substring(index+1); |
247 if(!keepExtension){ | 287 if(!keepExtension){ |
248 index = name.lastIndexOf('.'); | 288 index = name.lastIndexOf('.'); |
249 if(index != -1) | 289 if(index != -1) |
250 name = name.substring(0, index); | 290 name = name.substring(0, index); |
251 } | 291 } |
252 return name; | 292 return name; |
253 } | 293 } |
254 } | 294 } |