-
Notifications
You must be signed in to change notification settings - Fork 19
/
Map.java
6846 lines (5058 loc) · 211 KB
/
Map.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (C) 2005, 2021 Wolfgang Schramm and Contributors
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
*******************************************************************************/
/*
* 2007-04-29
* - completely removed DesignTime; this can be re-added later if needed
* - extends JXPanel -> extends Canvas
* - changed default constructor to receive standard SWT parameters parent and style
* - added getHeight() and getWidth() for those Swing calls
* - added isOpaque() for the same reason
* - added getInsets() for the same reason
* - renamed doPaintComponent to paintControl (SWT default); it now also receives a
* PaintEvent as parameter instead of a Graphics object; it's not private anymore,
* which is also a SWT convention
* - addPaintListener() for that method
* - ported paintControl() to SWT graphics operations
* - added computeSize()
* ! basically works
* however needs much work, no Listeners are implemented yet and there are some
* issues with thread access
* - thread access problem found and fixed; UI methods may only be called from the
* UI thread!
* 2007-04-30
* - fixed memory leaks; all images should now be disposed, when no longer needed
* - implemented Listeners except MouseWheel
*/
package de.byteholder.geoclipse.map;
import de.byteholder.geoclipse.Messages;
import de.byteholder.geoclipse.map.event.IHoveredTourListener;
import de.byteholder.geoclipse.map.event.IMapGridListener;
import de.byteholder.geoclipse.map.event.IMapInfoListener;
import de.byteholder.geoclipse.map.event.IMapPositionListener;
import de.byteholder.geoclipse.map.event.IPOIListener;
import de.byteholder.geoclipse.map.event.IPositionListener;
import de.byteholder.geoclipse.map.event.ITourSelectionListener;
import de.byteholder.geoclipse.map.event.MapHoveredTourEvent;
import de.byteholder.geoclipse.map.event.MapPOIEvent;
import de.byteholder.geoclipse.map.event.MapPositionEvent;
import de.byteholder.geoclipse.mapprovider.ImageDataResources;
import de.byteholder.geoclipse.mapprovider.MP;
import de.byteholder.geoclipse.mapprovider.MapProviderManager;
import de.byteholder.geoclipse.preferences.IMappingPreferences;
import de.byteholder.geoclipse.ui.TextWrapPainter;
import java.awt.Dimension;
import java.awt.geom.Point2D;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.tourbook.Images;
import net.tourbook.application.TourbookPlugin;
import net.tourbook.common.UI;
import net.tourbook.common.color.ColorCacheSWT;
import net.tourbook.common.color.ColorUtil;
import net.tourbook.common.color.ThemeUtil;
import net.tourbook.common.formatter.FormatManager;
import net.tourbook.common.map.GeoPosition;
import net.tourbook.common.util.HoveredAreaContext;
import net.tourbook.common.util.IToolTipProvider;
import net.tourbook.common.util.ITourToolTipProvider;
import net.tourbook.common.util.MtMath;
import net.tourbook.common.util.StatusUtil;
import net.tourbook.common.util.TourToolTip;
import net.tourbook.common.util.Util;
import net.tourbook.data.TourData;
import net.tourbook.data.TourWayPoint;
import net.tourbook.map2.view.Map2View;
import net.tourbook.map2.view.WayPointToolTipProvider;
import net.tourbook.preferences.ITourbookPreferences;
import net.tourbook.tour.SelectionTourId;
import net.tourbook.tour.SelectionTourIds;
import net.tourbook.tour.TourManager;
import net.tourbook.tour.filter.geo.TourGeoFilter;
import net.tourbook.tour.filter.geo.TourGeoFilter_Manager;
import net.tourbook.ui.IInfoToolTipProvider;
import net.tourbook.ui.IMapToolTipProvider;
import net.tourbook.ui.MTRectangle;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.TransferData;
import org.eclipse.swt.dnd.URLTransfer;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Path;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Resource;
import org.eclipse.swt.graphics.Transform;
import org.eclipse.swt.internal.DPIUtil;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
public class Map extends Canvas {
private static final String TOUR_TOOLTIP_LABEL_DISTANCE = net.tourbook.ui.Messages.Tour_Tooltip_Label_Distance;
private static final String TOUR_TOOLTIP_LABEL_MOVING_TIME = net.tourbook.ui.Messages.Tour_Tooltip_Label_MovingTime;
private static final String TOUR_TOOLTIP_LABEL_RECORDED_TIME = net.tourbook.ui.Messages.Tour_Tooltip_Label_RecordedTime;
private static final IDialogSettings _geoFilterState = TourGeoFilter_Manager.getState();
/**
* Min zoomlevels which the maps supports
*/
public static final int MAP_MIN_ZOOM_LEVEL = 0;
/**
* Max zoomlevels which the maps supports
*/
public static final int MAP_MAX_ZOOM_LEVEL = 22;
/**
* these zoom levels are displayed in the UI therefore they start with 1 instead of 0
*/
public static final int UI_MIN_ZOOM_LEVEL = MAP_MIN_ZOOM_LEVEL + 1;
public static final int UI_MAX_ZOOM_LEVEL = MAP_MAX_ZOOM_LEVEL + 1;
public static final int EXPANDED_HOVER_SIZE = 20;
public static final int EXPANDED_HOVER_SIZE2 = EXPANDED_HOVER_SIZE / 2;
private static final String DIRECTION_E = "E"; //$NON-NLS-1$
private static final String DIRECTION_N = "N"; //$NON-NLS-1$
private static final String VALUE_FORMAT_2 = "%s %s"; //$NON-NLS-1$
private static final String VALUE_FORMAT_3 = "%s %s %s"; //$NON-NLS-1$
/*
* Wikipedia data
*/
// private static final String WIKI_PARAMETER_DIM = "dim"; //$NON-NLS-1$
private static final String WIKI_PARAMETER_TYPE = "type"; //$NON-NLS-1$
// http://toolserver.org/~geohack/geohack.php?pagename=Sydney&language=de¶ms=33.85_S_151.2_E_region:AU-NSW_type:city(3641422)
// http://toolserver.org/~geohack/geohack.php?pagename=Palm_Island,_Queensland¶ms=18_44_S_146_35_E_scale:20000_type:city
// http://toolserver.org/~geohack/geohack.php?pagename=P%C3%B3voa_de_Varzim¶ms=41_22_57_N_8_46_45_W_region:PT_type:city//
//
// where D is degrees, M is minutes, S is seconds, and NS/EWO are the directions
//
// D;D
// D_N_D_E
// D_M_N_D_M_E
// D_M_S_N_D_M_S_E
private static final String PATTERN_SEPARATOR = "_"; //$NON-NLS-1$
private static final String PATTERN_END = "_?(.*)"; //$NON-NLS-1$
private static final String PATTERN_WIKI_URL = ".*pagename=([^&]*).*params=(.*)"; //$NON-NLS-1$
private static final String PATTERN_WIKI_PARAMETER_KEY_VALUE_SEPARATOR = ":"; //$NON-NLS-1$
private static final String PATTERN_DOUBLE = "([-+]?[0-9]*\\.?[0-9]+)"; //$NON-NLS-1$
private static final String PATTERN_DOUBLE_SEP = PATTERN_DOUBLE + PATTERN_SEPARATOR;
private static final String PATTERN_DIRECTION_NS = "([NS])_"; //$NON-NLS-1$
private static final String PATTERN_DIRECTION_WE = "([WE])"; //$NON-NLS-1$
// private static final String PATTERN_WIKI_POSITION_10 = "([-+]?[0-9]*\\.?[0-9]+)_([NS])_([-+]?[0-9]*\\.?[0-9]+)_([WE])_?(.*)"; //$NON-NLS-1$
// private static final String PATTERN_WIKI_POSITION_20 = "([0-9]*)_([NS])_([0-9]*)_([WE])_?(.*)"; //$NON-NLS-1$
// private static final String PATTERN_WIKI_POSITION_21 = "([0-9]*)_([0-9]*)_([NS])_([0-9]*)_([0-9]*)_([WE])_?(.*)"; //$NON-NLS-1$
// private static final String PATTERN_WIKI_POSITION_22 = "([0-9]*)_([0-9]*)_([0-9]*)_([NS])_([0-9]*)_([0-9]*)_([0-9]*)_([WE])_?(.*)"; //$NON-NLS-1$
private static final String PATTERN_WIKI_POSITION_D_D = PATTERN_DOUBLE + ";" //$NON-NLS-1$
+ PATTERN_DOUBLE
+ PATTERN_END;
private static final String PATTERN_WIKI_POSITION_D_N_D_E = PATTERN_DOUBLE_SEP
+ PATTERN_DIRECTION_NS
+ PATTERN_DOUBLE_SEP
+ PATTERN_DIRECTION_WE
+ PATTERN_END;
private static final String PATTERN_WIKI_POSITION_D_M_N_D_M_E = PATTERN_DOUBLE_SEP
+ PATTERN_DOUBLE_SEP
+ PATTERN_DIRECTION_NS
+ PATTERN_DOUBLE_SEP
+ PATTERN_DOUBLE_SEP
+ PATTERN_DIRECTION_WE
+ PATTERN_END;
private static final String PATTERN_WIKI_POSITION_D_M_S_N_D_M_S_E = PATTERN_DOUBLE_SEP
+ PATTERN_DOUBLE_SEP
+ PATTERN_DOUBLE_SEP
+ PATTERN_DIRECTION_NS
+ PATTERN_DOUBLE_SEP
+ PATTERN_DOUBLE_SEP
+ PATTERN_DOUBLE_SEP
+ PATTERN_DIRECTION_WE
+ PATTERN_END;
private static final Pattern _patternWikiUrl = Pattern.compile(PATTERN_WIKI_URL);
private static final Pattern _patternWikiPosition_D_D = Pattern.compile(PATTERN_WIKI_POSITION_D_D);
private static final Pattern _patternWikiPosition_D_N_D_E = Pattern.compile(PATTERN_WIKI_POSITION_D_N_D_E);
private static final Pattern _patternWikiPosition_D_M_N_D_M_E = Pattern.compile(PATTERN_WIKI_POSITION_D_M_N_D_M_E);
private static final Pattern _patternWikiPosition_D_M_S_N_D_M_S_E = Pattern.compile(PATTERN_WIKI_POSITION_D_M_S_N_D_M_S_E);
private static final Pattern _patternWikiParamter = Pattern.compile(PATTERN_SEPARATOR);
private static final Pattern _patternWikiKeyValue = Pattern.compile(PATTERN_WIKI_PARAMETER_KEY_VALUE_SEPARATOR);
private static final ColorCacheSWT _colorCache = new ColorCacheSWT();
// [181,208,208] is the color of water in the standard OSM material
public static final RGB OSM_BACKGROUND_RGB = new RGB(181, 208, 208);
private static final RGB MAP_DEFAULT_BACKGROUND_RGB = new RGB(0x40, 0x40, 0x40);
private static RGB MAP_TRANSPARENT_RGB;
private final IPreferenceStore _prefStore = TourbookPlugin.getPrefStore();
{
MAP_TRANSPARENT_RGB = net.tourbook.common.UI.IS_OSX //
// ? new RGB(0x7e, 0x7f, 0x80)
// ? new RGB(0xfe, 0x00, 0x00)
? new RGB(0xfe, 0xfe, 0xfe)
: new RGB(0xfe, 0xfe, 0xfe)
//
;
}
private Color SYS_COLOR_BLACK;
private Color SYS_COLOR_DARK_GRAY;
private Color SYS_COLOR_GRAY;
private Color SYS_COLOR_WHITE;
private Color SYS_COLOR_YELLOW;
/**
* Map zoom level which is currently be used to display tiles. Normally a value between around 0
* and 20.
*/
private int _mapZoomLevel;
private boolean _isZoomWithMousePosition;
/**
* This image contains the map which is painted in the map viewport
*/
private Image _mapImage;
private Image _9PartImage;
private GC _9PartGC;
/**
* Indicates whether or not to draw the borders between tiles. Defaults to false. not very nice
* looking, very much a product of testing Consider whether this should really be a property or
* not.
*/
private boolean _isShowDebug_TileInfo;
private boolean _isShowDebug_TileBorder;
private boolean _isShowDebug_GeoGrid;
/**
* Factory used by this component to grab the tiles necessary for painting the map.
*/
private MP _mp;
/**
* The position in latitude/longitude of the "address" being mapped. This is a special coordinate
* that, when moved, will cause the map to be moved as well. It is separate from "center" in that
* "center" tracks the current center (in pixels) of the view port whereas this will not change
* when panning or zooming. Whenever the addressLocation is changed, however, the map will be
* repositioned.
*/
private GeoPosition _addressLocation;
/**
* The overlay to delegate to for painting the "foreground" of the map component. This would
* include painting waypoints, day/night, etc. Also receives mouse events.
*/
private final List<MapPainter> _overlays = new ArrayList<>();
private final TileLoadObserver _tileImageLoadObserver = new TileLoadObserver();
private final Cursor _cursorCross;
private final Cursor _cursorDefault;
private final Cursor _cursorHand;
private final Cursor _cursorPan;
private final Cursor _cursorSearchTour;
private final Cursor _cursorSearchTour_Scroll;
private final AtomicInteger _redrawMapCounter = new AtomicInteger();
private final AtomicInteger _overlayRunnableCounter = new AtomicInteger();
private boolean _isLeftMouseButtonPressed;
private boolean _isMapPanned;
private Point _mouseDownPosition;
private int _mouseMove_DevPosition_X = Integer.MIN_VALUE;
private int _mouseMove_DevPosition_Y = Integer.MIN_VALUE;
private int _mouseMove_DevPosition_X_Last;
private int _mouseMove_DevPosition_Y_Last;
private GeoPosition _mouseMove_GeoPosition;
private Thread _overlayThread;
private long _nextOverlayRedrawTime;
private final NumberFormat _nf1;
private final NumberFormat _nf2;
private final NumberFormat _nf3;
private final NumberFormat _nfLatLon;
{
_nf1 = NumberFormat.getNumberInstance();
_nf2 = NumberFormat.getNumberInstance();
_nf3 = NumberFormat.getNumberInstance();
_nf1.setMinimumFractionDigits(1);
_nf1.setMaximumFractionDigits(1);
_nf2.setMinimumFractionDigits(2);
_nf2.setMaximumFractionDigits(2);
_nf3.setMinimumFractionDigits(3);
_nf3.setMaximumFractionDigits(3);
_nfLatLon = NumberFormat.getNumberInstance();
_nfLatLon.setMinimumFractionDigits(4);
_nfLatLon.setMaximumFractionDigits(4);
}
private final TextWrapPainter _textWrapper = new TextWrapPainter();
/**
* cache for overlay images
*/
private OverlayImageCache _overlayImageCache;
/**
* This queue contains tiles which overlay image must be painted
*/
private final ConcurrentLinkedQueue<Tile> _tileOverlayPaintQueue = new ConcurrentLinkedQueue<>();
private boolean _isRunningDrawOverlay;
private String _overlayKey;
/**
* This painter is called when the map is painted in the onPaint event
*/
private IDirectPainter _directMapPainter;
private final DirectPainterContext _directMapPainterContext = new DirectPainterContext();
/**
* When <code>true</code> the overlays are painted
*/
private boolean _isDrawOverlays;
/**
* Contains a legend which is painted in the map
*/
private MapLegend _mapLegend;
private boolean _isLegendVisible;
/**
* This is the most important point for the map because all operations depend on it.
* <p>
* Center position of the map viewport in <I>world pixel</I>. Dragging the map component will
* change the center position. Zooming in/out will cause the center to be recalculated so as to
* remain in the center of the new "map".
* <p>
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* <br>
* This MUST be in {@link Double} to be accurate when the map is zoomed<br>
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* <br>
*/
private Point2D _worldPixel_MapCenter = null;
/**
* Viewport in the map where the {@link #_mapImage} is painted <br>
* <br>
* <b>x</b> and <b>y</b> is the <b>top/left</b> corner in world pixel<br>
* <b>width</b> and <b>height</b> contains the visible area in device pixel
* <p>
* <b>!!! x/y values can also be negative when the map is smaller than the viewport !!!</b>
* <p>
* I havn't yet fully understood how it works but I adjusted the map successfully in 10.7 and
* tried to document this behaviour.
*/
private Rectangle _worldPixel_TopLeft_Viewport;
/**
* Size in device pixel where the map is displayed
*/
private Rectangle _devMapViewport;
/**
* Size of the map in tiles at the current zoom level {@link #_mapZoomLevel} (num tiles tall by
* num tiles wide)
*/
private Dimension _mapTileSize;
/**
* Size of a tile in pixel (tile is quadratic)
*/
private int _tilePixelSize;
/**
* Size of a geo grid of 0.01 degree in pixel, this depends on the zoom level
*/
private double _devGridPixelSize_X;
private double _devGridPixelSize_Y;
/**
* Contains the client area of the map without trimmings, this rectangle has the width and height
* of the map image
*/
private Rectangle _clientArea;
private final ListenerList<IMapGridListener> _allMapGridListener = new ListenerList<>(ListenerList.IDENTITY);
private final ListenerList<IMapInfoListener> _allMapInfoListener = new ListenerList<>(ListenerList.IDENTITY);
private final ListenerList<IMapPositionListener> _allMapPositionListener = new ListenerList<>(ListenerList.IDENTITY);
private final ListenerList<ITourSelectionListener> _allTourSelectionListener = new ListenerList<>(ListenerList.IDENTITY);
private final ListenerList<IPositionListener> _mousePositionListeners = new ListenerList<>(ListenerList.IDENTITY);
private final ListenerList<IPOIListener> _poiListeners = new ListenerList<>(ListenerList.IDENTITY);
private final ListenerList<IHoveredTourListener> _hoveredTourListeners = new ListenerList<>(ListenerList.IDENTITY);
// measurement system
private float _distanceUnitValue = 1;
private String _distanceUnitLabel = UI.EMPTY_STRING;
private boolean _isScaleVisible;
private final Color _transparentColor;
private Color _defaultBackgroundColor;
/*
* POI image
*/
private boolean _isPoiVisible;
private boolean _isPoiPositionInViewport;
//
private final Image _poiImage;
private final Rectangle _poiImageBounds;
private final Point _poiImageDevPosition = new Point(0, 0);
/*
* POI tooltip
*/
private PoiToolTip _poi_Tooltip;
private final int _poi_Tooltip_OffsetY = 5;
private TourToolTip _tour_ToolTip;
/**
* Hovered/selected tour
*/
private boolean _isShowHoveredSelectedTour = Map2View.STATE_IS_SHOW_HOVERED_SELECTED_TOUR_DEFAULT;
private long _hovered_SelectedTourId = Long.MIN_VALUE;
private ArrayList<Long> _allHoveredTourIds = new ArrayList<>();
private ArrayList<Point> _allDevHoveredPoints = new ArrayList<>();
private int _hoveredSelectedTour_Hovered_Opacity;
private Color _hoveredSelectedTour_Hovered_Color;
private int _hoveredSelectedTour_HoveredAndSelected_Opacity;
private Color _hoveredSelectedTour_HoveredAndSelected_Color;
private int _hoveredSelectedTour_Selected_Opacity;
private Color _hoveredSelectedTour_Selected_Color;
/**
* When <code>true</code> the loading... image is not displayed
*/
private boolean _isLiveView;
private long _lastMapDrawTime;
/*
* All painted tiles in the map are within these 4 tile positions
*/
private int _tilePos_MinX;
private int _tilePos_MaxX;
private int _tilePos_MinY;
private int _tilePos_MaxY;
//
private Tile[][] _allPaintedTiles;
//
private final Display _display;
private final Thread _displayThread;
//
private int _jobCounterSplitImages = 0;
private Object _splitJobFamily = new Object();
private boolean _isCancelSplitJobs;
/*
* Download offline images
*/
private boolean _offline_IsSelectingOfflineArea;
private boolean _offline_IsOfflineSelectionStarted;
private boolean _offline_IsPaintOfflineArea;
private Point _offline_DevMouse_Start;
private Point _offline_DevMouse_End;
private Point _offline_DevTileStart;
private Point _offline_DevTileEnd;
private Point _offline_WorldMouse_Start;
private Point _offline_WorldMouse_End;
private Point _offline_WorldMouse_Move;
private IMapContextProvider _mapContextProvider;
/**
* Is <code>true</code> when the map context menu can be displayed
*/
private boolean _isContextMenuEnabled = true;
private DropTarget _dropTarget;
private boolean _isRedrawEnabled = true;
private HoveredAreaContext _hoveredAreaContext;
private int _overlayAlpha = 0xff;
private MapGridData _grid_Data_Hovered;
private MapGridData _grid_Data_Selected;
private boolean _grid_Label_IsHovered;
private Rectangle _grid_Label_Outline;
private GeoPosition _grid_MapGeoCenter;
private int _grid_MapZoomLevel;
private int[] _grid_AutoScrollCounter = new int[1];
private boolean _grid_IsGridAutoScroll;
private ActionManageOfflineImages _actionManageOfflineImages;
/**
* When <code>true</code> the tour is painted in the map in the enhanced mode otherwise in the
* simple mode
*/
private boolean _isTourPaintMethodEnhanced;
private boolean _isShowTourPaintMethodEnhancedWarning;
private boolean _isMapBackgroundDark;
private boolean _isFastMapPainting;
private boolean _isFastMapPainting_Active;
private boolean _isInInverseKeyboardPanning;
/*
* Direction arrows
*/
private boolean _isDrawTourDirection;
private int _tourDirection_MarkerGap;
private int _tourDirection_LineWidth;
private RGB _tourDirection_RGB;
private float _tourDirection_SymbolSize;
private int _fastMapPainting_skippedValues;
private MapTourBreadcrumb _tourBreadcrumb;
private Font _boldFont = JFaceResources.getFontRegistry().getBold(JFaceResources.DIALOG_FONT);
/**
* This observer is called in the {@link Tile} when a tile image is set into the tile
*/
private final class TileLoadObserver implements Observer {
@Override
public void update(final Observable observable, final Object arg) {
if (observable instanceof Tile) {
final Tile tile = (Tile) observable;
if (tile.getZoom() == _mapZoomLevel) {
/*
* Because we are not in the UI thread, we have to queue the call for redraw and
* cannot do it directly.
*/
paint();
tile.deleteObserver(this);
}
}
}
}
/**
* Create a new Map
*
* @param state
*/
public Map(final Composite parent, final int style, final IDialogSettings state) {
super(parent, style | SWT.DOUBLE_BUFFERED);
_display = getDisplay();
_displayThread = _display.getThread();
addAllListener();
addDropTarget();
createActions();
createContextMenu();
updateGraphColors();
grid_UpdatePaintingStateData();
_cursorCross = new Cursor(_display, SWT.CURSOR_CROSS);
_cursorDefault = new Cursor(_display, SWT.CURSOR_ARROW);
_cursorHand = new Cursor(_display, SWT.CURSOR_HAND);
_cursorPan = new Cursor(_display, SWT.CURSOR_SIZEALL);
_cursorSearchTour = UI.createCursorFromImage(TourbookPlugin.getImageDescriptor(Images.SearchTours_ByLocation));
_cursorSearchTour_Scroll = UI.createCursorFromImage(TourbookPlugin.getImageDescriptor(Images.SearchTours_ByLocation_Scroll));
_transparentColor = new Color(MAP_TRANSPARENT_RGB);
SYS_COLOR_BLACK = _display.getSystemColor(SWT.COLOR_BLACK);
SYS_COLOR_DARK_GRAY = _display.getSystemColor(SWT.COLOR_DARK_GRAY);
SYS_COLOR_GRAY = _display.getSystemColor(SWT.COLOR_GRAY);
SYS_COLOR_WHITE = _display.getSystemColor(SWT.COLOR_WHITE);
SYS_COLOR_YELLOW = _display.getSystemColor(SWT.COLOR_YELLOW);
_poiImage = TourbookPlugin.getImageDescriptor(Images.POI_InMap).createImage();
_poiImageBounds = _poiImage.getBounds();
_tourBreadcrumb = new MapTourBreadcrumb(this);
paint_Overlay_0_SetupThread();
parent.getDisplay().asyncExec(() -> {
// must be run async because dark theme colors could not yet be initialized
_defaultBackgroundColor = UI.IS_DARK_THEME
? ThemeUtil.getDarkestBackgroundColor()
: new Color(MAP_DEFAULT_BACKGROUND_RGB);
});
}
/**
* @return Returns rgb values for the color which is used as transparent color in the map.
*/
public static RGB getTransparentRGB() {
return MAP_TRANSPARENT_RGB;
}
public void actionManageOfflineImages(final Event event) {
// check if offline image is active
final IPreferenceStore prefStore = TourbookPlugin.getDefault().getPreferenceStore();
if (prefStore.getBoolean(IMappingPreferences.OFFLINE_CACHE_USE_OFFLINE) == false) {
MessageDialog.openInformation(
_display.getActiveShell(),
Messages.Dialog_OfflineArea_Error,
Messages.Dialog_OfflineArea_Error_NoOffline);
return;
}
// check if offline loading is running
if (OfflineLoadManager.isLoading()) {
MessageDialog.openInformation(
_display.getActiveShell(),
Messages.Dialog_OfflineArea_Error,
Messages.Dialog_OfflineArea_Error_IsLoading);
return;
}
_offline_IsPaintOfflineArea = true;
_offline_IsSelectingOfflineArea = true;
_offline_DevMouse_Start = null;
_offline_DevMouse_End = null;
setCursor(_cursorCross);
redraw();
paint();
}
public void actionSearchTourByLocation(final Event event) {
_grid_Data_Hovered = new MapGridData();
// auto open geo filter slideout
final boolean isAutoOpenSlideout = Util.getStateBoolean(TourGeoFilter_Manager.getState(),
TourGeoFilter_Manager.STATE_IS_AUTO_OPEN_SLIDEOUT,
TourGeoFilter_Manager.STATE_IS_AUTO_OPEN_SLIDEOUT_DEFAULT);
if (isAutoOpenSlideout) {
TourGeoFilter_Manager.setGeoFilter_OpenSlideout(true, false);
}
grid_UpdatePaintingStateData();
_isFastMapPainting_Active = true;
final Point worldMousePosition = new Point(
_worldPixel_TopLeft_Viewport.x + _mouseMove_DevPosition_X,
_worldPixel_TopLeft_Viewport.y + _mouseMove_DevPosition_Y);
_grid_Data_Hovered.geo_MouseMove = _mp.pixelToGeo(
new Point2D.Double(worldMousePosition.x, worldMousePosition.y),
_mapZoomLevel);
setCursor(_cursorSearchTour);
redraw();
paint();
}
private void addAllListener() {
addPaintListener(this::onPaint);
addDisposeListener(this::onDispose);
addFocusListener(new FocusListener() {
@Override
public void focusGained(final FocusEvent e) {
updatePoiVisibility();
}
@Override
public void focusLost(final FocusEvent e) {
// this is critical because the tool tip get's hidden when there are actions available in the tool tip shell
// hidePoiToolTip();
}
});
addMouseListener(new MouseListener() {
@Override
public void mouseDoubleClick(final MouseEvent event) {
onMouse_DoubleClick(event);
}
@Override
public void mouseDown(final MouseEvent event) {
onMouse_Down(event);
}
@Override
public void mouseUp(final MouseEvent event) {
onMouse_Up(event);
}
});
addMouseTrackListener(new MouseTrackListener() {
@Override
public void mouseEnter(final MouseEvent e) {}
@Override
public void mouseExit(final MouseEvent e) {
onMouse_Exit();
}
@Override
public void mouseHover(final MouseEvent e) {}
});
addMouseMoveListener(this::onMouse_Move);
addListener(SWT.MouseVerticalWheel, this::onMouse_Wheel);
addListener(SWT.MouseHorizontalWheel, this::onMouse_Wheel);
addListener(SWT.KeyDown, this::onKey_Down);
addControlListener(new ControlAdapter() {
@Override
public void controlResized(final ControlEvent e) {
onResize();
}
});
// enable traverse keys
addTraverseListener(traverseEvent -> traverseEvent.doit = true);
}
/**
* Set map as drop target
*/
private void addDropTarget() {
_dropTarget = new DropTarget(this, DND.DROP_MOVE | DND.DROP_COPY);
_dropTarget.setTransfer(URLTransfer.getInstance(), TextTransfer.getInstance());
_dropTarget.addDropListener(new DropTargetAdapter() {
@Override
public void dragEnter(final DropTargetEvent event) {
if ((event.detail == DND.DROP_DEFAULT) || (event.detail == DND.DROP_MOVE)) {
event.detail = DND.DROP_COPY;
}
}
@Override
public void dragLeave(final DropTargetEvent event) {
}
@Override
public void dragOver(final DropTargetEvent event) {
if ((event.detail == DND.DROP_DEFAULT) || (event.detail == DND.DROP_MOVE)) {
event.detail = DND.DROP_COPY;
}
}
@Override
public void drop(final DropTargetEvent event) {
if (event.data == null) {
event.detail = DND.DROP_NONE;
return;
}
/*
* run async to free the mouse cursor from the drop operation
*/
_display.asyncExec(() -> onDropRunnable(event));
}
});
}
public void addHoveredTourListener(final IHoveredTourListener hoveredTourListener) {
_hoveredTourListeners.add(hoveredTourListener);
}
public void addMapGridBoxListener(final IMapGridListener mapListener) {
_allMapGridListener.add(mapListener);
}
public void addMapInfoListener(final IMapInfoListener mapInfoListener) {
_allMapInfoListener.add(mapInfoListener);
}
public void addMapPositionListener(final IMapPositionListener mapPanListener) {
_allMapPositionListener.add(mapPanListener);
}
public void addMousePositionListener(final IPositionListener mapListener) {
_mousePositionListeners.add(mapListener);
}
/**
* Adds a map overlay. This is a Painter which will paint on top of the map. It can be used to
* draw waypoints, lines, or static overlays like text messages.
*
* @param overlay
* the map overlay to use
* @see org.jdesktop.swingx.painters.Painter
*/
public void addOverlayPainter(final MapPainter overlay) {
_overlays.add(overlay);
paint();
}
public void addPOIListener(final IPOIListener poiListener) {
_poiListeners.add(poiListener);
}
public void addTourSelectionListener(final ITourSelectionListener iTourSelectionListener) {
_allTourSelectionListener.add(iTourSelectionListener);
}
/**
* Checks if an image can be reused, this is true if the image exists and has the same size
*
* @param newWidth
* @param newHeight
* @return
*/
private boolean canReuseImage(final Image image, final Rectangle clientArea) {
// check if we could reuse the existing image
if ((image == null) || image.isDisposed()) {
return false;
}
// image exist, check image bounds
final Rectangle oldBounds = image.getBounds();
if (!((oldBounds.width == clientArea.width) && (oldBounds.height == clientArea.height))) {
return false;
}
return true;
}
/**
* make sure that the parted overlay image has the correct size
*/
private void checkImageTemplate9Parts() {
final int parts = 3;
final int partedTileSize = _tilePixelSize * parts;
if ((_9PartImage != null) && (_9PartImage.isDisposed() == false)) {
if (_9PartImage.getBounds().width == partedTileSize) {
// image is OK
return;
}
}
if (_9PartImage != null) {
_9PartImage.dispose();
}
if (_9PartGC != null) {
_9PartGC.dispose();
}
// create 9 part image/gc
final ImageData transparentImageData = de.byteholder.geoclipse.map.UI.createTransparentImageData(
partedTileSize);
_9PartImage = new Image(_display, transparentImageData);
_9PartGC = new GC(_9PartImage);
}
/**
* Checks validation of a world pixel by using the current zoom level and map tile size.
*
* @param newWorldPixelCenter
* @return Returns adjusted world pixel when necessary.
*/
private Point2D.Double checkWorldPixel(final Point2D newWorldPixelCenter) {
final long maxWidth = _mapTileSize.width * _tilePixelSize;
final long maxHeight = _mapTileSize.height * _tilePixelSize;
double newCenterX = newWorldPixelCenter.getX();
double newCenterY = newWorldPixelCenter.getY();
if (newCenterX < 0) {
newCenterX = -1;
}
if (newCenterX > maxWidth) {
newCenterX = maxWidth + 1;
}
if (newCenterY < 0) {
newCenterY = -1;
}
if (newCenterY > maxHeight) {
newCenterY = maxHeight + 1;
}
return new Point2D.Double(newCenterX, newCenterY);