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