View Javadoc
1   package edu.jiangxin.apktoolbox.convert.time;
2   
3   import edu.jiangxin.apktoolbox.swing.extend.EasyPanel;
4   import edu.jiangxin.apktoolbox.utils.Constants;
5   import edu.jiangxin.apktoolbox.utils.DateUtils;
6   
7   import javax.swing.*;
8   import javax.swing.text.DateFormatter;
9   import java.awt.*;
10  import java.awt.event.ActionEvent;
11  import java.awt.event.ActionListener;
12  import java.text.SimpleDateFormat;
13  import java.util.Arrays;
14  import java.util.Calendar;
15  import java.util.Date;
16  import java.util.TimeZone;
17  
18  import static java.util.Calendar.*;
19  
20  public class TimeConvertPanel extends EasyPanel {
21  
22      private JSpinner timestamp2Spinner;
23  
24      private JComboBox<Object> timestampComboBox;
25  
26      private JTextField timeTextField;
27  
28      private JTextField currentTimestampTextField;
29  
30      private JTextField currentTimeTextField;
31  
32      private JButton pauseButton;
33  
34      private boolean isPaused;
35  
36      private JFormattedTextField inTimeTextField;
37      private JComboBox inTimezoneComboBox;
38      private JComboBox outTimezoneComboBox;
39      private JTextField outTimeTextField;
40  
41      private static final String SDF_PATTERN = "yyyy-MM-dd HH:mm";
42      private final SimpleDateFormat SDF = new SimpleDateFormat(SDF_PATTERN);
43      private final DateFormatter dtFormatter = new DateFormatter(SDF);
44  
45      final static String[] sortedTimeZones;
46  
47      static {
48          String[] t = TimeZone.getAvailableIDs();
49          sortedTimeZones = Arrays.copyOf(t, t.length);
50          Arrays.sort(sortedTimeZones, String.CASE_INSENSITIVE_ORDER);
51      }
52  
53      public TimeConvertPanel() throws HeadlessException {
54          super();
55      }
56  
57      @Override
58      public void initUI() {
59          BoxLayout boxLayout = new BoxLayout(this, BoxLayout.Y_AXIS);
60          setLayout(boxLayout);
61  
62          createConvertPanel();
63          add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
64  
65          createConvertButtonsPanel();
66          add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER * 3));
67  
68          createCurrentPanel();
69          add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER * 3));
70  
71          createCommentPanel();
72          add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER * 3));
73  
74          createTimezonePanel();
75      }
76  
77      private void createConvertPanel() {
78          JPanel convertPanel = new JPanel();
79          convertPanel.setLayout(new BoxLayout(convertPanel, BoxLayout.X_AXIS));
80          add(convertPanel);
81  
82          JPanel timestampPanel = new JPanel();
83          timestampPanel.setLayout(new BoxLayout(timestampPanel, BoxLayout.X_AXIS));
84  
85          JPanel timePanel = new JPanel();
86          timePanel.setLayout(new BoxLayout(timePanel, BoxLayout.X_AXIS));
87  
88          convertPanel.add(timestampPanel);
89          convertPanel.add(Box.createHorizontalStrut(40));
90          convertPanel.add(timePanel);
91  
92          JLabel timestampLabel = new JLabel("TimeStamp: ");
93  
94          timestamp2Spinner = new JSpinner();
95          timestamp2Spinner.setPreferredSize(new Dimension(150, 25));
96          timestamp2Spinner.setModel(new SpinnerNumberModel(Long.valueOf(0L), Long.valueOf(0L), Long.valueOf(Long.MAX_VALUE), Long.valueOf(1L)));
97  
98          timestampComboBox = new JComboBox<>();
99          timestampComboBox.addItem("Second(s)");
100         timestampComboBox.addItem("Millisecond(ms)");
101 
102         timestampPanel.add(timestampLabel);
103         timestampPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
104         timestampPanel.add(timestamp2Spinner);
105         timestampPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
106         timestampPanel.add(timestampComboBox);
107 
108         JLabel timeLabel = new JLabel("Time: ");
109 
110         timeTextField = new JTextField(20);
111 
112         timePanel.add(timeLabel);
113         timePanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
114         timePanel.add(timeTextField);
115     }
116 
117     private void createConvertButtonsPanel() {
118         JPanel convertButtonsPanel = new JPanel();
119         BoxLayout boxLayout = new BoxLayout(convertButtonsPanel, BoxLayout.X_AXIS);
120         convertButtonsPanel.setLayout(boxLayout);
121         add(convertButtonsPanel);
122 
123         JButton timestamp2TimeButton = new JButton();
124         timestamp2TimeButton.setText("Timestamp->Time");
125         timestamp2TimeButton.addActionListener(new Timestamp2TimeButtonActionListener());
126         convertButtonsPanel.add(timestamp2TimeButton);
127 
128         convertButtonsPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
129 
130         JButton time2TimestampButton = new JButton();
131         time2TimestampButton.setText("Time->Timestamp");
132         time2TimestampButton.addActionListener(new Time2TimestampButtonActionListener());
133         convertButtonsPanel.add(time2TimestampButton);
134 
135         convertButtonsPanel.add(Box.createHorizontalGlue());
136     }
137 
138     private void createCurrentPanel() {
139         JPanel currentPanel = new JPanel();
140         BoxLayout boxLayout = new BoxLayout(currentPanel, BoxLayout.X_AXIS);
141         currentPanel.setLayout(boxLayout);
142         add(currentPanel);
143 
144         JLabel currentTimestampLabel = new JLabel("Current timestamp: ");
145         currentPanel.add(currentTimestampLabel);
146         currentPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
147 
148         currentTimestampTextField = new JTextField();
149         currentPanel.add(currentTimestampTextField);
150         currentPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
151 
152         JLabel currentTimeTitleLabel = new JLabel("Current time: ");
153         currentPanel.add(currentTimeTitleLabel);
154         currentPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
155 
156         currentTimeTextField = new JTextField();
157         currentPanel.add(currentTimeTextField);
158         currentPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
159 
160         pauseButton = new JButton("Pause");
161         currentPanel.add(pauseButton);
162         pauseButton.addActionListener(e -> {
163             if (isPaused) {
164                 pauseButton.setText("Pause");
165                 isPaused = false;
166             } else {
167                 pauseButton.setText("Resume");
168                 isPaused = true;
169             }
170         });
171 
172         new Thread(() -> {
173             while (true) {
174                 long currentTimeStamp = System.currentTimeMillis() / 1000;
175                 if (!isPaused) {
176                     currentTimestampTextField.setText(String.valueOf(currentTimeStamp));
177                     currentTimeTextField.setText(DateUtils.secondToHumanFormat(currentTimeStamp));
178                 }
179                 try {
180                     Thread.sleep(1000);
181                 } catch (InterruptedException e) {
182                     logger.error("sleep InterruptedException");
183                     Thread.currentThread().interrupt();
184                 }
185             }
186         }).start();
187     }
188 
189     private void createCommentPanel() {
190         JPanel commentPanel = new JPanel();
191         BoxLayout boxLayout = new BoxLayout(commentPanel, BoxLayout.X_AXIS);
192         commentPanel.setLayout(boxLayout);
193         add(commentPanel);
194 
195         JTextArea textArea1 = new JTextArea("1s = 1000ms");
196         textArea1.setEditable(false);
197         JTextArea textArea2 = new JTextArea("1ms = 1000μs");
198         textArea2.setEditable(false);
199         JTextArea textArea3 = new JTextArea("1μs = 1000ns");
200         textArea3.setEditable(false);
201         JTextArea textArea4 = new JTextArea("1ns = 1000ps");
202         textArea4.setEditable(false);
203 
204         commentPanel.add(textArea1);
205         commentPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
206 
207         commentPanel.add(textArea2);
208         commentPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
209 
210         commentPanel.add(textArea3);
211         commentPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
212 
213         commentPanel.add(textArea4);
214         commentPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
215     }
216 
217     private void createTimezonePanel() {
218         JPanel timezonePanel = new JPanel();
219         BoxLayout boxLayout = new BoxLayout(timezonePanel, BoxLayout.X_AXIS);
220         timezonePanel.setLayout(boxLayout);
221         add(timezonePanel);
222 
223         JLabel label1 = new JLabel("From Time: ");
224         label1.setLabelFor(inTimeTextField);
225         timezonePanel.add(label1);
226 
227         inTimeTextField = new JFormattedTextField(dtFormatter);
228         inTimeTextField.setToolTipText("Enter time (" + SDF_PATTERN + "): ");
229         inTimeTextField.setValue(new Date());
230         inTimeTextField.addPropertyChangeListener("value", event -> update());
231 
232         JLabel label2 = new JLabel("From Timezone: ");
233         label2.setLabelFor(inTimezoneComboBox);
234         inTimezoneComboBox = new JComboBox(sortedTimeZones);
235         inTimezoneComboBox.setPreferredSize(new Dimension(70, 0));
236         inTimezoneComboBox.addActionListener(e -> update());
237 
238         JLabel label3 = new JLabel("To Timezone: ");
239         label3.setLabelFor(outTimezoneComboBox);
240         outTimezoneComboBox = new JComboBox(sortedTimeZones);
241         outTimezoneComboBox.setPreferredSize(new Dimension(70, 0));
242         outTimezoneComboBox.addActionListener(e -> update());
243 
244         JLabel label4 = new JLabel("To Time: ");
245         label4.setLabelFor(outTimeTextField);
246         outTimeTextField = new JTextField(10);
247 
248         JButton swapButton = new JButton("Swap");
249         swapButton.addActionListener(e -> {
250             final String src = (String) inTimezoneComboBox.getSelectedItem();
251             inTimezoneComboBox.setSelectedItem(outTimezoneComboBox.getSelectedItem());
252             outTimezoneComboBox.setSelectedItem(src);
253         });
254 
255         timezonePanel.add(label1);
256         timezonePanel.add(inTimeTextField);
257         timezonePanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
258         timezonePanel.add(label2);
259         timezonePanel.add(inTimezoneComboBox);
260         timezonePanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
261         timezonePanel.add(label3);
262         timezonePanel.add(outTimezoneComboBox);
263         timezonePanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
264         timezonePanel.add(label4);
265         timezonePanel.add(outTimeTextField);
266         timezonePanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
267         timezonePanel.add(swapButton);
268     }
269 
270     private final class Timestamp2TimeButtonActionListener implements ActionListener {
271         @Override
272         public void actionPerformed(ActionEvent e) {
273             Long timestamp = (Long) timestamp2Spinner.getValue();
274             Integer index = timestampComboBox.getSelectedIndex();
275             String result = "";
276             if (index.equals(0)) {
277                 result = DateUtils.secondToHumanFormat(timestamp);
278             } else if (index.equals(1)) {
279                 result = DateUtils.millisecondToHumanFormat(timestamp);
280             }
281             timeTextField.setText(result);
282         }
283     }
284 
285     private final class Time2TimestampButtonActionListener implements ActionListener {
286         @Override
287         public void actionPerformed(ActionEvent e) {
288             String string = timeTextField.getText();
289             Integer index = timestampComboBox.getSelectedIndex();
290             Long result = 0L;
291             if (index.equals(0)) {
292                 result = DateUtils.humanFormatToSecond(string);
293             } else if (index.equals(1)) {
294                 result = DateUtils.humanFormatToMillisecond(string);
295             }
296             timestamp2Spinner.setValue(result);
297         }
298     }
299 
300     private static Date getDate(final int hour, final int minute) {
301         Calendar cal = Calendar.getInstance();
302         cal.set(HOUR_OF_DAY, hour);
303         cal.set(MINUTE, minute);
304         return cal.getTime();
305     }
306 
307 
308     private void update() {
309         // Get the timezone objects:
310         final TimeZone sourceTimezone = TimeZone.getTimeZone(
311                 (String) inTimezoneComboBox.getSelectedItem());
312         final TimeZone destTimezone = TimeZone.getTimeZone(
313                 (String) outTimezoneComboBox.getSelectedItem());
314 
315         // Get the entered date:
316         Date sourceDate = (Date) inTimeTextField.getValue();
317 
318         // Compute the source:
319         Calendar localTime = Calendar.getInstance();
320         localTime.setTime(sourceDate);
321         Calendar sourceTime = Calendar.getInstance(sourceTimezone);
322         sourceTime.set(localTime.get(YEAR),
323                 localTime.get(MONTH),
324                 localTime.get(DATE),
325                 localTime.get(HOUR_OF_DAY),
326                 localTime.get(MINUTE));
327 
328         // Destination:
329         SimpleDateFormat sdf = (SimpleDateFormat) SDF.clone();
330         sdf.setTimeZone(destTimezone);
331         outTimeTextField.setText(sdf.format(sourceTime.getTime()));
332     }
333 }