Changeset 32638 in osm for applications/editors/josm/plugins/NanoLog/src/nanolog/NanoLogLayer.java
- Timestamp:
- 2016-07-11T23:01:43+02:00 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/NanoLog/src/nanolog/NanoLogLayer.java
r32329 r32638 57 57 private NLLMouseAdapter mouseListener; 58 58 59 public NanoLogLayer( List<NanoLogEntry> entries) {59 public NanoLogLayer(List<NanoLogEntry> entries) { 60 60 super(tr("NanoLog")); 61 61 log = new ArrayList<>(entries); … … 76 76 } 77 77 78 public NanoLogLayer( File file) throws IOException {78 public NanoLogLayer(File file) throws IOException { 79 79 this(readNanoLog(file)); 80 80 } 81 81 82 public void addListener( NanoLogLayerListener listener) {82 public void addListener(NanoLogLayerListener listener) { 83 83 listeners.add(listener); 84 84 } 85 85 86 public void removeListener( NanoLogLayerListener listener) {86 public void removeListener(NanoLogLayerListener listener) { 87 87 listeners.remove(listener); 88 88 } 89 89 90 90 protected void fireMarkersChanged() { 91 for ( NanoLogLayerListener listener : listeners )91 for (NanoLogLayerListener listener : listeners) { 92 92 listener.markersUpdated(this); 93 } 93 94 } 94 95 95 96 protected void fireMarkerSelected() { 96 for ( NanoLogLayerListener listener : listeners )97 for (NanoLogLayerListener listener : listeners) { 97 98 listener.markerActivated(this, selectedEntry < 0 ? null : log.get(selectedEntry)); 99 } 98 100 } 99 101 … … 102 104 } 103 105 104 public static List<NanoLogEntry> readNanoLog( File file) throws IOException {106 public static List<NanoLogEntry> readNanoLog(File file) throws IOException { 105 107 final Pattern NANOLOG_LINE = Pattern.compile("(.+?)\\t(.+?)(?:\\s*\\{\\{(-?\\d+\\.\\d+),\\s*(-?\\d+\\.\\d+)(?:,\\s*(\\d+))?\\}\\})?"); 106 108 final SimpleDateFormat fmt = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss.SS"); 107 109 List<NanoLogEntry> result = new ArrayList<>(); 108 110 try (BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF8"))) { 109 while( r.ready() ) { 110 String line = r.readLine(); 111 if( line != null ) { 112 Matcher m = NANOLOG_LINE.matcher(line); 113 if( m.matches() ) { 114 String time = m.group(1); 115 String message = m.group(2); 116 String lat = m.group(3); 117 String lon = m.group(4); 118 String dir = m.group(5); 119 Date timeDate = null; 120 try { 121 timeDate = fmt.parse(time); 122 } catch( ParseException e ) { 123 } 124 if( message == null || message.length() == 0 || timeDate == null ) 125 continue; 126 LatLon pos = null; 127 Integer direction = null; 128 if( lat != null && lon != null ) { 129 try { 130 pos = new LatLon(Double.parseDouble(lat), Double.parseDouble(lon)); 131 direction = new Integer(dir); 132 } catch( NumberFormatException e ) { 133 // well... 134 } 135 } 136 NanoLogEntry entry = new NanoLogEntry(timeDate, message, pos, direction); 137 result.add(entry); 138 } 139 } 140 } 111 while (r.ready()) { 112 String line = r.readLine(); 113 if (line != null) { 114 Matcher m = NANOLOG_LINE.matcher(line); 115 if (m.matches()) { 116 String time = m.group(1); 117 String message = m.group(2); 118 String lat = m.group(3); 119 String lon = m.group(4); 120 String dir = m.group(5); 121 Date timeDate = null; 122 try { 123 timeDate = fmt.parse(time); 124 } catch (ParseException e) { 125 Main.warn(e); 126 } 127 if (message == null || message.length() == 0 || timeDate == null) 128 continue; 129 LatLon pos = null; 130 Integer direction = null; 131 if (lat != null && lon != null) { 132 try { 133 pos = new LatLon(Double.parseDouble(lat), Double.parseDouble(lon)); 134 direction = new Integer(dir); 135 } catch (NumberFormatException e) { 136 Main.trace(e); 137 } 138 } 139 NanoLogEntry entry = new NanoLogEntry(timeDate, message, pos, direction); 140 result.add(entry); 141 } 142 } 143 } 141 144 } 142 145 return result; … … 144 147 145 148 @Override 146 public void paint( Graphics2D g, MapView mv, Bounds box) {149 public void paint(Graphics2D g, MapView mv, Bounds box) { 147 150 // todo 148 for ( int i = 0; i < log.size(); i++) {151 for (int i = 0; i < log.size(); i++) { 149 152 NanoLogEntry entry = log.get(i); 150 153 int radius = 4; 151 if ( entry.getPos() != null) {154 if (entry.getPos() != null) { 152 155 Point p = mv.getPoint(entry.getPos()); 153 156 g.setColor(selectedEntry == i ? Color.red : Color.yellow); … … 168 171 169 172 @Override 170 public void mergeFrom( Layer from) {173 public void mergeFrom(Layer from) { 171 174 // todo 172 175 } 173 176 174 177 @Override 175 public boolean isMergable( Layer other) {178 public boolean isMergable(Layer other) { 176 179 return other instanceof NanoLogLayer; 177 180 } 178 181 179 182 @Override 180 public void visitBoundingBox( BoundingXYVisitor v) {181 for ( NanoLogEntry entry : log )183 public void visitBoundingBox(BoundingXYVisitor v) { 184 for (NanoLogEntry entry : log) { 182 185 v.visit(entry.getPos()); 186 } 183 187 } 184 188 … … 187 191 StringBuilder b = new StringBuilder(); 188 192 int cnt = 0; 189 for ( NanoLogEntry e : log )190 if ( e.getPos() != null)193 for (NanoLogEntry e : log) { 194 if (e.getPos() != null) 191 195 cnt++; 196 } 192 197 b.append(tr("NanoLog of {0} lines, {1} of them with coordinates.", log.size(), cnt)); 193 198 return b.toString(); … … 212 217 public void jumpToNextMarker() { 213 218 selectedEntry++; 214 if ( selectedEntry < 0)219 if (selectedEntry < 0) 215 220 selectedEntry = 0; 216 else if ( selectedEntry >= log.size())221 else if (selectedEntry >= log.size()) 217 222 selectedEntry = log.size() - 1; 218 223 Main.map.repaint(); … … 222 227 public void jumpToPreviousMarker() { 223 228 selectedEntry--; 224 if ( selectedEntry < 0)229 if (selectedEntry < 0) 225 230 selectedEntry = 0; 226 else if ( selectedEntry >= log.size())231 else if (selectedEntry >= log.size()) 227 232 selectedEntry = log.size() - 1; 228 233 Main.map.repaint(); 229 234 } 230 235 231 protected void setSelected( int i) {236 protected void setSelected(int i) { 232 237 int newSelected = i >= 0 && i < log.size() ? i : -1; 233 if ( newSelected != selectedEntry) {238 if (newSelected != selectedEntry) { 234 239 // System.out.println("selected: " + log.get(newSelected).getMessage()); 235 240 selectedEntry = newSelected; … … 239 244 } 240 245 241 public void setSelected( NanoLogEntry entry) {242 if ( entry == null)246 public void setSelected(NanoLogEntry entry) { 247 if (entry == null) 243 248 setSelected(-1); 244 249 else { 245 for ( int i = 0; i < log.size(); i++) {246 if ( entry.equals(log.get(i))) {250 for (int i = 0; i < log.size(); i++) { 251 if (entry.equals(log.get(i))) { 247 252 setSelected(i); 248 253 break; … … 255 260 private int dragging; 256 261 257 public int nearestEntry( MouseEvent e) {262 public int nearestEntry(MouseEvent e) { 258 263 LatLon ll = Main.map.mapView.getLatLon(e.getX(), e.getY()); 259 264 int radius = 8; 260 if ( ll != null) {265 if (ll != null) { 261 266 LatLon lld = Main.map.mapView.getLatLon(e.getX() + radius, e.getY() + radius); 262 267 double distance = Math.max(lld.lat() - ll.lat(), lld.lon() - ll.lon()); 263 268 boolean selectedIsSelected = false; 264 269 int newSelected = -1; 265 for ( int i = 0; i < log.size(); i++) {266 if ( log.get(i).getPos() != null && log.get(i).getPos().distance(ll) < distance) {270 for (int i = 0; i < log.size(); i++) { 271 if (log.get(i).getPos() != null && log.get(i).getPos().distance(ll) < distance) { 267 272 newSelected = i; 268 if ( i == selectedEntry)273 if (i == selectedEntry) 269 274 selectedIsSelected = true; 270 275 } 271 276 } 272 if ( newSelected >= 0)277 if (newSelected >= 0) 273 278 return selectedIsSelected ? selectedEntry : newSelected; 274 279 } … … 277 282 278 283 @Override 279 public void mouseMoved( MouseEvent e) {284 public void mouseMoved(MouseEvent e) { 280 285 int nearest = nearestEntry(e); 281 if ( nearest > 0)286 if (nearest > 0) 282 287 setSelected(nearest); 283 288 } 284 289 285 290 @Override 286 public void mouseDragged( MouseEvent e) {291 public void mouseDragged(MouseEvent e) { 287 292 doDrag(e); 288 293 } 289 294 290 295 @Override 291 public void mouseReleased( MouseEvent e) {292 if ( dragging > 0) {296 public void mouseReleased(MouseEvent e) { 297 if (dragging > 0) { 293 298 dragging = 0; 294 299 } … … 296 301 297 302 @Override 298 public void mousePressed( MouseEvent e) {303 public void mousePressed(MouseEvent e) { 299 304 int nearest = nearestEntry(e); 300 if ( nearest > 0 && Main.getLayerManager().getActiveLayer() == NanoLogLayer.this) {305 if (nearest > 0 && Main.getLayerManager().getActiveLayer() == NanoLogLayer.this) { 301 306 dragging = nearest; 302 307 doDrag(e); … … 304 309 } 305 310 306 private void doDrag( MouseEvent e) {307 if ( dragging > 0)311 private void doDrag(MouseEvent e) { 312 if (dragging > 0) 308 313 dragTo(dragging, e.getX(), e.getY()); 309 314 } 310 315 } 311 316 312 protected void dragTo( int entry, int x, int y) {317 protected void dragTo(int entry, int x, int y) { 313 318 GpxLayer gpx = GPXChooser.topLayer(); 314 if ( gpx == null)319 if (gpx == null) 315 320 return; 316 321 EastNorth eastNorth = Main.map.mapView.getEastNorth(x, y); 317 322 double tolerance = eastNorth.distance(Main.map.mapView.getEastNorth(x + 300, y)); 318 323 WayPoint wp = gpx.data.nearestPointOnTrack(eastNorth, tolerance); 319 if ( wp == null)324 if (wp == null) 320 325 return; 321 326 long newTime = Correlator.getGpxDate(gpx.data, wp.getCoor()); 322 if ( newTime <= 0)327 if (newTime <= 0) 323 328 return; 324 329 Correlator.revertPos(log); … … 330 335 private boolean toZero; 331 336 332 public CorrelateEntries( boolean toZero ) { 333 super(toZero ? tr("Correlate with GPX...") : tr("Put on GPX..."), "nanolog/correlate", tr("Correlate entries with GPS trace"), null, false); 337 CorrelateEntries(boolean toZero) { 338 super(toZero ? tr("Correlate with GPX...") : tr("Put on GPX..."), "nanolog/correlate", 339 tr("Correlate entries with GPS trace"), null, false); 334 340 this.toZero = toZero; 335 341 } 336 342 337 343 @Override 338 public void actionPerformed( ActionEvent e) {344 public void actionPerformed(ActionEvent e) { 339 345 // 1. Select GPX trace or display message to load one 340 346 // (better yet, disable when no GPX traces) … … 342 348 // 2. Correlate by default, sticking by date 343 349 // (if does not match, shift so hours-minutes stay) 344 if ( layer != null) {350 if (layer != null) { 345 351 long offset = toZero ? 0 : Correlator.crudeMatch(log, layer.data); 346 352 Correlator.revertPos(log); … … 356 362 private class SaveLayer extends JosmAction { 357 363 358 publicSaveLayer() {364 SaveLayer() { 359 365 super(tr("Save layer..."), "nanolog/save", tr("Save NanoLog layer"), null, false); 360 366 } 361 367 362 368 @Override 363 public void actionPerformed( ActionEvent e) {369 public void actionPerformed(ActionEvent e) { 364 370 // todo 365 371 JOptionPane.showMessageDialog(Main.parent, "Sorry, no saving yet", "NanoLog", JOptionPane.ERROR_MESSAGE); … … 367 373 } 368 374 369 public static interface NanoLogLayerListener { 370 void markersUpdated( NanoLogLayer layer ); 371 void markerActivated( NanoLogLayer layer, NanoLogEntry entry ); 375 public interface NanoLogLayerListener { 376 void markersUpdated(NanoLogLayer layer); 377 378 void markerActivated(NanoLogLayer layer, NanoLogEntry entry); 372 379 } 373 380 }
Note:
See TracChangeset
for help on using the changeset viewer.