2 * © 2008 INA Service GmbH
5 package net.pterodactylus.jsite.gui;
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;
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;
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;
29 * The configuration dialog.
31 * @author <a href="mailto:dr@ina-germany.de">David Roden</a>
34 public class ConfigurationDialog extends JDialog implements I18nable {
36 /** The “okay” action. */
37 private I18nAction okayAction;
39 /** The “cancel” action. */
40 private I18nAction cancelAction;
42 /** The “beautify GUI” action. */
43 private I18nAction beautifyAction;
45 /** The “use custom control font” action. */
46 private I18nAction useCustomControlFontAction;
48 /** The “use custom user font” action. */
49 private I18nAction useCustomUserFontAction;
51 /** The “beautify” checkbox. */
52 private JCheckBox beautifyCheckBox;
54 /** The “use custom” fonts checkbox. */
55 private JCheckBox useCustomControlFontCheckBox;
57 /** The control font list. */
58 private FontComboBox controlFontList;
60 /** The checkbox for “use same as control font”. */
61 private JCheckBox useCustomUserFontCheckBox;
63 /** The user font list. */
64 private FontComboBox userFontList;
66 /** Whether the dialog was cancelled. */
67 private boolean cancelled;
70 * Creates a new configuration dialog.
72 * @param swingInterface
75 public ConfigurationDialog(SwingInterface swingInterface) {
76 super(swingInterface.getMainWindow(), I18n.get("configurationDialog.title"), true);
80 SwingUtils.center(this);
81 I18n.registerI18nable(this);
89 * Returns whether the dialog was cancelled or confirmed. If the dialog was
90 * cancelled, no further processing should be done.
92 * @return <code>true</code> if the dialog was cancelled,
93 * <code>false</code> otherwise
95 public boolean wasCancelled() {
100 * Returns whether the “beautify” checkbox has been selected. The result of
101 * this method should not be used if {@link #wasCancelled()} returned
104 * @return <code>true</code> if the checkbox was selected,
105 * <code>false</code> otherwise
107 public boolean getBeautify() {
108 return beautifyCheckBox.isSelected();
112 * Sets the state of the “beautify” checkbox.
115 * The state of the checkbox
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());
126 * Returns the font for the controls.
128 * @return The control font, or <code>null</code> if no custom control
131 public String getControlFont() {
132 return (String) ((beautifyCheckBox.isSelected() && useCustomControlFontCheckBox.isSelected()) ? controlFontList.getSelectedItem() : null);
136 * Sets the font for the controls.
139 * The control font, or <code>null</code> if no custom control
142 public void setControlFont(String controlFont) {
143 useCustomControlFontCheckBox.setSelected(controlFont != null);
144 controlFontList.setEnabled(beautifyCheckBox.isSelected() && (controlFont != null));
145 controlFontList.setSelectedItem(controlFont);
149 * Returns the font for user input.
151 * @return The font for user input, or <code>null</code> if no custom user
152 * input font is to be used
154 public String getUserFont() {
155 return (String) ((beautifyCheckBox.isSelected() && useCustomUserFontCheckBox.isSelected()) ? userFontList.getSelectedItem() : null);
159 * Sets the font for user input.
162 * The font for user input, or <code>null</code> if no custom
163 * user input font is to be used
165 public void setUserFont(String userFont) {
166 useCustomUserFontCheckBox.setSelected(userFont != null);
167 userFontList.setEnabled(beautifyCheckBox.isSelected() && (userFont != null));
168 userFontList.setSelectedItem(userFont);
176 * Creates all actions.
178 private void initActions() {
179 okayAction = new I18nAction("general.button.okay") {
182 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
184 @SuppressWarnings("synthetic-access")
185 public void actionPerformed(ActionEvent actionEvent) {
189 cancelAction = new I18nAction("general.button.cancel") {
192 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
194 @SuppressWarnings("synthetic-access")
195 public void actionPerformed(ActionEvent actionEvent) {
199 beautifyAction = new I18nAction("configurationDialog.page.interface.item.beautify") {
202 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
204 @SuppressWarnings("synthetic-access")
205 public void actionPerformed(ActionEvent actionEvent) {
206 setBeautify(beautifyCheckBox.isSelected());
209 useCustomControlFontAction = new I18nAction("configurationDialog.page.interface.item.useCustomControlFont") {
214 @SuppressWarnings("synthetic-access")
215 public void actionPerformed(ActionEvent e) {
216 controlFontList.setEnabled(useCustomControlFontCheckBox.isSelected());
219 useCustomUserFontAction = new I18nAction("configurationDialog.page.interface.item.useCustomUserFont") {
224 @SuppressWarnings("synthetic-access")
225 public void actionPerformed(ActionEvent e) {
226 userFontList.setEnabled(useCustomUserFontCheckBox.isSelected());
232 * Creates all internal components.
234 private void initComponents() {
235 JPanel contentPane = new JPanel(new BorderLayout(12, 12));
236 contentPane.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
238 JTabbedPane tabbedPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
239 contentPane.add(tabbedPane, BorderLayout.CENTER);
241 JComponent interfaceConfig = createInterfaceConfig();
242 tabbedPane.add("Swing Interface", interfaceConfig);
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);
252 getContentPane().add(contentPane, BorderLayout.CENTER);
256 * Creates the panel for the interface configuration.
258 * @return The interface configuration panel
260 private JComponent createInterfaceConfig() {
261 JPanel interfaceConfigPanel = new JPanel(new GridBagLayout());
262 interfaceConfigPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
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));
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));
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));
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));
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));
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));
281 return interfaceConfigPanel;
289 * Called when the “okay” button is clicked.
291 private void actionOkay() {
297 * Called when the “cancel” button is clicked.
299 private void actionCancel() {
305 // INTERFACE I18nable
309 * @see net.pterodactylus.jsite.i18n.I18nable#updateI18n()
311 public void updateI18n() {
312 okayAction.updateI18n();
313 cancelAction.updateI18n();
314 beautifyAction.updateI18n();
315 SwingUtils.repackCentered(this);