1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package de.aikiit.spamprotector;
19
20 import com.google.common.annotations.VisibleForTesting;
21 import de.aikiit.spamprotector.converter.SpamProtector;
22
23 import javax.swing.JButton;
24 import javax.swing.JLabel;
25 import javax.swing.JOptionPane;
26 import javax.swing.JPanel;
27 import javax.swing.JTextField;
28 import javax.swing.SwingUtilities;
29 import java.awt.BorderLayout;
30 import java.awt.Dimension;
31 import java.awt.FlowLayout;
32 import java.time.LocalDate;
33
34 import static de.aikiit.spamprotector.util.LocalizationHelper.getBundleString;
35 import static de.aikiit.spamprotector.util.LocalizationHelper.getParameterizedBundleString;
36
37
38
39
40
41 class GUI extends JPanel {
42
43
44
45
46 private static final Dimension BOX_DIMENSION = new Dimension(300, 200);
47
48 @VisibleForTesting
49 static final LocalDate now = LocalDate.now();
50
51
52
53
54 GUI() {
55 SwingUtilities.invokeLater(this::init);
56 }
57
58
59
60
61
62 private void init() {
63
64 final JPanel buttonArea = new JPanel();
65 JButton help = new JButton(getBundleString("spamschutz.ui.help"));
66 help.setMnemonic(getBundleString("spamschutz.ui.help.mnemonic").charAt(0));
67 help.addActionListener(e -> JOptionPane.showMessageDialog(null, getParameterizedBundleString("spamschutz.ui.help.text",
68 String.valueOf(now.getYear()),
69 de.aikiit.spamprotector.util.Version.VERSION),
70 getBundleString("spamschutz.ui.help.title"),
71 JOptionPane.INFORMATION_MESSAGE));
72
73 final JTextField input = new JTextField();
74 input.setSize(BOX_DIMENSION);
75 input.setPreferredSize(BOX_DIMENSION);
76 final JTextField output = new JTextField(getBundleString("spamschutz.ui.default.output"));
77 output.setSize(BOX_DIMENSION);
78 output.setPreferredSize(BOX_DIMENSION);
79
80
81 final JButton start = new JButton(getBundleString("spamschutz.ui.button.rtl"));
82 start.setMnemonic(getBundleString("spamschutz.ui.button.rtl.mnemonic").charAt(0));
83 start.addActionListener(e -> output.setText(SpamProtector.toEncoded(input.getText())));
84
85
86 final JButton revert = new JButton(getBundleString("spamschutz.ui.button.ltr"));
87 revert.setMnemonic(getBundleString("spamschutz.ui.button.ltr.mnemonic").charAt(0));
88 revert.addActionListener(e -> input.setText(SpamProtector.toPlain(output.getText())));
89
90 final JButton reset = new JButton(getBundleString("spamschutz.ui.reset"));
91 reset.setMnemonic(getBundleString("spamschutz.ui.reset.mnemonic").charAt(0));
92 reset.addActionListener(e -> {
93 input.setText("");
94 output.setText("");
95 });
96
97 buttonArea.add(revert);
98 buttonArea.add(start);
99 buttonArea.add(reset);
100
101
102 final JPanel ioArea = new JPanel(new FlowLayout());
103
104 final JLabel inputLabel = new JLabel(getBundleString("spamschutz.ui.input"));
105 inputLabel.setDisplayedMnemonic(getBundleString("spamschutz.ui.input.mnemonic").charAt(0));
106 inputLabel.setLabelFor(input);
107 ioArea.add(inputLabel);
108 ioArea.add(input);
109
110 final JLabel outputLabel = new JLabel(getBundleString("spamschutz.ui.output"));
111 outputLabel.setDisplayedMnemonic(getBundleString("spamschutz.ui.output.mnemonic").charAt(0));
112 outputLabel.setLabelFor(output);
113 ioArea.add(outputLabel);
114 ioArea.add(output);
115
116
117 this.setLayout(new BorderLayout());
118 this.add(buttonArea, BorderLayout.NORTH);
119 this.add(ioArea, BorderLayout.CENTER);
120
121 final JButton end = new JButton(getBundleString("spamschutz.ui.end"));
122 end.setMnemonic(getBundleString("spamschutz.ui.end.mnemonic").charAt(0));
123 end.addActionListener(e -> System.exit(0));
124 buttonArea.add(end);
125 buttonArea.add(help);
126 this.setVisible(true);
127 }
128 }