]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/deletebydate/DeleteByDateFunction.java
Version 17, September 2014
[GpsPrune.git] / tim / prune / function / deletebydate / DeleteByDateFunction.java
1 package tim.prune.function.deletebydate;
2
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;
12
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;
20
21 import tim.prune.App;
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;
27
28 /**
29  * Function to select a date or dates,
30  * and delete the corresponding points
31  */
32 public class DeleteByDateFunction extends MarkAndDeleteFunction
33 {
34         /** dialog for selecting dates */
35         private JDialog _dialog = null;
36         /** Ok button */
37         private JButton _okButton = null;
38         /** date info list */
39         private DateInfoList _infoList = new DateInfoList();
40
41
42         /**
43          * Constructor
44          * @param inApp App object
45          */
46         public DeleteByDateFunction(App inApp)
47         {
48                 super(inApp);
49         }
50
51         @Override
52         public String getNameKey() {
53                 return "function.deletebydate";
54         }
55
56         @Override
57         public void begin()
58         {
59                 // Make a list of which dates are present in the track
60                 _infoList.clearAll();
61                 final int numPoints = _app.getTrackInfo().getTrack().getNumPoints();
62                 for (int i=0; i<numPoints; i++)
63                 {
64                         DataPoint point = _app.getTrackInfo().getTrack().getPoint(i);
65                         if (point != null)
66                         {
67                                 if (point.hasTimestamp()) {
68                                         _infoList.addPoint(point.getTimestamp().getCalendar().getTime());
69                                 }
70                                 else {
71                                         _infoList.addPoint(null); // no timestamp available
72                                 }
73                         }
74                 }
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);
80
81                 // Loop over entries for debug
82 //              if (!checkOk)
83 //              {
84 //                      for (int i=0; i<_infoList.getNumEntries(); i++)
85 //                      {
86 //                              DateInfo info = _infoList.getDateInfo(i);
87 //                              System.out.println("   " + i + " (" + info.getPointCount() + " points) - " +
88 //                                      (info.isDateless() ? "no date" : "date"));
89 //                      }
90 //              }
91
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)
94                 {
95                         _app.showErrorMessage(getNameKey(), "dialog.deletebydate.onlyonedate");
96                 }
97                 else
98                 {
99                         // Create and build dialog if necessary
100                         if (_dialog == null)
101                         {
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());
106                                 _dialog.pack();
107                         }
108                         // Show dialog
109                         _dialog.setVisible(true);
110                 }
111         }
112
113         /**
114          * Create dialog components
115          * @return Panel containing all gui elements in dialog
116          */
117         private Component makeDialogComponents()
118         {
119                 JPanel dialogPanel = new JPanel();
120                 dialogPanel.setLayout(new BorderLayout(5, 5));
121                 // Label at top
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);
125
126                 // close window if escape pressed
127                 KeyAdapter escListener = new KeyAdapter() {
128                         public void keyReleased(KeyEvent inE) {
129                                 if (inE.getKeyCode() == KeyEvent.VK_ESCAPE) {
130                                         _dialog.dispose();
131                                 }
132                         }
133                 };
134
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);
140
141                 // button panel at bottom
142                 JPanel buttonPanel = new JPanel();
143                 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
144                 // OK button
145                 _okButton = new JButton(I18nManager.getText("button.ok"));
146                 _okButton.addActionListener(new ActionListener() {
147                         public void actionPerformed(ActionEvent e) {
148                                 performDelete();
149                         }
150                 });
151                 buttonPanel.add(_okButton);
152                 _okButton.addKeyListener(escListener);
153                 // Cancel button
154                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
155                 cancelButton.addActionListener(new ActionListener() {
156                         public void actionPerformed(ActionEvent e) {
157                                 _dialog.dispose();
158                         }
159                 });
160                 cancelButton.addKeyListener(new KeyAdapter() {
161                         public void keyPressed(KeyEvent inE) {
162                                 if (inE.getKeyCode() == KeyEvent.VK_ESCAPE) {_dialog.dispose();}
163                         }
164                 });
165                 buttonPanel.add(cancelButton);
166                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
167                 return dialogPanel;
168         }
169
170         /**
171          * Do the actual point deletion according to the
172          * selected rows in the table
173          */
174         private void performDelete()
175         {
176                 int numMarked = 0;
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++)
181                 {
182                         DataPoint point = _app.getTrackInfo().getTrack().getPoint(p);
183                         if (point != null)
184                         {
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++)
189                                 {
190                                         DateInfo info = _infoList.getDateInfo(d);
191                                         if ( (info.isDateless() && date == null) // matches dateless
192                                           || (!info.isDateless() && date != null && info.isSameDate(date)))
193                                         {
194                                                 pointMarked = info.getDeleteFlag();
195                                                 break;
196                                         }
197                                 }
198                                 point.setMarkedForDeletion(pointMarked);
199                                 if (pointMarked) {
200                                         numMarked++;
201                                 }
202                         }
203                 }
204                 // Now points have been marked, we can ask user to delete them (or delete automatically)
205                 if (numMarked > 0) {
206                         optionallyDeleteMarkedPoints(numMarked);
207                 }
208                 else {
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);
212                 }
213                 _dialog.dispose();
214         }
215 }