001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.actions; 003 004import static org.openstreetmap.josm.gui.help.HelpUtil.ht; 005import static org.openstreetmap.josm.tools.I18n.tr; 006 007import java.awt.event.ActionEvent; 008import java.io.File; 009import java.io.IOException; 010import java.io.InputStream; 011import java.net.URI; 012import java.nio.file.Files; 013import java.nio.file.StandardCopyOption; 014import java.util.Arrays; 015import java.util.EnumSet; 016import java.util.List; 017 018import javax.swing.JFileChooser; 019import javax.swing.JOptionPane; 020import javax.swing.SwingUtilities; 021 022import org.openstreetmap.josm.data.projection.ProjectionRegistry; 023import org.openstreetmap.josm.gui.HelpAwareOptionPane; 024import org.openstreetmap.josm.gui.MainApplication; 025import org.openstreetmap.josm.gui.Notification; 026import org.openstreetmap.josm.gui.PleaseWaitRunnable; 027import org.openstreetmap.josm.gui.layer.Layer; 028import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference; 029import org.openstreetmap.josm.gui.progress.ProgressMonitor; 030import org.openstreetmap.josm.gui.util.FileFilterAllFiles; 031import org.openstreetmap.josm.gui.widgets.AbstractFileChooser; 032import org.openstreetmap.josm.io.IllegalDataException; 033import org.openstreetmap.josm.io.session.SessionImporter; 034import org.openstreetmap.josm.io.session.SessionReader; 035import org.openstreetmap.josm.io.session.SessionReader.SessionProjectionChoiceData; 036import org.openstreetmap.josm.io.session.SessionReader.SessionViewportData; 037import org.openstreetmap.josm.io.session.SessionWriter; 038import org.openstreetmap.josm.tools.CheckParameterUtil; 039import org.openstreetmap.josm.tools.JosmRuntimeException; 040import org.openstreetmap.josm.tools.Logging; 041import org.openstreetmap.josm.tools.Utils; 042import org.openstreetmap.josm.tools.bugreport.ReportedException; 043 044/** 045 * Loads a JOSM session. 046 * @since 4668 047 */ 048public class SessionLoadAction extends DiskAccessAction { 049 050 /** 051 * Constructs a new {@code SessionLoadAction}. 052 */ 053 public SessionLoadAction() { 054 super(tr("Load Session"), "open", tr("Load a session from file."), null, true, "load-session", true); 055 setHelpId(ht("/Action/SessionLoad")); 056 } 057 058 @Override 059 public void actionPerformed(ActionEvent e) { 060 AbstractFileChooser fc = createAndOpenFileChooser(true, false, tr("Open session"), 061 Arrays.asList(SessionImporter.FILE_FILTER, FileFilterAllFiles.getInstance()), 062 SessionImporter.FILE_FILTER, JFileChooser.FILES_ONLY, "lastDirectory"); 063 if (fc == null) 064 return; 065 File file = fc.getSelectedFile(); 066 boolean zip = Utils.hasExtension(file, "joz"); 067 MainApplication.worker.submit(new Loader(file, zip)); 068 } 069 070 /** 071 * JOSM session loader 072 */ 073 public static class Loader extends PleaseWaitRunnable { 074 075 private boolean canceled; 076 private File file; 077 private final URI uri; 078 private final InputStream is; 079 private final boolean zip; 080 private List<Layer> layers; 081 private Layer active; 082 private List<Runnable> postLoadTasks; 083 private SessionViewportData viewport; 084 private SessionProjectionChoiceData projectionChoice; 085 086 /** 087 * Constructs a new {@code Loader} for local session file. 088 * @param file The JOSM session file 089 * @param zip {@code true} if the file is a session archive file (*.joz) 090 */ 091 public Loader(File file, boolean zip) { 092 super(tr("Loading session ''{0}''", file.getName())); 093 CheckParameterUtil.ensureParameterNotNull(file, "file"); 094 this.file = file; 095 this.uri = null; 096 this.is = null; 097 this.zip = zip; 098 } 099 100 /** 101 * Constructs a new {@code Loader} for session file input stream (may be a remote file). 102 * @param is The input stream to session file 103 * @param uri The file URI 104 * @param zip {@code true} if the file is a session archive file (*.joz) 105 * @since 6245 106 */ 107 public Loader(InputStream is, URI uri, boolean zip) { 108 super(tr("Loading session ''{0}''", uri)); 109 CheckParameterUtil.ensureParameterNotNull(is, "is"); 110 CheckParameterUtil.ensureParameterNotNull(uri, "uri"); 111 this.file = null; 112 this.uri = uri; 113 this.is = is; 114 this.zip = zip; 115 } 116 117 @Override 118 public void cancel() { 119 canceled = true; 120 } 121 122 @Override 123 protected void finish() { 124 SwingUtilities.invokeLater(() -> { 125 if (canceled) 126 return; 127 if (projectionChoice != null) { 128 ProjectionPreference.setProjection( 129 projectionChoice.getProjectionChoiceId(), 130 projectionChoice.getSubPreferences(), 131 false); 132 } 133 addLayers(); 134 runPostLoadTasks(); 135 }); 136 } 137 138 private void addLayers() { 139 if (!Utils.isEmpty(layers)) { 140 boolean noMap = MainApplication.getMap() == null; 141 for (Layer l : layers) { 142 if (canceled) 143 return; 144 addLayer(l); 145 } 146 if (active != null) { 147 MainApplication.getLayerManager().setActiveLayer(active); 148 } 149 if (noMap && viewport != null) { 150 MainApplication.getMap().mapView.scheduleZoomTo(viewport.getEastNorthViewport(ProjectionRegistry.getProjection())); 151 } 152 } 153 } 154 155 /** 156 * Tries to add a new layer. 157 * @param l layer to add 158 * @return {@code true} if layer has been added, {@code false} if it wasn't needed or if an error occurred 159 */ 160 static boolean addLayer(Layer l) { 161 // NoteImporter directly loads notes into current note layer 162 if (!MainApplication.getLayerManager().containsLayer(l)) { 163 try { 164 MainApplication.getLayerManager().addLayer(l); 165 } catch (ReportedException e) { 166 Logging.error(e); 167 new Notification(tr("Unable to add layer ''{0}'': {1}", l.getName(), e.getMessage())) 168 .setIcon(JOptionPane.ERROR_MESSAGE).setDuration(Notification.TIME_LONG).show(); 169 if (MainApplication.getLayerManager().containsLayer(l)) { 170 MainApplication.getLayerManager().removeLayer(l); 171 } 172 return false; 173 } 174 } 175 return true; 176 } 177 178 private void runPostLoadTasks() { 179 if (postLoadTasks != null) { 180 for (Runnable task : postLoadTasks) { 181 if (canceled) 182 return; 183 if (task == null) { 184 continue; 185 } 186 task.run(); 187 } 188 } 189 } 190 191 @Override 192 protected void realRun() { 193 try { 194 ProgressMonitor monitor = getProgressMonitor(); 195 SessionReader reader = new SessionReader(); 196 boolean tempFile = false; 197 try { 198 if (file == null) { 199 // Download and write entire joz file as a temp file on disk as we need random access later 200 file = File.createTempFile("session_", ".joz", Utils.getJosmTempDir()); 201 tempFile = true; 202 Files.copy(is, file.toPath(), StandardCopyOption.REPLACE_EXISTING); 203 } 204 reader.loadSession(file, zip, monitor); 205 layers = reader.getLayers(); 206 active = reader.getActive(); 207 postLoadTasks = reader.getPostLoadTasks(); 208 viewport = reader.getViewport(); 209 projectionChoice = reader.getProjectionChoice(); 210 final EnumSet<SessionWriter.SessionWriterFlags> flagSet = EnumSet.noneOf(SessionWriter.SessionWriterFlags.class); 211 if (zip) { 212 flagSet.add(SessionWriter.SessionWriterFlags.IS_ZIP); 213 } 214 if (reader.loadedPluginData()) { 215 flagSet.add(SessionWriter.SessionWriterFlags.SAVE_PLUGIN_INFORMATION); 216 } 217 SessionSaveAction.setCurrentSession(file, reader.getLayers(), flagSet); 218 } finally { 219 if (tempFile) { 220 Utils.deleteFile(file); 221 file = null; 222 } 223 } 224 } catch (IllegalDataException e) { 225 handleException(tr("Data Error"), e); 226 } catch (IOException e) { 227 handleException(tr("IO Error"), e); 228 } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException e) { 229 cancel(); 230 throw e; 231 } 232 } 233 234 private void handleException(String dialogTitle, Exception e) { 235 Logging.error(e); 236 HelpAwareOptionPane.showMessageDialogInEDT( 237 MainApplication.getMainFrame(), 238 tr("<html>Could not load session file ''{0}''.<br>Error is:<br>{1}</html>", 239 uri != null ? uri : file.getName(), Utils.escapeReservedCharactersHTML(e.getMessage())), 240 dialogTitle, 241 JOptionPane.ERROR_MESSAGE, 242 null 243 ); 244 cancel(); 245 } 246 } 247}