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