Changeset 6245 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2013-09-22T16:59:52+02:00 (11 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 2 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/ExtensionFileFilter.java
r6084 r6245 53 53 "org.openstreetmap.josm.io.JpgImporter", 54 54 "org.openstreetmap.josm.io.WMSLayerImporter", 55 "org.openstreetmap.josm.io.AllFormatsImporter" 55 "org.openstreetmap.josm.io.AllFormatsImporter", 56 "org.openstreetmap.josm.io.session.SessionImporter" 56 57 }; 57 58 -
trunk/src/org/openstreetmap/josm/actions/OpenLocationAction.java
r6104 r6245 28 28 import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask; 29 29 import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmUrlTask; 30 import org.openstreetmap.josm.actions.downloadtasks.DownloadSessionTask; 30 31 import org.openstreetmap.josm.actions.downloadtasks.DownloadTask; 31 32 import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler; … … 62 63 addDownloadTaskClass(DownloadOsmCompressedTask.class); 63 64 addDownloadTaskClass(DownloadOsmChangeCompressedTask.class); 65 addDownloadTaskClass(DownloadSessionTask.class); 64 66 } 65 67 -
trunk/src/org/openstreetmap/josm/actions/SessionLoadAction.java
r6084 r6245 7 7 import java.awt.event.ActionEvent; 8 8 import java.io.File; 9 import java.io.FileOutputStream; 9 10 import java.io.IOException; 11 import java.io.InputStream; 12 import java.net.URI; 10 13 import java.util.Arrays; 11 14 import java.util.List; … … 23 26 import org.openstreetmap.josm.gui.util.FileFilterAllFiles; 24 27 import org.openstreetmap.josm.io.IllegalDataException; 28 import org.openstreetmap.josm.io.session.SessionImporter; 25 29 import org.openstreetmap.josm.io.session.SessionReader; 26 30 import org.openstreetmap.josm.tools.CheckParameterUtil; 31 import org.openstreetmap.josm.tools.Utils; 32 33 /** 34 * Loads a JOSM session 35 * @since 4668 36 */ 27 37 public class SessionLoadAction extends DiskAccessAction { 28 38 39 /** 40 * Constructs a new {@code SessionLoadAction}. 41 */ 29 42 public SessionLoadAction() { 30 43 super(tr("Load Session"), "open", tr("Load a session from file."), null, true, "load-session", true); … … 34 47 @Override 35 48 public void actionPerformed(ActionEvent e) { 36 ExtensionFileFilter ff = new ExtensionFileFilter("jos,joz", "jos", tr("Session file (*.jos, *.joz)")); 37 JFileChooser fc = createAndOpenFileChooser(true, false, tr("Open session"), Arrays.asList(ff, FileFilterAllFiles.getInstance()), ff, JFileChooser.FILES_ONLY, "lastDirectory"); 49 JFileChooser fc = createAndOpenFileChooser(true, false, tr("Open session"), 50 Arrays.asList(SessionImporter.FILE_FILTER, FileFilterAllFiles.getInstance()), 51 SessionImporter.FILE_FILTER, JFileChooser.FILES_ONLY, "lastDirectory"); 38 52 if (fc == null) return; 39 53 File file = fc.getSelectedFile(); … … 42 56 } 43 57 58 /** 59 * JOSM session loader 60 */ 44 61 public static class Loader extends PleaseWaitRunnable { 45 62 46 63 private boolean canceled; 47 64 private File file; 48 private boolean zip; 65 private final URI uri; 66 private final InputStream is; 67 private final boolean zip; 49 68 private List<Layer> layers; 50 69 private List<Runnable> postLoadTasks; 51 70 private ViewportData viewport; 52 71 72 /** 73 * Constructs a new {@code Loader} for local session file. 74 * @param file The JOSM session file 75 * @param zip {@code true} if the file is a session archive file (*.joz) 76 */ 53 77 public Loader(File file, boolean zip) { 54 78 super(tr("Loading session ''{0}''", file.getName())); 79 CheckParameterUtil.ensureParameterNotNull(file, "file"); 55 80 this.file = file; 81 this.uri = null; 82 this.is = null; 56 83 this.zip = zip; 57 84 } 58 85 86 /** 87 * Constructs a new {@code Loader} for session file input stream (may be a remote file). 88 * @param is The input stream to session file 89 * @param uri The file URI 90 * @param zip {@code true} if the file is a session archive file (*.joz) 91 * @since 6245 92 */ 93 public Loader(InputStream is, URI uri, boolean zip) { 94 super(tr("Loading session ''{0}''", uri)); 95 CheckParameterUtil.ensureParameterNotNull(is, "is"); 96 CheckParameterUtil.ensureParameterNotNull(uri, "uri"); 97 this.file = null; 98 this.uri = uri; 99 this.is = is; 100 this.zip = zip; 101 } 102 59 103 @Override 60 p rotectedvoid cancel() {104 public void cancel() { 61 105 Thread.dumpStack(); 62 106 canceled = true; … … 99 143 ProgressMonitor monitor = getProgressMonitor(); 100 144 SessionReader reader = new SessionReader(); 101 reader.loadSession(file, zip, monitor); 102 layers = reader.getLayers(); 103 postLoadTasks = reader.getPostLoadTasks(); 104 viewport = reader.getViewport(); 145 boolean tempFile = false; 146 try { 147 if (file == null) { 148 // Download and write entire joz file as a temp file on disk as we need random access later 149 file = File.createTempFile("session_", ".joz", Utils.getJosmTempDir()); 150 tempFile = true; 151 FileOutputStream out = new FileOutputStream(file); 152 try { 153 Utils.copyStream(is, out); 154 } finally { 155 Utils.close(out); 156 } 157 } 158 reader.loadSession(file, zip, monitor); 159 layers = reader.getLayers(); 160 postLoadTasks = reader.getPostLoadTasks(); 161 viewport = reader.getViewport(); 162 } finally { 163 if (tempFile) { 164 if (!file.delete()) { 165 file.deleteOnExit(); 166 } 167 file = null; 168 } 169 } 105 170 } catch (IllegalDataException e) { 106 171 e.printStackTrace(); 107 172 HelpAwareOptionPane.showMessageDialogInEDT( 108 173 Main.parent, 109 tr("<html>Could not load session file ''{0}''.<br>Error is:<br>{1}</html>", file.getName(), e.getMessage()),174 tr("<html>Could not load session file ''{0}''.<br>Error is:<br>{1}</html>", uri != null ? uri : file.getName(), e.getMessage()), 110 175 tr("Data Error"), 111 176 JOptionPane.ERROR_MESSAGE, … … 117 182 HelpAwareOptionPane.showMessageDialogInEDT( 118 183 Main.parent, 119 tr("<html>Could not load session file ''{0}''.<br>Error is:<br>{1}</html>", file.getName(), e.getMessage()),184 tr("<html>Could not load session file ''{0}''.<br>Error is:<br>{1}</html>", uri != null ? uri : file.getName(), e.getMessage()), 120 185 tr("IO Error"), 121 186 JOptionPane.ERROR_MESSAGE, -
trunk/src/org/openstreetmap/josm/actions/SessionSaveAsAction.java
r6149 r6245 42 42 import org.openstreetmap.josm.tools.WindowGeometry; 43 43 44 /** 45 * Saves a JOSM session 46 * @since 4685 47 */ 44 48 public class SessionSaveAsAction extends DiskAccessAction { 45 49 … … 50 54 private boolean zipRequired; 51 55 56 /** 57 * Constructs a new {@code SessionSaveAsAction}. 58 */ 52 59 public SessionSaveAsAction() { 53 60 super(tr("Save Session As..."), "save_as", tr("Save the current session to a new file."), null, true, "save_as-session", true); … … 134 141 } 135 142 143 /** 144 * The "Save Session" dialog 145 */ 136 146 public class SessionSaveAsDialog extends ExtendedDialog { 137 147 148 /** 149 * Constructs a new {@code SessionSaveAsDialog}. 150 */ 138 151 public SessionSaveAsDialog() { 139 152 super(Main.parent, tr("Save Session"), new String[] {tr("Save As"), tr("Cancel")}); … … 229 242 setEnabled(Main.isDisplayingMapView()); 230 243 } 231 232 244 } -
trunk/src/org/openstreetmap/josm/io/session/SessionReader.java
r5684 r6245 85 85 } 86 86 87 private File sessionFile;88 private boolean zip; / * true, if session file is a .joz file; false if it is a .jos file */87 private URI sessionFileURI; 88 private boolean zip; // true, if session file is a .joz file; false if it is a .jos file 89 89 private ZipFile zipFile; 90 90 private List<Layer> layers = new ArrayList<Layer>(); … … 208 208 // relative to session file - "../" step out of the archive 209 209 String relPath = uri.getPath().substring(3); 210 return new File(sessionFile .toURI().resolve(relPath));210 return new File(sessionFileURI.resolve(relPath)); 211 211 } else { 212 212 // file inside zip archive … … 215 215 } 216 216 } else 217 return new File(sessionFile .toURI().resolve(uri));217 return new File(sessionFileURI.resolve(uri)); 218 218 } 219 219 } else … … 225 225 226 226 /** 227 * Returns true if we are reading from a .joz file. 227 * Determines if we are reading from a .joz file. 228 * @return {@code true} if we are reading from a .joz file, {@code false} otherwise 228 229 */ 229 230 public boolean isZip() { … … 278 279 } 279 280 280 private void error(String msg) throws IllegalDataException {281 private static void error(String msg) throws IllegalDataException { 281 282 throw new IllegalDataException(msg); 282 283 } … … 530 531 progressMonitor = NullProgressMonitor.INSTANCE; 531 532 } 532 this.sessionFile = sessionFile;533 this.zip = zip;534 533 535 534 InputStream josIS = null; … … 538 537 try { 539 538 zipFile = new ZipFile(sessionFile); 540 ZipEntry josEntry = null; 541 Enumeration<? extends ZipEntry> entries = zipFile.entries(); 542 while (entries.hasMoreElements()) { 543 ZipEntry entry = entries.nextElement(); 544 if (entry.getName().toLowerCase().endsWith(".jos")) { 545 josEntry = entry; 546 break; 547 } 548 } 549 if (josEntry == null) { 550 error(tr("expected .jos file inside .joz archive")); 551 } 552 josIS = zipFile.getInputStream(josEntry); 539 josIS = getZipInputStream(zipFile); 553 540 } catch (ZipException ze) { 554 541 throw new IOException(ze); … … 562 549 } 563 550 551 loadSession(josIS, sessionFile.toURI(), zip, progressMonitor); 552 } 553 554 private static InputStream getZipInputStream(ZipFile zipFile) throws ZipException, IOException, IllegalDataException { 555 ZipEntry josEntry = null; 556 Enumeration<? extends ZipEntry> entries = zipFile.entries(); 557 while (entries.hasMoreElements()) { 558 ZipEntry entry = entries.nextElement(); 559 if (entry.getName().toLowerCase().endsWith(".jos")) { 560 josEntry = entry; 561 break; 562 } 563 } 564 if (josEntry == null) { 565 error(tr("expected .jos file inside .joz archive")); 566 } 567 return zipFile.getInputStream(josEntry); 568 } 569 570 private void loadSession(InputStream josIS, URI sessionFileURI, boolean zip, ProgressMonitor progressMonitor) throws IOException, IllegalDataException { 571 572 this.sessionFileURI = sessionFileURI; 573 this.zip = zip; 574 564 575 try { 565 576 DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); … … 581 592 return (Element) els.item(0); 582 593 } 583 584 594 } -
trunk/src/org/openstreetmap/josm/tools/Utils.java
r6222 r6245 728 728 return all.toString(); 729 729 } 730 731 /** 732 * Returns the JOSM temp directory. 733 * @return The JOSM temp directory ({@code <java.io.tmpdir>/JOSM}), or {@code null} if {@code java.io.tmpdir} is not defined 734 * @since 6245 735 */ 736 public static File getJosmTempDir() { 737 String tmpDir = System.getProperty("java.io.tmpdir"); 738 if (tmpDir == null) { 739 return null; 740 } 741 File josmTmpDir = new File(tmpDir, "JOSM"); 742 if (!josmTmpDir.exists()) { 743 if (!josmTmpDir.mkdirs()) { 744 Main.warn("Unable to create temp directory "+josmTmpDir); 745 } 746 } 747 return josmTmpDir; 748 } 730 749 }
Note:
See TracChangeset
for help on using the changeset viewer.