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.util.ArrayList;
12 import javax.swing.BorderFactory;
13 import javax.swing.JButton;
14 import javax.swing.JDialog;
15 import javax.swing.JLabel;
16 import javax.swing.JPanel;
17 import javax.swing.JScrollPane;
18 import javax.swing.JTable;
19 import javax.swing.event.ListSelectionEvent;
20 import javax.swing.event.ListSelectionListener;
23 import tim.prune.GenericFunction;
24 import tim.prune.I18nManager;
25 import tim.prune.data.DataPoint;
26 import tim.prune.data.TrackInfo;
29 * Class to provide the distance measuring function between waypoints
31 public class DistanceFunction extends GenericFunction
34 private JDialog _dialog = null;
35 /** Table for 'from' point selection */
36 private JTable _pointTable = null;
37 /** Model for 'from' table */
38 private FromTableModel _fromModel = null;
39 /** Model for distance table */
40 private DistanceTableModel _distModel = null;
45 * @param inApp App object
47 public DistanceFunction(App inApp)
52 /** Get the name key */
53 public String getNameKey() {
54 return "function.distances";
64 _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
65 _dialog.setLocationRelativeTo(_parentFrame);
66 _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
67 _dialog.getContentPane().add(makeDialogComponents());
70 ArrayList<DataPoint> pointList = getPointList(_app.getTrackInfo());
71 // Check if point list has size at least 2
72 if (pointList.size() < 2) {
73 _app.showErrorMessage(getNameKey(), "dialog.distances.toofewpoints");
76 _fromModel.init(pointList);
77 _distModel.init(pointList);
78 _pointTable.getSelectionModel().setSelectionInterval(0, 0);
79 _distModel.recalculate(0);
80 _dialog.setVisible(true);
84 * Create dialog components
85 * @return Panel containing all gui elements in dialog
87 private Component makeDialogComponents()
89 JPanel dialogPanel = new JPanel();
90 dialogPanel.setLayout(new BorderLayout(5, 5));
92 JLabel topLabel = new JLabel(I18nManager.getText("dialog.distances.intro"));
93 topLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
94 dialogPanel.add(topLabel, BorderLayout.NORTH);
96 JPanel mainPanel = new JPanel();
97 mainPanel.setLayout(new GridLayout(1, 2));
98 // First table for 'from point'
99 _fromModel = new FromTableModel();
100 _pointTable = new JTable(_fromModel);
101 _pointTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
102 /** selection changed */
103 public void valueChanged(ListSelectionEvent e) {
104 if (!e.getValueIsAdjusting()) {
105 _distModel.recalculate(_pointTable.getSelectedRow());
109 JScrollPane scrollPane = new JScrollPane(_pointTable);
110 scrollPane.setPreferredSize(new Dimension(100, 250));
111 mainPanel.add(scrollPane);
112 // second table for distances
113 _distModel = new DistanceTableModel();
114 JTable distTable = new JTable(_distModel);
115 distTable.setAutoCreateRowSorter(true);
116 scrollPane = new JScrollPane(distTable);
117 scrollPane.setPreferredSize(new Dimension(200, 250));
118 mainPanel.add(scrollPane);
119 dialogPanel.add(mainPanel, BorderLayout.CENTER);
121 // button panel at bottom
122 JPanel buttonPanel = new JPanel();
123 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
124 JButton closeButton = new JButton(I18nManager.getText("button.close"));
125 closeButton.addActionListener(new ActionListener() {
126 public void actionPerformed(ActionEvent e)
131 buttonPanel.add(closeButton);
132 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
137 * Get the point list for the tables
138 * @param inTrackInfo TrackInfo object
140 private static ArrayList<DataPoint> getPointList(TrackInfo inTrackInfo)
142 // Get the list of waypoints (if any)
143 ArrayList<DataPoint> pointList = new ArrayList<DataPoint>();
144 inTrackInfo.getTrack().getWaypoints(pointList);
145 // Get the current point (if any)
146 DataPoint currPoint = inTrackInfo.getCurrentPoint();
147 if (currPoint != null && !currPoint.isWaypoint()) {
148 // Add current point to start of list
149 pointList.add(0, currPoint);