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 “beautify GUI” action. */
47 private I18nAction antialiasAction;
49 /** The “use custom control font” action. */
50 private I18nAction useCustomControlFontAction;
52 /** The “use custom user font” action. */
53 private I18nAction useCustomUserFontAction;
55 /** The “restart required” warning label. */
56 private I18nLabel restartRequiredLabel;
58 /** The “beautify” checkbox. */
59 private JCheckBox antialiasCheckBox;
61 /** The “use custom” fonts checkbox. */
62 private JCheckBox useCustomControlFontCheckBox;
64 /** The control font list. */
65 private FontComboBox controlFontList;
67 /** The control font size spinner. */
68 private JSpinner controlFontSizeSpinner;
70 /** The checkbox for “use same as control font”. */
71 private JCheckBox useCustomUserFontCheckBox;
73 /** The user font list. */
74 private FontComboBox userFontList;
76 /** The user font size spinner. */
77 private JSpinner userFontSizeSpinner;
79 /** Whether the dialog was cancelled. */
80 private boolean cancelled;
83 * Creates a new configuration dialog.
85 * @param swingInterface
88 public ConfigurationDialog(SwingInterface swingInterface) {
89 super(swingInterface.getMainWindow(), I18n.get("configurationDialog.title"), true);
93 SwingUtils.center(this);
94 I18n.registerI18nable(this);
102 * Returns whether the dialog was cancelled or confirmed. If the dialog was
103 * cancelled, no further processing should be done.
105 * @return <code>true</code> if the dialog was cancelled,
106 * <code>false</code> otherwise
108 public boolean wasCancelled() {
113 * Returns whether the “beautify” checkbox has been selected. The result of
114 * this method should not be used if {@link #wasCancelled()} returned
117 * @return <code>true</code> if the checkbox was selected,
118 * <code>false</code> otherwise
120 public boolean isAntialias() {
121 return antialiasCheckBox.isSelected();
125 * Sets the state of the “antialias” checkbox.
128 * The state of the checkbox
130 public void setAntialias(boolean antialias) {
131 antialiasCheckBox.setSelected(antialias);
135 * Returns the font for the controls.
137 * @return The control font, or <code>null</code> if no custom control
140 public String getControlFont() {
141 return useCustomControlFontCheckBox.isSelected() ? controlFontList.getSelectedItem() + "-" + controlFontSizeSpinner.getValue() : null;
145 * Sets the font for the controls.
148 * The control font, or <code>null</code> if no custom control
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());
161 controlFontSizeSpinner.setValue(12);
162 controlFontList.setSelectedItem(null);
167 * Returns the font for user input.
169 * @return The font for user input, or <code>null</code> if no custom user
170 * input font is to be used
172 public String getUserFont() {
173 return useCustomUserFontCheckBox.isSelected() ? userFontList.getSelectedItem() + "-" + userFontSizeSpinner.getValue() : null;
177 * Sets the font for user input.
180 * The font for user input, or <code>null</code> if no custom
181 * user input font is to be used
183 public void setUserFont(String userFont) {
184 boolean hasUserFont = userFont != null;
185 useCustomUserFontCheckBox.setSelected(hasUserFont);
186 userFontList.setEnabled(hasUserFont);
187 userFontSizeSpinner.setEnabled(hasUserFont);
189 Font font = Font.decode(userFont);
190 userFontSizeSpinner.setValue(font.getSize());
191 userFontList.setSelectedItem(font.getName());
193 userFontSizeSpinner.setValue(12);
194 userFontList.setSelectedItem(null);
203 * Creates all actions.
205 private void initActions() {
206 okayAction = new I18nAction("general.button.okay") {
209 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
211 @SuppressWarnings("synthetic-access")
212 public void actionPerformed(ActionEvent actionEvent) {
216 cancelAction = new I18nAction("general.button.cancel") {
219 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
221 @SuppressWarnings("synthetic-access")
222 public void actionPerformed(ActionEvent actionEvent) {
226 antialiasAction = new I18nAction("configurationDialog.page.interface.item.antialias") {
229 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
231 public void actionPerformed(ActionEvent actionEvent) {
235 useCustomControlFontAction = new I18nAction("configurationDialog.page.interface.item.useCustomControlFont") {
240 @SuppressWarnings("synthetic-access")
241 public void actionPerformed(ActionEvent e) {
242 boolean selected = useCustomControlFontCheckBox.isSelected();
243 controlFontList.setEnabled(selected);
244 controlFontSizeSpinner.setEnabled(selected);
247 useCustomUserFontAction = new I18nAction("configurationDialog.page.interface.item.useCustomUserFont") {
252 @SuppressWarnings("synthetic-access")
253 public void actionPerformed(ActionEvent e) {
254 boolean selected = useCustomUserFontCheckBox.isSelected();
255 userFontList.setEnabled(selected);
256 userFontSizeSpinner.setEnabled(selected);
262 * Creates all internal components.
264 private void initComponents() {
265 JPanel contentPane = new JPanel(new BorderLayout(12, 12));
266 contentPane.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
268 JTabbedPane tabbedPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
269 contentPane.add(tabbedPane, BorderLayout.CENTER);
271 JComponent interfaceTweaksConfig = createInterfaceTweaksConfig();
272 tabbedPane.add(I18n.get("configurationDialog.page.interface.name"), interfaceTweaksConfig);
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);
282 getContentPane().add(contentPane, BorderLayout.CENTER);
286 * Creates the panel for the interface configuration.
288 * @return The interface configuration panel
290 private JComponent createInterfaceTweaksConfig() {
291 JPanel interfaceConfigPanel = new JPanel(new GridBagLayout());
292 interfaceConfigPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
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));
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));
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));
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));
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));
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));
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));
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));
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));
320 return interfaceConfigPanel;
328 * Called when the “okay” button is clicked.
330 private void actionOkay() {
336 * Called when the “cancel” button is clicked.
338 private void actionCancel() {
344 // INTERFACE I18nable
348 * @see net.pterodactylus.jsite.i18n.I18nable#updateI18n()
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);