add advanced mode
[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.Font;
10 import java.awt.GridBagConstraints;
11 import java.awt.GridBagLayout;
12 import java.awt.Insets;
13 import java.awt.event.ActionEvent;
14
15 import javax.swing.BorderFactory;
16 import javax.swing.JButton;
17 import javax.swing.JCheckBox;
18 import javax.swing.JComponent;
19 import javax.swing.JDialog;
20 import javax.swing.JPanel;
21 import javax.swing.JSpinner;
22 import javax.swing.JTabbedPane;
23 import javax.swing.SpinnerNumberModel;
24 import javax.swing.SwingConstants;
25
26 import net.pterodactylus.jsite.i18n.I18n;
27 import net.pterodactylus.jsite.i18n.I18nable;
28 import net.pterodactylus.jsite.i18n.gui.I18nAction;
29 import net.pterodactylus.jsite.i18n.gui.I18nLabel;
30 import net.pterodactylus.util.swing.SwingUtils;
31
32 /**
33  * The configuration dialog.
34  *
35  * @author <a href="mailto:dr@ina-germany.de">David Roden</a>
36  * @version $Id$
37  */
38 public class ConfigurationDialog extends JDialog implements I18nable {
39
40         /** The “okay” action. */
41         private I18nAction okayAction;
42
43         /** The “cancel” action. */
44         private I18nAction cancelAction;
45
46         /** The “advanced mode” action. */
47         private I18nAction advancedModeAction;
48
49         /** The “advanced mode” checkbox. */
50         private JCheckBox advancedModeCheckBox;
51
52         /** The “beautify GUI” action. */
53         private I18nAction antialiasAction;
54
55         /** The “use custom control font” action. */
56         private I18nAction useCustomControlFontAction;
57
58         /** The “use custom user font” action. */
59         private I18nAction useCustomUserFontAction;
60
61         /** The “restart required” warning label. */
62         private I18nLabel restartRequiredLabel;
63
64         /** The “beautify” checkbox. */
65         private JCheckBox antialiasCheckBox;
66
67         /** The “use custom” fonts checkbox. */
68         private JCheckBox useCustomControlFontCheckBox;
69
70         /** The control font list. */
71         private FontComboBox controlFontList;
72
73         /** The control font size spinner. */
74         private JSpinner controlFontSizeSpinner;
75
76         /** The checkbox for “use same as control font”. */
77         private JCheckBox useCustomUserFontCheckBox;
78
79         /** The user font list. */
80         private FontComboBox userFontList;
81
82         /** The user font size spinner. */
83         private JSpinner userFontSizeSpinner;
84
85         /** Whether the dialog was cancelled. */
86         private boolean cancelled;
87
88         /**
89          * Creates a new configuration dialog.
90          *
91          * @param swingInterface
92          *            The Swing interface
93          */
94         public ConfigurationDialog(SwingInterface swingInterface) {
95                 super(swingInterface.getMainWindow(), I18n.get("configurationDialog.title"), true);
96                 initActions();
97                 initComponents();
98                 pack();
99                 SwingUtils.center(this);
100                 I18n.registerI18nable(this);
101         }
102
103         //
104         // ACCESSORS
105         //
106
107         /**
108          * Returns whether the dialog was cancelled or confirmed. If the dialog was
109          * cancelled, no further processing should be done.
110          *
111          * @return <code>true</code> if the dialog was cancelled,
112          *         <code>false</code> otherwise
113          */
114         public boolean wasCancelled() {
115                 return cancelled;
116         }
117
118         /**
119          * Returns whether the advanced mode is selected.
120          *
121          * @return <code>true</code> if the advanced mode is selected,
122          *         <code>false</code> otherwise
123          */
124         public boolean isAdvancedMode() {
125                 return advancedModeCheckBox.isSelected();
126         }
127
128         /**
129          * Sets whether the advanced mode is selected.
130          *
131          * @param advancedMode
132          *            <code>true</code> if the advanced mode is selected,
133          *            <code>false</code> otherwise
134          */
135         public void setAdvancedMode(boolean advancedMode) {
136                 advancedModeCheckBox.setSelected(advancedMode);
137         }
138
139         /**
140          * Returns whether the “beautify” checkbox has been selected. The result of
141          * this method should not be used if {@link #wasCancelled()} returned
142          * <code>true</code>!
143          *
144          * @return <code>true</code> if the checkbox was selected,
145          *         <code>false</code> otherwise
146          */
147         public boolean isAntialias() {
148                 return antialiasCheckBox.isSelected();
149         }
150
151         /**
152          * Sets the state of the “antialias” checkbox.
153          *
154          * @param antialias
155          *            The state of the checkbox
156          */
157         public void setAntialias(boolean antialias) {
158                 antialiasCheckBox.setSelected(antialias);
159         }
160
161         /**
162          * Returns the font for the controls.
163          *
164          * @return The control font, or <code>null</code> if no custom control
165          *         font is to be used
166          */
167         public String getControlFont() {
168                 return useCustomControlFontCheckBox.isSelected() ? controlFontList.getSelectedItem() + "-" + controlFontSizeSpinner.getValue() : null;
169         }
170
171         /**
172          * Sets the font for the controls.
173          *
174          * @param controlFont
175          *            The control font, or <code>null</code> if no custom control
176          *            font is to be used
177          */
178         public void setControlFont(String controlFont) {
179                 boolean hasControlFont = controlFont != null;
180                 useCustomControlFontCheckBox.setSelected(hasControlFont);
181                 controlFontList.setEnabled(hasControlFont);
182                 controlFontSizeSpinner.setEnabled(hasControlFont);
183                 if (hasControlFont) {
184                         Font font = Font.decode(controlFont);
185                         controlFontSizeSpinner.setValue(font.getSize());
186                         controlFontList.setSelectedItem(font.getName());
187                 } else {
188                         controlFontSizeSpinner.setValue(12);
189                         controlFontList.setSelectedItem(null);
190                 }
191         }
192
193         /**
194          * Returns the font for user input.
195          *
196          * @return The font for user input, or <code>null</code> if no custom user
197          *         input font is to be used
198          */
199         public String getUserFont() {
200                 return useCustomUserFontCheckBox.isSelected() ? userFontList.getSelectedItem() + "-" + userFontSizeSpinner.getValue() : null;
201         }
202
203         /**
204          * Sets the font for user input.
205          *
206          * @param userFont
207          *            The font for user input, or <code>null</code> if no custom
208          *            user input font is to be used
209          */
210         public void setUserFont(String userFont) {
211                 boolean hasUserFont = userFont != null;
212                 useCustomUserFontCheckBox.setSelected(hasUserFont);
213                 userFontList.setEnabled(hasUserFont);
214                 userFontSizeSpinner.setEnabled(hasUserFont);
215                 if (hasUserFont) {
216                         Font font = Font.decode(userFont);
217                         userFontSizeSpinner.setValue(font.getSize());
218                         userFontList.setSelectedItem(font.getName());
219                 } else {
220                         userFontSizeSpinner.setValue(12);
221                         userFontList.setSelectedItem(null);
222                 }
223         }
224
225         //
226         // PRIVATE METHODS
227         //
228
229         /**
230          * Creates all actions.
231          */
232         private void initActions() {
233                 okayAction = new I18nAction("general.button.okay") {
234
235                         /**
236                          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
237                          */
238                         @SuppressWarnings("synthetic-access")
239                         public void actionPerformed(ActionEvent actionEvent) {
240                                 actionOkay();
241                         }
242                 };
243                 cancelAction = new I18nAction("general.button.cancel") {
244
245                         /**
246                          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
247                          */
248                         @SuppressWarnings("synthetic-access")
249                         public void actionPerformed(ActionEvent actionEvent) {
250                                 actionCancel();
251                         }
252                 };
253                 advancedModeAction = new I18nAction("configurationDialog.page.interface.item.advancedMode") {
254
255                         /**
256                          * {@inheritDoc}
257                          */
258                         public void actionPerformed(ActionEvent e) {
259                                 /* do nothing. */
260                         }
261                 };
262                 antialiasAction = new I18nAction("configurationDialog.page.interfaceTweaks.item.antialias") {
263
264                         /**
265                          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
266                          */
267                         public void actionPerformed(ActionEvent actionEvent) {
268                                 /* do nothing. */
269                         }
270                 };
271                 useCustomControlFontAction = new I18nAction("configurationDialog.page.interfaceTweaks.item.useCustomControlFont") {
272
273                         /**
274                          * {@inheritDoc}
275                          */
276                         @SuppressWarnings("synthetic-access")
277                         public void actionPerformed(ActionEvent e) {
278                                 boolean selected = useCustomControlFontCheckBox.isSelected();
279                                 controlFontList.setEnabled(selected);
280                                 controlFontSizeSpinner.setEnabled(selected);
281                         }
282                 };
283                 useCustomUserFontAction = new I18nAction("configurationDialog.page.interfaceTweaks.item.useCustomUserFont") {
284
285                         /**
286                          * {@inheritDoc}
287                          */
288                         @SuppressWarnings("synthetic-access")
289                         public void actionPerformed(ActionEvent e) {
290                                 boolean selected = useCustomUserFontCheckBox.isSelected();
291                                 userFontList.setEnabled(selected);
292                                 userFontSizeSpinner.setEnabled(selected);
293                         }
294                 };
295         }
296
297         /**
298          * Creates all internal components.
299          */
300         private void initComponents() {
301                 JPanel contentPane = new JPanel(new BorderLayout(12, 12));
302                 contentPane.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
303
304                 JTabbedPane tabbedPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
305                 contentPane.add(tabbedPane, BorderLayout.CENTER);
306
307                 JComponent interfaceConfig = createInterfaceConfig();
308                 tabbedPane.add(I18n.get("configurationDialog.page.interface.name"), interfaceConfig);
309
310                 JComponent interfaceTweaksConfig = createInterfaceTweaksConfig();
311                 tabbedPane.add(I18n.get("configurationDialog.page.interfaceTweaks.name"), interfaceTweaksConfig);
312
313                 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
314                 contentPane.add(buttonPanel, BorderLayout.PAGE_END);
315                 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
316                 buttonPanel.add(new JButton(cancelAction));
317                 JButton okayButton = new JButton(okayAction);
318                 buttonPanel.add(okayButton);
319                 getRootPane().setDefaultButton(okayButton);
320
321                 getContentPane().add(contentPane, BorderLayout.CENTER);
322         }
323
324         /**
325          * Creates the interface configuration panel.
326          *
327          * @return The interface configuration panel
328          */
329         private JComponent createInterfaceConfig() {
330                 JPanel interfaceConfigPanel = new JPanel(new GridBagLayout());
331                 interfaceConfigPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
332
333                 advancedModeCheckBox = new JCheckBox(advancedModeAction);
334                 interfaceConfigPanel.add(advancedModeCheckBox, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
335
336                 interfaceConfigPanel.add(new JPanel(), new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
337                 return interfaceConfigPanel;
338         }
339
340         /**
341          * Creates the panel for the interface tweaks configuration.
342          *
343          * @return The interface tweaks configuration panel
344          */
345         private JComponent createInterfaceTweaksConfig() {
346                 JPanel interfaceTweaksConfigPanel = new JPanel(new GridBagLayout());
347                 interfaceTweaksConfigPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
348
349                 restartRequiredLabel = new I18nLabel("configurationDialog.page.interfaceTweaks.item.restartRequired");
350                 interfaceTweaksConfigPanel.add(restartRequiredLabel, new GridBagConstraints(0, 0, 3, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
351
352                 antialiasCheckBox = new JCheckBox(antialiasAction);
353                 interfaceTweaksConfigPanel.add(antialiasCheckBox, new GridBagConstraints(0, 1, 3, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(18, 0, 0, 0), 0, 0));
354
355                 useCustomControlFontCheckBox = new JCheckBox(useCustomControlFontAction);
356                 interfaceTweaksConfigPanel.add(useCustomControlFontCheckBox, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(6, 0, 0, 0), 0, 0));
357
358                 controlFontList = new FontComboBox();
359                 interfaceTweaksConfigPanel.add(controlFontList, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(6, 6, 0, 0), 0, 0));
360
361                 controlFontSizeSpinner = new JSpinner(new SpinnerNumberModel(12, 6, 80, 1));
362                 interfaceTweaksConfigPanel.add(controlFontSizeSpinner, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(6, 6, 0, 0), 0, 0));
363
364                 useCustomUserFontCheckBox = new JCheckBox(useCustomUserFontAction);
365                 interfaceTweaksConfigPanel.add(useCustomUserFontCheckBox, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 0, 0, 0), 0, 0));
366
367                 userFontList = new FontComboBox();
368                 interfaceTweaksConfigPanel.add(userFontList, new GridBagConstraints(1, 3, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(6, 6, 0, 0), 0, 0));
369
370                 userFontSizeSpinner = new JSpinner(new SpinnerNumberModel(12, 6, 80, 1));
371                 interfaceTweaksConfigPanel.add(userFontSizeSpinner, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(6, 6, 0, 0), 0, 0));
372
373                 interfaceTweaksConfigPanel.add(new JPanel(), new GridBagConstraints(0, 4, 3, 1, 1.0, 1.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
374
375                 return interfaceTweaksConfigPanel;
376         }
377
378         //
379         // PRIVATE ACTIONS
380         //
381
382         /**
383          * Called when the “okay” button is clicked.
384          */
385         private void actionOkay() {
386                 cancelled = false;
387                 setVisible(false);
388         }
389
390         /**
391          * Called when the “cancel” button is clicked.
392          */
393         private void actionCancel() {
394                 cancelled = true;
395                 setVisible(false);
396         }
397
398         //
399         // INTERFACE I18nable
400         //
401
402         /**
403          * @see net.pterodactylus.jsite.i18n.I18nable#updateI18n()
404          */
405         public void updateI18n() {
406                 okayAction.updateI18n();
407                 cancelAction.updateI18n();
408                 restartRequiredLabel.updateI18n();
409                 antialiasAction.updateI18n();
410                 useCustomControlFontAction.updateI18n();
411                 useCustomUserFontAction.updateI18n();
412                 SwingUtils.repackCentered(this);
413         }
414
415 }