File: SelectTracksFunction.java

package info (click to toggle)
gpsprune 17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,984 kB
  • ctags: 5,218
  • sloc: java: 39,403; sh: 25; makefile: 17; python: 15
file content (194 lines) | stat: -rw-r--r-- 5,886 bytes parent folder | download | duplicates (5)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package tim.prune.function;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import tim.prune.App;
import tim.prune.GenericFunction;
import tim.prune.I18nManager;
import tim.prune.data.DataPoint;
import tim.prune.data.SourceInfo;
import tim.prune.data.Track;
import tim.prune.load.TrackNameList;

/**
 * Function to allow the selection of which tracks to load from the file / stream
 */
public class SelectTracksFunction extends GenericFunction
{
	private Track _track = null;
	private SourceInfo _sourceInfo = null;
	private TrackNameList _trackNameList = null;
	private JDialog _dialog = null;
	private JList<String> _trackList = null;

	/**
	 * Constructor
	 * @param inApp app object to use for load
	 * @param inTrack loaded track object
	 * @param inSourceInfo source information
	 * @param inTrackNameList track name list
	 */
	public SelectTracksFunction(App inApp, Track inTrack, SourceInfo inSourceInfo,
		TrackNameList inTrackNameList)
	{
		super(inApp);
		_track = inTrack;
		_sourceInfo = inSourceInfo;
		_trackNameList = inTrackNameList;
	}

	/**
	 * Start the function
	 */
	public void begin()
	{
		_dialog = new JDialog(_parentFrame, I18nManager.getText("function.open"));
		_dialog.setLocationRelativeTo(_parentFrame);
		_dialog.getContentPane().add(makeContents());
		_dialog.pack();
		_dialog.setVisible(true);
		selectAll();
	}

	/**
	 * @return the contents of the window as a Component
	 */
	private Component makeContents()
	{
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new BorderLayout());
		mainPanel.add(new JLabel(I18nManager.getText("dialog.selecttracks.intro")), BorderLayout.NORTH);
		// track list
		final int numTracks = _trackNameList.getNumTracks();
		String[] names = new String[numTracks];
		for (int i=0; i<numTracks; i++)
		{
			String name = _trackNameList.getTrackName(i);
			if (name == null || name.equals("")) {
				name = I18nManager.getText("dialog.selecttracks.noname");
			}
			names[i] = name + " (" + _trackNameList.getNumPointsInTrack(i) + ")";
		}
		_trackList = new JList<String>(names);
		_trackList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		mainPanel.add(new JScrollPane(_trackList), BorderLayout.CENTER);
		// select all button
		JButton selectAllButton = new JButton(I18nManager.getText("button.selectall"));
		selectAllButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				selectAll();
			}
		});
		JPanel eastPanel = new JPanel();
		eastPanel.add(selectAllButton);
		mainPanel.add(eastPanel, BorderLayout.EAST);

		// button panel at bottom
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		JButton okButton = new JButton(I18nManager.getText("button.ok"));
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				finish();
			}
		});
		buttonPanel.add(okButton);
		JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				_dialog.dispose();
				_app.informNoDataLoaded();
			}
		});
		buttonPanel.add(cancelButton);
		mainPanel.add(buttonPanel, BorderLayout.SOUTH);
		mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
		return mainPanel;
	}

	/** @return name key */
	public String getNameKey() {
		return "dialog.selecttracks";
	}

	/**
	 * Select all the tracks in the list
	 */
	private void selectAll()
	{
		_trackList.setSelectionInterval(0, _trackNameList.getNumTracks()-1);
	}

	/**
	 * OK pressed, so finish the load
	 */
	private void finish()
	{
		_dialog.dispose();
		int[] tracks = _trackList.getSelectedIndices();
		// Check if all tracks are selected, then don't have to filter at all
		if (tracks.length == _trackNameList.getNumTracks()) {
			_app.informDataLoaded(_track, _sourceInfo);
		}
		else
		{
			// build array of which tracks have been selected
			boolean[] selectedTracks = new boolean[_trackNameList.getNumTracks()];
			for (int i=0; i<tracks.length; i++) {
				selectedTracks[tracks[i]] = true;
			}
			// Loop over all points, counting points which survive filter and making flag array
			int numPointsSelected = 0;
			int currentTrack = -1;
			final int totalPoints = _track.getNumPoints();
			boolean[] selectedPoints = new boolean[totalPoints];
			for (int i=0; i<totalPoints; i++)
			{
				final int startOfNextTrack = _trackNameList.getStartIndex(currentTrack+1);
				if (i == startOfNextTrack) {currentTrack++;}
				if (currentTrack < 0 || selectedTracks[currentTrack] || _track.getPoint(i).isWaypoint()) {
					selectedPoints[i] = true;
					numPointsSelected++;
				}
			}
			// If none of the points have been selected, then load nothing
			if (numPointsSelected <= 0) {
				_app.informNoDataLoaded();
			}
			else
			{
				// Create new point array of required length
				DataPoint[] croppedPoints = new DataPoint[numPointsSelected];
				// Loop over all points again, copying surviving ones
				int currPoint = 0;
				for (int i=0; i<totalPoints; i++)
				{
					if (selectedPoints[i]) {
						croppedPoints[currPoint] = _track.getPoint(i);
						currPoint++;
					}
				}
				// Construct Track and call informDataLoaded
				Track filteredTrack = new Track(_track.getFieldList(), croppedPoints);
				// Tell source info object which points were selected (pass selectedPoints array)
				_sourceInfo.setPointIndices(selectedPoints);
				_app.informDataLoaded(filteredTrack, _sourceInfo);
			}
		}
	}
}