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 _pointTable.getSelectionModel().setSelectionInterval(0, 0);
81 _distModel.recalculate(0);
82 _dialog.setVisible(true);
86 * Create dialog components
87 * @return Panel containing all gui elements in dialog
89 private Component makeDialogComponents()
91 JPanel dialogPanel = new JPanel();
92 dialogPanel.setLayout(new BorderLayout(5, 5));
94 JLabel topLabel = new JLabel(I18nManager.getText("dialog.distances.intro"));
95 topLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
96 dialogPanel.add(topLabel, BorderLayout.NORTH);
98 JPanel mainPanel = new JPanel();
99 mainPanel.setLayout(new GridLayout(1, 2));
100 // First table for 'from point'
101 _fromModel = new FromTableModel();
102 _pointTable = new JTable(_fromModel);
103 _pointTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
104 /** selection changed */
105 public void valueChanged(ListSelectionEvent e) {
106 if (!e.getValueIsAdjusting()) {
107 _distModel.recalculate(_pointTable.getSelectedRow());
111 JScrollPane scrollPane = new JScrollPane(_pointTable);
112 scrollPane.setPreferredSize(new Dimension(100, 250));
113 mainPanel.add(scrollPane);
114 // second table for distances
115 _distModel = new DistanceTableModel();
116 JTable distTable = new JTable(_distModel);
117 distTable.setAutoCreateRowSorter(true);
118 scrollPane = new JScrollPane(distTable);
119 scrollPane.setPreferredSize(new Dimension(200, 250));
120 mainPanel.add(scrollPane);
121 dialogPanel.add(mainPanel, BorderLayout.CENTER);
123 // close window if escape pressed
124 KeyAdapter escListener = new KeyAdapter() {
125 public void keyReleased(KeyEvent inE) {
126 if (inE.getKeyCode() == KeyEvent.VK_ESCAPE) {
131 _pointTable.addKeyListener(escListener);
132 distTable.addKeyListener(escListener);
134 // button panel at bottom
135 JPanel buttonPanel = new JPanel();
136 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
137 JButton closeButton = new JButton(I18nManager.getText("button.close"));
138 closeButton.addActionListener(new ActionListener() {
139 public void actionPerformed(ActionEvent e)
144 buttonPanel.add(closeButton);
145 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
150 * Get the point list for the tables
151 * @param inTrackInfo TrackInfo object
153 private static ArrayList<DataPoint> getPointList(TrackInfo inTrackInfo)
155 // Get the list of waypoints (if any)
156 ArrayList<DataPoint> pointList = new ArrayList<DataPoint>();
157 inTrackInfo.getTrack().getWaypoints(pointList);
158 // Get the current point (if any)
159 DataPoint currPoint = inTrackInfo.getCurrentPoint();
160 if (currPoint != null && !currPoint.isWaypoint()) {
161 // Add current point to start of list
162 pointList.add(0, currPoint);