Changeset 9670 in josm
- Timestamp:
- 2016-01-29T17:39:58+01:00 (9 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/SaveActionBase.java
r9510 r9670 156 156 } 157 157 158 private static File checkFileAndConfirmOverWrite(AbstractFileChooser fc, String extension) { 158 /** 159 * Checks if selected filename has the given extension. If not, adds the extension and asks for overwrite if filename exists. 160 * 161 * @param fc FileChooser where file was already selected 162 * @return the {@code File} or {@code null} if the user cancelled the dialog. 163 */ 164 public static File checkFileAndConfirmOverWrite(AbstractFileChooser fc, String extension) { 159 165 if (fc == null) return null; 160 166 File file = fc.getSelectedFile(); -
trunk/src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java
r9668 r9670 45 45 import org.openstreetmap.josm.actions.ExpertToggleAction; 46 46 import org.openstreetmap.josm.actions.RenameLayerAction; 47 import org.openstreetmap.josm.actions.SaveActionBase;48 47 import org.openstreetmap.josm.actions.ToggleUploadDiscouragedLayerAction; 49 48 import org.openstreetmap.josm.data.APIDataSet; … … 92 91 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 93 92 import org.openstreetmap.josm.gui.util.GuiHelper; 93 import org.openstreetmap.josm.gui.widgets.FileChooserManager; 94 94 import org.openstreetmap.josm.gui.widgets.JosmTextArea; 95 import org.openstreetmap.josm.io.OsmImporter; 95 96 import org.openstreetmap.josm.tools.CheckParameterUtil; 96 97 import org.openstreetmap.josm.tools.FilteredCollection; … … 1002 1003 public File createAndOpenSaveFileChooser() { 1003 1004 String extension = Main.pref.get("save.extension.osm", "osm"); 1004 return SaveActionBase.createAndOpenSaveFileChooser(tr("Save OSM file"), extension); 1005 File file = getAssociatedFile(); 1006 if (file == null && isRenamed()) { 1007 String filename = Main.pref.get("lastDirectory") + '/' + getName(); 1008 if (!OsmImporter.FILE_FILTER.acceptName(filename)) filename = filename + '.' + extension; 1009 file = new File(filename); 1010 } 1011 return new FileChooserManager() 1012 .title(tr("Save OSM file")) 1013 .extension(extension) 1014 .file(file) 1015 .getFileForSave(); 1005 1016 } 1006 1017 -
trunk/src/org/openstreetmap/josm/gui/widgets/FileChooserManager.java
r8540 r9670 38 38 private final String curDir; 39 39 40 private boolean multiple = false; 41 private String title = null; 42 private Collection<? extends FileFilter> filters; 43 private FileFilter defaultFilter; 44 private int selectionMode = JFileChooser.FILES_ONLY; 45 private String extension = null; 46 private boolean allTypes = false; 47 private File file = null; 48 40 49 private AbstractFileChooser fc; 50 51 /** 52 * Creates a new {@code FileChooserManager} with default values. 53 * @see #createFileChooser 54 */ 55 public FileChooserManager() { 56 this(false, null, null); 57 } 41 58 42 59 /** … … 102 119 */ 103 120 public final FileChooserManager createFileChooser() { 104 return doCreateFileChooser( false, null, null, null, null, JFileChooser.FILES_ONLY, false);121 return doCreateFileChooser(); 105 122 } 106 123 … … 119 136 */ 120 137 public final FileChooserManager createFileChooser(boolean multiple, String title, FileFilter filter, int selectionMode) { 121 doCreateFileChooser(multiple, title, Collections.singleton(filter), filter, null, selectionMode, false); 122 getFileChooser().setAcceptAllFileFilterUsed(false); 138 multiple(multiple); 139 title(title); 140 filters(Collections.singleton(filter)); 141 defaultFilter(filter); 142 selectionMode(selectionMode); 143 144 doCreateFileChooser(); 145 fc.setAcceptAllFileFilterUsed(false); 123 146 return this; 124 147 } … … 140 163 public final FileChooserManager createFileChooser(boolean multiple, String title, Collection<? extends FileFilter> filters, 141 164 FileFilter defaultFilter, int selectionMode) { 142 return doCreateFileChooser(multiple, title, filters, defaultFilter, null, selectionMode, false); 165 multiple(multiple); 166 title(title); 167 filters(filters); 168 defaultFilter(defaultFilter); 169 selectionMode(selectionMode); 170 return doCreateFileChooser(); 143 171 } 144 172 … … 159 187 */ 160 188 public final FileChooserManager createFileChooser(boolean multiple, String title, String extension, boolean allTypes, int selectionMode) { 161 return doCreateFileChooser(multiple, title, null, null, extension, selectionMode, allTypes); 162 } 163 164 private FileChooserManager doCreateFileChooser(boolean multiple, String title, Collection<? extends FileFilter> filters, 165 FileFilter defaultFilter, String extension, int selectionMode, boolean allTypes) { 189 multiple(multiple); 190 title(title); 191 extension(extension); 192 allTypes(allTypes); 193 selectionMode(selectionMode); 194 return doCreateFileChooser(); 195 } 196 197 /** 198 * Builder method to set {@code multiple} property. 199 * @param value If true, makes the dialog allow multiple file selections 200 * @return this 201 */ 202 public FileChooserManager multiple(boolean value) { 203 multiple = value; 204 return this; 205 } 206 207 /** 208 * Builder method to set {@code title} property. 209 * @param value The string that goes in the dialog window's title bar 210 * @return this 211 */ 212 public FileChooserManager title(String value) { 213 title = value; 214 return this; 215 } 216 217 /** 218 * Builder method to set {@code filters} property. 219 * @param value The file filters that will be proposed by the dialog 220 * @return this 221 */ 222 public FileChooserManager filters(Collection<? extends FileFilter> value) { 223 filters = value; 224 return this; 225 } 226 227 /** 228 * Builder method to set {@code defaultFilter} property. 229 * @param value The file filter that will be selected by default 230 * @return this 231 */ 232 public FileChooserManager defaultFilter(FileFilter value) { 233 defaultFilter = value; 234 return this; 235 } 236 237 /** 238 * Builder method to set {@code selectionMode} property. 239 * @param value The selection mode that allows the user to:<br><ul> 240 * <li>just select files ({@code JFileChooser.FILES_ONLY})</li> 241 * <li>just select directories ({@code JFileChooser.DIRECTORIES_ONLY})</li> 242 * <li>select both files and directories ({@code JFileChooser.FILES_AND_DIRECTORIES})</li></ul> 243 * @return this 244 */ 245 public FileChooserManager selectionMode(int value) { 246 selectionMode = value; 247 return this; 248 } 249 250 /** 251 * Builder method to set {@code extension} property. 252 * @param value The file extension that will be selected as the default file filter 253 * @return this 254 */ 255 public FileChooserManager extension(String value) { 256 extension = value; 257 return this; 258 } 259 260 /** 261 * Builder method to set {@code allTypes} property. 262 * @param value If true, all the files types known by JOSM will be proposed in the "file type" combobox. 263 * If false, only the file filters that include {@code extension} will be proposed 264 * @return this 265 */ 266 public FileChooserManager allTypes(boolean value) { 267 allTypes = value; 268 return this; 269 } 270 271 /** 272 * Builder method to set {@code file} property. 273 * @param value {@link File} object with default filename 274 * @return this 275 */ 276 public FileChooserManager file(File value) { 277 file = value; 278 return this; 279 } 280 281 /** 282 * Builds {@code FileChooserManager} object using properties set by builder methods or default values. 283 * @return this 284 */ 285 public FileChooserManager doCreateFileChooser() { 166 286 File file = new File(curDir); 167 287 // Use native dialog is preference is set, unless an unsupported selection mode is specifically wanted … … 179 299 fc.setMultiSelectionEnabled(multiple); 180 300 fc.setAcceptAllFileFilterUsed(false); 301 fc.setSelectedFile(this.file); 181 302 182 303 if (filters != null) { … … 196 317 197 318 /** 198 * Opens the {@code AbstractFileChooser} that has been created. Nothing happens if it has not been created yet.319 * Opens the {@code AbstractFileChooser} that has been created. 199 320 * @return the {@code AbstractFileChooser} if the user effectively choses a file or directory. {@code null} if the user cancelled the dialog. 200 321 */ … … 205 326 /** 206 327 * Opens the {@code AbstractFileChooser} that has been created and waits for the user to choose a file/directory, or cancel the dialog.<br> 207 * Nothing happens if the dialog has not been created yet.<br>208 328 * When the user choses a file or directory, the {@code lastDirProperty} is updated to the chosen directory path. 209 329 * … … 212 332 */ 213 333 public AbstractFileChooser openFileChooser(Component parent) { 214 if (fc != null) { 215 if (parent == null) { 216 parent = Main.parent; 217 } 218 219 int answer = open ? fc.showOpenDialog(parent) : fc.showSaveDialog(parent); 220 if (answer != JFileChooser.APPROVE_OPTION) { 334 if (fc == null) doCreateFileChooser(); 335 336 if (parent == null) { 337 parent = Main.parent; 338 } 339 340 int answer = open ? fc.showOpenDialog(parent) : fc.showSaveDialog(parent); 341 if (answer != JFileChooser.APPROVE_OPTION) { 342 return null; 343 } 344 345 if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir)) { 346 Main.pref.put(lastDirProperty, fc.getCurrentDirectory().getAbsolutePath()); 347 } 348 349 if (!open) { 350 File file = fc.getSelectedFile(); 351 if (!SaveActionBase.confirmOverwrite(file)) { 221 352 return null; 222 353 } 223 224 if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir)) {225 Main.pref.put(lastDirProperty, fc.getCurrentDirectory().getAbsolutePath());226 }227 228 if (!open) {229 File file = fc.getSelectedFile();230 if (!SaveActionBase.confirmOverwrite(file)) {231 return null;232 }233 }234 354 } 235 355 return fc; 236 356 } 357 358 /** 359 * Opens the file chooser dialog, then checks if filename has the given extension. If not, adds the extension and asks for overwrite if filename exists. 360 * 361 * @return the {@code File} or {@code null} if the user cancelled the dialog. 362 */ 363 public File getFileForSave() { 364 return SaveActionBase.checkFileAndConfirmOverWrite(openFileChooser(), extension); 365 } 237 366 }
Note:
See TracChangeset
for help on using the changeset viewer.