X-Git-Url: https://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=tim%2Fprune%2Ffunction%2FAddTimeOffset.java;fp=tim%2Fprune%2Fgui%2FTimeOffsetDialog.java;h=8b5d4a82dd548c1472fd7220c3b5d6b2353a2bc2;hp=11524fe633412c14346ad7f2a0ef630d03d35343;hb=54b9d8bc8f0025ccf97a67d9dd217ef1f9cf082f;hpb=52bf9e8686c916be37a26a0b75340393d4478b05 diff --git a/tim/prune/gui/TimeOffsetDialog.java b/tim/prune/function/AddTimeOffset.java similarity index 65% rename from tim/prune/gui/TimeOffsetDialog.java rename to tim/prune/function/AddTimeOffset.java index 11524fe..8b5d4a8 100644 --- a/tim/prune/gui/TimeOffsetDialog.java +++ b/tim/prune/function/AddTimeOffset.java @@ -1,4 +1,4 @@ -package tim.prune.gui; +package tim.prune.function; import java.awt.BorderLayout; import java.awt.Component; @@ -6,61 +6,74 @@ import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.awt.event.MouseAdapter; import javax.swing.BorderFactory; import javax.swing.BoxLayout; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JDialog; -import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.SwingConstants; import tim.prune.App; +import tim.prune.GenericFunction; import tim.prune.I18nManager; +import tim.prune.data.Field; +import tim.prune.gui.WholeNumberField; /** - * Class to show a dialog for adding a time offset to a track range + * Class to provide the function to add a time offset to a track range */ -public class TimeOffsetDialog +public class AddTimeOffset extends GenericFunction { - private App _app = null; - private JFrame _parentFrame = null; private JDialog _dialog = null; private JRadioButton _addRadio = null, _subtractRadio = null; private WholeNumberField _dayField = null, _hourField = null; private WholeNumberField _minuteField = null; + private JButton _okButton = null; /** * Constructor * @param inApp application object for callback - * @param inParentFrame parent frame */ - public TimeOffsetDialog(App inApp, JFrame inParentFrame) + public AddTimeOffset(App inApp) { - _app = inApp; - _parentFrame = inParentFrame; + super(inApp); } + /** Get the name key */ + public String getNameKey() { + return "function.addtimeoffset"; + } /** - * Show the dialog to select options and export file + * Begin the function */ - public void showDialog() + public void begin() { + int selStart = _app.getTrackInfo().getSelection().getStart(); + int selEnd = _app.getTrackInfo().getSelection().getEnd(); + if (!_app.getTrackInfo().getTrack().hasData(Field.TIMESTAMP, selStart, selEnd)) + { + _app.showErrorMessage(getNameKey(), "dialog.addtimeoffset.notimestamps"); + return; + } // Make dialog window if (_dialog == null) { - _dialog = new JDialog(_parentFrame, I18nManager.getText("dialog.addtimeoffset.title"), true); + _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true); _dialog.setLocationRelativeTo(_parentFrame); _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); _dialog.getContentPane().add(makeDialogComponents()); _dialog.pack(); } - _dialog.show(); + _dialog.setVisible(true); } @@ -87,8 +100,6 @@ public class TimeOffsetDialog radioGroup.add(_subtractRadio); mainPanel.add(radioPanel); - // Make a listener to validate the text boxes during typing (to en/disable OK button) - // Make a central panel with the text boxes JPanel descPanel = new JPanel(); descPanel.setLayout(new GridLayout(0, 2)); @@ -104,18 +115,38 @@ public class TimeOffsetDialog mainPanel.add(descPanel); dialogPanel.add(mainPanel, BorderLayout.CENTER); + // Listeners to enable/disable ok button + KeyAdapter keyListener = new KeyAdapter() { + /** Key typed */ + public void keyTyped(KeyEvent arg0) { + _okButton.setEnabled(getOffsetSecs() != 0L); + } + }; + MouseAdapter mouseListener = new MouseAdapter() { + public void mouseReleased(java.awt.event.MouseEvent arg0) { + _okButton.setEnabled(getOffsetSecs() != 0L); + }; + }; + _dayField.addKeyListener(keyListener); + _hourField.addKeyListener(keyListener); + _minuteField.addKeyListener(keyListener); + _dayField.addMouseListener(mouseListener); + _hourField.addMouseListener(mouseListener); + _minuteField.addMouseListener(mouseListener); + // button panel at bottom JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); - JButton okButton = new JButton(I18nManager.getText("button.ok")); + _okButton = new JButton(I18nManager.getText("button.ok")); ActionListener okListener = new ActionListener() { public void actionPerformed(ActionEvent e) { finish(); } }; - okButton.addActionListener(okListener); - buttonPanel.add(okButton); + _okButton.addActionListener(okListener); + _okButton.setEnabled(false); + buttonPanel.add(_okButton); JButton cancelButton = new JButton(I18nManager.getText("button.cancel")); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) @@ -141,6 +172,17 @@ public class TimeOffsetDialog return label; } + /** + * @return the number of offset seconds entered by the user + */ + private long getOffsetSecs() + { + long offsetSecs = _minuteField.getValue() * 60L + + _hourField.getValue() * 60L * 60L + + _dayField.getValue() * 60L * 60L * 24L; + if (_subtractRadio.isSelected()) {offsetSecs = -offsetSecs;} + return offsetSecs; + } /** * Finish the dialog when OK pressed @@ -148,10 +190,7 @@ public class TimeOffsetDialog private void finish() { // Calculate offset to add or subtract - long offsetSecs = _minuteField.getValue() * 60L - + _hourField.getValue() * 60L * 60L - + _dayField.getValue() * 60L * 60L * 24L; - if (_subtractRadio.isSelected()) {offsetSecs = -offsetSecs;} + long offsetSecs = getOffsetSecs(); if (offsetSecs != 0L) { // Pass offset back to app and close dialog