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