1 | package org.openstreetmap.josm.gui.layer;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.awt.Graphics;
|
---|
5 | import java.awt.Point;
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.ActionListener;
|
---|
8 | import java.io.File;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.LinkedList;
|
---|
11 |
|
---|
12 | import javax.swing.Icon;
|
---|
13 | import javax.swing.JColorChooser;
|
---|
14 | import javax.swing.JFileChooser;
|
---|
15 | import javax.swing.JMenuItem;
|
---|
16 | import javax.swing.JOptionPane;
|
---|
17 | import javax.swing.JPopupMenu;
|
---|
18 | import javax.swing.filechooser.FileFilter;
|
---|
19 |
|
---|
20 | import org.openstreetmap.josm.Main;
|
---|
21 | import org.openstreetmap.josm.actions.GpxExportAction;
|
---|
22 | import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
|
---|
23 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
24 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
25 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
26 | import org.openstreetmap.josm.gui.MapView;
|
---|
27 | import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
|
---|
28 | import org.openstreetmap.josm.tools.ColorHelper;
|
---|
29 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * A layer holding data from a gps source.
|
---|
33 | * The data is read only.
|
---|
34 | *
|
---|
35 | * @author imi
|
---|
36 | */
|
---|
37 | public class RawGpsLayer extends Layer {
|
---|
38 |
|
---|
39 | private static Icon icon;
|
---|
40 |
|
---|
41 | public static class GpsPoint {
|
---|
42 | public final LatLon latlon;
|
---|
43 | public final EastNorth eastNorth;
|
---|
44 | public final String time;
|
---|
45 | public GpsPoint(LatLon ll, String t) {
|
---|
46 | latlon = ll;
|
---|
47 | eastNorth = Main.proj.latlon2eastNorth(ll);
|
---|
48 | time = t;
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * A list of ways which containing a list of points.
|
---|
54 | */
|
---|
55 | public final Collection<Collection<GpsPoint>> data;
|
---|
56 |
|
---|
57 | public RawGpsLayer(Collection<Collection<GpsPoint>> data, String name) {
|
---|
58 | super(name);
|
---|
59 | this.data = data;
|
---|
60 | Main.pref.listener.add(new PreferenceChangedListener(){
|
---|
61 | public void preferenceChanged(String key, String newValue) {
|
---|
62 | if (Main.map != null && (key.equals("drawRawGpsLines") || key.equals("forceRawGpsLines")))
|
---|
63 | Main.map.repaint();
|
---|
64 | }
|
---|
65 | });
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Return a static icon.
|
---|
70 | */
|
---|
71 | @Override public Icon getIcon() {
|
---|
72 | if (icon == null)
|
---|
73 | icon = ImageProvider.get("layer", "rawgps");
|
---|
74 | return icon;
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Override public void paint(Graphics g, MapView mv) {
|
---|
78 | String gpsCol = Main.pref.get("color.gps point");
|
---|
79 | String gpsColSpecial = Main.pref.get("color.layer "+name);
|
---|
80 | if (!gpsColSpecial.equals(""))
|
---|
81 | g.setColor(ColorHelper.html2color(gpsColSpecial));
|
---|
82 | else if (!gpsCol.equals(""))
|
---|
83 | g.setColor(ColorHelper.html2color(gpsCol));
|
---|
84 | else
|
---|
85 | g.setColor(Color.GRAY);
|
---|
86 | Point old = null;
|
---|
87 | for (Collection<GpsPoint> c : data) {
|
---|
88 | if (!Main.pref.getBoolean("forceRawGpsLines"))
|
---|
89 | old = null;
|
---|
90 | for (GpsPoint p : c) {
|
---|
91 | Point screen = mv.getPoint(p.eastNorth);
|
---|
92 | if (Main.pref.getBoolean("drawRawGpsLines") && old != null)
|
---|
93 | g.drawLine(old.x, old.y, screen.x, screen.y);
|
---|
94 | else
|
---|
95 | g.drawRect(screen.x, screen.y, 0, 0);
|
---|
96 | old = screen;
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Override public String getToolTipText() {
|
---|
102 | int points = 0;
|
---|
103 | for (Collection<GpsPoint> c : data)
|
---|
104 | points += c.size();
|
---|
105 | return data.size()+" tracks, "+points+" points.";
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Override public void mergeFrom(Layer from) {
|
---|
109 | RawGpsLayer layer = (RawGpsLayer)from;
|
---|
110 | data.addAll(layer.data);
|
---|
111 | }
|
---|
112 |
|
---|
113 | @Override public boolean isMergable(Layer other) {
|
---|
114 | return other instanceof RawGpsLayer;
|
---|
115 | }
|
---|
116 |
|
---|
117 | @Override public void visitBoundingBox(BoundingXYVisitor v) {
|
---|
118 | for (Collection<GpsPoint> c : data)
|
---|
119 | for (GpsPoint p : c)
|
---|
120 | v.visit(p.eastNorth);
|
---|
121 | }
|
---|
122 |
|
---|
123 | @Override public Object getInfoComponent() {
|
---|
124 | StringBuilder b = new StringBuilder();
|
---|
125 | int points = 0;
|
---|
126 | for (Collection<GpsPoint> c : data) {
|
---|
127 | b.append(" a track with "+c.size()+" points<br>");
|
---|
128 | points += c.size();
|
---|
129 | }
|
---|
130 | b.append("</html>");
|
---|
131 | return "<html>"+name+" consists of "+data.size()+" tracks ("+points+" points)<br>"+b.toString();
|
---|
132 | }
|
---|
133 |
|
---|
134 | @Override public void addMenuEntries(JPopupMenu menu) {
|
---|
135 | menu.add(new JMenuItem(new GpxExportAction(this)));
|
---|
136 |
|
---|
137 | JMenuItem color = new JMenuItem("Customize Color", ImageProvider.get("colorchooser"));
|
---|
138 | color.addActionListener(new ActionListener(){
|
---|
139 | public void actionPerformed(ActionEvent e) {
|
---|
140 | String col = Main.pref.get("color.layer "+name, Main.pref.get("color.gps point", ColorHelper.color2html(Color.gray)));
|
---|
141 | JColorChooser c = new JColorChooser(ColorHelper.html2color(col));
|
---|
142 | Object[] options = new Object[]{"OK", "Cancel", "Default"};
|
---|
143 | int answer = JOptionPane.showOptionDialog(Main.parent, c, "Choose a color", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
|
---|
144 | switch (answer) {
|
---|
145 | case 0:
|
---|
146 | Main.pref.put("color.layer "+name, ColorHelper.color2html(c.getColor()));
|
---|
147 | break;
|
---|
148 | case 1:
|
---|
149 | return;
|
---|
150 | case 2:
|
---|
151 | Main.pref.put("color.layer "+name, null);
|
---|
152 | break;
|
---|
153 | }
|
---|
154 | Main.map.repaint();
|
---|
155 | }
|
---|
156 | });
|
---|
157 | menu.add(color);
|
---|
158 |
|
---|
159 | JMenuItem tagimage = new JMenuItem("Import images", ImageProvider.get("tagimages"));
|
---|
160 | tagimage.addActionListener(new ActionListener(){
|
---|
161 | public void actionPerformed(ActionEvent e) {
|
---|
162 | JFileChooser fc = new JFileChooser(Main.pref.get("tagimages.lastdirectory"));
|
---|
163 | fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
|
---|
164 | fc.setMultiSelectionEnabled(true);
|
---|
165 | fc.setAcceptAllFileFilterUsed(false);
|
---|
166 | fc.setFileFilter(new FileFilter(){
|
---|
167 | @Override public boolean accept(File f) {
|
---|
168 | return f.isDirectory() || f.getName().toLowerCase().endsWith(".jpg");
|
---|
169 | }
|
---|
170 | @Override public String getDescription() {
|
---|
171 | return "JPEG images (*.jpg)";
|
---|
172 | }
|
---|
173 | });
|
---|
174 | fc.showOpenDialog(Main.parent);
|
---|
175 | File[] sel = fc.getSelectedFiles();
|
---|
176 | if (sel == null || sel.length == 0)
|
---|
177 | return;
|
---|
178 | LinkedList<File> files = new LinkedList<File>();
|
---|
179 | addRecursiveFiles(files, sel);
|
---|
180 | Main.pref.put("tagimages.lastdirectory", fc.getCurrentDirectory().getPath());
|
---|
181 | GeoImageLayer.create(files, RawGpsLayer.this);
|
---|
182 | }
|
---|
183 |
|
---|
184 | private void addRecursiveFiles(LinkedList<File> files, File[] sel) {
|
---|
185 | for (File f : sel) {
|
---|
186 | if (f.isDirectory())
|
---|
187 | addRecursiveFiles(files, f.listFiles());
|
---|
188 | else if (f.getName().toLowerCase().endsWith(".jpg"))
|
---|
189 | files.add(f);
|
---|
190 | }
|
---|
191 | }
|
---|
192 | });
|
---|
193 | menu.add(tagimage);
|
---|
194 |
|
---|
195 | menu.addSeparator();
|
---|
196 | menu.add(new LayerListPopup.InfoAction(this));
|
---|
197 | }
|
---|
198 | }
|
---|