1 package tim.prune.function.distance;
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;
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;
25 import tim.prune.GenericFunction;
26 import tim.prune.I18nManager;
27 import tim.prune.data.DataPoint;
28 import tim.prune.data.TrackInfo;
31 * Class to provide the distance measuring function between waypoints
33 public class DistanceFunction extends GenericFunction
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;
47 * @param inApp App object
49 public DistanceFunction(App inApp)
54 /** Get the name key */
55 public String getNameKey() {
56 return "function.distances";
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());
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");
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);
87 * Create dialog components
88 * @return Panel containing all gui elements in dialog
90 private Component makeDialogComponents()
92 JPanel dialogPanel = new JPanel();
93 dialogPanel.setLayout(new BorderLayout(5, 5));
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);
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());
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);
124 // close window if escape pressed
125 KeyAdapter escListener = new KeyAdapter() {
126 public void keyReleased(KeyEvent inE) {
127 if (inE.getKeyCode() == KeyEvent.VK_ESCAPE) {
132 _pointTable.addKeyListener(escListener);
133 distTable.addKeyListener(escListener);
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)
145 buttonPanel.add(closeButton);
146 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
151 * Get the point list for the tables
152 * @param inTrackInfo TrackInfo object
154 private static ArrayList<DataPoint> getPointList(TrackInfo inTrackInfo)
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);
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
174 private static int getPointIndex(ArrayList<DataPoint> pointList, TrackInfo inTrackInfo)
176 DataPoint currPoint = inTrackInfo.getCurrentPoint();
177 if (currPoint != null && currPoint.isWaypoint())
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) {