add configuration dialog
[jSite2.git] / src / net / pterodactylus / jsite / gui / ConfigurationDialog.java
1 /**
2  * © 2008 INA Service GmbH
3  */
4 package net.pterodactylus.jsite.gui;
5
6 import java.awt.BorderLayout;
7 import java.awt.FlowLayout;
8 import java.awt.event.ActionEvent;
9
10 import javax.swing.BorderFactory;
11 import javax.swing.JButton;
12 import javax.swing.JCheckBox;
13 import javax.swing.JComponent;
14 import javax.swing.JDialog;
15 import javax.swing.JPanel;
16 import javax.swing.JTabbedPane;
17 import javax.swing.SwingConstants;
18
19 import net.pterodactylus.jsite.i18n.I18n;
20 import net.pterodactylus.jsite.i18n.I18nable;
21 import net.pterodactylus.jsite.i18n.gui.I18nAction;
22 import net.pterodactylus.util.swing.SwingUtils;
23
24 /**
25  * The configuration dialog.
26  * 
27  * @author <a href="mailto:dr@ina-germany.de">David Roden</a>
28  * @version $Id$
29  */
30 public class ConfigurationDialog extends JDialog implements I18nable {
31
32         /** The “okay” action. */
33         private I18nAction okayAction;
34
35         /** The “cancel” action. */
36         private I18nAction cancelAction;
37
38         /** The “beautify GUI” action. */
39         private I18nAction beautifyAction;
40
41         /** The “beautify” checkbox. */
42         private JCheckBox beautifyCheckBox;
43
44         /** Whether the dialog was cancelled. */
45         private boolean cancelled;
46
47         /**
48          * Creates a new configuration dialog.
49          * 
50          * @param swingInterface
51          *            The Swing interface
52          */
53         public ConfigurationDialog(SwingInterface swingInterface) {
54                 super(swingInterface.getMainWindow(), I18n.get("configurationDialog.title"), true);
55                 initActions();
56                 initComponents();
57                 pack();
58                 SwingUtils.center(this);
59                 I18n.registerI18nable(this);
60         }
61
62         //
63         // ACCESSORS
64         //
65
66         /**
67          * Returns whether the dialog was cancelled or confirmed. If the dialog was
68          * cancelled, no further processing should be done.
69          * 
70          * @return <code>true</code> if the dialog was cancelled,
71          *         <code>false</code> otherwise
72          */
73         public boolean wasCancelled() {
74                 return cancelled;
75         }
76
77         /**
78          * Returns whether the “beautify” checkbox has been selected. The result of
79          * this method should not be used if {@link #wasCancelled()} returned
80          * <code>true</code>!
81          * 
82          * @return <code>true</code> if the checkbox was selected,
83          *         <code>false</code> otherwise
84          */
85         public boolean getBeautify() {
86                 return beautifyCheckBox.isSelected();
87         }
88
89         /**
90          * Sets the state of the “beautify” checkbox.
91          * 
92          * @param beautify
93          *            The state of the checkbox
94          */
95         public void setBeautify(boolean beautify) {
96                 beautifyCheckBox.setSelected(beautify);
97         }
98
99         //
100         // PRIVATE METHODS
101         //
102
103         /**
104          * Creates all actions.
105          */
106         private void initActions() {
107                 okayAction = new I18nAction("general.button.okay") {
108                         /**
109                          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
110                          */
111                         @SuppressWarnings("synthetic-access")
112                         public void actionPerformed(ActionEvent actionEvent) {
113                                 actionOkay();
114                         }
115                 };
116                 cancelAction = new I18nAction("general.button.cancel") {
117                         /**
118                          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
119                          */
120                         @SuppressWarnings("synthetic-access")
121                         public void actionPerformed(ActionEvent actionEvent) {
122                                 actionCancel();
123                         }
124                 };
125                 beautifyAction = new I18nAction("configurationDialog.page.interface.item.beautify") {
126                         /**
127                          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
128                          */
129                         public void actionPerformed(ActionEvent actionEvent) {
130                                 /* don't do anything. */
131                         }
132                 };
133         }
134
135         /**
136          * Creates all internal components.
137          */
138         private void initComponents() {
139                 JPanel contentPane = new JPanel(new BorderLayout(12, 12));
140                 contentPane.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
141
142                 JTabbedPane tabbedPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
143                 contentPane.add(tabbedPane, BorderLayout.CENTER);
144
145                 JComponent interfaceConfig = createInterfaceConfig();
146                 tabbedPane.add("Swing Interface", interfaceConfig);
147
148                 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
149                 contentPane.add(buttonPanel, BorderLayout.PAGE_END);
150                 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
151                 buttonPanel.add(new JButton(cancelAction));
152                 JButton okayButton = new JButton(okayAction);
153                 buttonPanel.add(okayButton);
154                 getRootPane().setDefaultButton(okayButton);
155
156                 getContentPane().add(contentPane, BorderLayout.CENTER);
157         }
158
159         /**
160          * Creates the panel for the interface configuration.
161          * 
162          * @return The interface configuration panel
163          */
164         private JComponent createInterfaceConfig() {
165                 JPanel interfaceConfigPanel = new JPanel(new BorderLayout(12, 12));
166                 interfaceConfigPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
167
168                 beautifyCheckBox = new JCheckBox(beautifyAction);
169                 interfaceConfigPanel.add(beautifyCheckBox, BorderLayout.PAGE_START);
170
171                 return interfaceConfigPanel;
172         }
173
174         //
175         // PRIVATE ACTIONS
176         //
177
178         /**
179          * Called when the “okay” button is clicked.
180          */
181         private void actionOkay() {
182                 cancelled = false;
183                 setVisible(false);
184         }
185
186         /**
187          * Called when the “cancel” button is clicked.
188          */
189         private void actionCancel() {
190                 cancelled = true;
191                 setVisible(false);
192         }
193
194         //
195         // INTERFACE I18nable
196         //
197
198         /**
199          * @see net.pterodactylus.jsite.i18n.I18nable#updateI18n()
200          */
201         public void updateI18n() {
202                 okayAction.updateI18n();
203                 cancelAction.updateI18n();
204                 beautifyAction.updateI18n();
205                 SwingUtils.repackCentered(this);
206         }
207
208 }