1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.session;
|
---|
3 |
|
---|
4 | import java.awt.Component;
|
---|
5 | import java.awt.GridBagLayout;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.util.Collection;
|
---|
8 | import java.util.Collections;
|
---|
9 |
|
---|
10 | import javax.swing.JLabel;
|
---|
11 | import javax.swing.JPanel;
|
---|
12 | import javax.swing.SwingConstants;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
15 | import org.openstreetmap.josm.gui.layer.geoimage.GeoImageLayer;
|
---|
16 | import org.openstreetmap.josm.gui.layer.geoimage.ImageEntry;
|
---|
17 | import org.openstreetmap.josm.tools.GBC;
|
---|
18 | import org.openstreetmap.josm.tools.Logging;
|
---|
19 | import org.w3c.dom.Element;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Session exporter for {@link GeoImageLayer}.
|
---|
23 | * @since 5505
|
---|
24 | */
|
---|
25 | public class GeoImageSessionExporter extends AbstractSessionExporter<GeoImageLayer> {
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Constructs a new {@code GeoImageSessionExporter}.
|
---|
29 | * @param layer GeoImage layer to export
|
---|
30 | */
|
---|
31 | public GeoImageSessionExporter(GeoImageLayer layer) { // NO_UCD (unused code)
|
---|
32 | super(layer);
|
---|
33 | }
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | public Collection<Layer> getDependencies() {
|
---|
37 | if (layer.getGpxLayer() != null)
|
---|
38 | return Collections.singleton((Layer) layer.getGpxLayer());
|
---|
39 | else
|
---|
40 | return Collections.emptySet();
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public Component getExportPanel() {
|
---|
45 | final JPanel p = new JPanel(new GridBagLayout());
|
---|
46 | export.setSelected(true);
|
---|
47 | final JLabel lbl = new JLabel(layer.getName(), layer.getIcon(), SwingConstants.LEADING);
|
---|
48 | lbl.setToolTipText(layer.getToolTipText());
|
---|
49 | lbl.setLabelFor(export);
|
---|
50 | p.add(export, GBC.std());
|
---|
51 | p.add(lbl, GBC.std());
|
---|
52 | p.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
|
---|
53 | return p;
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Override
|
---|
57 | public Element export(SessionWriter.ExportSupport support) throws IOException {
|
---|
58 | Element layerElem = support.createElement("layer");
|
---|
59 | layerElem.setAttribute("type", "geoimage");
|
---|
60 | layerElem.setAttribute("version", "0.1");
|
---|
61 | addAttr("show-thumbnails", Boolean.toString(layer.isUseThumbs()), layerElem, support);
|
---|
62 |
|
---|
63 | for (ImageEntry entry : layer.getImages()) {
|
---|
64 |
|
---|
65 | Element imgElem = support.createElement("geoimage");
|
---|
66 |
|
---|
67 | if (entry.getFile() == null) {
|
---|
68 | Logging.warn("No file attribute for image - skipping entry");
|
---|
69 | break;
|
---|
70 | }
|
---|
71 | addAttr("file", entry.getFile().getPath(), imgElem, support);
|
---|
72 | // FIXME: relative filenames as option
|
---|
73 | // FIXME: include images as option (?)
|
---|
74 |
|
---|
75 | addAttr("thumbnail", Boolean.toString(entry.hasThumbnail()), imgElem, support);
|
---|
76 | if (entry.getPos() != null) {
|
---|
77 | Element posElem = support.createElement("position");
|
---|
78 | posElem.setAttribute("lat", Double.toString(entry.getPos().lat()));
|
---|
79 | posElem.setAttribute("lon", Double.toString(entry.getPos().lon()));
|
---|
80 | imgElem.appendChild(posElem);
|
---|
81 | }
|
---|
82 | if (entry.getSpeed() != null) {
|
---|
83 | addAttr("speed", entry.getSpeed().toString(), imgElem, support);
|
---|
84 | }
|
---|
85 | if (entry.getElevation() != null) {
|
---|
86 | addAttr("elevation", entry.getElevation().toString(), imgElem, support);
|
---|
87 | }
|
---|
88 | if (entry.hasGpsTime()) {
|
---|
89 | addAttr("gps-time", Long.toString(entry.getGpsInstant().toEpochMilli()), imgElem, support);
|
---|
90 | }
|
---|
91 | if (entry.getExifOrientation() != null) {
|
---|
92 | addAttr("exif-orientation", Integer.toString(entry.getExifOrientation()), imgElem, support);
|
---|
93 | }
|
---|
94 | if (entry.hasExifTime()) {
|
---|
95 | addAttr("exif-time", Long.toString(entry.getExifInstant().toEpochMilli()), imgElem, support);
|
---|
96 | }
|
---|
97 | if (entry.hasExifGpsTime()) {
|
---|
98 | addAttr("exif-gps-time", Long.toString(entry.getExifGpsInstant().toEpochMilli()), imgElem, support);
|
---|
99 | }
|
---|
100 | if (entry.getExifCoor() != null) {
|
---|
101 | Element posElem = support.createElement("exif-coordinates");
|
---|
102 | posElem.setAttribute("lat", Double.toString(entry.getExifCoor().lat()));
|
---|
103 | posElem.setAttribute("lon", Double.toString(entry.getExifCoor().lon()));
|
---|
104 | imgElem.appendChild(posElem);
|
---|
105 | }
|
---|
106 | if (entry.getExifImgDir() != null) {
|
---|
107 | addAttr("exif-image-direction", entry.getExifImgDir().toString(), imgElem, support);
|
---|
108 | }
|
---|
109 | if (entry.hasNewGpsData()) {
|
---|
110 | addAttr("is-new-gps-data", Boolean.toString(entry.hasNewGpsData()), imgElem, support);
|
---|
111 | }
|
---|
112 |
|
---|
113 | layerElem.appendChild(imgElem);
|
---|
114 | }
|
---|
115 | return layerElem;
|
---|
116 | }
|
---|
117 |
|
---|
118 | private static void addAttr(String name, String value, Element element, SessionWriter.ExportSupport support) {
|
---|
119 | Element attrElem = support.createElement(name);
|
---|
120 | attrElem.appendChild(support.createTextNode(value));
|
---|
121 | element.appendChild(attrElem);
|
---|
122 | }
|
---|
123 | }
|
---|