whitespace fixups
[jSite2.git] / src / net / pterodactylus / jsite / gui / AboutDialog.java
index 2d69b46..8f79a52 100644 (file)
 package net.pterodactylus.jsite.gui;
 
 import java.awt.BorderLayout;
+import java.awt.FlowLayout;
+import java.awt.Font;
+import java.awt.event.ActionEvent;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
 import java.util.List;
 
 import javax.swing.BorderFactory;
+import javax.swing.Box;
+import javax.swing.BoxLayout;
+import javax.swing.JButton;
 import javax.swing.JComponent;
 import javax.swing.JDialog;
+import javax.swing.JLabel;
 import javax.swing.JPanel;
+import javax.swing.JScrollPane;
 import javax.swing.JTabbedPane;
+import javax.swing.JTextArea;
 import javax.swing.SwingConstants;
 
 import net.pterodactylus.jsite.i18n.I18n;
+import net.pterodactylus.jsite.i18n.I18nable;
+import net.pterodactylus.jsite.i18n.gui.I18nAction;
+import net.pterodactylus.jsite.i18n.gui.I18nLabel;
+import net.pterodactylus.jsite.main.Version;
+import net.pterodactylus.util.io.StreamCopier;
+import net.pterodactylus.util.swing.SwingUtils;
 
 /**
  * An “about” dialog.
- * 
+ *
  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
- * @version $Id$
  */
-public class AboutDialog extends JDialog {
+public class AboutDialog extends JDialog implements I18nable {
 
        /** A list of all contributors. */
        private static final List<Contributor> CONTRIBUTORS = new ArrayList<Contributor>();
 
+       /** A list of all code usages. */
+       private static final List<CodeUsage> CODE_USAGES = new ArrayList<CodeUsage>();
+
        static {
                CONTRIBUTORS.add(new Contributor("David ‘Bombe’ Roden", "bombe@freenetproject.org", "Main code"));
+               CODE_USAGES.add(new CodeUsage("Tango Desktop Project", "http://tango.freedesktop.org/", "Creative Commons Attribution-Share Alike 2.5", "Icons"));
        }
 
+       /** The “okay” button action. */
+       private I18nAction okayAction;
+
+       /** The contributors label. */
+       private I18nLabel contributorsLabel;
+
+       /** The i18n maintainer label. */
+       private I18nLabel i18nMaintainerLabel;
+
+       /** The i18n maintainer’s name label. */
+       private JLabel i18nMaintainerNameLabel;
+
+       /** The “other people’s code used” label. */
+       private I18nLabel codeUsageLabel;
+
+       /** The license header. */
+       private I18nLabel licenseHeaderLabel;
+
+       /** The tabbed pane with all the pages. */
+       private JTabbedPane pagesPane;
+
        /**
         * Creates a new “about” dialog.
-        * 
+        *
         * @param swingInterface
         *            The Swing interface
         */
        public AboutDialog(SwingInterface swingInterface) {
-               super(swingInterface.getMainWindow());
+               super(swingInterface.getMainWindow(), I18n.get("aboutDialog.title"));
+               initActions();
                initComponents();
+               pack();
+               SwingUtils.center(this);
+               I18n.registerI18nable(this);
        }
 
        //
@@ -63,6 +110,22 @@ public class AboutDialog extends JDialog {
        //
 
        /**
+        * Initializes all actions.
+        */
+       private void initActions() {
+               okayAction = new I18nAction("general.button.okay") {
+
+                       /**
+                        * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
+                        */
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent e) {
+                               actionOkay();
+                       }
+               };
+       }
+
+       /**
         * Initiliazes all components of the dialog.
         */
        private void initComponents() {
@@ -70,38 +133,158 @@ public class AboutDialog extends JDialog {
                getContentPane().add(contentPane, BorderLayout.CENTER);
                contentPane.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
 
-               JTabbedPane tabbedPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
-               contentPane.add(tabbedPane, BorderLayout.CENTER);
+               pagesPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
+               contentPane.add(pagesPane, BorderLayout.CENTER);
+
+               pagesPane.addTab(I18n.get("aboutDialog.page.about.title"), createAboutPage());
+               pagesPane.setToolTipTextAt(0, I18n.get("aboutDialog.page.about.shortDescription"));
+               pagesPane.addTab(I18n.get("aboutDialog.page.license.title"), createLicensePage());
+               pagesPane.setToolTipTextAt(1, I18n.get("aboutDialog.page.license.shortDescription"));
 
-               tabbedPane.addTab(I18n.get("aboutDialog.page.about.title"), createAboutPage());
-               tabbedPane.setToolTipTextAt(0, I18n.get("aboutDialog.page.about.shortDescription"));
-               tabbedPane.addTab(I18n.get("aboutDialog.page.license.title"), createLicensePage());
-               tabbedPane.setToolTipTextAt(1, I18n.get("aboutDialog.page.license.shortDescription"));
+               JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
+               contentPane.add(buttonPanel, BorderLayout.PAGE_END);
+               buttonPanel.add(new JButton(okayAction));
        }
 
        /**
         * Creates the “about” page.
-        * 
+        *
         * @return The “about” page
         */
        private JComponent createAboutPage() {
-               return new JPanel();
+               JPanel aboutPanel = new JPanel(new BorderLayout(12, 12));
+               aboutPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
+
+               /* the about page consists of an image to the left and text to right. */
+               JComponent textPanel = new Box(BoxLayout.PAGE_AXIS);
+               aboutPanel.add(textPanel, BorderLayout.CENTER);
+
+               JLabel jSiteHeaderLabel = new JLabel("jSite " + Version.getVersion());
+               textPanel.add(jSiteHeaderLabel);
+               textPanel.add(Box.createVerticalStrut(24));
+               Font jSiteHeaderLabelFont = jSiteHeaderLabel.getFont();
+               jSiteHeaderLabel.setFont(jSiteHeaderLabelFont.deriveFont(jSiteHeaderLabelFont.getSize2D() * 2).deriveFont(Font.BOLD));
+
+               contributorsLabel = new I18nLabel("aboutDialog.page.about.label.contributor");
+               textPanel.add(contributorsLabel);
+               textPanel.add(Box.createVerticalStrut(12));
+               contributorsLabel.setFont(contributorsLabel.getFont().deriveFont(Font.BOLD));
+
+               for (Contributor contributor : CONTRIBUTORS) {
+                       JLabel contributorLabel = new JLabel(contributor.getName() + " <" + contributor.getEmail() + "> (" + contributor.getPart() + ")");
+                       textPanel.add(contributorLabel);
+               }
+               textPanel.add(Box.createVerticalStrut(24));
+
+               i18nMaintainerLabel = new I18nLabel("aboutDialog.page.about.label.i18nMaintainer");
+               textPanel.add(i18nMaintainerLabel);
+               textPanel.add(Box.createVerticalStrut(12));
+               i18nMaintainerLabel.setFont(i18nMaintainerLabel.getFont().deriveFont(Font.BOLD));
+
+               i18nMaintainerNameLabel = new JLabel(I18n.get("i18n.maintainer.name") + " <" + I18n.get("i18n.maintainer.email") + ">");
+               textPanel.add(i18nMaintainerNameLabel);
+               textPanel.add(Box.createVerticalStrut(24));
+
+               codeUsageLabel = new I18nLabel("aboutDialog.page.about.label.codeUsage");
+               codeUsageLabel.setFont(codeUsageLabel.getFont().deriveFont(Font.BOLD));
+               textPanel.add(codeUsageLabel);
+               textPanel.add(Box.createVerticalStrut(12));
+
+               for (CodeUsage codeUsage : CODE_USAGES) {
+                       JLabel usageLabel = new JLabel(codeUsage.getName() + " (" + codeUsage.getURL() + ", " + codeUsage.getLicense() + ")");
+                       textPanel.add(usageLabel);
+               }
+
+               return aboutPanel;
        }
 
        /**
         * Creates the “license” page.
-        * 
+        *
         * @return The “license” page
         */
        private JComponent createLicensePage() {
-               return new JPanel();
+               JPanel licensePanel = new JPanel(new BorderLayout(12, 12));
+               licensePanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
+
+               JPanel licenseHeaderPanel = new JPanel(new FlowLayout());
+               licensePanel.add(licenseHeaderPanel, BorderLayout.PAGE_START);
+
+               licenseHeaderLabel = new I18nLabel("aboutDialog.page.license.header", Version.getVersion());
+               licenseHeaderPanel.add(licenseHeaderLabel);
+               licenseHeaderLabel.setAlignmentX(0.5f);
+
+               String licenseText = loadLicenseText();
+               JTextArea licenseArea = new JTextArea(licenseText, 25, 80);
+               licensePanel.add(new JScrollPane(licenseArea), BorderLayout.CENTER);
+               licenseArea.setFont(new Font("Courier", Font.PLAIN, licenseArea.getFont().getSize()));
+               licenseArea.setEditable(false);
+
+               return licensePanel;
+       }
+
+       /**
+        * Loads the license text.
+        *
+        * @return The license text
+        */
+       private String loadLicenseText() {
+               InputStream licenseInputStream = getClass().getResourceAsStream("/LICENSE");
+               if (licenseInputStream == null) {
+                       return "Could not load LICENSE, check your installation.";
+               }
+               ByteArrayOutputStream licenseOutputStream = new ByteArrayOutputStream(20000);
+               try {
+                       StreamCopier.copy(licenseInputStream, licenseOutputStream);
+               } catch (IOException e) {
+                       return "Could not load LICENSE, check your installation.";
+               }
+               String licenseText;
+               try {
+                       licenseText = new String(licenseOutputStream.toByteArray(), "ISO-8859-1");
+               } catch (UnsupportedEncodingException e) {
+                       licenseText = new String(licenseOutputStream.toByteArray());
+               }
+               return licenseText;
+       }
+
+       //
+       // PRIVATE ACTIONS
+       //
+
+       /**
+        * Closes the dialog.
+        */
+       private void actionOkay() {
+               setVisible(false);
+       }
+
+       //
+       // INTERFACE I18nable
+       //
+
+       /**
+        * @see net.pterodactylus.jsite.i18n.I18nable#updateI18n()
+        */
+       public void updateI18n() {
+               contributorsLabel.updateI18n();
+               licenseHeaderLabel.updateI18n();
+               i18nMaintainerLabel.updateI18n();
+               codeUsageLabel.updateI18n();
+               i18nMaintainerNameLabel.setText(I18n.get("i18n.maintainer.name") + " <" + I18n.get("i18n.maintainer.email") + ">");
+               okayAction.updateI18n();
+               setTitle(I18n.get("aboutDialog.title"));
+               pagesPane.setTitleAt(0, I18n.get("aboutDialog.page.about.title"));
+               pagesPane.setToolTipTextAt(0, I18n.get("aboutDialog.page.about.shortDescription"));
+               pagesPane.setTitleAt(1, I18n.get("aboutDialog.page.license.title"));
+               pagesPane.setToolTipTextAt(1, I18n.get("aboutDialog.page.license.shortDescription"));
+               SwingUtils.repackCentered(this);
        }
 
        /**
         * Container for a contributor.
-        * 
+        *
         * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
-        * @version $Id$
         */
        private static class Contributor {
 
@@ -116,7 +299,7 @@ public class AboutDialog extends JDialog {
 
                /**
                 * Creates a new contributor.
-                * 
+                *
                 * @param name
                 *            The name of the contributor
                 * @param email
@@ -132,7 +315,7 @@ public class AboutDialog extends JDialog {
 
                /**
                 * Returns the name of the contributor.
-                * 
+                *
                 * @return The name of the contributor
                 */
                String getName() {
@@ -141,7 +324,7 @@ public class AboutDialog extends JDialog {
 
                /**
                 * Returns the email address of the contributor.
-                * 
+                *
                 * @return The email address of the contributor
                 */
                String getEmail() {
@@ -150,7 +333,7 @@ public class AboutDialog extends JDialog {
 
                /**
                 * Returns the parts where the contributor helped.
-                * 
+                *
                 * @return The parts where the contributor helped
                 */
                String getPart() {
@@ -159,4 +342,81 @@ public class AboutDialog extends JDialog {
 
        }
 
+       /**
+        * A code usage object describes code or other resources that have been
+        * taken from other projects.
+        *
+        * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
+        */
+       private static class CodeUsage {
+
+               /** The name of the project. */
+               private final String name;
+
+               /** The URL of the project. */
+               private final String url;
+
+               /** The license of the project. */
+               private final String license;
+
+               /** The part that is used. */
+               private final String part;
+
+               /**
+                * Creates a new code usage object.
+                *
+                * @param name
+                *            The name of the project
+                * @param url
+                *            The URL of the project
+                * @param license
+                *            The license of the used code
+                * @param part
+                *            The part that is used
+                */
+               public CodeUsage(String name, String url, String license, String part) {
+                       this.name = name;
+                       this.url = url;
+                       this.license = license;
+                       this.part = part;
+               }
+
+               /**
+                * Returns the name of the project.
+                *
+                * @return The name of the project
+                */
+               public String getName() {
+                       return name;
+               }
+
+               /**
+                * Returns the URL of the project.
+                *
+                * @return The URL of the project
+                */
+               public String getURL() {
+                       return url;
+               }
+
+               /**
+                * Returns the license of the used part.
+                *
+                * @return The license of the used part
+                */
+               public String getLicense() {
+                       return license;
+               }
+
+               /**
+                * Returns the used part.
+                *
+                * @return The used part
+                */
+               public String getPart() {
+                       return part;
+               }
+
+       }
+
 }