]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/compress/CompressTrackFunction.java
Version 16, February 2014
[GpsPrune.git] / tim / prune / function / compress / CompressTrackFunction.java
1 package tim.prune.function.compress;
2
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;
8
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;
16
17 import tim.prune.App;
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;
23
24 /**
25  * Class to provide the function for track compression
26  */
27 public class CompressTrackFunction extends GenericFunction
28 {
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;
34         /** flag to remember whether the automatic deletion has been set to always */
35         private boolean _automaticallyDelete = false;
36
37
38         /**
39          * Constructor
40          * @param inApp app object
41          */
42         public CompressTrackFunction(App inApp)
43         {
44                 super(inApp);
45                 _track = inApp.getTrackInfo().getTrack();
46                 makeAlgorithms();
47         }
48
49         /** Get the name key */
50         public String getNameKey() {
51                 return "function.compress";
52         }
53
54         /**
55          * Show the dialog to select compression parameters
56          */
57         public void begin()
58         {
59                 // Make dialog window
60                 if (_dialog == null)
61                 {
62                         _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
63                         _dialog.setLocationRelativeTo(_parentFrame);
64                         _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
65                         _dialog.getContentPane().add(makeDialogComponents());
66                         _dialog.pack();
67                 }
68                 preview();
69                 _dialog.setVisible(true);
70         }
71
72         /**
73          * Preview the compression by calling each algorithm in turn
74          * @return array of delete flags
75          */
76         private boolean[] preview()
77         {
78                 int numToDelete = 0;
79                 boolean[] deleteFlags = new boolean[_track.getNumPoints()];
80                 for (int i=0; i<_algorithms.length; i++)
81                 {
82                         numToDelete += _algorithms[i].preview(deleteFlags);
83                 }
84                 _summaryLabel.setValue(numToDelete);
85                 _okButton.setEnabled(numToDelete > 0);
86                 return deleteFlags;
87         }
88
89
90         /**
91          * Create dialog components
92          * @return Panel containing all gui elements in dialog
93          */
94         private JPanel makeDialogComponents()
95         {
96                 JPanel dialogPanel = new JPanel();
97                 dialogPanel.setLayout(new BorderLayout());
98
99                 // Make a central panel
100                 JPanel mainPanel = new JPanel();
101                 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
102
103                 // Add each of the algorithm components to the panel
104                 for (int i=0; i<_algorithms.length; i++)
105                 {
106                         mainPanel.add(_algorithms[i].getGuiComponents());
107                         mainPanel.add(Box.createRigidArea(new Dimension(0, 2)));
108                 }
109                 // Summary label below algorithms
110                 JPanel summaryPanel = new JPanel();
111                 _summaryLabel = new SummaryLabel(_track);
112                 summaryPanel.add(_summaryLabel);
113                 mainPanel.add(summaryPanel);
114                 dialogPanel.add(mainPanel, BorderLayout.NORTH);
115
116                 // button panel at bottom
117                 JPanel buttonPanel = new JPanel();
118                 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
119                 _okButton = new JButton(I18nManager.getText("button.ok"));
120                 _okButton.setEnabled(false);
121                 ActionListener okListener = new ActionListener() {
122                         public void actionPerformed(ActionEvent e)
123                         {
124                                 finish();
125                         }
126                 };
127                 _okButton.addActionListener(okListener);
128                 buttonPanel.add(_okButton);
129                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
130                 cancelButton.addActionListener(new ActionListener() {
131                         public void actionPerformed(ActionEvent e)
132                         {
133                                 _dialog.dispose();
134                         }
135                 });
136                 buttonPanel.add(cancelButton);
137                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
138                 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
139                 return dialogPanel;
140         }
141
142
143         /**
144          * Initialise all the algorithms to use
145          */
146         private void makeAlgorithms()
147         {
148                 // make listener to be informed of algorithm activation
149                 ActionListener changeListener = new ActionListener() {
150                         public void actionPerformed(ActionEvent arg0)
151                         {
152                                 preview();
153                         };
154                 };
155                 // construct track details to be used by all algorithms
156                 TrackDetails details = new TrackDetails(_track);
157                 // make array of algorithm objects
158                 _algorithms = new CompressionAlgorithm[] {
159                         new DuplicatePointAlgorithm(_track, details, changeListener),
160                         new ClosePointsAlgorithm(_track, details, changeListener),
161                         new WackyPointAlgorithm(_track, details, changeListener),
162                         new SingletonAlgorithm(_track, details, changeListener),
163                         new DouglasPeuckerAlgorithm(_track, details, changeListener)
164                 };
165         }
166
167
168         /**
169          * Finish the dialog when OK pressed
170          */
171         private void finish()
172         {
173                 boolean[] deleteFlags = preview();
174                 // All flags are now combined in deleteFlags array
175                 int numMarked = 0;
176                 for (int i=0; i<deleteFlags.length; i++)
177                 {
178                         DataPoint point = _track.getPoint(i);
179                         boolean deletePoint = deleteFlags[i] && !point.hasMedia();
180                         point.setMarkedForDeletion(deletePoint);
181                         if (deletePoint) numMarked++;
182                 }
183
184                 // Close dialog and inform listeners
185                 UpdateMessageBroker.informSubscribers();
186                 _dialog.dispose();
187                 // Show confirmation dialog with OK button (not status bar message)
188                 if (numMarked > 0)
189                 {
190                         // Allow calling of delete function with one click
191                         final String[] buttonTexts = {I18nManager.getText("button.yes"), I18nManager.getText("button.no"),
192                                 I18nManager.getText("button.always")};
193                         int answer = _automaticallyDelete ? JOptionPane.YES_OPTION :
194                                 JOptionPane.showOptionDialog(_parentFrame,
195                                 I18nManager.getTextWithNumber("dialog.compress.confirm", numMarked),
196                                 I18nManager.getText(getNameKey()), JOptionPane.YES_NO_CANCEL_OPTION,
197                                 JOptionPane.WARNING_MESSAGE, null, buttonTexts, buttonTexts[1]);
198                         if (answer == JOptionPane.CANCEL_OPTION) {_automaticallyDelete = true;} // "always" is third option
199                         if (_automaticallyDelete || answer == JOptionPane.YES_OPTION)
200                         {
201                                 new Thread(new Runnable() {
202                                         public void run() {
203                                                 _app.finishCompressTrack();
204                                         }
205                                 }).start();
206                         }
207                 }
208                 else
209                 {
210                         JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("dialog.compress.confirmnone"),
211                                 I18nManager.getText(getNameKey()), JOptionPane.INFORMATION_MESSAGE);
212                 }
213         }
214 }