1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui.layer;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.awt.Color;
|
---|
8 | import java.awt.Component;
|
---|
9 | import java.awt.Graphics;
|
---|
10 | import java.awt.GridBagLayout;
|
---|
11 | import java.awt.Point;
|
---|
12 | import java.awt.event.ActionEvent;
|
---|
13 | import java.awt.event.ActionListener;
|
---|
14 | import java.io.File;
|
---|
15 | import java.util.Collection;
|
---|
16 | import java.util.LinkedList;
|
---|
17 |
|
---|
18 | import javax.swing.AbstractAction;
|
---|
19 | import javax.swing.Box;
|
---|
20 | import javax.swing.ButtonGroup;
|
---|
21 | import javax.swing.Icon;
|
---|
22 | import javax.swing.JColorChooser;
|
---|
23 | import javax.swing.JFileChooser;
|
---|
24 | import javax.swing.JLabel;
|
---|
25 | import javax.swing.JMenuItem;
|
---|
26 | import javax.swing.JOptionPane;
|
---|
27 | import javax.swing.JPanel;
|
---|
28 | import javax.swing.JRadioButton;
|
---|
29 | import javax.swing.JSeparator;
|
---|
30 | import javax.swing.filechooser.FileFilter;
|
---|
31 |
|
---|
32 | import org.openstreetmap.josm.Main;
|
---|
33 | import org.openstreetmap.josm.actions.GpxExportAction;
|
---|
34 | import org.openstreetmap.josm.actions.RenameLayerAction;
|
---|
35 | import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
|
---|
36 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
37 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
38 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
39 | import org.openstreetmap.josm.data.osm.Node;
|
---|
40 | import org.openstreetmap.josm.data.osm.Way;
|
---|
41 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
42 | import org.openstreetmap.josm.gui.MapView;
|
---|
43 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
|
---|
44 | import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
|
---|
45 | import org.openstreetmap.josm.tools.ColorHelper;
|
---|
46 | import org.openstreetmap.josm.tools.DontShowAgainInfo;
|
---|
47 | import org.openstreetmap.josm.tools.GBC;
|
---|
48 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
49 | import org.openstreetmap.josm.tools.UrlLabel;
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * A layer holding data from a gps source.
|
---|
53 | * The data is read only.
|
---|
54 | *
|
---|
55 | * @author imi
|
---|
56 | */
|
---|
57 | public class RawGpsLayer extends Layer implements PreferenceChangedListener {
|
---|
58 |
|
---|
59 | public class ConvertToDataLayerAction extends AbstractAction {
|
---|
60 | public ConvertToDataLayerAction() {
|
---|
61 | super(tr("Convert to data layer"), ImageProvider.get("converttoosm"));
|
---|
62 | }
|
---|
63 | public void actionPerformed(ActionEvent e) {
|
---|
64 | JPanel msg = new JPanel(new GridBagLayout());
|
---|
65 | msg.add(new JLabel(tr("<html>Upload of unprocessed GPS data as map data is considered harmful.<br>If you want to upload traces, look here:")), GBC.eol());
|
---|
66 | msg.add(new UrlLabel(tr("http://www.openstreetmap.org/traces")), GBC.eop());
|
---|
67 | if (!DontShowAgainInfo.show("convert_to_data", msg))
|
---|
68 | return;
|
---|
69 | DataSet ds = new DataSet();
|
---|
70 | for (Collection<GpsPoint> c : data) {
|
---|
71 | Way w = new Way();
|
---|
72 | for (GpsPoint p : c) {
|
---|
73 | Node n = new Node(p.latlon);
|
---|
74 | ds.nodes.add(n);
|
---|
75 | w.nodes.add(n);
|
---|
76 | }
|
---|
77 | ds.ways.add(w);
|
---|
78 | }
|
---|
79 | Main.main.addLayer(new OsmDataLayer(ds, tr("Converted from: {0}", RawGpsLayer.this.name), null));
|
---|
80 | Main.main.removeLayer(RawGpsLayer.this);
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | public static class GpsPoint {
|
---|
85 | public final LatLon latlon;
|
---|
86 | public final EastNorth eastNorth;
|
---|
87 | public final String time;
|
---|
88 | public GpsPoint(LatLon ll, String t) {
|
---|
89 | latlon = ll;
|
---|
90 | eastNorth = Main.proj.latlon2eastNorth(ll);
|
---|
91 | time = t;
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * A list of ways which containing a list of points.
|
---|
97 | */
|
---|
98 | public final Collection<Collection<GpsPoint>> data;
|
---|
99 | public final boolean fromServer;
|
---|
100 |
|
---|
101 | public RawGpsLayer(boolean fromServer, Collection<Collection<GpsPoint>> data, String name, File associatedFile) {
|
---|
102 | super(name);
|
---|
103 | this.fromServer = fromServer;
|
---|
104 | this.associatedFile = associatedFile;
|
---|
105 | this.data = data;
|
---|
106 | Main.pref.listener.add(this);
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Return a static icon.
|
---|
111 | */
|
---|
112 | @Override public Icon getIcon() {
|
---|
113 | return ImageProvider.get("layer", "rawgps_small");
|
---|
114 | }
|
---|
115 |
|
---|
116 | @Override public void paint(Graphics g, MapView mv) {
|
---|
117 | String gpsCol = Main.pref.get("color.gps point");
|
---|
118 | String gpsColSpecial = Main.pref.get("color.layer "+name);
|
---|
119 | if (!gpsColSpecial.equals(""))
|
---|
120 | g.setColor(ColorHelper.html2color(gpsColSpecial));
|
---|
121 | else if (!gpsCol.equals(""))
|
---|
122 | g.setColor(ColorHelper.html2color(gpsCol));
|
---|
123 | else
|
---|
124 | g.setColor(Color.GRAY);
|
---|
125 | Point old = null;
|
---|
126 |
|
---|
127 | boolean force = Main.pref.getBoolean("draw.rawgps.lines.force");
|
---|
128 | boolean lines = Main.pref.getBoolean("draw.rawgps.lines");
|
---|
129 | String linesKey = "draw.rawgps.lines.layer "+name;
|
---|
130 | if (Main.pref.hasKey(linesKey))
|
---|
131 | lines = Main.pref.getBoolean(linesKey);
|
---|
132 | boolean large = Main.pref.getBoolean("draw.rawgps.large");
|
---|
133 | for (Collection<GpsPoint> c : data) {
|
---|
134 | if (!force)
|
---|
135 | old = null;
|
---|
136 | for (GpsPoint p : c) {
|
---|
137 | Point screen = mv.getPoint(p.eastNorth);
|
---|
138 | if (lines && old != null)
|
---|
139 | g.drawLine(old.x, old.y, screen.x, screen.y);
|
---|
140 | else if (!large)
|
---|
141 | g.drawRect(screen.x, screen.y, 0, 0);
|
---|
142 | if (large)
|
---|
143 | g.fillRect(screen.x-1, screen.y-1, 3, 3);
|
---|
144 | old = screen;
|
---|
145 | }
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | @Override public String getToolTipText() {
|
---|
150 | int points = 0;
|
---|
151 | for (Collection<GpsPoint> c : data)
|
---|
152 | points += c.size();
|
---|
153 | String tool = data.size()+" "+trn("track", "tracks", data.size())
|
---|
154 | +" "+points+" "+trn("point", "points", points);
|
---|
155 | if (associatedFile != null)
|
---|
156 | tool = "<html>"+tool+"<br>"+associatedFile.getPath()+"</html>";
|
---|
157 | return tool;
|
---|
158 | }
|
---|
159 |
|
---|
160 | @Override public void mergeFrom(Layer from) {
|
---|
161 | RawGpsLayer layer = (RawGpsLayer)from;
|
---|
162 | data.addAll(layer.data);
|
---|
163 | }
|
---|
164 |
|
---|
165 | @Override public boolean isMergable(Layer other) {
|
---|
166 | return other instanceof RawGpsLayer;
|
---|
167 | }
|
---|
168 |
|
---|
169 | @Override public void visitBoundingBox(BoundingXYVisitor v) {
|
---|
170 | for (Collection<GpsPoint> c : data)
|
---|
171 | for (GpsPoint p : c)
|
---|
172 | v.visit(p.eastNorth);
|
---|
173 | }
|
---|
174 |
|
---|
175 | @Override public Object getInfoComponent() {
|
---|
176 | StringBuilder b = new StringBuilder();
|
---|
177 | int points = 0;
|
---|
178 | for (Collection<GpsPoint> c : data) {
|
---|
179 | b.append(" "+trn("a track with {0} point","a track with {0} points", c.size(), c.size())+"<br>");
|
---|
180 | points += c.size();
|
---|
181 | }
|
---|
182 | b.append("</html>");
|
---|
183 | return "<html>"+trn("{0} consists of {1} track", "{0} consists of {1} tracks", data.size(), name, data.size())+" ("+trn("{0} point", "{0} points", points, points)+")<br>"+b.toString();
|
---|
184 | }
|
---|
185 |
|
---|
186 | @Override public Component[] getMenuEntries() {
|
---|
187 | JMenuItem line = new JMenuItem(tr("Customize line drawing"), ImageProvider.get("mapmode/addsegment"));
|
---|
188 | line.addActionListener(new ActionListener(){
|
---|
189 | public void actionPerformed(ActionEvent e) {
|
---|
190 | JRadioButton[] r = new JRadioButton[3];
|
---|
191 | r[0] = new JRadioButton(tr("Use global settings."));
|
---|
192 | r[1] = new JRadioButton(tr("Draw lines between points for this layer."));
|
---|
193 | r[2] = new JRadioButton(tr("Do not draw lines between points for this layer."));
|
---|
194 | ButtonGroup group = new ButtonGroup();
|
---|
195 | Box panel = Box.createVerticalBox();
|
---|
196 | for (JRadioButton b : r) {
|
---|
197 | group.add(b);
|
---|
198 | panel.add(b);
|
---|
199 | }
|
---|
200 | String propName = "draw.rawgps.lines.layer "+name;
|
---|
201 | if (Main.pref.hasKey(propName))
|
---|
202 | group.setSelected(r[Main.pref.getBoolean(propName) ? 1:2].getModel(), true);
|
---|
203 | else
|
---|
204 | group.setSelected(r[0].getModel(), true);
|
---|
205 | int answer = JOptionPane.showConfirmDialog(Main.parent, panel, tr("Select line drawing options"), JOptionPane.OK_CANCEL_OPTION);
|
---|
206 | if (answer == JOptionPane.CANCEL_OPTION)
|
---|
207 | return;
|
---|
208 | if (group.getSelection() == r[0].getModel())
|
---|
209 | Main.pref.put(propName, null);
|
---|
210 | else
|
---|
211 | Main.pref.put(propName, group.getSelection() == r[1].getModel());
|
---|
212 | Main.map.repaint();
|
---|
213 | }
|
---|
214 | });
|
---|
215 |
|
---|
216 | JMenuItem color = new JMenuItem(tr("Customize Color"), ImageProvider.get("colorchooser"));
|
---|
217 | color.addActionListener(new ActionListener(){
|
---|
218 | public void actionPerformed(ActionEvent e) {
|
---|
219 | String col = Main.pref.get("color.layer "+name, Main.pref.get("color.gps point", ColorHelper.color2html(Color.gray)));
|
---|
220 | JColorChooser c = new JColorChooser(ColorHelper.html2color(col));
|
---|
221 | Object[] options = new Object[]{tr("OK"), tr("Cancel"), tr("Default")};
|
---|
222 | int answer = JOptionPane.showOptionDialog(Main.parent, c, tr("Choose a color"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
|
---|
223 | switch (answer) {
|
---|
224 | case 0:
|
---|
225 | Main.pref.put("color.layer "+name, ColorHelper.color2html(c.getColor()));
|
---|
226 | break;
|
---|
227 | case 1:
|
---|
228 | return;
|
---|
229 | case 2:
|
---|
230 | Main.pref.put("color.layer "+name, null);
|
---|
231 | break;
|
---|
232 | }
|
---|
233 | Main.map.repaint();
|
---|
234 | }
|
---|
235 | });
|
---|
236 |
|
---|
237 | JMenuItem tagimage = new JMenuItem(tr("Import images"), ImageProvider.get("tagimages"));
|
---|
238 | tagimage.addActionListener(new ActionListener(){
|
---|
239 | public void actionPerformed(ActionEvent e) {
|
---|
240 | JFileChooser fc = new JFileChooser(Main.pref.get("tagimages.lastdirectory"));
|
---|
241 | fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
|
---|
242 | fc.setMultiSelectionEnabled(true);
|
---|
243 | fc.setAcceptAllFileFilterUsed(false);
|
---|
244 | fc.setFileFilter(new FileFilter(){
|
---|
245 | @Override public boolean accept(File f) {
|
---|
246 | return f.isDirectory() || f.getName().toLowerCase().endsWith(".jpg");
|
---|
247 | }
|
---|
248 | @Override public String getDescription() {
|
---|
249 | return tr("JPEG images (*.jpg)");
|
---|
250 | }
|
---|
251 | });
|
---|
252 | fc.showOpenDialog(Main.parent);
|
---|
253 | File[] sel = fc.getSelectedFiles();
|
---|
254 | if (sel == null || sel.length == 0)
|
---|
255 | return;
|
---|
256 | LinkedList<File> files = new LinkedList<File>();
|
---|
257 | addRecursiveFiles(files, sel);
|
---|
258 | Main.pref.put("tagimages.lastdirectory", fc.getCurrentDirectory().getPath());
|
---|
259 | GeoImageLayer.create(files, RawGpsLayer.this);
|
---|
260 | }
|
---|
261 |
|
---|
262 | private void addRecursiveFiles(LinkedList<File> files, File[] sel) {
|
---|
263 | for (File f : sel) {
|
---|
264 | if (f.isDirectory())
|
---|
265 | addRecursiveFiles(files, f.listFiles());
|
---|
266 | else if (f.getName().toLowerCase().endsWith(".jpg"))
|
---|
267 | files.add(f);
|
---|
268 | }
|
---|
269 | }
|
---|
270 | });
|
---|
271 |
|
---|
272 | if (Main.applet)
|
---|
273 | return new Component[]{
|
---|
274 | new JMenuItem(new LayerListDialog.ShowHideLayerAction(this)),
|
---|
275 | new JMenuItem(new LayerListDialog.DeleteLayerAction(this)),
|
---|
276 | new JSeparator(),
|
---|
277 | color,
|
---|
278 | line,
|
---|
279 | new JMenuItem(new ConvertToDataLayerAction()),
|
---|
280 | new JSeparator(),
|
---|
281 | new JMenuItem(new RenameLayerAction(associatedFile, this)),
|
---|
282 | new JSeparator(),
|
---|
283 | new JMenuItem(new LayerListPopup.InfoAction(this))};
|
---|
284 | return new Component[]{
|
---|
285 | new JMenuItem(new LayerListDialog.ShowHideLayerAction(this)),
|
---|
286 | new JMenuItem(new LayerListDialog.DeleteLayerAction(this)),
|
---|
287 | new JSeparator(),
|
---|
288 | new JMenuItem(new GpxExportAction(this)),
|
---|
289 | color,
|
---|
290 | line,
|
---|
291 | tagimage,
|
---|
292 | new JMenuItem(new ConvertToDataLayerAction()),
|
---|
293 | new JSeparator(),
|
---|
294 | new JMenuItem(new RenameLayerAction(associatedFile, this)),
|
---|
295 | new JSeparator(),
|
---|
296 | new JMenuItem(new LayerListPopup.InfoAction(this))};
|
---|
297 | }
|
---|
298 |
|
---|
299 | public void preferenceChanged(String key, String newValue) {
|
---|
300 | if (Main.map != null && (key.equals("draw.rawgps.lines") || key.equals("draw.rawgps.lines.force")))
|
---|
301 | Main.map.repaint();
|
---|
302 | }
|
---|
303 |
|
---|
304 | @Override public void destroy() {
|
---|
305 | Main.pref.listener.remove(RawGpsLayer.this);
|
---|
306 | }
|
---|
307 | }
|
---|