enhance configuration dialog with font selection
[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 beautifyAction;
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 beautifyCheckBox;
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 getBeautify() {
108                 return beautifyCheckBox.isSelected();
109         }
110
111         /**
112          * Sets the state of the “beautify” checkbox.
113          *
114          * @param beautify
115          *            The state of the checkbox
116          */
117         public void setBeautify(boolean beautify) {
118                 beautifyCheckBox.setSelected(beautify);
119                 useCustomControlFontCheckBox.setEnabled(beautify);
120                 controlFontList.setEnabled(beautify && useCustomControlFontCheckBox.isSelected());
121                 useCustomUserFontCheckBox.setEnabled(beautify);
122                 userFontList.setEnabled(beautify && useCustomUserFontCheckBox.isSelected());
123         }
124
125         /**
126          * Returns the font for the controls.
127          *
128          * @return The control font, or <code>null</code> if no custom control
129          *         font is to be used
130          */
131         public String getControlFont() {
132                 return (String) ((beautifyCheckBox.isSelected() && useCustomControlFontCheckBox.isSelected()) ? controlFontList.getSelectedItem() : null);
133         }
134
135         /**
136          * Sets the font for the controls.
137          *
138          * @param controlFont
139          *            The control font, or <code>null</code> if no custom control
140          *            font is to be used
141          */
142         public void setControlFont(String controlFont) {
143                 useCustomControlFontCheckBox.setSelected(controlFont != null);
144                 controlFontList.setEnabled(beautifyCheckBox.isSelected() && (controlFont != null));
145                 controlFontList.setSelectedItem(controlFont);
146         }
147
148         /**
149          * Returns the font for user input.
150          *
151          * @return The font for user input, or <code>null</code> if no custom user
152          *         input font is to be used
153          */
154         public String getUserFont() {
155                 return (String) ((beautifyCheckBox.isSelected() && useCustomUserFontCheckBox.isSelected()) ? userFontList.getSelectedItem() : null);
156         }
157
158         /**
159          * Sets the font for user input.
160          *
161          * @param userFont
162          *            The font for user input, or <code>null</code> if no custom
163          *            user input font is to be used
164          */
165         public void setUserFont(String userFont) {
166                 useCustomUserFontCheckBox.setSelected(userFont != null);
167                 userFontList.setEnabled(beautifyCheckBox.isSelected() && (userFont != null));
168                 userFontList.setSelectedItem(userFont);
169         }
170
171         //
172         // PRIVATE METHODS
173         //
174
175         /**
176          * Creates all actions.
177          */
178         private void initActions() {
179                 okayAction = new I18nAction("general.button.okay") {
180
181                         /**
182                          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
183                          */
184                         @SuppressWarnings("synthetic-access")
185                         public void actionPerformed(ActionEvent actionEvent) {
186                                 actionOkay();
187                         }
188                 };
189                 cancelAction = new I18nAction("general.button.cancel") {
190
191                         /**
192                          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
193                          */
194                         @SuppressWarnings("synthetic-access")
195                         public void actionPerformed(ActionEvent actionEvent) {
196                                 actionCancel();
197                         }
198                 };
199                 beautifyAction = new I18nAction("configurationDialog.page.interface.item.beautify") {
200
201                         /**
202                          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
203                          */
204                         @SuppressWarnings("synthetic-access")
205                         public void actionPerformed(ActionEvent actionEvent) {
206                                 setBeautify(beautifyCheckBox.isSelected());
207                         }
208                 };
209                 useCustomControlFontAction = new I18nAction("configurationDialog.page.interface.item.useCustomControlFont") {
210
211                         /**
212                          * {@inheritDoc}
213                          */
214                         @SuppressWarnings("synthetic-access")
215                         public void actionPerformed(ActionEvent e) {
216                                 controlFontList.setEnabled(useCustomControlFontCheckBox.isSelected());
217                         }
218                 };
219                 useCustomUserFontAction = new I18nAction("configurationDialog.page.interface.item.useCustomUserFont") {
220
221                         /**
222                          * {@inheritDoc}
223                          */
224                         @SuppressWarnings("synthetic-access")
225                         public void actionPerformed(ActionEvent e) {
226                                 userFontList.setEnabled(useCustomUserFontCheckBox.isSelected());
227                         }
228                 };
229         }
230
231         /**
232          * Creates all internal components.
233          */
234         private void initComponents() {
235                 JPanel contentPane = new JPanel(new BorderLayout(12, 12));
236                 contentPane.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
237
238                 JTabbedPane tabbedPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
239                 contentPane.add(tabbedPane, BorderLayout.CENTER);
240
241                 JComponent interfaceConfig = createInterfaceConfig();
242                 tabbedPane.add("Swing Interface", interfaceConfig);
243
244                 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
245                 contentPane.add(buttonPanel, BorderLayout.PAGE_END);
246                 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
247                 buttonPanel.add(new JButton(cancelAction));
248                 JButton okayButton = new JButton(okayAction);
249                 buttonPanel.add(okayButton);
250                 getRootPane().setDefaultButton(okayButton);
251
252                 getContentPane().add(contentPane, BorderLayout.CENTER);
253         }
254
255         /**
256          * Creates the panel for the interface configuration.
257          *
258          * @return The interface configuration panel
259          */
260         private JComponent createInterfaceConfig() {
261                 JPanel interfaceConfigPanel = new JPanel(new GridBagLayout());
262                 interfaceConfigPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
263
264                 beautifyCheckBox = new JCheckBox(beautifyAction);
265                 interfaceConfigPanel.add(beautifyCheckBox, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
266
267                 useCustomControlFontCheckBox = new JCheckBox(useCustomControlFontAction);
268                 interfaceConfigPanel.add(useCustomControlFontCheckBox, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(6, 18, 0, 0), 0, 0));
269
270                 controlFontList = new FontComboBox();
271                 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));
272
273                 useCustomUserFontCheckBox = new JCheckBox(useCustomUserFontAction);
274                 interfaceConfigPanel.add(useCustomUserFontCheckBox, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
275
276                 userFontList = new FontComboBox();
277                 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));
278
279                 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));
280
281                 return interfaceConfigPanel;
282         }
283
284         //
285         // PRIVATE ACTIONS
286         //
287
288         /**
289          * Called when the “okay” button is clicked.
290          */
291         private void actionOkay() {
292                 cancelled = false;
293                 setVisible(false);
294         }
295
296         /**
297          * Called when the “cancel” button is clicked.
298          */
299         private void actionCancel() {
300                 cancelled = true;
301                 setVisible(false);
302         }
303
304         //
305         // INTERFACE I18nable
306         //
307
308         /**
309          * @see net.pterodactylus.jsite.i18n.I18nable#updateI18n()
310          */
311         public void updateI18n() {
312                 okayAction.updateI18n();
313                 cancelAction.updateI18n();
314                 beautifyAction.updateI18n();
315                 SwingUtils.repackCentered(this);
316         }
317
318 }