View Javadoc
1   /**
2    * SpamSchutz - simple way to protect your mail adresses from naiive spammers
3    * Copyright (C) 2011, Aiki IT
4    * <p/>
5    * This program is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * <p/>
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * <p/>
15   * You should have received a copy of the GNU General Public License
16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
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   * This class contains the main UI of this application (both input windows
32   * and all command buttons).
33   * Created by IntelliJ IDEA. User: hirsch Date: Apr 10, 2003 Time: 3:12:32 PM
34   */
35  class GUI extends JPanel {
36  
37      /**
38       * Constant to define the dimension of a text box.
39       */
40      private static final Dimension BOX_DIMENSION = new Dimension(300, 200);
41  
42      @VisibleForTesting
43      static final LocalDate now = LocalDate.now();
44  
45      /**
46       * Launches and configures the main UI component, while taking care whether
47       * the application is started as an applet or standalone.
48       *
49       * @param isApplet Defines whether the application is started as standalone
50       *                 {@code false} or applet {@code true}.
51       */
52      GUI(final boolean isApplet) {
53          SwingUtilities.invokeLater(
54                  () -> init(isApplet)
55          );
56      }
57  
58      /**
59       * Internal helper to initialize the UI depending on the surrounding this
60       * application is started.
61       *
62       * @param isApplet Defines whether the application is started as standalone
63       *                 {@code false} or applet {@code true}.
64       */
65      private void init(final boolean isApplet) {
66          // command help
67          final JPanel buttonArea = new JPanel();
68          JButton help = new JButton(getBundleString("spamschutz.ui.help"));
69          help.setMnemonic(getBundleString("spamschutz.ui.help.mnemonic").charAt(0));
70          help.addActionListener(e -> JOptionPane.showMessageDialog(null, getParameterizedBundleString("spamschutz.ui.help.text",
71                  String.valueOf(now.getYear()),
72                  de.aikiit.spamprotector.util.Version.VERSION),
73                  getBundleString("spamschutz.ui.help.title"),
74                  JOptionPane.INFORMATION_MESSAGE));
75  
76          final JTextField input = new JTextField();
77          input.setSize(BOX_DIMENSION);
78          input.setPreferredSize(BOX_DIMENSION);
79          final JTextField output = new JTextField(getBundleString("spamschutz.ui.default.output"));
80          output.setSize(BOX_DIMENSION);
81          output.setPreferredSize(BOX_DIMENSION);
82  
83          // read input field
84          final JButton start = new JButton(getBundleString("spamschutz.ui.button.rtl"));
85          start.setMnemonic(getBundleString("spamschutz.ui.button.rtl.mnemonic").charAt(0));
86          start.addActionListener(e -> output.setText(SpamProtector.toEncoded(input.getText())));
87  
88          // read output field
89          final JButton revert = new JButton(getBundleString("spamschutz.ui.button.ltr"));
90          revert.setMnemonic(getBundleString("spamschutz.ui.button.ltr.mnemonic").charAt(0));
91          revert.addActionListener(e -> input.setText(SpamProtector.toPlain(output.getText())));
92  
93          final JButton reset = new JButton(getBundleString("spamschutz.ui.reset"));
94          reset.setMnemonic(getBundleString("spamschutz.ui.reset.mnemonic").charAt(0));
95          reset.addActionListener(e -> {
96              input.setText("");
97              output.setText("");
98          });
99  
100         buttonArea.add(revert);
101         buttonArea.add(start);
102         buttonArea.add(reset);
103 
104         // fields
105         final JPanel ioArea = new JPanel(new FlowLayout());
106 
107         final JLabel inputLabel = new JLabel(getBundleString("spamschutz.ui.input"));
108         inputLabel.setDisplayedMnemonic(getBundleString("spamschutz.ui.input.mnemonic").charAt(0));
109         inputLabel.setLabelFor(input);
110         ioArea.add(inputLabel);
111         ioArea.add(input);
112 
113         final JLabel outputLabel = new JLabel(getBundleString("spamschutz.ui.output"));
114         outputLabel.setDisplayedMnemonic(getBundleString("spamschutz.ui.output.mnemonic").charAt(0));
115         outputLabel.setLabelFor(output);
116         ioArea.add(outputLabel);
117         ioArea.add(output);
118 
119         // window layout
120         this.setLayout(new BorderLayout());
121         this.add(buttonArea, BorderLayout.NORTH);
122         this.add(ioArea, BorderLayout.CENTER);
123 
124         // if started standalone we do need a quit-button
125         if (!isApplet) {
126             final JButton end = new JButton(getBundleString("spamschutz.ui.end"));
127             end.setMnemonic(getBundleString("spamschutz.ui.end.mnemonic").charAt(0));
128             end.addActionListener(e -> System.exit(0));
129             buttonArea.add(end);
130         }
131         buttonArea.add(help);
132         this.setVisible(true);
133     }
134 }