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