added more german i18n
[jSite2.git] / src / net / pterodactylus / jsite / gui / AboutDialog.java
1 /*
2  * jSite2 - AboutDialog.java -
3  * Copyright © 2008 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package net.pterodactylus.jsite.gui;
21
22 import java.awt.BorderLayout;
23 import java.awt.FlowLayout;
24 import java.awt.Font;
25 import java.awt.event.ActionEvent;
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.UnsupportedEncodingException;
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import javax.swing.BorderFactory;
34 import javax.swing.Box;
35 import javax.swing.BoxLayout;
36 import javax.swing.JButton;
37 import javax.swing.JComponent;
38 import javax.swing.JDialog;
39 import javax.swing.JLabel;
40 import javax.swing.JPanel;
41 import javax.swing.JScrollPane;
42 import javax.swing.JTabbedPane;
43 import javax.swing.JTextArea;
44 import javax.swing.SwingConstants;
45
46 import net.pterodactylus.jsite.i18n.I18n;
47 import net.pterodactylus.jsite.i18n.I18nable;
48 import net.pterodactylus.jsite.i18n.gui.I18nAction;
49 import net.pterodactylus.jsite.i18n.gui.I18nLabel;
50 import net.pterodactylus.jsite.main.Version;
51 import net.pterodactylus.util.io.StreamCopier;
52 import net.pterodactylus.util.swing.SwingUtils;
53
54 /**
55  * An “about” dialog.
56  *
57  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
58  * @version $Id$
59  */
60 public class AboutDialog extends JDialog implements I18nable {
61
62         /** A list of all contributors. */
63         private static final List<Contributor> CONTRIBUTORS = new ArrayList<Contributor>();
64
65         static {
66                 CONTRIBUTORS.add(new Contributor("David ‘Bombe’ Roden", "bombe@freenetproject.org", "Main code"));
67         }
68
69         /** The “okay” button action. */
70         private I18nAction okayAction;
71
72         /** The contributors label. */
73         private I18nLabel contributorsLabel;
74
75         /** The i18n maintainer label. */
76         private I18nLabel i18nMaintainerLabel;
77
78         /** The i18n maintainer’s name label. */
79         private JLabel i18nMaintainerNameLabel;
80
81         /** The license header. */
82         private I18nLabel licenseHeaderLabel;
83
84         /** The tabbed pane with all the pages. */
85         private JTabbedPane pagesPane;
86
87         /**
88          * Creates a new “about” dialog.
89          *
90          * @param swingInterface
91          *            The Swing interface
92          */
93         public AboutDialog(SwingInterface swingInterface) {
94                 super(swingInterface.getMainWindow(), I18n.get("aboutDialog.title"));
95                 initActions();
96                 initComponents();
97                 pack();
98                 SwingUtils.center(this);
99                 I18n.registerI18nable(this);
100         }
101
102         //
103         // PRIVATE METHODS
104         //
105
106         /**
107          * Initializes all actions.
108          */
109         private void initActions() {
110                 okayAction = new I18nAction("general.button.okay") {
111
112                         /**
113                          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
114                          */
115                         @SuppressWarnings("synthetic-access")
116                         public void actionPerformed(ActionEvent e) {
117                                 actionOkay();
118                         }
119                 };
120         }
121
122         /**
123          * Initiliazes all components of the dialog.
124          */
125         private void initComponents() {
126                 JPanel contentPane = new JPanel(new BorderLayout(12, 12));
127                 getContentPane().add(contentPane, BorderLayout.CENTER);
128                 contentPane.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
129
130                 pagesPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
131                 contentPane.add(pagesPane, BorderLayout.CENTER);
132
133                 pagesPane.addTab(I18n.get("aboutDialog.page.about.title"), createAboutPage());
134                 pagesPane.setToolTipTextAt(0, I18n.get("aboutDialog.page.about.shortDescription"));
135                 pagesPane.addTab(I18n.get("aboutDialog.page.license.title"), createLicensePage());
136                 pagesPane.setToolTipTextAt(1, I18n.get("aboutDialog.page.license.shortDescription"));
137
138                 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
139                 contentPane.add(buttonPanel, BorderLayout.PAGE_END);
140                 buttonPanel.add(new JButton(okayAction));
141         }
142
143         /**
144          * Creates the “about” page.
145          *
146          * @return The “about” page
147          */
148         private JComponent createAboutPage() {
149                 JPanel aboutPanel = new JPanel(new BorderLayout(12, 12));
150                 aboutPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
151
152                 /* the about page consists of an image to the left and text to right. */
153                 JComponent textPanel = new Box(BoxLayout.PAGE_AXIS);
154                 aboutPanel.add(textPanel, BorderLayout.CENTER);
155
156                 JLabel jSiteHeaderLabel = new JLabel("jSite " + Version.getVersion());
157                 textPanel.add(jSiteHeaderLabel);
158                 textPanel.add(Box.createVerticalStrut(24));
159                 Font jSiteHeaderLabelFont = jSiteHeaderLabel.getFont();
160                 jSiteHeaderLabel.setFont(jSiteHeaderLabelFont.deriveFont(jSiteHeaderLabelFont.getSize2D() * 2).deriveFont(Font.BOLD));
161
162                 contributorsLabel = new I18nLabel("aboutDialog.page.about.label.contributor");
163                 textPanel.add(contributorsLabel);
164                 textPanel.add(Box.createVerticalStrut(12));
165                 contributorsLabel.setFont(contributorsLabel.getFont().deriveFont(Font.BOLD));
166
167                 for (Contributor contributor: CONTRIBUTORS) {
168                         JLabel contributorLabel = new JLabel(contributor.getName() + " <" + contributor.getEmail() + "> (" + contributor.getPart() + ")");
169                         textPanel.add(contributorLabel);
170                 }
171                 textPanel.add(Box.createVerticalStrut(24));
172
173                 i18nMaintainerLabel = new I18nLabel("aboutDialog.page.about.label.i18nMaintainer");
174                 textPanel.add(i18nMaintainerLabel);
175                 textPanel.add(Box.createVerticalStrut(12));
176                 i18nMaintainerLabel.setFont(i18nMaintainerLabel.getFont().deriveFont(Font.BOLD));
177
178                 i18nMaintainerNameLabel = new JLabel(I18n.get("i18n.maintainer.name") + " <" + I18n.get("i18n.maintainer.email") + ">");
179                 textPanel.add(i18nMaintainerNameLabel);
180
181                 return aboutPanel;
182         }
183
184         /**
185          * Creates the “license” page.
186          *
187          * @return The “license” page
188          */
189         private JComponent createLicensePage() {
190                 JPanel licensePanel = new JPanel(new BorderLayout(12, 12));
191                 licensePanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
192
193                 licenseHeaderLabel = new I18nLabel("aboutDialog.page.license.header", Version.getVersion());
194                 licensePanel.add(licenseHeaderLabel, BorderLayout.PAGE_START);
195                 licenseHeaderLabel.setAlignmentX(0.5f);
196
197                 String licenseText = loadLicenseText();
198                 JTextArea licenseArea = new JTextArea(licenseText, 25, 80);
199                 licensePanel.add(new JScrollPane(licenseArea), BorderLayout.CENTER);
200                 licenseArea.setFont(new Font("Courier", Font.PLAIN, licenseArea.getFont().getSize()));
201                 licenseArea.setEditable(false);
202
203                 return licensePanel;
204         }
205
206         /**
207          * Loads the license text.
208          *
209          * @return The license text
210          */
211         private String loadLicenseText() {
212                 InputStream licenseInputStream = getClass().getResourceAsStream("/LICENSE");
213                 if (licenseInputStream == null) {
214                         return "Could not load LICENSE, check your installation.";
215                 }
216                 ByteArrayOutputStream licenseOutputStream = new ByteArrayOutputStream(20000);
217                 try {
218                         StreamCopier.copy(licenseInputStream, licenseOutputStream);
219                 } catch (IOException e) {
220                         return "Could not load LICENSE, check your installation.";
221                 }
222                 String licenseText;
223                 try {
224                         licenseText = new String(licenseOutputStream.toByteArray(), "ISO-8859-1");
225                 } catch (UnsupportedEncodingException e) {
226                         licenseText = new String(licenseOutputStream.toByteArray());
227                 }
228                 return licenseText;
229         }
230
231         //
232         // PRIVATE ACTIONS
233         //
234
235         /**
236          * Closes the dialog.
237          */
238         private void actionOkay() {
239                 setVisible(false);
240         }
241
242         //
243         // INTERFACE I18nable
244         //
245
246         /**
247          * @see net.pterodactylus.jsite.i18n.I18nable#updateI18n()
248          */
249         public void updateI18n() {
250                 contributorsLabel.updateI18n();
251                 licenseHeaderLabel.updateI18n();
252                 i18nMaintainerLabel.updateI18n();
253                 i18nMaintainerNameLabel.setText(I18n.get("i18n.maintainer.name") + " <" + I18n.get("i18n.maintainer.email") + ">");
254                 okayAction.updateI18n();
255                 setTitle(I18n.get("aboutDialog.title"));
256                 pagesPane.setTitleAt(0, I18n.get("aboutDialog.page.about.title"));
257                 pagesPane.setToolTipTextAt(0, I18n.get("aboutDialog.page.about.shortDescription"));
258                 pagesPane.setTitleAt(1, I18n.get("aboutDialog.page.license.title"));
259                 pagesPane.setToolTipTextAt(1, I18n.get("aboutDialog.page.license.shortDescription"));
260                 SwingUtils.repackCentered(this);
261         }
262
263         /**
264          * Container for a contributor.
265          *
266          * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
267          * @version $Id$
268          */
269         private static class Contributor {
270
271                 /** The name of the contributor. */
272                 private final String name;
273
274                 /** The email address of the contributor. */
275                 private final String email;
276
277                 /** The parts where the contributor helped. */
278                 private final String part;
279
280                 /**
281                  * Creates a new contributor.
282                  *
283                  * @param name
284                  *            The name of the contributor
285                  * @param email
286                  *            The email address of the contributor
287                  * @param part
288                  *            The parts where the contributor helped
289                  */
290                 public Contributor(String name, String email, String part) {
291                         this.name = name;
292                         this.email = email;
293                         this.part = part;
294                 }
295
296                 /**
297                  * Returns the name of the contributor.
298                  *
299                  * @return The name of the contributor
300                  */
301                 String getName() {
302                         return name;
303                 }
304
305                 /**
306                  * Returns the email address of the contributor.
307                  *
308                  * @return The email address of the contributor
309                  */
310                 String getEmail() {
311                         return email;
312                 }
313
314                 /**
315                  * Returns the parts where the contributor helped.
316                  *
317                  * @return The parts where the contributor helped
318                  */
319                 String getPart() {
320                         return part;
321                 }
322
323         }
324
325 }