source: osm/applications/editors/josm/plugins/livegps/src/livegps/LiveGpsLayer.java@ 23737

Last change on this file since 23737 was 23191, checked in by stoecker, 14 years ago

remove tabs

  • Property svn:eol-style set to native
File size: 5.6 KB
Line 
1package livegps;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import java.awt.Color;
6import java.awt.Graphics2D;
7import java.awt.Point;
8import java.beans.PropertyChangeEvent;
9import java.beans.PropertyChangeListener;
10import java.text.SimpleDateFormat;
11import java.util.Date;
12import java.util.HashMap;
13import java.util.Map;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.data.Bounds;
17import org.openstreetmap.josm.data.coor.LatLon;
18import org.openstreetmap.josm.data.gpx.GpxData;
19import org.openstreetmap.josm.data.gpx.GpxTrack;
20import org.openstreetmap.josm.data.gpx.WayPoint;
21import org.openstreetmap.josm.gui.MapView;
22import org.openstreetmap.josm.gui.layer.GpxLayer;
23
24public class LiveGpsLayer extends GpxLayer implements PropertyChangeListener {
25 public static final String LAYER_NAME = tr("LiveGPS layer");
26 public static final String KEY_LIVEGPS_COLOR = "color.livegps.position";
27 LatLon lastPos;
28 WayPoint lastPoint;
29 private final AppendableGpxTrackSegment trackSegment;
30 float speed;
31 float course;
32 // JLabel lbl;
33 boolean autocenter;
34 private SimpleDateFormat dateFormat = new SimpleDateFormat(
35 "yyyy-MM-dd'T'HH:mm:ss.SSS");
36
37 /**
38 * The suppressor is queried, if the GUI shall be re-drawn.
39 */
40 private ILiveGpsSuppressor suppressor;
41
42 public LiveGpsLayer(GpxData data) {
43 super(data, LAYER_NAME);
44 trackSegment = new AppendableGpxTrackSegment();
45
46 Map<String, Object> attr = new HashMap<String, Object>();
47 attr.put("desc", "josm live gps");
48
49 GpxTrack trackBeingWritten = new SingleSegmentGpxTrack(trackSegment, attr);
50 data.tracks.add(trackBeingWritten);
51 }
52
53 void setCurrentPosition(double lat, double lon) {
54 // System.out.println("adding pos " + lat + "," + lon);
55 LatLon thisPos = new LatLon(lat, lon);
56 if ((lastPos != null) && (thisPos.equalsEpsilon(lastPos))) {
57 // no change in position
58 // maybe show a "paused" cursor or some such
59 return;
60 }
61
62 lastPos = thisPos;
63 lastPoint = new WayPoint(thisPos);
64 lastPoint.attr.put("time", dateFormat.format(new Date()));
65 trackSegment.addWaypoint(lastPoint);
66 if (autocenter && allowRedraw()) {
67 center();
68 }
69
70 // Main.map.repaint();
71 }
72
73 public void center() {
74 if (lastPoint != null)
75 Main.map.mapView.zoomTo(lastPoint.getCoor());
76 }
77
78 // void setStatus(String status)
79 // {
80 // this.status = status;
81 // Main.map.repaint();
82 // System.out.println("LiveGps status: " + status);
83 // }
84
85 void setSpeed(float metresPerSecond) {
86 speed = metresPerSecond;
87 // Main.map.repaint();
88 }
89
90 void setCourse(float degrees) {
91 course = degrees;
92 // Main.map.repaint();
93 }
94
95 public void setAutoCenter(boolean ac) {
96 autocenter = ac;
97 }
98
99 @Override
100 public void paint(Graphics2D g, MapView mv, Bounds bounds) {
101 // System.out.println("in paint");
102 // System.out.println("in synced paint");
103 super.paint(g, mv, bounds);
104 // int statusHeight = 50;
105 // Rectangle mvs = mv.getBounds();
106 // mvs.y = mvs.y + mvs.height - statusHeight;
107 // mvs.height = statusHeight;
108 // g.setColor(new Color(1.0f, 1.0f, 1.0f, 0.8f));
109 // g.fillRect(mvs.x, mvs.y, mvs.width, mvs.height);
110
111 if (lastPoint != null) {
112 Point screen = mv.getPoint(lastPoint.getCoor());
113 g.setColor(Main.pref.getColor(KEY_LIVEGPS_COLOR, Color.RED));
114 g.drawOval(screen.x - 10, screen.y - 10, 20, 20);
115 g.drawOval(screen.x - 9, screen.y - 9, 18, 18);
116 }
117
118 // lbl.setText("gpsd: "+status+" Speed: " + speed +
119 // " Course: "+course);
120 // lbl.setBounds(0, 0, mvs.width-10, mvs.height-10);
121 // Graphics sub = g.create(mvs.x+5, mvs.y+5, mvs.width-10,
122 // mvs.height-10);
123 // lbl.paint(sub);
124
125 // if(status != null) {
126 // g.setColor(Color.WHITE);
127 // g.drawString("gpsd: " + status, 5, mv.getBounds().height - 15);
128 // // lower left corner
129 // }
130 }
131
132 /* (non-Javadoc)
133 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
134 */
135 public void propertyChange(PropertyChangeEvent evt) {
136 if (!isVisible()) {
137 return;
138 }
139 if ("gpsdata".equals(evt.getPropertyName())) {
140 LiveGpsData data = (LiveGpsData) evt.getNewValue();
141 if (data.isFix()) {
142 setCurrentPosition(data.getLatitude(), data.getLongitude());
143 if (!Float.isNaN(data.getSpeed())) {
144 setSpeed(data.getSpeed());
145 }
146 if (!Float.isNaN(data.getCourse())) {
147 setCourse(data.getCourse());
148 }
149 if (!autocenter && allowRedraw()) {
150 Main.map.repaint();
151 }
152 }
153 }
154 }
155
156 /**
157 * @param suppressor the suppressor to set
158 */
159 public void setSuppressor(ILiveGpsSuppressor suppressor) {
160 this.suppressor = suppressor;
161 }
162
163 /**
164 * @return the suppressor
165 */
166 public ILiveGpsSuppressor getSuppressor() {
167 return suppressor;
168 }
169
170 /**
171 * Check, if a redraw is currently allowed.
172 *
173 * @return true, if a redraw is permitted, false, if a re-draw
174 * should be suppressed.
175 */
176 private boolean allowRedraw() {
177 if (this.suppressor != null) {
178 return this.suppressor.isAllowUpdate();
179 } else {
180 return true;
181 }
182 }
183}
Note: See TracBrowser for help on using the repository browser.