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