private static boolean doInternalSave(Layer layer, File file) { if (file == null) return false; try { boolean exported = false; for (FileExporter exporter : ExtensionFileFilter.exporters) { if (exporter.acceptFile(file, layer)) { exporter.exportData(file, layer); exported = true; break; } } if (!exported) { JOptionPane.showMessageDialog( Main.parent, tr("No Exporter found! Nothing saved."), tr("Warning"), JOptionPane.WARNING_MESSAGE); return false; } layer.setName(file.getName()); layer.setAssociatedFile(file); if (layer instanceof OsmDataLayer) { ((OsmDataLayer) layer).onPostSaveToFile(); } Main.parent.repaint(); } catch (IOException e) { e.printStackTrace(); return false; } return true; }
private void parseJos(Document doc, ProgressMonitor progressMonitor) throws IllegalDataException { Element root = doc.getDocumentElement(); if (!equal(root.getTagName(), "josm-session")) { error(tr("Unexpected root element ''{0}'' in session file", root.getTagName())); } String version = root.getAttribute("version"); if (!"0.1".equals(version)) { error(tr("Version ''{0}'' of session file is not supported. Expected: 0.1", version)); } Element viewportEl = getElementByTagName(root, "viewport"); if (viewportEl != null) { EastNorth center = null; Element centerEl = getElementByTagName(viewportEl, "center"); if (centerEl != null && centerEl.hasAttribute("lat") && centerEl.hasAttribute("lon")) { try { LatLon centerLL = new LatLon( Double.parseDouble(centerEl.getAttribute("lat")), Double.parseDouble(centerEl.getAttribute("lon"))); center = Projections.project(centerLL); } catch (NumberFormatException ex) { } } if (center != null) { Element scaleEl = getElementByTagName(viewportEl, "scale"); if (scaleEl != null && scaleEl.hasAttribute("meter-per-pixel")) { try { double meterPerPixel = Double.parseDouble(scaleEl.getAttribute("meter-per-pixel")); Projection proj = Main.getProjection(); // Get a "typical" distance in east/north units that // corresponds to a couple of pixels. Shouldn't be too // large, to keep it within projection bounds and // not too small to avoid rounding errors. double dist = 0.01 * proj.getDefaultZoomInPPD(); LatLon ll1 = proj.eastNorth2latlon(new EastNorth(center.east() - dist, center.north())); LatLon ll2 = proj.eastNorth2latlon(new EastNorth(center.east() + dist, center.north())); double meterPerEasting = ll1.greatCircleDistance(ll2) / dist / 2; double scale = meterPerPixel / meterPerEasting; // unit: easting per pixel viewport = new ViewportData(center, scale); } catch (NumberFormatException ex) { } } } } Element layersEl = getElementByTagName(root, "layers"); if (layersEl == null) return; String activeAtt = layersEl.getAttribute("active"); try { active = (activeAtt != null && !activeAtt.isEmpty()) ? Integer.parseInt(activeAtt) - 1 : -1; } catch (NumberFormatException e) { Main.warn( "Unsupported value for 'active' layer attribute. Ignoring it. Error was: " + e.getMessage()); active = -1; } MultiMap<Integer, Integer> deps = new MultiMap<Integer, Integer>(); Map<Integer, Element> elems = new HashMap<Integer, Element>(); NodeList nodes = layersEl.getChildNodes(); for (int i = 0; i < nodes.getLength(); ++i) { Node node = nodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element e = (Element) node; if (equal(e.getTagName(), "layer")) { if (!e.hasAttribute("index")) { error(tr("missing mandatory attribute ''index'' for element ''layer''")); } Integer idx = null; try { idx = Integer.parseInt(e.getAttribute("index")); } catch (NumberFormatException ex) { } if (idx == null) { error(tr("unexpected format of attribute ''index'' for element ''layer''")); } if (elems.containsKey(idx)) { error( tr( "attribute ''index'' ({0}) for element ''layer'' must be unique", Integer.toString(idx))); } elems.put(idx, e); deps.putVoid(idx); String depStr = e.getAttribute("depends"); if (depStr != null) { for (String sd : depStr.split(",")) { Integer d = null; try { d = Integer.parseInt(sd); } catch (NumberFormatException ex) { Main.warn(ex); } if (d != null) { deps.put(idx, d); } } } } } } List<Integer> sorted = Utils.topologicalSort(deps); final Map<Integer, Layer> layersMap = new TreeMap<Integer, Layer>(Collections.reverseOrder()); final Map<Integer, SessionLayerImporter> importers = new HashMap<Integer, SessionLayerImporter>(); final Map<Integer, String> names = new HashMap<Integer, String>(); progressMonitor.setTicksCount(sorted.size()); LAYER: for (int idx : sorted) { Element e = elems.get(idx); if (e == null) { error(tr("missing layer with index {0}", idx)); } if (!e.hasAttribute("name")) { error(tr("missing mandatory attribute ''name'' for element ''layer''")); } String name = e.getAttribute("name"); names.put(idx, name); if (!e.hasAttribute("type")) { error(tr("missing mandatory attribute ''type'' for element ''layer''")); } String type = e.getAttribute("type"); SessionLayerImporter imp = getSessionLayerImporter(type); if (imp == null) { CancelOrContinueDialog dialog = new CancelOrContinueDialog(); dialog.show( tr("Unable to load layer"), tr("Cannot load layer of type ''{0}'' because no suitable importer was found.", type), JOptionPane.WARNING_MESSAGE, progressMonitor); if (dialog.isCancel()) { progressMonitor.cancel(); return; } else { continue; } } else { importers.put(idx, imp); List<LayerDependency> depsImp = new ArrayList<LayerDependency>(); for (int d : deps.get(idx)) { SessionLayerImporter dImp = importers.get(d); if (dImp == null) { CancelOrContinueDialog dialog = new CancelOrContinueDialog(); dialog.show( tr("Unable to load layer"), tr( "Cannot load layer {0} because it depends on layer {1} which has been skipped.", idx, d), JOptionPane.WARNING_MESSAGE, progressMonitor); if (dialog.isCancel()) { progressMonitor.cancel(); return; } else { continue LAYER; } } depsImp.add(new LayerDependency(d, layersMap.get(d), dImp)); } ImportSupport support = new ImportSupport(name, idx, depsImp); Layer layer = null; Exception exception = null; try { layer = imp.load(e, support, progressMonitor.createSubTaskMonitor(1, false)); } catch (IllegalDataException ex) { exception = ex; } catch (IOException ex) { exception = ex; } if (exception != null) { exception.printStackTrace(); CancelOrContinueDialog dialog = new CancelOrContinueDialog(); dialog.show( tr("Error loading layer"), tr( "<html>Could not load layer {0} ''{1}''.<br>Error is:<br>{2}</html>", idx, name, exception.getMessage()), JOptionPane.ERROR_MESSAGE, progressMonitor); if (dialog.isCancel()) { progressMonitor.cancel(); return; } else { continue; } } if (layer == null) throw new RuntimeException(); layersMap.put(idx, layer); } progressMonitor.worked(1); } layers = new ArrayList<Layer>(); for (int idx : layersMap.keySet()) { Layer layer = layersMap.get(idx); if (layer == null) { continue; } Element el = elems.get(idx); if (el.hasAttribute("visible")) { layer.setVisible(Boolean.parseBoolean(el.getAttribute("visible"))); } if (el.hasAttribute("opacity")) { try { double opacity = Double.parseDouble(el.getAttribute("opacity")); layer.setOpacity(opacity); } catch (NumberFormatException ex) { Main.warn(ex); } } } for (Entry<Integer, Layer> e : layersMap.entrySet()) { Layer l = e.getValue(); if (l == null) { continue; } l.setName(names.get(e.getKey())); layers.add(l); } }