]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/AddTimeOffset.java
Allow to specify a time offset in blocks of 1024-week
[GpsPrune.git] / src / tim / prune / function / AddTimeOffset.java
1 package tim.prune.function;
2
3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.awt.FlowLayout;
6 import java.awt.GridLayout;
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.awt.event.MouseAdapter;
12
13 import javax.swing.BorderFactory;
14 import javax.swing.BoxLayout;
15 import javax.swing.ButtonGroup;
16 import javax.swing.JButton;
17 import javax.swing.JDialog;
18 import javax.swing.JLabel;
19 import javax.swing.JPanel;
20 import javax.swing.JRadioButton;
21 import javax.swing.SwingConstants;
22
23 import tim.prune.App;
24 import tim.prune.GenericFunction;
25 import tim.prune.I18nManager;
26 import tim.prune.data.Field;
27 import tim.prune.gui.WholeNumberField;
28
29 /**
30  * Class to provide the function to add a time offset to a track range
31  */
32 public class AddTimeOffset extends GenericFunction
33 {
34         private JDialog _dialog = null;
35         private JRadioButton _addRadio = null, _subtractRadio = null;
36         private WholeNumberField _1024weekField = null, _dayField = null;
37         private WholeNumberField _hourField = null, _minuteField = null;
38         private JButton _okButton = null;
39
40
41         /**
42          * Constructor
43          * @param inApp application object for callback
44          */
45         public AddTimeOffset(App inApp)
46         {
47                 super(inApp);
48         }
49
50         /** Get the name key */
51         public String getNameKey() {
52                 return "function.addtimeoffset";
53         }
54
55         /**
56          * Begin the function
57          */
58         public void begin()
59         {
60                 int selStart = _app.getTrackInfo().getSelection().getStart();
61                 int selEnd = _app.getTrackInfo().getSelection().getEnd();
62                 if (!_app.getTrackInfo().getTrack().hasData(Field.TIMESTAMP, selStart, selEnd))
63                 {
64                         _app.showErrorMessage(getNameKey(), "dialog.addtimeoffset.notimestamps");
65                         return;
66                 }
67                 // Make dialog window
68                 if (_dialog == null)
69                 {
70                         _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
71                         _dialog.setLocationRelativeTo(_parentFrame);
72                         _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
73                         _dialog.getContentPane().add(makeDialogComponents());
74                         _dialog.pack();
75                 }
76                 _dialog.setVisible(true);
77         }
78
79
80         /**
81          * Create dialog components
82          * @return Panel containing all gui elements in dialog
83          */
84         private Component makeDialogComponents()
85         {
86                 JPanel dialogPanel = new JPanel();
87                 dialogPanel.setLayout(new BorderLayout());
88                 JPanel mainPanel = new JPanel();
89                 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
90                 // Make a panel for the two radio buttons
91                 JPanel radioPanel = new JPanel();
92                 _addRadio = new JRadioButton(I18nManager.getText("dialog.addtimeoffset.add"));
93                 _addRadio.setSelected(true);
94                 radioPanel.add(_addRadio);
95                 _subtractRadio = new JRadioButton(I18nManager.getText("dialog.addtimeoffset.subtract"));
96                 _subtractRadio.setSelected(false);
97                 radioPanel.add(_subtractRadio);
98                 ButtonGroup radioGroup = new ButtonGroup();
99                 radioGroup.add(_addRadio);
100                 radioGroup.add(_subtractRadio);
101                 mainPanel.add(radioPanel);
102
103                 // Make a central panel with the text boxes
104                 JPanel descPanel = new JPanel();
105                 descPanel.setLayout(new GridLayout(0, 2));
106                 descPanel.add(makeRightLabel("dialog.addtimeoffset.1024week"));
107                 _1024weekField = new WholeNumberField(3);
108                 descPanel.add(_1024weekField);
109                 descPanel.add(makeRightLabel("dialog.addtimeoffset.days"));
110                 _dayField = new WholeNumberField(4);
111                 descPanel.add(_dayField);
112                 descPanel.add(makeRightLabel("dialog.addtimeoffset.hours"));
113                 _hourField = new WholeNumberField(3);
114                 descPanel.add(_hourField);
115                 descPanel.add(makeRightLabel("dialog.addtimeoffset.minutes"));
116                 _minuteField = new WholeNumberField(3);
117                 descPanel.add(_minuteField);
118                 mainPanel.add(descPanel);
119                 dialogPanel.add(mainPanel, BorderLayout.CENTER);
120
121                 // Listeners to enable/disable ok button
122                 KeyAdapter keyListener = new KeyAdapter() {
123                         /** Key typed */
124                         public void keyTyped(KeyEvent event) {
125                                 final boolean isNumber = "1234567890".indexOf(event.getKeyChar()) >= 0;
126                                 _okButton.setEnabled(isNumber || getOffsetSecs() != 0L);
127                         }
128                 };
129                 MouseAdapter mouseListener = new MouseAdapter() {
130                         public void mouseReleased(java.awt.event.MouseEvent arg0) {
131                                 _okButton.setEnabled(getOffsetSecs() != 0L);
132                         };
133                 };
134                 _1024weekField.addKeyListener(keyListener);
135                 _dayField.addKeyListener(keyListener);
136                 _hourField.addKeyListener(keyListener);
137                 _minuteField.addKeyListener(keyListener);
138                 _1024weekField.addMouseListener(mouseListener);
139                 _dayField.addMouseListener(mouseListener);
140                 _hourField.addMouseListener(mouseListener);
141                 _minuteField.addMouseListener(mouseListener);
142
143                 // button panel at bottom
144                 JPanel buttonPanel = new JPanel();
145                 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
146                 _okButton = new JButton(I18nManager.getText("button.ok"));
147                 ActionListener okListener = new ActionListener() {
148                         public void actionPerformed(ActionEvent e)
149                         {
150                                 finish();
151                         }
152                 };
153                 _okButton.addActionListener(okListener);
154                 _okButton.setEnabled(false);
155                 buttonPanel.add(_okButton);
156                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
157                 cancelButton.addActionListener(new ActionListener() {
158                         public void actionPerformed(ActionEvent e)
159                         {
160                                 _dialog.dispose();
161                         }
162                 });
163                 buttonPanel.add(cancelButton);
164                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
165                 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
166                 return dialogPanel;
167         }
168
169
170         /**
171          * @param inKey text key
172          * @return right-aligned label
173          */
174         private static final JLabel makeRightLabel(String inKey)
175         {
176                 JLabel label = new JLabel(I18nManager.getText(inKey) + " : ");
177                 label.setHorizontalAlignment(SwingConstants.RIGHT);
178                 return label;
179         }
180
181         /**
182          * @return the number of offset seconds entered by the user
183          */
184         private long getOffsetSecs()
185         {
186                 long offsetSecs = _minuteField.getValue() * 60L
187                   + _hourField.getValue() * 60L * 60L
188                   + _dayField.getValue() * 60L * 60L * 24L
189                   + _1024weekField.getValue() * 60L * 60L * 24L * 7L * 1024L;
190                 if (_subtractRadio.isSelected()) {offsetSecs = -offsetSecs;}
191                 return offsetSecs;
192         }
193
194         /**
195          * Finish the dialog when OK pressed
196          */
197         private void finish()
198         {
199                 // Calculate offset to add or subtract
200                 long offsetSecs = getOffsetSecs();
201                 if (offsetSecs != 0L)
202                 {
203                         // Pass offset back to app and close dialog
204                         _app.finishAddTimeOffsetSeconds(offsetSecs);
205                         _dialog.dispose();
206                 }
207         }
208 }