rename configuration tab
[jSite2.git] / src / net / pterodactylus / jsite / gui / ConfigurationDialog.java
1 /**
2  * © 2008 INA Service GmbH
3  */
4
5 package net.pterodactylus.jsite.gui;
6
7 import java.awt.BorderLayout;
8 import java.awt.FlowLayout;
9 import java.awt.GridBagConstraints;
10 import java.awt.GridBagLayout;
11 import java.awt.Insets;
12 import java.awt.event.ActionEvent;
13
14 import javax.swing.BorderFactory;
15 import javax.swing.JButton;
16 import javax.swing.JCheckBox;
17 import javax.swing.JComponent;
18 import javax.swing.JDialog;
19 import javax.swing.JPanel;
20 import javax.swing.JTabbedPane;
21 import javax.swing.SwingConstants;
22
23 import net.pterodactylus.jsite.i18n.I18n;
24 import net.pterodactylus.jsite.i18n.I18nable;
25 import net.pterodactylus.jsite.i18n.gui.I18nAction;
26 import net.pterodactylus.util.swing.SwingUtils;
27
28 /**
29  * The configuration dialog.
30  *
31  * @author <a href="mailto:dr@ina-germany.de">David Roden</a>
32  * @version $Id$
33  */
34 public class ConfigurationDialog extends JDialog implements I18nable {
35
36         /** The “okay” action. */
37         private I18nAction okayAction;
38
39         /** The “cancel” action. */
40         private I18nAction cancelAction;
41
42         /** The “beautify GUI” action. */
43         private I18nAction antialiasAction;
44
45         /** The “use custom control font” action. */
46         private I18nAction useCustomControlFontAction;
47
48         /** The “use custom user font” action. */
49         private I18nAction useCustomUserFontAction;
50
51         /** The “beautify” checkbox. */
52         private JCheckBox antialiasCheckBox;
53
54         /** The “use custom” fonts checkbox. */
55         private JCheckBox useCustomControlFontCheckBox;
56
57         /** The control font list. */
58         private FontComboBox controlFontList;
59
60         /** The checkbox for “use same as control font”. */
61         private JCheckBox useCustomUserFontCheckBox;
62
63         /** The user font list. */
64         private FontComboBox userFontList;
65
66         /** Whether the dialog was cancelled. */
67         private boolean cancelled;
68
69         /**
70          * Creates a new configuration dialog.
71          *
72          * @param swingInterface
73          *            The Swing interface
74          */
75         public ConfigurationDialog(SwingInterface swingInterface) {
76                 super(swingInterface.getMainWindow(), I18n.get("configurationDialog.title"), true);
77                 initActions();
78                 initComponents();
79                 pack();
80                 SwingUtils.center(this);
81                 I18n.registerI18nable(this);
82         }
83
84         //
85         // ACCESSORS
86         //
87
88         /**
89          * Returns whether the dialog was cancelled or confirmed. If the dialog was
90          * cancelled, no further processing should be done.
91          *
92          * @return <code>true</code> if the dialog was cancelled,
93          *         <code>false</code> otherwise
94          */
95         public boolean wasCancelled() {
96                 return cancelled;
97         }
98
99         /**
100          * Returns whether the “beautify” checkbox has been selected. The result of
101          * this method should not be used if {@link #wasCancelled()} returned
102          * <code>true</code>!
103          *
104          * @return <code>true</code> if the checkbox was selected,
105          *         <code>false</code> otherwise
106          */
107         public boolean isAntialias() {
108                 return antialiasCheckBox.isSelected();
109         }
110
111         /**
112          * Sets the state of the “antialias” checkbox.
113          *
114          * @param antialias
115          *            The state of the checkbox
116          */
117         public void setAntialias(boolean antialias) {
118                 antialiasCheckBox.setSelected(antialias);
119         }
120
121         /**
122          * Returns the font for the controls.
123          *
124          * @return The control font, or <code>null</code> if no custom control
125          *         font is to be used
126          */
127         public String getControlFont() {
128                 return (String) (useCustomControlFontCheckBox.isSelected() ? controlFontList.getSelectedItem() : null);
129         }
130
131         /**
132          * Sets the font for the controls.
133          *
134          * @param controlFont
135          *            The control font, or <code>null</code> if no custom control
136          *            font is to be used
137          */
138         public void setControlFont(String controlFont) {
139                 useCustomControlFontCheckBox.setSelected(controlFont != null);
140                 controlFontList.setEnabled(controlFont != null);
141                 controlFontList.setSelectedItem(controlFont);
142         }
143
144         /**
145          * Returns the font for user input.
146          *
147          * @return The font for user input, or <code>null</code> if no custom user
148          *         input font is to be used
149          */
150         public String getUserFont() {
151                 return (String) (useCustomUserFontCheckBox.isSelected() ? userFontList.getSelectedItem() : null);
152         }
153
154         /**
155          * Sets the font for user input.
156          *
157          * @param userFont
158          *            The font for user input, or <code>null</code> if no custom
159          *            user input font is to be used
160          */
161         public void setUserFont(String userFont) {
162                 useCustomUserFontCheckBox.setSelected(userFont != null);
163                 userFontList.setEnabled(userFont != null);
164                 userFontList.setSelectedItem(userFont);
165         }
166
167         //
168         // PRIVATE METHODS
169         //
170
171         /**
172          * Creates all actions.
173          */
174         private void initActions() {
175                 okayAction = new I18nAction("general.button.okay") {
176
177                         /**
178                          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
179                          */
180                         @SuppressWarnings("synthetic-access")
181                         public void actionPerformed(ActionEvent actionEvent) {
182                                 actionOkay();
183                         }
184                 };
185                 cancelAction = new I18nAction("general.button.cancel") {
186
187                         /**
188                          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
189                          */
190                         @SuppressWarnings("synthetic-access")
191                         public void actionPerformed(ActionEvent actionEvent) {
192                                 actionCancel();
193                         }
194                 };
195                 antialiasAction = new I18nAction("configurationDialog.page.interface.item.beautify") {
196
197                         /**
198                          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
199                          */
200                         public void actionPerformed(ActionEvent actionEvent) {
201                                 /* do nothing. */
202                         }
203                 };
204                 useCustomControlFontAction = new I18nAction("configurationDialog.page.interface.item.useCustomControlFont") {
205
206                         /**
207                          * {@inheritDoc}
208                          */
209                         @SuppressWarnings("synthetic-access")
210                         public void actionPerformed(ActionEvent e) {
211                                 controlFontList.setEnabled(useCustomControlFontCheckBox.isSelected());
212                         }
213                 };
214                 useCustomUserFontAction = new I18nAction("configurationDialog.page.interface.item.useCustomUserFont") {
215
216                         /**
217                          * {@inheritDoc}
218                          */
219                         @SuppressWarnings("synthetic-access")
220                         public void actionPerformed(ActionEvent e) {
221                                 userFontList.setEnabled(useCustomUserFontCheckBox.isSelected());
222                         }
223                 };
224         }
225
226         /**
227          * Creates all internal components.
228          */
229         private void initComponents() {
230                 JPanel contentPane = new JPanel(new BorderLayout(12, 12));
231                 contentPane.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
232
233                 JTabbedPane tabbedPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
234                 contentPane.add(tabbedPane, BorderLayout.CENTER);
235
236                 JComponent interfaceConfig = createInterfaceConfig();
237                 tabbedPane.add(I18n.get("configurationDialog.page.interface.name"), interfaceConfig);
238
239                 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
240                 contentPane.add(buttonPanel, BorderLayout.PAGE_END);
241                 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
242                 buttonPanel.add(new JButton(cancelAction));
243                 JButton okayButton = new JButton(okayAction);
244                 buttonPanel.add(okayButton);
245                 getRootPane().setDefaultButton(okayButton);
246
247                 getContentPane().add(contentPane, BorderLayout.CENTER);
248         }
249
250         /**
251          * Creates the panel for the interface configuration.
252          *
253          * @return The interface configuration panel
254          */
255         private JComponent createInterfaceConfig() {
256                 JPanel interfaceConfigPanel = new JPanel(new GridBagLayout());
257                 interfaceConfigPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
258
259                 antialiasCheckBox = new JCheckBox(antialiasAction);
260                 interfaceConfigPanel.add(antialiasCheckBox, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
261
262                 useCustomControlFontCheckBox = new JCheckBox(useCustomControlFontAction);
263                 interfaceConfigPanel.add(useCustomControlFontCheckBox, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(6, 0, 0, 0), 0, 0));
264
265                 controlFontList = new FontComboBox();
266                 interfaceConfigPanel.add(controlFontList, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(6, 6, 0, 0), 0, 0));
267
268                 useCustomUserFontCheckBox = new JCheckBox(useCustomUserFontAction);
269                 interfaceConfigPanel.add(useCustomUserFontCheckBox, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 0, 0, 0), 0, 0));
270
271                 userFontList = new FontComboBox();
272                 interfaceConfigPanel.add(userFontList, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(6, 6, 0, 0), 0, 0));
273
274                 interfaceConfigPanel.add(new JPanel(), new GridBagConstraints(0, 3, 2, 1, 1.0, 1.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
275
276                 return interfaceConfigPanel;
277         }
278
279         //
280         // PRIVATE ACTIONS
281         //
282
283         /**
284          * Called when the “okay” button is clicked.
285          */
286         private void actionOkay() {
287                 cancelled = false;
288                 setVisible(false);
289         }
290
291         /**
292          * Called when the “cancel” button is clicked.
293          */
294         private void actionCancel() {
295                 cancelled = true;
296                 setVisible(false);
297         }
298
299         //
300         // INTERFACE I18nable
301         //
302
303         /**
304          * @see net.pterodactylus.jsite.i18n.I18nable#updateI18n()
305          */
306         public void updateI18n() {
307                 okayAction.updateI18n();
308                 cancelAction.updateI18n();
309                 antialiasAction.updateI18n();
310                 useCustomControlFontAction.updateI18n();
311                 useCustomUserFontAction.updateI18n();
312                 SwingUtils.repackCentered(this);
313         }
314
315 }