File: CompressTrackFunction.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 (181 lines) | stat: -rw-r--r-- 4,829 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
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
package tim.prune.function.compress;

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

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;

import tim.prune.App;
import tim.prune.GenericFunction;
import tim.prune.I18nManager;
import tim.prune.UpdateMessageBroker;
import tim.prune.data.DataPoint;
import tim.prune.data.Track;

/**
 * Class to provide the function for track compression
 */
public class CompressTrackFunction extends GenericFunction
{
	private Track _track = null;
	private JDialog _dialog = null;
	private JButton _okButton = null;
	private CompressionAlgorithm[] _algorithms = null;
	private SummaryLabel _summaryLabel = null;


	/**
	 * Constructor
	 * @param inApp app object
	 */
	public CompressTrackFunction(App inApp)
	{
		super(inApp);
		_track = inApp.getTrackInfo().getTrack();
		makeAlgorithms();
	}

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

	/**
	 * Show the dialog to select compression parameters
	 */
	public void begin()
	{
		// Make dialog window
		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();
		}
		preview();
		_dialog.setVisible(true);
	}

	/**
	 * Preview the compression by calling each algorithm in turn
	 * @return array of delete flags
	 */
	private boolean[] preview()
	{
		int numToDelete = 0;
		boolean[] deleteFlags = new boolean[_track.getNumPoints()];
		for (int i=0; i<_algorithms.length; i++)
		{
			numToDelete += _algorithms[i].preview(deleteFlags);
		}
		_summaryLabel.setValue(numToDelete);
		_okButton.setEnabled(numToDelete > 0);
		return deleteFlags;
	}


	/**
	 * Create dialog components
	 * @return Panel containing all gui elements in dialog
	 */
	private JPanel makeDialogComponents()
	{
		JPanel dialogPanel = new JPanel();
		dialogPanel.setLayout(new BorderLayout());

		// Make a central panel
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

		// Add each of the algorithm components to the panel
		for (int i=0; i<_algorithms.length; i++)
		{
			mainPanel.add(_algorithms[i].getGuiComponents());
			mainPanel.add(Box.createRigidArea(new Dimension(0, 2)));
		}
		// Summary label below algorithms
		JPanel summaryPanel = new JPanel();
		_summaryLabel = new SummaryLabel(_track);
		summaryPanel.add(_summaryLabel);
		mainPanel.add(summaryPanel);
		dialogPanel.add(mainPanel, BorderLayout.NORTH);

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


	/**
	 * Initialise all the algorithms to use
	 */
	private void makeAlgorithms()
	{
		// make listener to be informed of algorithm activation
		ActionListener changeListener = new ActionListener() {
			public void actionPerformed(ActionEvent arg0)
			{
				preview();
			};
		};
		// construct track details to be used by all algorithms
		TrackDetails details = new TrackDetails(_track);
		// make array of algorithm objects
		_algorithms = new CompressionAlgorithm[] {
			new DuplicatePointAlgorithm(_track, details, changeListener),
			new ClosePointsAlgorithm(_track, details, changeListener),
			new WackyPointAlgorithm(_track, details, changeListener),
			new SingletonAlgorithm(_track, details, changeListener)
		};
	}


	/**
	 * Finish the dialog when OK pressed
	 */
	private void finish()
	{
		boolean[] deleteFlags = preview();
		// All flags are now combined in deleteFlags array
		for (int i=0; i<deleteFlags.length; i++)
		{
			DataPoint point = _track.getPoint(i);
			point.setMarkedForDeletion(deleteFlags[i] && point.getPhoto() == null);
		}

		// Close dialog and inform listeners
		UpdateMessageBroker.informSubscribers();
		_dialog.dispose();
	}
}