]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/compress/CompressTrackFunction.java
Version 14, October 2012
[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
35
36         /**
37          * Constructor
38          * @param inApp app object
39          */
40         public CompressTrackFunction(App inApp)
41         {
42                 super(inApp);
43                 _track = inApp.getTrackInfo().getTrack();
44                 makeAlgorithms();
45         }
46
47         /** Get the name key */
48         public String getNameKey() {
49                 return "function.compress";
50         }
51
52         /**
53          * Show the dialog to select compression parameters
54          */
55         public void begin()
56         {
57                 // Make dialog window
58                 if (_dialog == null)
59                 {
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());
64                         _dialog.pack();
65                 }
66                 preview();
67                 _dialog.setVisible(true);
68         }
69
70         /**
71          * Preview the compression by calling each algorithm in turn
72          * @return array of delete flags
73          */
74         private boolean[] preview()
75         {
76                 int numToDelete = 0;
77                 boolean[] deleteFlags = new boolean[_track.getNumPoints()];
78                 for (int i=0; i<_algorithms.length; i++)
79                 {
80                         numToDelete += _algorithms[i].preview(deleteFlags);
81                 }
82                 _summaryLabel.setValue(numToDelete);
83                 _okButton.setEnabled(numToDelete > 0);
84                 return deleteFlags;
85         }
86
87
88         /**
89          * Create dialog components
90          * @return Panel containing all gui elements in dialog
91          */
92         private JPanel makeDialogComponents()
93         {
94                 JPanel dialogPanel = new JPanel();
95                 dialogPanel.setLayout(new BorderLayout());
96
97                 // Make a central panel
98                 JPanel mainPanel = new JPanel();
99                 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
100
101                 // Add each of the algorithm components to the panel
102                 for (int i=0; i<_algorithms.length; i++)
103                 {
104                         mainPanel.add(_algorithms[i].getGuiComponents());
105                         mainPanel.add(Box.createRigidArea(new Dimension(0, 2)));
106                 }
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);
113
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)
121                         {
122                                 finish();
123                         }
124                 };
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)
130                         {
131                                 _dialog.dispose();
132                         }
133                 });
134                 buttonPanel.add(cancelButton);
135                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
136                 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
137                 return dialogPanel;
138         }
139
140
141         /**
142          * Initialise all the algorithms to use
143          */
144         private void makeAlgorithms()
145         {
146                 // make listener to be informed of algorithm activation
147                 ActionListener changeListener = new ActionListener() {
148                         public void actionPerformed(ActionEvent arg0)
149                         {
150                                 preview();
151                         };
152                 };
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)
162                 };
163         }
164
165
166         /**
167          * Finish the dialog when OK pressed
168          */
169         private void finish()
170         {
171                 boolean[] deleteFlags = preview();
172                 // All flags are now combined in deleteFlags array
173                 int numMarked = 0;
174                 for (int i=0; i<deleteFlags.length; i++)
175                 {
176                         DataPoint point = _track.getPoint(i);
177                         boolean deletePoint = deleteFlags[i] && !point.hasMedia();
178                         point.setMarkedForDeletion(deletePoint);
179                         if (deletePoint) numMarked++;
180                 }
181
182                 // Close dialog and inform listeners
183                 UpdateMessageBroker.informSubscribers();
184                 _dialog.dispose();
185                 // Show confirmation dialog with OK button (not status bar message)
186                 if (numMarked > 0) {
187                         JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("dialog.compress.confirm1")
188                                 + " " + numMarked + " " + I18nManager.getText("dialog.compress.confirm2"),
189                                 I18nManager.getText(getNameKey()), JOptionPane.INFORMATION_MESSAGE);
190                 }
191                 else {
192                         JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("dialog.compress.confirmnone"),
193                                 I18nManager.getText(getNameKey()), JOptionPane.INFORMATION_MESSAGE);
194                 }
195         }
196 }