whitespace fixups
[jSite2.git] / src / net / pterodactylus / jsite / gui / ConfigurationDialog.java
index fc93c3b..92c547a 100644 (file)
@@ -11,17 +11,24 @@ import java.awt.GridBagConstraints;
 import java.awt.GridBagLayout;
 import java.awt.Insets;
 import java.awt.event.ActionEvent;
+import java.util.ArrayList;
+import java.util.List;
 
 import javax.swing.BorderFactory;
 import javax.swing.JButton;
 import javax.swing.JCheckBox;
+import javax.swing.JComboBox;
 import javax.swing.JComponent;
 import javax.swing.JDialog;
+import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import javax.swing.JSpinner;
 import javax.swing.JTabbedPane;
+import javax.swing.LookAndFeel;
 import javax.swing.SpinnerNumberModel;
 import javax.swing.SwingConstants;
+import javax.swing.UIManager;
+import javax.swing.UIManager.LookAndFeelInfo;
 
 import net.pterodactylus.jsite.i18n.I18n;
 import net.pterodactylus.jsite.i18n.I18nable;
@@ -33,7 +40,6 @@ import net.pterodactylus.util.swing.SwingUtils;
  * The configuration dialog.
  *
  * @author <a href="mailto:dr@ina-germany.de">David Roden</a>
- * @version $Id$
  */
 public class ConfigurationDialog extends JDialog implements I18nable {
 
@@ -43,6 +49,12 @@ public class ConfigurationDialog extends JDialog implements I18nable {
        /** The “cancel” action. */
        private I18nAction cancelAction;
 
+       /** The “advanced mode” action. */
+       private I18nAction advancedModeAction;
+
+       /** The “advanced mode” checkbox. */
+       private JCheckBox advancedModeCheckBox;
+
        /** The “beautify GUI” action. */
        private I18nAction antialiasAction;
 
@@ -52,6 +64,9 @@ public class ConfigurationDialog extends JDialog implements I18nable {
        /** The “use custom user font” action. */
        private I18nAction useCustomUserFontAction;
 
+       /** The “use custom L&F” action. */
+       private I18nAction useCustomLAFAction;
+
        /** The “restart required” warning label. */
        private I18nLabel restartRequiredLabel;
 
@@ -76,6 +91,12 @@ public class ConfigurationDialog extends JDialog implements I18nable {
        /** The user font size spinner. */
        private JSpinner userFontSizeSpinner;
 
+       /** The checkbox for custom L&F. */
+       private JCheckBox useCustomLAFCheckBox;
+
+       /** The combo box for the L&Fs. */
+       private JComboBox customLAFComboBox;
+
        /** Whether the dialog was cancelled. */
        private boolean cancelled;
 
@@ -110,6 +131,27 @@ public class ConfigurationDialog extends JDialog implements I18nable {
        }
 
        /**
+        * Returns whether the advanced mode is selected.
+        *
+        * @return <code>true</code> if the advanced mode is selected,
+        *         <code>false</code> otherwise
+        */
+       public boolean isAdvancedMode() {
+               return advancedModeCheckBox.isSelected();
+       }
+
+       /**
+        * Sets whether the advanced mode is selected.
+        *
+        * @param advancedMode
+        *            <code>true</code> if the advanced mode is selected,
+        *            <code>false</code> otherwise
+        */
+       public void setAdvancedMode(boolean advancedMode) {
+               advancedModeCheckBox.setSelected(advancedMode);
+       }
+
+       /**
         * Returns whether the “beautify” checkbox has been selected. The result of
         * this method should not be used if {@link #wasCancelled()} returned
         * <code>true</code>!
@@ -195,6 +237,44 @@ public class ConfigurationDialog extends JDialog implements I18nable {
                }
        }
 
+       /**
+        * Returns the class name of the selected look and feel, if a custom look
+        * and feel is selected.
+        *
+        * @return The class name of the look and feel to load, or <code>null</code>
+        *         if no special look and feel should be used
+        */
+       public String getLookAndFeel() {
+               if (!useCustomLAFCheckBox.isSelected()) {
+                       return null;
+               }
+               return ((LookAndFeelWrapper) customLAFComboBox.getSelectedItem()).getClassName();
+       }
+
+       /**
+        * Sets the given look and feel.
+        *
+        * @param lookAndFeel
+        *            The class name of the look and feel, or <code>null</code> to
+        *            not select a custom look and feel
+        */
+       public void setLookAndFeel(String lookAndFeel) {
+               useCustomLAFCheckBox.setSelected(false);
+               customLAFComboBox.setEnabled(false);
+               if (lookAndFeel == null) {
+                       return;
+               }
+               for (int lookAndFeelIndex = 0; lookAndFeelIndex < customLAFComboBox.getItemCount(); lookAndFeelIndex++) {
+                       LookAndFeelWrapper lookAndFeelWrapper = (LookAndFeelWrapper) customLAFComboBox.getItemAt(lookAndFeelIndex);
+                       if (lookAndFeelWrapper.getClassName().equals(lookAndFeel)) {
+                               customLAFComboBox.setSelectedIndex(lookAndFeelIndex);
+                               customLAFComboBox.setEnabled(true);
+                               useCustomLAFCheckBox.setSelected(true);
+                               break;
+                       }
+               }
+       }
+
        //
        // PRIVATE METHODS
        //
@@ -223,6 +303,15 @@ public class ConfigurationDialog extends JDialog implements I18nable {
                                actionCancel();
                        }
                };
+               advancedModeAction = new I18nAction("configurationDialog.page.interface.item.advancedMode") {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       public void actionPerformed(ActionEvent e) {
+                               /* do nothing. */
+                       }
+               };
                antialiasAction = new I18nAction("configurationDialog.page.interfaceTweaks.item.antialias") {
 
                        /**
@@ -256,6 +345,16 @@ public class ConfigurationDialog extends JDialog implements I18nable {
                                userFontSizeSpinner.setEnabled(selected);
                        }
                };
+               useCustomLAFAction = new I18nAction("configurationDialog.page.interfaceTweaks.item.useCustomLAF") {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent e) {
+                               customLAFComboBox.setEnabled(useCustomLAFCheckBox.isSelected());
+                       }
+               };
        }
 
        /**
@@ -292,6 +391,12 @@ public class ConfigurationDialog extends JDialog implements I18nable {
         */
        private JComponent createInterfaceConfig() {
                JPanel interfaceConfigPanel = new JPanel(new GridBagLayout());
+               interfaceConfigPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
+
+               advancedModeCheckBox = new JCheckBox(advancedModeAction);
+               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));
+
+               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));
                return interfaceConfigPanel;
        }
 
@@ -328,7 +433,18 @@ public class ConfigurationDialog extends JDialog implements I18nable {
                userFontSizeSpinner = new JSpinner(new SpinnerNumberModel(12, 6, 80, 1));
                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));
 
-               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));
+               useCustomLAFCheckBox = new JCheckBox(useCustomLAFAction);
+               interfaceTweaksConfigPanel.add(useCustomLAFCheckBox, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(6, 0, 0, 0), 0, 0));
+
+               LookAndFeelInfo[] lookAndFeelInfos = UIManager.getInstalledLookAndFeels();
+               List<LookAndFeelWrapper> lookAndFeelWrappers = new ArrayList<LookAndFeelWrapper>();
+               for (LookAndFeelInfo lookAndFeelInfo : lookAndFeelInfos) {
+                       lookAndFeelWrappers.add(new LookAndFeelWrapper(lookAndFeelInfo.getClassName(), lookAndFeelInfo.getName()));
+               }
+               customLAFComboBox = new JComboBox(lookAndFeelWrappers.toArray(new LookAndFeelWrapper[0]));
+               interfaceTweaksConfigPanel.add(customLAFComboBox, new GridBagConstraints(1, 4, 2, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(6, 6, 0, 0), 0, 0));
+
+               interfaceTweaksConfigPanel.add(new JPanel(), new GridBagConstraints(0, 5, 3, 1, 1.0, 1.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
 
                return interfaceTweaksConfigPanel;
        }
@@ -341,6 +457,18 @@ public class ConfigurationDialog extends JDialog implements I18nable {
         * Called when the “okay” button is clicked.
         */
        private void actionOkay() {
+               if (useCustomControlFontCheckBox.isSelected()) {
+                       if (controlFontList.getSelectedItem() == null) {
+                               JOptionPane.showMessageDialog(this, I18n.get("configurationDialog.error.noControlFontSelected.message"), I18n.get("configurationDialog.error.noControlFontSelected.title"), JOptionPane.ERROR_MESSAGE);
+                               return;
+                       }
+               }
+               if (useCustomUserFontCheckBox.isSelected()) {
+                       if (userFontList.getSelectedItem() == null) {
+                               JOptionPane.showMessageDialog(this, I18n.get("configurationDialog.error.noUserFontSelected.message"), I18n.get("configurationDialog.error.noUserFontSelected.title"), JOptionPane.ERROR_MESSAGE);
+                               return;
+                       }
+               }
                cancelled = false;
                setVisible(false);
        }
@@ -363,11 +491,59 @@ public class ConfigurationDialog extends JDialog implements I18nable {
        public void updateI18n() {
                okayAction.updateI18n();
                cancelAction.updateI18n();
+               advancedModeAction.updateI18n();
                restartRequiredLabel.updateI18n();
                antialiasAction.updateI18n();
                useCustomControlFontAction.updateI18n();
                useCustomUserFontAction.updateI18n();
+               useCustomLAFAction.updateI18n();
                SwingUtils.repackCentered(this);
        }
 
+       /**
+        * Wrapper around class name and name of a {@link LookAndFeel}.
+        *
+        * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
+        */
+       private static class LookAndFeelWrapper {
+
+               /** The class name of the look and feel. */
+               private final String className;
+
+               /** The name of a look and feel. */
+               private final String lookAndFeelName;
+
+               /**
+                * Creates a new wrapper around the given class name and name of a look
+                * and feel.
+                *
+                * @param className
+                *            The class name of the look and feel
+                * @param lookAndFeelName
+                *            The name of the look and feel
+                */
+               public LookAndFeelWrapper(String className, String lookAndFeelName) {
+                       this.className = className;
+                       this.lookAndFeelName = lookAndFeelName;
+               }
+
+               /**
+                * Returns the class name of the look and feel.
+                *
+                * @return The class name of the look and feel
+                */
+               public String getClassName() {
+                       return className;
+               }
+
+               /**
+                * {@inheritDoc}
+                */
+               @Override
+               public String toString() {
+                       return lookAndFeelName;
+               }
+
+       }
+
 }