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;
10 import javax.swing.BorderFactory;
11 import javax.swing.BoxLayout;
12 import javax.swing.ButtonGroup;
13 import javax.swing.JButton;
14 import javax.swing.JDialog;
15 import javax.swing.JFrame;
16 import javax.swing.JLabel;
17 import javax.swing.JPanel;
18 import javax.swing.JRadioButton;
19 import javax.swing.SwingConstants;
22 import tim.prune.I18nManager;
25 * Class to show a dialog for adding a time offset to a track range
27 public class TimeOffsetDialog
29 private App _app = null;
30 private JFrame _parentFrame = null;
31 private JDialog _dialog = null;
32 private JRadioButton _addRadio = null, _subtractRadio = null;
33 private WholeNumberField _dayField = null, _hourField = null;
34 private WholeNumberField _minuteField = null;
39 * @param inApp application object for callback
40 * @param inParentFrame parent frame
42 public TimeOffsetDialog(App inApp, JFrame inParentFrame)
45 _parentFrame = inParentFrame;
50 * Show the dialog to select options and export file
52 public void showDialog()
57 _dialog = new JDialog(_parentFrame, I18nManager.getText("dialog.addtimeoffset.title"), true);
58 _dialog.setLocationRelativeTo(_parentFrame);
59 _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
60 _dialog.getContentPane().add(makeDialogComponents());
68 * Create dialog components
69 * @return Panel containing all gui elements in dialog
71 private Component makeDialogComponents()
73 JPanel dialogPanel = new JPanel();
74 dialogPanel.setLayout(new BorderLayout());
75 JPanel mainPanel = new JPanel();
76 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
77 // Make a panel for the two radio buttons
78 JPanel radioPanel = new JPanel();
79 _addRadio = new JRadioButton(I18nManager.getText("dialog.addtimeoffset.add"));
80 _addRadio.setSelected(true);
81 radioPanel.add(_addRadio);
82 _subtractRadio = new JRadioButton(I18nManager.getText("dialog.addtimeoffset.subtract"));
83 _subtractRadio.setSelected(false);
84 radioPanel.add(_subtractRadio);
85 ButtonGroup radioGroup = new ButtonGroup();
86 radioGroup.add(_addRadio);
87 radioGroup.add(_subtractRadio);
88 mainPanel.add(radioPanel);
90 // Make a listener to validate the text boxes during typing (to en/disable OK button)
92 // Make a central panel with the text boxes
93 JPanel descPanel = new JPanel();
94 descPanel.setLayout(new GridLayout(0, 2));
95 descPanel.add(makeRightLabel("dialog.addtimeoffset.days"));
96 _dayField = new WholeNumberField(3);
97 descPanel.add(_dayField);
98 descPanel.add(makeRightLabel("dialog.addtimeoffset.hours"));
99 _hourField = new WholeNumberField(3);
100 descPanel.add(_hourField);
101 descPanel.add(makeRightLabel("dialog.addtimeoffset.minutes"));
102 _minuteField = new WholeNumberField(3);
103 descPanel.add(_minuteField);
104 mainPanel.add(descPanel);
105 dialogPanel.add(mainPanel, BorderLayout.CENTER);
107 // button panel at bottom
108 JPanel buttonPanel = new JPanel();
109 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
110 JButton okButton = new JButton(I18nManager.getText("button.ok"));
111 ActionListener okListener = new ActionListener() {
112 public void actionPerformed(ActionEvent e)
117 okButton.addActionListener(okListener);
118 buttonPanel.add(okButton);
119 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
120 cancelButton.addActionListener(new ActionListener() {
121 public void actionPerformed(ActionEvent e)
126 buttonPanel.add(cancelButton);
127 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
128 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
134 * @param inKey text key
135 * @return right-aligned label
137 private static final JLabel makeRightLabel(String inKey)
139 JLabel label = new JLabel(I18nManager.getText(inKey) + " : ");
140 label.setHorizontalAlignment(SwingConstants.RIGHT);
146 * Finish the dialog when OK pressed
148 private void finish()
150 // Calculate offset to add or subtract
151 long offsetSecs = _minuteField.getValue() * 60L
152 + _hourField.getValue() * 60L * 60L
153 + _dayField.getValue() * 60L * 60L * 24L;
154 if (_subtractRadio.isSelected()) {offsetSecs = -offsetSecs;}
155 if (offsetSecs != 0L)
157 // Pass offset back to app and close dialog
158 _app.finishAddTimeOffset(offsetSecs);