File: DistanceFunction.java

package info (click to toggle)
gpsprune 10-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,220 kB
  • ctags: 3,013
  • sloc: java: 22,662; sh: 23; makefile: 16; python: 15
file content (153 lines) | stat: -rw-r--r-- 4,526 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package tim.prune.function.distance;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import tim.prune.App;
import tim.prune.GenericFunction;
import tim.prune.I18nManager;
import tim.prune.data.DataPoint;
import tim.prune.data.TrackInfo;

/**
 * Class to provide the distance measuring function between waypoints
 */
public class DistanceFunction extends GenericFunction
{
	/** Dialog */
	private JDialog _dialog = null;
	/** Table for 'from' point selection */
	private JTable _pointTable = null;
	/** Model for 'from' table */
	private FromTableModel _fromModel = null;
	/** Model for distance table */
	private DistanceTableModel _distModel = null;


	/**
	 * Constructor
	 * @param inApp App object
	 */
	public DistanceFunction(App inApp)
	{
		super(inApp);
	}

	/** Get the name key */
	public String getNameKey() {
		return "function.distances";
	}

	/**
	 * Begin the function
	 */
	public void begin()
	{
		if (_dialog == null)
		{
			_dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
			_dialog.setLocationRelativeTo(_parentFrame);
			_dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			_dialog.getContentPane().add(makeDialogComponents());
			_dialog.pack();
		}
		ArrayList<DataPoint> pointList = getPointList(_app.getTrackInfo());
		// Check if point list has size at least 2
		if (pointList.size() < 2) {
			_app.showErrorMessage(getNameKey(), "dialog.distances.toofewpoints");
			return;
		}
		_fromModel.init(pointList);
		_distModel.init(pointList);
		_pointTable.getSelectionModel().setSelectionInterval(0, 0);
		_distModel.recalculate(0);
		_dialog.setVisible(true);
	}

	/**
	 * Create dialog components
	 * @return Panel containing all gui elements in dialog
	 */
	private Component makeDialogComponents()
	{
		JPanel dialogPanel = new JPanel();
		dialogPanel.setLayout(new BorderLayout(5, 5));
		// Label at top
		JLabel topLabel = new JLabel(I18nManager.getText("dialog.distances.intro"));
		topLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		dialogPanel.add(topLabel, BorderLayout.NORTH);

		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new GridLayout(1, 2));
		// First table for 'from point'
		_fromModel = new FromTableModel();
		_pointTable = new JTable(_fromModel);
		_pointTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			/** selection changed */
			public void valueChanged(ListSelectionEvent e) {
				if (!e.getValueIsAdjusting()) {
					_distModel.recalculate(_pointTable.getSelectedRow());
				}
			}
		});
		JScrollPane scrollPane = new JScrollPane(_pointTable);
		scrollPane.setPreferredSize(new Dimension(100, 250));
		mainPanel.add(scrollPane);
		// second table for distances
		_distModel = new DistanceTableModel();
		JTable distTable = new JTable(_distModel);
		distTable.setAutoCreateRowSorter(true);
		scrollPane = new JScrollPane(distTable);
		scrollPane.setPreferredSize(new Dimension(200, 250));
		mainPanel.add(scrollPane);
		dialogPanel.add(mainPanel, BorderLayout.CENTER);

		// button panel at bottom
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		JButton closeButton = new JButton(I18nManager.getText("button.close"));
		closeButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				_dialog.dispose();
			}
		});
		buttonPanel.add(closeButton);
		dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
		return dialogPanel;
	}

	/**
	 * Get the point list for the tables
	 * @param inTrackInfo TrackInfo object
	 */
	private static ArrayList<DataPoint> getPointList(TrackInfo inTrackInfo)
	{
		// Get the list of waypoints (if any)
		ArrayList<DataPoint> pointList = new ArrayList<DataPoint>();
		inTrackInfo.getTrack().getWaypoints(pointList);
		// Get the current point (if any)
		DataPoint currPoint = inTrackInfo.getCurrentPoint();
		if (currPoint != null && !currPoint.isWaypoint()) {
			// Add current point to start of list
			pointList.add(0, currPoint);
		}
		return pointList;
	}
}