Changeset 5438 in josm for trunk/src/org/openstreetmap/josm/actions
- Timestamp:
- 2012-08-13T00:12:48+02:00 (12 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/actions
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/DiskAccessAction.java
r4608 r5438 2 2 package org.openstreetmap.josm.actions; 3 3 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 6 import java.io.File; 4 import java.util.Collection; 7 5 8 6 import javax.swing.JFileChooser; 7 import javax.swing.filechooser.FileFilter; 9 8 10 import org.openstreetmap.josm.Main; 11 import org.openstreetmap.josm.gui.ExtendedDialog; 9 import org.openstreetmap.josm.gui.widgets.JFileChooserManager; 12 10 import org.openstreetmap.josm.tools.Shortcut; 13 11 14 12 /** 15 * Helper class for all actions that access the disk 13 * Helper class for all actions that access the disk. 14 * @since 78 16 15 */ 17 16 abstract public class DiskAccessAction extends JosmAction { 18 17 18 /** 19 * Constructs a new {@code DiskAccessAction}. 20 * 21 * @param name The action's text as displayed on the menu (if it is added to a menu) 22 * @param iconName The filename of the icon to use 23 * @param tooltip A longer description of the action that will be displayed in the tooltip 24 * @param shortcut A ready-created shortcut object or {@code null} if you don't want a shortcut 25 * @since 1084 26 */ 19 27 public DiskAccessAction(String name, String iconName, String tooltip, Shortcut shortcut) { 20 28 super(name, iconName, tooltip, shortcut, true); 21 29 } 22 30 31 /** 32 * Constructs a new {@code DiskAccessAction}. 33 * 34 * @param name The action's text as displayed on the menu (if it is added to a menu) 35 * @param iconName The filename of the icon to use 36 * @param tooltip A longer description of the action that will be displayed in the tooltip 37 * @param shortcut A ready-created shortcut object or null if you don't want a shortcut 38 * @param register Register this action for the toolbar preferences? 39 * @param toolbarId Identifier for the toolbar preferences. The iconName is used, if this parameter is null 40 * @param installAdapters False, if you don't want to install layer changed and selection changed adapters 41 * @since 5438 42 */ 43 public DiskAccessAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register, String toolbarId, boolean installAdapters) { 44 super(name, iconName, tooltip, shortcut, register, toolbarId, installAdapters); 45 } 46 47 /** 48 * Creates a new {@link JFileChooser} and makes it visible. 49 * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog 50 * @param multiple If true, makes the dialog allow multiple file selections 51 * @param title The string that goes in the dialog window's title bar 52 * @return The {@code JFileChooser}. 53 * @since 1646 54 */ 23 55 public static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title) { 24 56 return createAndOpenFileChooser(open, multiple, title, null); 25 57 } 26 58 59 /** 60 * Creates a new {@link JFileChooser} and makes it visible. 61 * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog 62 * @param multiple If true, makes the dialog allow multiple file selections 63 * @param title The string that goes in the dialog window's title bar 64 * @param extension The file extension that will be selected as the default file filter 65 * @return The {@code JFileChooser}. 66 * @since 2020 67 */ 27 68 public static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title, String extension) { 28 String curDir = Main.pref.get("lastDirectory"); 29 if (curDir.equals("")) { 30 curDir = "."; 31 } 32 JFileChooser fc = new JFileChooser(new File(curDir)); 33 if (title != null) { 34 fc.setDialogTitle(title); 35 } 69 return createAndOpenFileChooser(open, multiple, title, extension, JFileChooser.FILES_ONLY, true, null); 70 } 36 71 37 fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 38 fc.setMultiSelectionEnabled(multiple); 39 fc.setAcceptAllFileFilterUsed(false); 40 ExtensionFileFilter.applyChoosableImportFileFilters(fc, extension); 72 /** 73 * Creates a new {@link JFileChooser} and makes it visible. 74 * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog 75 * @param multiple If true, makes the dialog allow multiple file selections 76 * @param title The string that goes in the dialog window's title bar 77 * @param extension The file extension that will be selected as the default file filter 78 * @param selectionMode The selection mode that allows the user to:<br/> 79 * <li>just select files ({@code JFileChooser.FILES_ONLY})</li> 80 * <li>just select directories ({@code JFileChooser.DIRECTORIES_ONLY})</li> 81 * <li>select both files and directories ({@code JFileChooser.FILES_AND_DIRECTORIES})</li> 82 * @param allTypes If true, all the files types known by JOSM will be proposed in the "file type" combobox. 83 * If false, only the file filters that include {@code extension} will be proposed 84 * @param lastDirProperty The name of the property used to setup the JFileChooser initial directory. 85 * This property will then be updated to the new "last directory" chosen by the user. If null, the default property "lastDirectory" will be used. 86 * @return The {@code JFileChooser}. 87 * @since 5438 88 */ 89 public static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title, String extension, int selectionMode, boolean allTypes, String lastDirProperty) { 90 return new JFileChooserManager(open, lastDirProperty).createFileChooser(multiple, title, extension, allTypes, selectionMode).openFileChooser(); 91 } 41 92 42 int answer = open ? fc.showOpenDialog(Main.parent) : fc.showSaveDialog(Main.parent); 43 if (answer != JFileChooser.APPROVE_OPTION) 44 return null; 93 /** 94 * Creates a new {@link JFileChooser} for a single {@link FileFilter} and makes it visible. 95 * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog 96 * @param multiple If true, makes the dialog allow multiple file selections 97 * @param title The string that goes in the dialog window's title bar 98 * @param filter The only file filter that will be proposed by the dialog 99 * @param selectionMode The selection mode that allows the user to:<br/> 100 * <li>just select files ({@code JFileChooser.FILES_ONLY})</li> 101 * <li>just select directories ({@code JFileChooser.DIRECTORIES_ONLY})</li> 102 * <li>select both files and directories ({@code JFileChooser.FILES_AND_DIRECTORIES})</li> 103 * @param lastDirProperty The name of the property used to setup the JFileChooser initial directory. This property will then be updated to the new "last directory" chosen by the user 104 * @return The {@code JFileChooser}. 105 * @since 5438 106 */ 107 public static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title, FileFilter filter, int selectionMode, String lastDirProperty) { 108 return new JFileChooserManager(open, lastDirProperty).createFileChooser(multiple, title, filter, selectionMode).openFileChooser(); 109 } 45 110 46 if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir)) { 47 Main.pref.put("lastDirectory", fc.getCurrentDirectory().getAbsolutePath()); 48 } 49 50 if (!open) { 51 File file = fc.getSelectedFile(); 52 if (file != null && file.exists()) { 53 ExtendedDialog dialog = new ExtendedDialog( 54 Main.parent, 55 tr("Overwrite"), 56 new String[] {tr("Overwrite"), tr("Cancel")} 57 ); 58 dialog.setContent(tr("File exists. Overwrite?")); 59 dialog.setButtonIcons(new String[] {"save_as.png", "cancel.png"}); 60 dialog.showDialog(); 61 if (dialog.getValue() != 1) 62 return null; 63 } 64 } 65 66 return fc; 111 /** 112 * Creates a new {@link JFileChooser} for several {@link FileFilter}s and makes it visible. 113 * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog 114 * @param multiple If true, makes the dialog allow multiple file selections 115 * @param title The string that goes in the dialog window's title bar 116 * @param filters The file filters that will be proposed by the dialog 117 * @param defaultFilter The file filter that will be selected by default 118 * @param selectionMode The selection mode that allows the user to:<br/> 119 * <li>just select files ({@code JFileChooser.FILES_ONLY})</li> 120 * <li>just select directories ({@code JFileChooser.DIRECTORIES_ONLY})</li> 121 * <li>select both files and directories ({@code JFileChooser.FILES_AND_DIRECTORIES})</li> 122 * @param lastDirProperty The name of the property used to setup the JFileChooser initial directory. This property will then be updated to the new "last directory" chosen by the user 123 * @return The {@code JFileChooser}. 124 * @since 5438 125 */ 126 public static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title, 127 Collection<? extends FileFilter> filters, FileFilter defaultFilter, int selectionMode, String lastDirProperty) { 128 return new JFileChooserManager(open, lastDirProperty).createFileChooser(multiple, title, filters, defaultFilter, selectionMode).openFileChooser(); 67 129 } 68 130 } -
trunk/src/org/openstreetmap/josm/actions/ExtensionFileFilter.java
r5266 r5438 19 19 * A file filter that filters after the extension. Also includes a list of file 20 20 * filters used in JOSM. 21 * 21 * @since 32 22 22 */ 23 23 public class ExtensionFileFilter extends FileFilter { 24 24 25 25 /** 26 * list of supported formats 26 * List of supported formats for import. 27 * @since 4869 27 28 */ 28 29 public static final ArrayList<FileImporter> importers; 29 30 31 /** 32 * List of supported formats for export. 33 * @since 4869 34 */ 30 35 public static final ArrayList<FileExporter> exporters; 31 36 … … 95 100 * Updates the {@link AllFormatsImporter} that is contained in the importers list. If 96 101 * you do not use the importers variable directly, you don’t need to call this. 102 * <p> 103 * Updating the AllFormatsImporter is required when plugins add new importers that 104 * support new file extensions. The old AllFormatsImporter doesn’t include the new 105 * extensions and thus will not display these files. 97 106 * 98 * Updating the AllFormatsImporter is required when plugins add new importers that 99 * support new file extensions. The old AllFormatsImporter doesn’t include the new 100 * extensions and thus will not display these files. 107 * @since 5131 101 108 */ 102 109 public static void updateAllFormatsImporter() { … … 114 121 * 115 122 * @return an ordered list of {@link ExtensionFileFilter}s for importing. 123 * @since 2029 116 124 */ 117 125 public static List<ExtensionFileFilter> getImportExtensionFileFilters() { … … 131 139 * 132 140 * @return an ordered list of {@link ExtensionFileFilter}s for exporting. 141 * @since 2029 133 142 */ 134 143 public static List<ExtensionFileFilter> getExportExtensionFileFilters() { … … 149 158 * @param extension the extension 150 159 * @return the default {@link ExtensionFileFilter} for a given extension 160 * @since 2029 151 161 */ 152 162 public static ExtensionFileFilter getDefaultImportExtensionFileFilter(String extension) { … … 164 174 * @param extension the extension 165 175 * @return the default {@link ExtensionFileFilter} for a given extension 176 * @since 2029 166 177 */ 167 178 public static ExtensionFileFilter getDefaultExportExtensionFileFilter(String extension) { … … 180 191 * @param fileChooser the file chooser 181 192 * @param extension the default extension 182 */ 183 public static void applyChoosableImportFileFilters(JFileChooser fileChooser, String extension) { 193 * @param allTypes If true, all the files types known by JOSM will be proposed in the "file type" combobox. 194 * If false, only the file filters that include {@code extension} will be proposed 195 * @since 5438 196 */ 197 public static void applyChoosableImportFileFilters(JFileChooser fileChooser, String extension, boolean allTypes) { 184 198 for (ExtensionFileFilter filter: getImportExtensionFileFilters()) { 185 fileChooser.addChoosableFileFilter(filter); 199 if (allTypes || filter.acceptName("file."+extension)) { 200 fileChooser.addChoosableFileFilter(filter); 201 } 186 202 } 187 203 fileChooser.setFileFilter(getDefaultImportExtensionFileFilter(extension)); … … 194 210 * @param fileChooser the file chooser 195 211 * @param extension the default extension 196 */ 197 public static void applyChoosableExportFileFilters(JFileChooser fileChooser, String extension) { 212 * @param allTypes If true, all the files types known by JOSM will be proposed in the "file type" combobox. 213 * If false, only the file filters that include {@code extension} will be proposed 214 * @since 5438 215 */ 216 public static void applyChoosableExportFileFilters(JFileChooser fileChooser, String extension, boolean allTypes) { 198 217 for (ExtensionFileFilter filter: getExportExtensionFileFilters()) { 199 fileChooser.addChoosableFileFilter(filter); 218 if (allTypes || filter.acceptName("file."+extension)) { 219 fileChooser.addChoosableFileFilter(filter); 220 } 200 221 } 201 222 fileChooser.setFileFilter(getDefaultExportExtensionFileFilter(extension)); … … 204 225 /** 205 226 * Construct an extension file filter by giving the extension to check after. 227 * @param extension The comma-separated list of file extensions 228 * @param defaultExtension The default extension 229 * @param description A short textual description of the file type 230 * @since 1169 206 231 */ 207 232 public ExtensionFileFilter(String extension, String defaultExtension, String description) { … … 211 236 } 212 237 238 /** 239 * Returns true if this file filter accepts the given filename. 240 * @param filename The filename to check after 241 * @return true if this file filter accepts the given filename (i.e if this filename ends with one of the extensions) 242 * @since 1169 243 */ 213 244 public boolean acceptName(String filename) { 214 245 String name = filename.toLowerCase(); … … 219 250 } 220 251 221 @Override public boolean accept(File pathname) { 252 @Override 253 public boolean accept(File pathname) { 222 254 if (pathname.isDirectory()) 223 255 return true; … … 225 257 } 226 258 227 @Override public String getDescription() { 259 @Override 260 public String getDescription() { 228 261 return description; 229 262 } 230 263 264 /** 265 * Replies the comma-separated list of file extensions of this file filter. 266 * @return the comma-separated list of file extensions of this file filter, as a String 267 * @since 5131 268 */ 231 269 public String getExtensions() { 232 270 return extensions; 233 271 } 234 272 273 /** 274 * Replies the default file extension of this file filter. 275 * @return the default file extension of this file filter 276 * @since 2029 277 */ 235 278 public String getDefaultExtension() { 236 279 return defaultExtension; -
trunk/src/org/openstreetmap/josm/actions/SaveActionBase.java
r5014 r5438 182 182 183 183 public static File createAndOpenSaveFileChooser(String title, String extension) { 184 String curDir = Main.pref.get("lastDirectory"); 185 if (curDir.equals("")) { 186 curDir = "."; 187 } 188 JFileChooser fc = new JFileChooser(new File(curDir)); 189 if (title != null) { 190 fc.setDialogTitle(title); 191 } 192 193 fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 194 fc.setMultiSelectionEnabled(false); 195 fc.setAcceptAllFileFilterUsed(false); 196 ExtensionFileFilter.applyChoosableExportFileFilters(fc, extension); 197 int answer = fc.showSaveDialog(Main.parent); 198 if (answer != JFileChooser.APPROVE_OPTION) 199 return null; 200 201 if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir)) { 202 Main.pref.put("lastDirectory", fc.getCurrentDirectory().getAbsolutePath()); 203 } 184 185 JFileChooser fc = createAndOpenFileChooser(false, false, title, extension); 186 if (fc == null) return null; 204 187 205 188 File file = fc.getSelectedFile(); 206 189 String fn = file.getPath(); 207 if (fn.indexOf('.') == -1)190 if (fn.indexOf('.') == -1) 208 191 { 209 192 FileFilter ff = fc.getFileFilter(); -
trunk/src/org/openstreetmap/josm/actions/SessionLoadAction.java
r4874 r5438 13 13 import javax.swing.JOptionPane; 14 14 import javax.swing.SwingUtilities; 15 import javax.swing.filechooser.FileFilter;16 15 17 16 import org.openstreetmap.josm.Main; … … 23 22 import org.openstreetmap.josm.io.session.SessionReader; 24 23 25 public class SessionLoadAction extends JosmAction { 24 public class SessionLoadAction extends DiskAccessAction { 25 26 26 public SessionLoadAction() { 27 27 super(tr("Load Session"), "open", tr("Load a session from file."), null, true, "load-session", true); … … 30 30 31 31 public void actionPerformed(ActionEvent e) { 32 String curDir = Main.pref.get("lastDirectory"); 33 if (curDir.equals("")) { 34 curDir = "."; 35 } 36 JFileChooser fc = new JFileChooser(new File(curDir)); 37 fc.setDialogTitle(tr("Open session")); 38 fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 39 fc.setMultiSelectionEnabled(false); 40 fc.setAcceptAllFileFilterUsed(true); 41 FileFilter ff = new ExtensionFileFilter("jos,joz", "jos", tr("Session file (*.jos, *.joz)")); 42 fc.addChoosableFileFilter(ff); 43 int answer = fc.showOpenDialog(Main.parent); 44 if (answer != JFileChooser.APPROVE_OPTION) 45 return; 46 47 if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir)) { 48 Main.pref.put("lastDirectory", fc.getCurrentDirectory().getAbsolutePath()); 49 } 32 ExtensionFileFilter ff = new ExtensionFileFilter("jos,joz", "jos", tr("Session file (*.jos, *.joz)")); 33 JFileChooser fc = createAndOpenFileChooser(true, false, tr("Open session"), ff, JFileChooser.FILES_ONLY, "lastDirectory"); 34 if (fc == null) return; 50 35 File file = fc.getSelectedFile(); 51 boolean zip = true; 52 if (file.getName().toLowerCase().endsWith(".jos")) { 53 zip = false; 54 } 36 boolean zip = file.getName().toLowerCase().endsWith(".joz"); 55 37 Main.worker.submit(new Loader(file, zip)); 56 38 } … … 72 54 @Override 73 55 protected void cancel() { 74 Thread. currentThread().dumpStack();56 Thread.dumpStack(); 75 57 canceled = true; 76 58 } -
trunk/src/org/openstreetmap/josm/actions/SessionSaveAsAction.java
r4910 r5438 2 2 package org.openstreetmap.josm.actions; 3 3 4 import static org.openstreetmap.josm.gui.help.HelpUtil.ht; 4 5 import static org.openstreetmap.josm.tools.I18n.tr; 5 import static org.openstreetmap.josm.gui.help.HelpUtil.ht;6 6 7 7 import java.awt.Component; … … 12 12 import java.io.IOException; 13 13 import java.util.ArrayList; 14 import java.util.Arrays; 14 15 import java.util.Collection; 16 import java.util.Collections; 15 17 import java.util.HashMap; 16 18 import java.util.HashSet; … … 32 34 33 35 import org.openstreetmap.josm.Main; 34 import org.openstreetmap.josm.actions.ExtensionFileFilter;35 36 import org.openstreetmap.josm.gui.ExtendedDialog; 36 37 import org.openstreetmap.josm.gui.HelpAwareOptionPane; … … 42 43 import org.openstreetmap.josm.tools.WindowGeometry; 43 44 44 public class SessionSaveAsAction extends JosmAction {45 public class SessionSaveAsAction extends DiskAccessAction { 45 46 46 47 private List<Layer> layers; … … 52 53 /** 53 54 * Construct the action with "Save" as label. 54 * @param layer Save this layer.55 55 */ 56 56 public SessionSaveAsAction() { … … 77 77 } 78 78 } 79 80 String curDir = Main.pref.get("lastDirectory"); 81 if (curDir.equals("")) { 82 curDir = "."; 83 } 84 JFileChooser fc = new JFileChooser(new File(curDir)); 85 fc.setDialogTitle(tr("Save session")); 86 fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 79 87 80 FileFilter joz = new ExtensionFileFilter("joz", "joz", tr("Session file (archive) (*.joz)")); 88 81 FileFilter jos = new ExtensionFileFilter("jos", "jos", tr("Session file (*.jos)")); 82 83 JFileChooser fc; 84 89 85 if (zipRequired) { 90 fc .addChoosableFileFilter(joz);86 fc = createAndOpenFileChooser(false, false, tr("Save session"), joz, JFileChooser.FILES_ONLY, "lastDirectory"); 91 87 } else { 92 fc.addChoosableFileFilter(jos); 93 fc.addChoosableFileFilter(joz); 94 fc.setFileFilter(jos); 95 } 96 int answer = fc.showSaveDialog(Main.parent); 97 if (answer != JFileChooser.APPROVE_OPTION) 88 fc = createAndOpenFileChooser(false, false, tr("Save session"), Arrays.asList(new FileFilter[]{jos, joz}), jos, JFileChooser.FILES_ONLY, "lastDirectory"); 89 } 90 91 if (fc == null) 98 92 return; 99 100 if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir)) {101 Main.pref.put("lastDirectory", fc.getCurrentDirectory().getAbsolutePath());102 }103 93 104 94 File file = fc.getSelectedFile();
Note:
See TracChangeset
for help on using the changeset viewer.