]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/compress/CompressionAlgorithm.java
cd1f9dfac0073a10de728a9496cdf371c3bde00c
[GpsPrune.git] / tim / prune / function / compress / CompressionAlgorithm.java
1 package tim.prune.function.compress;
2
3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.awt.event.ActionListener;
6
7 import javax.swing.BorderFactory;
8 import javax.swing.JCheckBox;
9 import javax.swing.JPanel;
10
11 import tim.prune.I18nManager;
12 import tim.prune.data.Track;
13
14 /**
15  * Abstract class to act as an algorithm for track compression
16  */
17 public abstract class CompressionAlgorithm
18 {
19         protected JCheckBox _activateCheckBox = null;
20         protected SummaryLabel _summaryLabel = null;
21         protected Track _track = null;
22         protected TrackDetails _trackDetails = null;
23
24
25         /**
26          * Constructor giving Track
27          * @param inTrack track object to use for compression
28          * @param inDetails track details object
29          * @param inListener listener to be informed of activation clicks
30          */
31         public CompressionAlgorithm(Track inTrack, TrackDetails inDetails,
32                 ActionListener inListener)
33         {
34                 _track = inTrack;
35                 _trackDetails = inDetails;
36                 _activateCheckBox = new JCheckBox(I18nManager.getText(getTitleTextKey()));
37                 _activateCheckBox.setSelected(false);
38                 _activateCheckBox.addActionListener(inListener);
39         }
40
41
42         /**
43          * @return true if this algorithm has been activated
44          */
45         public boolean isActivated()
46         {
47                 return _activateCheckBox.isSelected();
48         }
49
50
51         /**
52          * @return JPanel containing gui components
53          */
54         public JPanel getGuiComponents()
55         {
56                 JPanel panel = new JPanel();
57                 panel.setBorder(BorderFactory.createTitledBorder(""));
58                 panel.setLayout(new BorderLayout());
59                 panel.add(_activateCheckBox, BorderLayout.NORTH);
60                 Component specifics = getSpecificGuiComponents();
61                 if (specifics != null) {
62                         panel.add(specifics, BorderLayout.CENTER);
63                 }
64                 // Add label at bottom
65                 _summaryLabel = new SummaryLabel(_track);
66                 panel.add(_summaryLabel, BorderLayout.SOUTH);
67                 return panel;
68         }
69
70         /**
71          * Preview the algorithm by counting the number of points deleted
72          * @param inFlags array of deletion flags from previous algorithms
73          * @return number of points to be deleted by this algorithm
74          */
75         public int preview(boolean[] inFlags)
76         {
77                 int numDeleted = 0;
78                 if (isActivated())
79                 {
80                         // Run the compression and set the deletion flags
81                         numDeleted = compress(inFlags);
82                         _summaryLabel.setValue(numDeleted);
83                 }
84                 else {
85                         _summaryLabel.clearValue();
86                 }
87                 return numDeleted;
88         }
89
90
91         /**
92          * @return key to use for title text of algorithm
93          */
94         protected abstract String getTitleTextKey();
95
96         /**
97          * @return gui components controlling algorithm (if any)
98          */
99         protected abstract Component getSpecificGuiComponents();
100
101         /**
102          * Perform the compression and set the results in the given array
103          * @param inFlags deletion flags from previous algorithms
104          * @return number of points deleted by this algorithm
105          */
106         protected abstract int compress(boolean[] inFlags);
107
108 }