1 package tim.prune.function.compress;
3 import java.awt.BorderLayout;
4 import java.awt.Dimension;
5 import java.awt.FlowLayout;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
9 import javax.swing.BorderFactory;
10 import javax.swing.Box;
11 import javax.swing.BoxLayout;
12 import javax.swing.JButton;
13 import javax.swing.JDialog;
14 import javax.swing.JPanel;
17 import tim.prune.GenericFunction;
18 import tim.prune.I18nManager;
19 import tim.prune.UpdateMessageBroker;
20 import tim.prune.data.DataPoint;
21 import tim.prune.data.Track;
24 * Class to provide the function for track compression
26 public class CompressTrackFunction extends GenericFunction
28 private Track _track = null;
29 private JDialog _dialog = null;
30 private JButton _okButton = null;
31 private CompressionAlgorithm[] _algorithms = null;
32 private SummaryLabel _summaryLabel = null;
37 * @param inApp app object
39 public CompressTrackFunction(App inApp)
42 _track = inApp.getTrackInfo().getTrack();
46 /** Get the name key */
47 public String getNameKey() {
48 return "function.compress";
52 * Show the dialog to select compression parameters
59 _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
60 _dialog.setLocationRelativeTo(_parentFrame);
61 _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
62 _dialog.getContentPane().add(makeDialogComponents());
66 _dialog.setVisible(true);
70 * Preview the compression by calling each algorithm in turn
71 * @return array of delete flags
73 private boolean[] preview()
76 boolean[] deleteFlags = new boolean[_track.getNumPoints()];
77 for (int i=0; i<_algorithms.length; i++)
79 numToDelete += _algorithms[i].preview(deleteFlags);
81 _summaryLabel.setValue(numToDelete);
82 _okButton.setEnabled(numToDelete > 0);
88 * Create dialog components
89 * @return Panel containing all gui elements in dialog
91 private JPanel makeDialogComponents()
93 JPanel dialogPanel = new JPanel();
94 dialogPanel.setLayout(new BorderLayout());
96 // Make a central panel
97 JPanel mainPanel = new JPanel();
98 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
100 // Add each of the algorithm components to the panel
101 for (int i=0; i<_algorithms.length; i++)
103 mainPanel.add(_algorithms[i].getGuiComponents());
104 mainPanel.add(Box.createRigidArea(new Dimension(0, 2)));
106 // Summary label below algorithms
107 JPanel summaryPanel = new JPanel();
108 _summaryLabel = new SummaryLabel(_track);
109 summaryPanel.add(_summaryLabel);
110 mainPanel.add(summaryPanel);
111 dialogPanel.add(mainPanel, BorderLayout.NORTH);
113 // button panel at bottom
114 JPanel buttonPanel = new JPanel();
115 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
116 _okButton = new JButton(I18nManager.getText("button.ok"));
117 _okButton.setEnabled(false);
118 ActionListener okListener = new ActionListener() {
119 public void actionPerformed(ActionEvent e)
124 _okButton.addActionListener(okListener);
125 buttonPanel.add(_okButton);
126 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
127 cancelButton.addActionListener(new ActionListener() {
128 public void actionPerformed(ActionEvent e)
133 buttonPanel.add(cancelButton);
134 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
135 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
141 * Initialise all the algorithms to use
143 private void makeAlgorithms()
145 // make listener to be informed of algorithm activation
146 ActionListener changeListener = new ActionListener() {
147 public void actionPerformed(ActionEvent arg0)
152 // construct track details to be used by all algorithms
153 TrackDetails details = new TrackDetails(_track);
154 // make array of algorithm objects
155 _algorithms = new CompressionAlgorithm[] {
156 new DuplicatePointAlgorithm(_track, details, changeListener),
157 new ClosePointsAlgorithm(_track, details, changeListener),
158 new WackyPointAlgorithm(_track, details, changeListener),
159 new SingletonAlgorithm(_track, details, changeListener)
165 * Finish the dialog when OK pressed
167 private void finish()
169 boolean[] deleteFlags = preview();
170 // All flags are now combined in deleteFlags array
171 for (int i=0; i<deleteFlags.length; i++)
173 DataPoint point = _track.getPoint(i);
174 point.setMarkedForDeletion(deleteFlags[i] && point.getPhoto() == null);
177 // Close dialog and inform listeners
178 UpdateMessageBroker.informSubscribers();