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.JOptionPane;
15 import javax.swing.JPanel;
18 import tim.prune.GenericFunction;
19 import tim.prune.I18nManager;
20 import tim.prune.UpdateMessageBroker;
21 import tim.prune.data.DataPoint;
22 import tim.prune.data.Track;
25 * Class to provide the function for track compression
27 public class CompressTrackFunction extends GenericFunction
29 private Track _track = null;
30 private JDialog _dialog = null;
31 private JButton _okButton = null;
32 private CompressionAlgorithm[] _algorithms = null;
33 private SummaryLabel _summaryLabel = null;
38 * @param inApp app object
40 public CompressTrackFunction(App inApp)
43 _track = inApp.getTrackInfo().getTrack();
47 /** Get the name key */
48 public String getNameKey() {
49 return "function.compress";
53 * Show the dialog to select compression parameters
60 _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
61 _dialog.setLocationRelativeTo(_parentFrame);
62 _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
63 _dialog.getContentPane().add(makeDialogComponents());
67 _dialog.setVisible(true);
71 * Preview the compression by calling each algorithm in turn
72 * @return array of delete flags
74 private boolean[] preview()
77 boolean[] deleteFlags = new boolean[_track.getNumPoints()];
78 for (int i=0; i<_algorithms.length; i++)
80 numToDelete += _algorithms[i].preview(deleteFlags);
82 _summaryLabel.setValue(numToDelete);
83 _okButton.setEnabled(numToDelete > 0);
89 * Create dialog components
90 * @return Panel containing all gui elements in dialog
92 private JPanel makeDialogComponents()
94 JPanel dialogPanel = new JPanel();
95 dialogPanel.setLayout(new BorderLayout());
97 // Make a central panel
98 JPanel mainPanel = new JPanel();
99 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
101 // Add each of the algorithm components to the panel
102 for (int i=0; i<_algorithms.length; i++)
104 mainPanel.add(_algorithms[i].getGuiComponents());
105 mainPanel.add(Box.createRigidArea(new Dimension(0, 2)));
107 // Summary label below algorithms
108 JPanel summaryPanel = new JPanel();
109 _summaryLabel = new SummaryLabel(_track);
110 summaryPanel.add(_summaryLabel);
111 mainPanel.add(summaryPanel);
112 dialogPanel.add(mainPanel, BorderLayout.NORTH);
114 // button panel at bottom
115 JPanel buttonPanel = new JPanel();
116 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
117 _okButton = new JButton(I18nManager.getText("button.ok"));
118 _okButton.setEnabled(false);
119 ActionListener okListener = new ActionListener() {
120 public void actionPerformed(ActionEvent e)
125 _okButton.addActionListener(okListener);
126 buttonPanel.add(_okButton);
127 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
128 cancelButton.addActionListener(new ActionListener() {
129 public void actionPerformed(ActionEvent e)
134 buttonPanel.add(cancelButton);
135 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
136 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
142 * Initialise all the algorithms to use
144 private void makeAlgorithms()
146 // make listener to be informed of algorithm activation
147 ActionListener changeListener = new ActionListener() {
148 public void actionPerformed(ActionEvent arg0)
153 // construct track details to be used by all algorithms
154 TrackDetails details = new TrackDetails(_track);
155 // make array of algorithm objects
156 _algorithms = new CompressionAlgorithm[] {
157 new DuplicatePointAlgorithm(_track, details, changeListener),
158 new ClosePointsAlgorithm(_track, details, changeListener),
159 new WackyPointAlgorithm(_track, details, changeListener),
160 new SingletonAlgorithm(_track, details, changeListener),
161 new DouglasPeuckerAlgorithm(_track, details, changeListener)
167 * Finish the dialog when OK pressed
169 private void finish()
171 boolean[] deleteFlags = preview();
172 // All flags are now combined in deleteFlags array
174 for (int i=0; i<deleteFlags.length; i++)
176 DataPoint point = _track.getPoint(i);
177 boolean deletePoint = deleteFlags[i] && !point.hasMedia();
178 point.setMarkedForDeletion(deletePoint);
179 if (deletePoint) numMarked++;
182 // Close dialog and inform listeners
183 UpdateMessageBroker.informSubscribers();
185 // Show confirmation dialog with OK button (not status bar message)
187 JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("dialog.compress.confirm1")
188 + " " + numMarked + " " + I18nManager.getText("dialog.compress.confirm2"),
189 I18nManager.getText(getNameKey()), JOptionPane.INFORMATION_MESSAGE);
192 JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("dialog.compress.confirmnone"),
193 I18nManager.getText(getNameKey()), JOptionPane.INFORMATION_MESSAGE);