]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/AddTimeOffset.java
2e259bb5f5895e94690355d2cbee039080a8434d
[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 _dayField = null, _hourField = null;
37         private WholeNumberField _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.days"));
107                 _dayField = new WholeNumberField(3);
108                 descPanel.add(_dayField);
109                 descPanel.add(makeRightLabel("dialog.addtimeoffset.hours"));
110                 _hourField = new WholeNumberField(3);
111                 descPanel.add(_hourField);
112                 descPanel.add(makeRightLabel("dialog.addtimeoffset.minutes"));
113                 _minuteField = new WholeNumberField(3);
114                 descPanel.add(_minuteField);
115                 mainPanel.add(descPanel);
116                 dialogPanel.add(mainPanel, BorderLayout.CENTER);
117
118                 // Listeners to enable/disable ok button
119                 KeyAdapter keyListener = new KeyAdapter() {
120                         /** Key typed */
121                         public void keyTyped(KeyEvent event) {
122                                 final boolean isNumber = "1234567890".indexOf(event.getKeyChar()) >= 0;
123                                 _okButton.setEnabled(isNumber || getOffsetSecs() != 0L);
124                         }
125                 };
126                 MouseAdapter mouseListener = new MouseAdapter() {
127                         public void mouseReleased(java.awt.event.MouseEvent arg0) {
128                                 _okButton.setEnabled(getOffsetSecs() != 0L);
129                         };
130                 };
131                 _dayField.addKeyListener(keyListener);
132                 _hourField.addKeyListener(keyListener);
133                 _minuteField.addKeyListener(keyListener);
134                 _dayField.addMouseListener(mouseListener);
135                 _hourField.addMouseListener(mouseListener);
136                 _minuteField.addMouseListener(mouseListener);
137
138                 // button panel at bottom
139                 JPanel buttonPanel = new JPanel();
140                 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
141                 _okButton = new JButton(I18nManager.getText("button.ok"));
142                 ActionListener okListener = new ActionListener() {
143                         public void actionPerformed(ActionEvent e)
144                         {
145                                 finish();
146                         }
147                 };
148                 _okButton.addActionListener(okListener);
149                 _okButton.setEnabled(false);
150                 buttonPanel.add(_okButton);
151                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
152                 cancelButton.addActionListener(new ActionListener() {
153                         public void actionPerformed(ActionEvent e)
154                         {
155                                 _dialog.dispose();
156                         }
157                 });
158                 buttonPanel.add(cancelButton);
159                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
160                 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
161                 return dialogPanel;
162         }
163
164
165         /**
166          * @param inKey text key
167          * @return right-aligned label
168          */
169         private static final JLabel makeRightLabel(String inKey)
170         {
171                 JLabel label = new JLabel(I18nManager.getText(inKey) + " : ");
172                 label.setHorizontalAlignment(SwingConstants.RIGHT);
173                 return label;
174         }
175
176         /**
177          * @return the number of offset seconds entered by the user
178          */
179         private long getOffsetSecs()
180         {
181                 long offsetSecs = _minuteField.getValue() * 60L
182                   + _hourField.getValue() * 60L * 60L
183                   + _dayField.getValue() * 60L * 60L * 24L;
184                 if (_subtractRadio.isSelected()) {offsetSecs = -offsetSecs;}
185                 return offsetSecs;
186         }
187
188         /**
189          * Finish the dialog when OK pressed
190          */
191         private void finish()
192         {
193                 // Calculate offset to add or subtract
194                 long offsetSecs = getOffsetSecs();
195                 if (offsetSecs != 0L)
196                 {
197                         // Pass offset back to app and close dialog
198                         _app.finishAddTimeOffsetSeconds(offsetSecs);
199                         _dialog.dispose();
200                 }
201         }
202 }