source: josm/trunk/src/org/openstreetmap/josm/gui/layer/RawGpsLayer.java@ 2381

Last change on this file since 2381 was 2381, checked in by jttt, 14 years ago

Change most occurrences of Dataset.nodes/ways/relations with getNodes()/../.. or addPrimitive

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