]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/distance/DistanceFunction.java
5fe6e7e7d9e307d901ad25c23b2a4ea021acc3d3
[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                 _pointTable.getSelectionModel().setSelectionInterval(0, 0);
81                 _distModel.recalculate(0);
82                 _dialog.setVisible(true);
83         }
84
85         /**
86          * Create dialog components
87          * @return Panel containing all gui elements in dialog
88          */
89         private Component makeDialogComponents()
90         {
91                 JPanel dialogPanel = new JPanel();
92                 dialogPanel.setLayout(new BorderLayout(5, 5));
93                 // Label at top
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);
97
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());
108                                 }
109                         }
110                 });
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                 // Use reflection to call distTable.setAutoCreateRowSorter(true) which is new with Java 1.6
118                 try {
119                         Class<?> d = Class.forName("javax.swing.JTable");
120                         d.getDeclaredMethod("setAutoCreateRowSorter", new Class[]{Boolean.TYPE}).invoke(distTable, Boolean.TRUE);
121                 }
122                 catch (Exception e) {}
123                 scrollPane = new JScrollPane(distTable);
124                 scrollPane.setPreferredSize(new Dimension(200, 250));
125                 mainPanel.add(scrollPane);
126                 dialogPanel.add(mainPanel, BorderLayout.CENTER);
127
128                 // close window if escape pressed
129                 KeyAdapter escListener = new KeyAdapter() {
130                         public void keyReleased(KeyEvent inE) {
131                                 if (inE.getKeyCode() == KeyEvent.VK_ESCAPE) {
132                                         _dialog.dispose();
133                                 }
134                         }
135                 };
136                 _pointTable.addKeyListener(escListener);
137                 distTable.addKeyListener(escListener);
138
139                 // button panel at bottom
140                 JPanel buttonPanel = new JPanel();
141                 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
142                 JButton closeButton = new JButton(I18nManager.getText("button.close"));
143                 closeButton.addActionListener(new ActionListener() {
144                         public void actionPerformed(ActionEvent e)
145                         {
146                                 _dialog.dispose();
147                         }
148                 });
149                 buttonPanel.add(closeButton);
150                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
151                 return dialogPanel;
152         }
153
154         /**
155          * Get the point list for the tables
156          * @param inTrackInfo TrackInfo object
157          */
158         private static ArrayList<DataPoint> getPointList(TrackInfo inTrackInfo)
159         {
160                 // Get the list of waypoints (if any)
161                 ArrayList<DataPoint> pointList = new ArrayList<DataPoint>();
162                 inTrackInfo.getTrack().getWaypoints(pointList);
163                 // Get the current point (if any)
164                 DataPoint currPoint = inTrackInfo.getCurrentPoint();
165                 if (currPoint != null && !currPoint.isWaypoint()) {
166                         // Add current point to start of list
167                         pointList.add(0, currPoint);
168                 }
169                 return pointList;
170         }
171 }