]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/TimeOffsetDialog.java
Version 6, October 2008
[GpsPrune.git] / tim / prune / gui / TimeOffsetDialog.java
1 package tim.prune.gui;
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
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;
20
21 import tim.prune.App;
22 import tim.prune.I18nManager;
23
24 /**
25  * Class to show a dialog for adding a time offset to a track range
26  */
27 public class TimeOffsetDialog
28 {
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;
35
36
37         /**
38          * Constructor
39          * @param inApp application object for callback
40          * @param inParentFrame parent frame
41          */
42         public TimeOffsetDialog(App inApp, JFrame inParentFrame)
43         {
44                 _app = inApp;
45                 _parentFrame = inParentFrame;
46         }
47
48
49         /**
50          * Show the dialog to select options and export file
51          */
52         public void showDialog()
53         {
54                 // Make dialog window
55                 if (_dialog == null)
56                 {
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());
61                         _dialog.pack();
62                 }
63                 _dialog.show();
64         }
65
66
67         /**
68          * Create dialog components
69          * @return Panel containing all gui elements in dialog
70          */
71         private Component makeDialogComponents()
72         {
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);
89
90                 // Make a listener to validate the text boxes during typing (to en/disable OK button)
91
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);
106
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)
113                         {
114                                 finish();
115                         }
116                 };
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)
122                         {
123                                 _dialog.dispose();
124                         }
125                 });
126                 buttonPanel.add(cancelButton);
127                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
128                 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
129                 return dialogPanel;
130         }
131
132
133         /**
134          * @param inKey text key
135          * @return right-aligned label
136          */
137         private static final JLabel makeRightLabel(String inKey)
138         {
139                 JLabel label = new JLabel(I18nManager.getText(inKey) + " : ");
140                 label.setHorizontalAlignment(SwingConstants.RIGHT);
141                 return label;
142         }
143
144
145         /**
146          * Finish the dialog when OK pressed
147          */
148         private void finish()
149         {
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)
156                 {
157                         // Pass offset back to app and close dialog
158                         _app.finishAddTimeOffset(offsetSecs);
159                         _dialog.dispose();
160                 }
161         }
162 }