2 * © 2008 INA Service GmbH
5 package net.pterodactylus.jsite.gui;
7 import java.awt.BorderLayout;
8 import java.awt.FlowLayout;
10 import java.awt.GridBagConstraints;
11 import java.awt.GridBagLayout;
12 import java.awt.Insets;
13 import java.awt.event.ActionEvent;
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;
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;
33 * The configuration dialog.
35 * @author <a href="mailto:dr@ina-germany.de">David Roden</a>
38 public class ConfigurationDialog extends JDialog implements I18nable {
40 /** The “okay” action. */
41 private I18nAction okayAction;
43 /** The “cancel” action. */
44 private I18nAction cancelAction;
46 /** The “advanced mode” action. */
47 private I18nAction advancedModeAction;
49 /** The “advanced mode” checkbox. */
50 private JCheckBox advancedModeCheckBox;
52 /** The “beautify GUI” action. */
53 private I18nAction antialiasAction;
55 /** The “use custom control font” action. */
56 private I18nAction useCustomControlFontAction;
58 /** The “use custom user font” action. */
59 private I18nAction useCustomUserFontAction;
61 /** The “restart required” warning label. */
62 private I18nLabel restartRequiredLabel;
64 /** The “beautify” checkbox. */
65 private JCheckBox antialiasCheckBox;
67 /** The “use custom” fonts checkbox. */
68 private JCheckBox useCustomControlFontCheckBox;
70 /** The control font list. */
71 private FontComboBox controlFontList;
73 /** The control font size spinner. */
74 private JSpinner controlFontSizeSpinner;
76 /** The checkbox for “use same as control font”. */
77 private JCheckBox useCustomUserFontCheckBox;
79 /** The user font list. */
80 private FontComboBox userFontList;
82 /** The user font size spinner. */
83 private JSpinner userFontSizeSpinner;
85 /** Whether the dialog was cancelled. */
86 private boolean cancelled;
89 * Creates a new configuration dialog.
91 * @param swingInterface
94 public ConfigurationDialog(SwingInterface swingInterface) {
95 super(swingInterface.getMainWindow(), I18n.get("configurationDialog.title"), true);
99 SwingUtils.center(this);
100 I18n.registerI18nable(this);
108 * Returns whether the dialog was cancelled or confirmed. If the dialog was
109 * cancelled, no further processing should be done.
111 * @return <code>true</code> if the dialog was cancelled,
112 * <code>false</code> otherwise
114 public boolean wasCancelled() {
119 * Returns whether the advanced mode is selected.
121 * @return <code>true</code> if the advanced mode is selected,
122 * <code>false</code> otherwise
124 public boolean isAdvancedMode() {
125 return advancedModeCheckBox.isSelected();
129 * Sets whether the advanced mode is selected.
131 * @param advancedMode
132 * <code>true</code> if the advanced mode is selected,
133 * <code>false</code> otherwise
135 public void setAdvancedMode(boolean advancedMode) {
136 advancedModeCheckBox.setSelected(advancedMode);
140 * Returns whether the “beautify” checkbox has been selected. The result of
141 * this method should not be used if {@link #wasCancelled()} returned
144 * @return <code>true</code> if the checkbox was selected,
145 * <code>false</code> otherwise
147 public boolean isAntialias() {
148 return antialiasCheckBox.isSelected();
152 * Sets the state of the “antialias” checkbox.
155 * The state of the checkbox
157 public void setAntialias(boolean antialias) {
158 antialiasCheckBox.setSelected(antialias);
162 * Returns the font for the controls.
164 * @return The control font, or <code>null</code> if no custom control
167 public String getControlFont() {
168 return useCustomControlFontCheckBox.isSelected() ? controlFontList.getSelectedItem() + "-" + controlFontSizeSpinner.getValue() : null;
172 * Sets the font for the controls.
175 * The control font, or <code>null</code> if no custom control
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());
188 controlFontSizeSpinner.setValue(12);
189 controlFontList.setSelectedItem(null);
194 * Returns the font for user input.
196 * @return The font for user input, or <code>null</code> if no custom user
197 * input font is to be used
199 public String getUserFont() {
200 return useCustomUserFontCheckBox.isSelected() ? userFontList.getSelectedItem() + "-" + userFontSizeSpinner.getValue() : null;
204 * Sets the font for user input.
207 * The font for user input, or <code>null</code> if no custom
208 * user input font is to be used
210 public void setUserFont(String userFont) {
211 boolean hasUserFont = userFont != null;
212 useCustomUserFontCheckBox.setSelected(hasUserFont);
213 userFontList.setEnabled(hasUserFont);
214 userFontSizeSpinner.setEnabled(hasUserFont);
216 Font font = Font.decode(userFont);
217 userFontSizeSpinner.setValue(font.getSize());
218 userFontList.setSelectedItem(font.getName());
220 userFontSizeSpinner.setValue(12);
221 userFontList.setSelectedItem(null);
230 * Creates all actions.
232 private void initActions() {
233 okayAction = new I18nAction("general.button.okay") {
236 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
238 @SuppressWarnings("synthetic-access")
239 public void actionPerformed(ActionEvent actionEvent) {
243 cancelAction = new I18nAction("general.button.cancel") {
246 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
248 @SuppressWarnings("synthetic-access")
249 public void actionPerformed(ActionEvent actionEvent) {
253 advancedModeAction = new I18nAction("configurationDialog.page.interface.item.advancedMode") {
258 public void actionPerformed(ActionEvent e) {
262 antialiasAction = new I18nAction("configurationDialog.page.interfaceTweaks.item.antialias") {
265 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
267 public void actionPerformed(ActionEvent actionEvent) {
271 useCustomControlFontAction = new I18nAction("configurationDialog.page.interfaceTweaks.item.useCustomControlFont") {
276 @SuppressWarnings("synthetic-access")
277 public void actionPerformed(ActionEvent e) {
278 boolean selected = useCustomControlFontCheckBox.isSelected();
279 controlFontList.setEnabled(selected);
280 controlFontSizeSpinner.setEnabled(selected);
283 useCustomUserFontAction = new I18nAction("configurationDialog.page.interfaceTweaks.item.useCustomUserFont") {
288 @SuppressWarnings("synthetic-access")
289 public void actionPerformed(ActionEvent e) {
290 boolean selected = useCustomUserFontCheckBox.isSelected();
291 userFontList.setEnabled(selected);
292 userFontSizeSpinner.setEnabled(selected);
298 * Creates all internal components.
300 private void initComponents() {
301 JPanel contentPane = new JPanel(new BorderLayout(12, 12));
302 contentPane.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
304 JTabbedPane tabbedPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
305 contentPane.add(tabbedPane, BorderLayout.CENTER);
307 JComponent interfaceConfig = createInterfaceConfig();
308 tabbedPane.add(I18n.get("configurationDialog.page.interface.name"), interfaceConfig);
310 JComponent interfaceTweaksConfig = createInterfaceTweaksConfig();
311 tabbedPane.add(I18n.get("configurationDialog.page.interfaceTweaks.name"), interfaceTweaksConfig);
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);
321 getContentPane().add(contentPane, BorderLayout.CENTER);
325 * Creates the interface configuration panel.
327 * @return The interface configuration panel
329 private JComponent createInterfaceConfig() {
330 JPanel interfaceConfigPanel = new JPanel(new GridBagLayout());
331 interfaceConfigPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
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));
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;
341 * Creates the panel for the interface tweaks configuration.
343 * @return The interface tweaks configuration panel
345 private JComponent createInterfaceTweaksConfig() {
346 JPanel interfaceTweaksConfigPanel = new JPanel(new GridBagLayout());
347 interfaceTweaksConfigPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
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));
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));
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));
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));
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));
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));
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));
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));
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));
375 return interfaceTweaksConfigPanel;
383 * Called when the “okay” button is clicked.
385 private void actionOkay() {
391 * Called when the “cancel” button is clicked.
393 private void actionCancel() {
399 // INTERFACE I18nable
403 * @see net.pterodactylus.jsite.i18n.I18nable#updateI18n()
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);