1 package tim.prune.function.deletebydate;
3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.awt.Dimension;
6 import java.awt.FlowLayout;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9 import java.awt.event.KeyAdapter;
10 import java.awt.event.KeyEvent;
11 import java.util.Date;
13 import javax.swing.BorderFactory;
14 import javax.swing.JButton;
15 import javax.swing.JDialog;
16 import javax.swing.JLabel;
17 import javax.swing.JPanel;
18 import javax.swing.JScrollPane;
19 import javax.swing.JTable;
22 import tim.prune.DataSubscriber;
23 import tim.prune.I18nManager;
24 import tim.prune.UpdateMessageBroker;
25 import tim.prune.data.DataPoint;
26 import tim.prune.function.compress.MarkAndDeleteFunction;
29 * Function to select a date or dates,
30 * and delete the corresponding points
32 public class DeleteByDateFunction extends MarkAndDeleteFunction
34 /** dialog for selecting dates */
35 private JDialog _dialog = null;
37 private JButton _okButton = null;
39 private DateInfoList _infoList = new DateInfoList();
44 * @param inApp App object
46 public DeleteByDateFunction(App inApp)
52 public String getNameKey() {
53 return "function.deletebydate";
59 // Make a list of which dates are present in the track
61 final int numPoints = _app.getTrackInfo().getTrack().getNumPoints();
62 for (int i=0; i<numPoints; i++)
64 DataPoint point = _app.getTrackInfo().getTrack().getPoint(i);
67 if (point.hasTimestamp()) {
68 _infoList.addPoint(point.getTimestamp().getCalendar().getTime());
71 _infoList.addPoint(null); // no timestamp available
75 // System.out.println("Debug: info list has dateless points? " + (_infoList.hasDatelessPoints() ? "yes":"no"));
76 // System.out.println("Debug: info list has " + _infoList.getNumEntries() + " different entries");
77 // System.out.println("Debug: info list has " + _infoList.getTotalNumPoints() + " total points");
78 // final boolean checkOk = (_infoList.getTotalNumPoints() == numPoints);
79 // System.out.println("Debug: which " + (checkOk?"IS":"ISN'T!") + " the same as track: " + numPoints);
81 // Loop over entries for debug
84 // for (int i=0; i<_infoList.getNumEntries(); i++)
86 // DateInfo info = _infoList.getDateInfo(i);
87 // System.out.println(" " + i + " (" + info.getPointCount() + " points) - " +
88 // (info.isDateless() ? "no date" : "date"));
92 // Complain if there is only one entry in the list - this means all points are on the same day
93 if (_infoList.getNumEntries() < 2)
95 _app.showErrorMessage(getNameKey(), "dialog.deletebydate.onlyonedate");
99 // Create and build dialog if necessary
102 _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
103 _dialog.setLocationRelativeTo(_parentFrame);
104 _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
105 _dialog.getContentPane().add(makeDialogComponents());
109 _dialog.setVisible(true);
114 * Create dialog components
115 * @return Panel containing all gui elements in dialog
117 private Component makeDialogComponents()
119 JPanel dialogPanel = new JPanel();
120 dialogPanel.setLayout(new BorderLayout(5, 5));
122 JLabel topLabel = new JLabel(I18nManager.getText("dialog.deletebydate.intro"));
123 topLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
124 dialogPanel.add(topLabel, BorderLayout.NORTH);
126 // close window if escape pressed
127 KeyAdapter escListener = new KeyAdapter() {
128 public void keyReleased(KeyEvent inE) {
129 if (inE.getKeyCode() == KeyEvent.VK_ESCAPE) {
135 JTable infoTable = new JTable(new DeletionTableModel(_infoList));
136 JScrollPane pane = new JScrollPane(infoTable);
137 pane.setPreferredSize(new Dimension(300, 80));
138 pane.setBorder(BorderFactory.createEmptyBorder(2, 50, 2, 50));
139 dialogPanel.add(pane, BorderLayout.CENTER);
141 // button panel at bottom
142 JPanel buttonPanel = new JPanel();
143 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
145 _okButton = new JButton(I18nManager.getText("button.ok"));
146 _okButton.addActionListener(new ActionListener() {
147 public void actionPerformed(ActionEvent e) {
151 buttonPanel.add(_okButton);
152 _okButton.addKeyListener(escListener);
154 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
155 cancelButton.addActionListener(new ActionListener() {
156 public void actionPerformed(ActionEvent e) {
160 cancelButton.addKeyListener(new KeyAdapter() {
161 public void keyPressed(KeyEvent inE) {
162 if (inE.getKeyCode() == KeyEvent.VK_ESCAPE) {_dialog.dispose();}
165 buttonPanel.add(cancelButton);
166 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
171 * Do the actual point deletion according to the
172 * selected rows in the table
174 private void performDelete()
177 final int numPoints = _app.getTrackInfo().getTrack().getNumPoints();
178 final int numDates = _infoList.getNumEntries();
179 // Loop over all points to mark each one for deletion or not
180 for (int p=0; p<numPoints; p++)
182 DataPoint point = _app.getTrackInfo().getTrack().getPoint(p);
185 Date date = (point.hasTimestamp() ? point.getTimestamp().getCalendar().getTime() : null);
186 boolean pointMarked = false;
187 // Try to match each of the date info objects in the list
188 for (int d=0; d<numDates; d++)
190 DateInfo info = _infoList.getDateInfo(d);
191 if ( (info.isDateless() && date == null) // matches dateless
192 || (!info.isDateless() && date != null && info.isSameDate(date)))
194 pointMarked = info.getDeleteFlag();
198 point.setMarkedForDeletion(pointMarked);
204 // Now points have been marked, we can ask user to delete them (or delete automatically)
206 optionallyDeleteMarkedPoints(numMarked);
209 // Do nothing //System.out.println("Nothing selected to delete!");
210 // delete flags might have been reset, so refresh display
211 UpdateMessageBroker.informSubscribers(DataSubscriber.SELECTION_CHANGED);