]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/distance/DistanceFunction.java
Version 7, February 2009
[GpsPrune.git] / 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.util.ArrayList;
11
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;
21
22 import tim.prune.App;
23 import tim.prune.GenericFunction;
24 import tim.prune.I18nManager;
25 import tim.prune.data.DataPoint;
26 import tim.prune.data.TrackInfo;
27
28 /**
29  * Class to provide the distance measuring function between waypoints
30  */
31 public class DistanceFunction extends GenericFunction
32 {
33         /** Dialog */
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;
41
42
43         /**
44          * Constructor
45          * @param inApp App object
46          */
47         public DistanceFunction(App inApp)
48         {
49                 super(inApp);
50         }
51
52         /** Get the name key */
53         public String getNameKey() {
54                 return "function.distances";
55         }
56
57         /**
58          * Begin the function
59          */
60         public void begin()
61         {
62                 if (_dialog == null)
63                 {
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());
68                         _dialog.pack();
69                 }
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");
74                         return;
75                 }
76                 _fromModel.init(pointList);
77                 _distModel.init(pointList);
78                 _pointTable.getSelectionModel().setSelectionInterval(0, 0);
79                 _distModel.recalculate(0);
80                 _dialog.setVisible(true);
81         }
82
83         /**
84          * Create dialog components
85          * @return Panel containing all gui elements in dialog
86          */
87         private Component makeDialogComponents()
88         {
89                 JPanel dialogPanel = new JPanel();
90                 dialogPanel.setLayout(new BorderLayout(5, 5));
91                 // Label at top
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);
95
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());
106                                 }
107                         }
108                 });
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);
120
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)
127                         {
128                                 _dialog.dispose();
129                         }
130                 });
131                 buttonPanel.add(closeButton);
132                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
133                 return dialogPanel;
134         }
135
136         /**
137          * Get the point list for the tables
138          * @param inTrackInfo TrackInfo object
139          */
140         private static ArrayList<DataPoint> getPointList(TrackInfo inTrackInfo)
141         {
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);
150                 }
151                 return pointList;
152         }
153 }