]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/distance/DistanceFunction.java
Version 20.3, April 2021
[GpsPrune.git] / src / tim / prune / function / distance / DistanceFunction.java
1 package tim.prune.function.distance;
2
3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.awt.Dimension;
6 import java.awt.FlowLayout;
7 import java.awt.GridLayout;
8 import java.awt.event.ActionEvent;
9 import java.awt.event.ActionListener;
10 import java.awt.event.KeyAdapter;
11 import java.awt.event.KeyEvent;
12 import java.util.ArrayList;
13
14 import javax.swing.BorderFactory;
15 import javax.swing.JButton;
16 import javax.swing.JDialog;
17 import javax.swing.JLabel;
18 import javax.swing.JPanel;
19 import javax.swing.JScrollPane;
20 import javax.swing.JTable;
21 import javax.swing.event.ListSelectionEvent;
22 import javax.swing.event.ListSelectionListener;
23
24 import tim.prune.App;
25 import tim.prune.GenericFunction;
26 import tim.prune.I18nManager;
27 import tim.prune.data.DataPoint;
28 import tim.prune.data.TrackInfo;
29
30 /**
31  * Class to provide the distance measuring function between waypoints
32  */
33 public class DistanceFunction extends GenericFunction
34 {
35         /** Dialog */
36         private JDialog _dialog = null;
37         /** Table for 'from' point selection */
38         private JTable _pointTable = null;
39         /** Model for 'from' table */
40         private FromTableModel _fromModel = null;
41         /** Model for distance table */
42         private DistanceTableModel _distModel = null;
43
44
45         /**
46          * Constructor
47          * @param inApp App object
48          */
49         public DistanceFunction(App inApp)
50         {
51                 super(inApp);
52         }
53
54         /** Get the name key */
55         public String getNameKey() {
56                 return "function.distances";
57         }
58
59         /**
60          * Begin the function
61          */
62         public void begin()
63         {
64                 if (_dialog == null)
65                 {
66                         _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
67                         _dialog.setLocationRelativeTo(_parentFrame);
68                         _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
69                         _dialog.getContentPane().add(makeDialogComponents());
70                         _dialog.pack();
71                 }
72                 ArrayList<DataPoint> pointList = getPointList(_app.getTrackInfo());
73                 // Check if point list has size at least 2
74                 if (pointList.size() < 2) {
75                         _app.showErrorMessage(getNameKey(), "dialog.distances.toofewpoints");
76                         return;
77                 }
78                 _fromModel.init(pointList);
79                 _distModel.init(pointList);
80                 final int pointIndex = getPointIndex(pointList, _app.getTrackInfo());
81                 _pointTable.getSelectionModel().setSelectionInterval(pointIndex, pointIndex);
82                 _distModel.recalculate(pointIndex);
83                 _dialog.setVisible(true);
84         }
85
86         /**
87          * Create dialog components
88          * @return Panel containing all gui elements in dialog
89          */
90         private Component makeDialogComponents()
91         {
92                 JPanel dialogPanel = new JPanel();
93                 dialogPanel.setLayout(new BorderLayout(5, 5));
94                 // Label at top
95                 JLabel topLabel = new JLabel(I18nManager.getText("dialog.distances.intro"));
96                 topLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
97                 dialogPanel.add(topLabel, BorderLayout.NORTH);
98
99                 JPanel mainPanel = new JPanel();
100                 mainPanel.setLayout(new GridLayout(1, 2));
101                 // First table for 'from point'
102                 _fromModel = new FromTableModel();
103                 _pointTable = new JTable(_fromModel);
104                 _pointTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
105                         /** selection changed */
106                         public void valueChanged(ListSelectionEvent e) {
107                                 if (!e.getValueIsAdjusting()) {
108                                         _distModel.recalculate(_pointTable.getSelectedRow());
109                                 }
110                         }
111                 });
112                 JScrollPane scrollPane = new JScrollPane(_pointTable);
113                 scrollPane.setPreferredSize(new Dimension(100, 250));
114                 mainPanel.add(scrollPane);
115                 // second table for distances
116                 _distModel = new DistanceTableModel();
117                 JTable distTable = new JTable(_distModel);
118                 distTable.setAutoCreateRowSorter(true);
119                 scrollPane = new JScrollPane(distTable);
120                 scrollPane.setPreferredSize(new Dimension(200, 250));
121                 mainPanel.add(scrollPane);
122                 dialogPanel.add(mainPanel, BorderLayout.CENTER);
123
124                 // close window if escape pressed
125                 KeyAdapter escListener = new KeyAdapter() {
126                         public void keyReleased(KeyEvent inE) {
127                                 if (inE.getKeyCode() == KeyEvent.VK_ESCAPE) {
128                                         _dialog.dispose();
129                                 }
130                         }
131                 };
132                 _pointTable.addKeyListener(escListener);
133                 distTable.addKeyListener(escListener);
134
135                 // button panel at bottom
136                 JPanel buttonPanel = new JPanel();
137                 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
138                 JButton closeButton = new JButton(I18nManager.getText("button.close"));
139                 closeButton.addActionListener(new ActionListener() {
140                         public void actionPerformed(ActionEvent e)
141                         {
142                                 _dialog.dispose();
143                         }
144                 });
145                 buttonPanel.add(closeButton);
146                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
147                 return dialogPanel;
148         }
149
150         /**
151          * Get the point list for the tables
152          * @param inTrackInfo TrackInfo object
153          */
154         private static ArrayList<DataPoint> getPointList(TrackInfo inTrackInfo)
155         {
156                 // Get the list of waypoints (if any)
157                 ArrayList<DataPoint> pointList = new ArrayList<DataPoint>();
158                 inTrackInfo.getTrack().getWaypoints(pointList);
159                 // Get the current point (if any)
160                 DataPoint currPoint = inTrackInfo.getCurrentPoint();
161                 if (currPoint != null && !currPoint.isWaypoint()) {
162                         // Add current point to start of list
163                         pointList.add(0, currPoint);
164                 }
165                 return pointList;
166         }
167
168         /**
169          * Find the point to select from the given point list
170          * @param pointList list of points
171          * @param inTrackInfo current track info to get selected point (if any)
172          * @return index of point to be selected
173          */
174         private static int getPointIndex(ArrayList<DataPoint> pointList, TrackInfo inTrackInfo)
175         {
176                 DataPoint currPoint = inTrackInfo.getCurrentPoint();
177                 if (currPoint != null && currPoint.isWaypoint())
178                 {
179                         // Currently selected point is a waypoint, so select this one for convenience
180                         for (int i=0; i<pointList.size(); i++) {
181                                 if (pointList.get(i) == currPoint) {
182                                         return i;
183                                 }
184                         }
185                 }
186                 return 0;
187         }
188 }