1 | |
package net.mtu.eggplant.app; |
2 | |
|
3 | |
import java.awt.BorderLayout; |
4 | |
import java.awt.event.ActionEvent; |
5 | |
import java.awt.event.ActionListener; |
6 | |
import java.text.DateFormat; |
7 | |
import java.text.ParseException; |
8 | |
import java.text.SimpleDateFormat; |
9 | |
import java.util.Date; |
10 | |
|
11 | |
import javax.swing.JPanel; |
12 | |
import javax.swing.JTextField; |
13 | |
|
14 | |
import net.mtu.eggplant.util.gui.GraphicsUtils; |
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
public class TimeConvert extends JPanel { |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
public static void main(final String[] args) { |
25 | 0 | TimeConvert tc = new TimeConvert(); |
26 | 0 | GraphicsUtils.basicGUIMain(tc, false, "Time Convert"); |
27 | 0 | } |
28 | |
|
29 | 0 | public TimeConvert() { |
30 | 0 | setLayout(new BorderLayout()); |
31 | |
|
32 | 0 | final SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS zzz"); |
33 | 0 | final JTextField time = new JTextField("MM/dd/yyyy HH:mm:ss.SSS zzz"); |
34 | |
|
35 | 0 | final JTextField number = new JTextField(); |
36 | 0 | number.addActionListener(new NumberListener(time, number, format)); |
37 | |
|
38 | 0 | time.addActionListener(new TimeListener(time, number, format)); |
39 | |
|
40 | 0 | add(number, BorderLayout.NORTH); |
41 | 0 | add(time, BorderLayout.CENTER); |
42 | 0 | } |
43 | |
|
44 | |
private static class BaseListener { |
45 | |
protected final JTextField mTime; |
46 | |
|
47 | |
protected final JTextField mNumber; |
48 | |
|
49 | |
protected final DateFormat mFormat; |
50 | |
|
51 | |
public BaseListener(final JTextField time, |
52 | |
final JTextField number, |
53 | 0 | final DateFormat format) { |
54 | 0 | mTime = time; |
55 | 0 | mNumber = number; |
56 | 0 | mFormat = format; |
57 | 0 | } |
58 | |
} |
59 | |
|
60 | |
private static class NumberListener extends BaseListener implements ActionListener { |
61 | |
public NumberListener(final JTextField time, |
62 | |
final JTextField number, |
63 | |
final DateFormat format) { |
64 | 0 | super(time, number, format); |
65 | 0 | } |
66 | |
|
67 | |
public void actionPerformed(final ActionEvent ae) { |
68 | 0 | final Date d = new Date(Long.parseLong(mNumber.getText())); |
69 | 0 | mTime.setText(mFormat.format(d)); |
70 | 0 | } |
71 | |
} |
72 | |
|
73 | |
private static class TimeListener extends BaseListener implements ActionListener { |
74 | |
public TimeListener(final JTextField time, |
75 | |
final JTextField number, |
76 | |
final DateFormat format) { |
77 | 0 | super(time, number, format); |
78 | 0 | } |
79 | |
|
80 | |
public void actionPerformed(final ActionEvent ae) { |
81 | |
try { |
82 | 0 | mNumber.setText(String.valueOf(mFormat.parse(mTime.getText()).getTime())); |
83 | 0 | } catch (final ParseException pe) { |
84 | 0 | mNumber.setText("-1"); |
85 | 0 | } |
86 | 0 | } |
87 | |
} |
88 | |
|
89 | |
} |