]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/compress/MarkPointsInRectangleFunction.java
Version 16, February 2014
[GpsPrune.git] / tim / prune / function / compress / MarkPointsInRectangleFunction.java
1 package tim.prune.function.compress;
2
3 import javax.swing.JOptionPane;
4
5 import tim.prune.App;
6 import tim.prune.GenericFunction;
7 import tim.prune.I18nManager;
8 import tim.prune.UpdateMessageBroker;
9 import tim.prune.data.DataPoint;
10
11 /**
12  * Function to mark all the points in the selected rectangle
13  */
14 public class MarkPointsInRectangleFunction extends GenericFunction
15 {
16         /** Minimum and maximum latitude values of rectangle */
17         private double _minLat = 0.0, _maxLat = 0.0;
18         /** Minimum and maximum longitude values of rectangle */
19         private double _minLon = 0.0, _maxLon = 0.0;
20         /** flag to remember whether the automatic deletion has been set to always */
21         private boolean _automaticallyDelete = false;
22
23
24         /**
25          * Constructor
26          * @param inApp App object
27          */
28         public MarkPointsInRectangleFunction(App inApp)
29         {
30                 super(inApp);
31         }
32
33         /** @return name key */
34         public String getNameKey() {
35                 return "menu.track.markrectangle";
36         }
37
38         /**
39          * Set the coordinates of the rectangle
40          * @param inLon1 first longitude value
41          * @param inLat1 first latitude value
42          * @param inLon2 second longitude value
43          * @param inLat2 second latitude value
44          */
45         public void setRectCoords(double inLon1, double inLat1, double inLon2, double inLat2)
46         {
47                 if (inLon1 == inLon2 || inLat1 == inLat2)
48                 {
49                         // Coordinates not valid
50                         _minLat = _maxLat = _minLon = _maxLon = 0.0;
51                 }
52                 else
53                 {
54                         if (inLon2 > inLon1) {
55                                 _minLon = inLon1; _maxLon = inLon2;
56                         }
57                         else {
58                                 _minLon = inLon2; _maxLon = inLon1;
59                         }
60                         if (inLat2 > inLat1) {
61                                 _minLat = inLat1; _maxLat = inLat2;
62                         }
63                         else {
64                                 _minLat = inLat2; _maxLat = inLat1;
65                         }
66                 }
67         }
68
69         /**
70          * Begin the function using the set parameters
71          */
72         public void begin()
73         {
74                 if (_maxLon == _minLon || _maxLat == _minLat) {
75                         return;
76                 }
77
78                 // Loop over all points in track
79                 final int numPoints = _app.getTrackInfo().getTrack().getNumPoints();
80                 int numMarked = 0;
81                 for (int i=0; i<numPoints; i++)
82                 {
83                         DataPoint point = _app.getTrackInfo().getTrack().getPoint(i);
84                         // For each point, see if it's within the rectangle
85                         final double pointLon = point.getLongitude().getDouble();
86                         final double pointLat = point.getLatitude().getDouble();
87                         final boolean insideRect = (pointLon >= _minLon && pointLon <= _maxLon
88                                 && pointLat >= _minLat && pointLat <= _maxLat);
89                         // If so, then mark it
90                         point.setMarkedForDeletion(insideRect);
91                         if (insideRect) {
92                                 numMarked++;
93                         }
94                 }
95
96                 // Inform subscribers to update display
97                 UpdateMessageBroker.informSubscribers();
98                 // Confirm message showing how many marked
99                 if (numMarked > 0)
100                 {
101                         // Allow calling of delete function with one click
102                         final String[] buttonTexts = {I18nManager.getText("button.yes"), I18nManager.getText("button.no"),
103                                 I18nManager.getText("button.always")};
104                         int answer = _automaticallyDelete ? JOptionPane.YES_OPTION :
105                                 JOptionPane.showOptionDialog(_parentFrame,
106                                 I18nManager.getTextWithNumber("dialog.compress.confirm", numMarked),
107                                 I18nManager.getText(getNameKey()), JOptionPane.YES_NO_CANCEL_OPTION,
108                                 JOptionPane.WARNING_MESSAGE, null, buttonTexts, buttonTexts[1]);
109                         if (answer == JOptionPane.CANCEL_OPTION) {_automaticallyDelete = true;} // "always" is third option
110                         if (_automaticallyDelete || answer == JOptionPane.YES_OPTION)
111                         {
112                                 new Thread(new Runnable() {
113                                         public void run() {
114                                                 _app.finishCompressTrack();
115                                         }
116                                 }).start();
117                         }
118                 }
119         }
120 }