add size selector for fonts
[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.interface.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.interface.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.interface.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                 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
275                 contentPane.add(buttonPanel, BorderLayout.PAGE_END);
276                 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
277                 buttonPanel.add(new JButton(cancelAction));
278                 JButton okayButton = new JButton(okayAction);
279                 buttonPanel.add(okayButton);
280                 getRootPane().setDefaultButton(okayButton);
281
282                 getContentPane().add(contentPane, BorderLayout.CENTER);
283         }
284
285         /**
286          * Creates the panel for the interface configuration.
287          *
288          * @return The interface configuration panel
289          */
290         private JComponent createInterfaceConfig() {
291                 JPanel interfaceConfigPanel = new JPanel(new GridBagLayout());
292                 interfaceConfigPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
293
294                 restartRequiredLabel = new I18nLabel("configurationDialog.page.interface.item.restartRequired");
295                 interfaceConfigPanel.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));
296
297                 antialiasCheckBox = new JCheckBox(antialiasAction);
298                 interfaceConfigPanel.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));
299
300                 useCustomControlFontCheckBox = new JCheckBox(useCustomControlFontAction);
301                 interfaceConfigPanel.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));
302
303                 controlFontList = new FontComboBox();
304                 interfaceConfigPanel.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));
305
306                 controlFontSizeSpinner = new JSpinner(new SpinnerNumberModel(12, 6, 80, 1));
307                 interfaceConfigPanel.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));
308
309                 useCustomUserFontCheckBox = new JCheckBox(useCustomUserFontAction);
310                 interfaceConfigPanel.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));
311
312                 userFontList = new FontComboBox();
313                 interfaceConfigPanel.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));
314
315                 userFontSizeSpinner = new JSpinner(new SpinnerNumberModel(12, 6, 80, 1));
316                 interfaceConfigPanel.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));
317
318                 interfaceConfigPanel.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));
319
320                 return interfaceConfigPanel;
321         }
322
323         //
324         // PRIVATE ACTIONS
325         //
326
327         /**
328          * Called when the “okay” button is clicked.
329          */
330         private void actionOkay() {
331                 cancelled = false;
332                 setVisible(false);
333         }
334
335         /**
336          * Called when the “cancel” button is clicked.
337          */
338         private void actionCancel() {
339                 cancelled = true;
340                 setVisible(false);
341         }
342
343         //
344         // INTERFACE I18nable
345         //
346
347         /**
348          * @see net.pterodactylus.jsite.i18n.I18nable#updateI18n()
349          */
350         public void updateI18n() {
351                 okayAction.updateI18n();
352                 cancelAction.updateI18n();
353                 restartRequiredLabel.updateI18n();
354                 antialiasAction.updateI18n();
355                 useCustomControlFontAction.updateI18n();
356                 useCustomUserFontAction.updateI18n();
357                 SwingUtils.repackCentered(this);
358         }
359
360 }