001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.Color;
007import java.awt.Dimension;
008import java.awt.GridBagLayout;
009import java.awt.event.ActionEvent;
010import java.awt.event.KeyEvent;
011import java.io.BufferedReader;
012import java.io.IOException;
013import java.io.InputStream;
014import java.io.InputStreamReader;
015
016import javax.swing.BorderFactory;
017import javax.swing.JLabel;
018import javax.swing.JPanel;
019import javax.swing.JScrollPane;
020import javax.swing.JTabbedPane;
021import javax.swing.JTextArea;
022
023import org.openstreetmap.josm.Main;
024import org.openstreetmap.josm.data.Version;
025import org.openstreetmap.josm.gui.ExtendedDialog;
026import org.openstreetmap.josm.gui.util.GuiHelper;
027import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
028import org.openstreetmap.josm.gui.widgets.JosmTextArea;
029import org.openstreetmap.josm.gui.widgets.UrlLabel;
030import org.openstreetmap.josm.plugins.PluginHandler;
031import org.openstreetmap.josm.tools.GBC;
032import org.openstreetmap.josm.tools.ImageProvider;
033import org.openstreetmap.josm.tools.Shortcut;
034
035/**
036 * Nice about screen.
037 *
038 * The REVISION resource is read and if present, it shows the revision information of the jar-file.
039 *
040 * @author imi
041 */
042public final class AboutAction extends JosmAction {
043
044    /**
045     * Constructs a new {@code AboutAction}.
046     */
047    public AboutAction() {
048        super(tr("About"), "logo", tr("Display the about screen."),
049            Shortcut.registerShortcut("system:about", tr("About"),
050            KeyEvent.VK_F1, Shortcut.SHIFT), true);
051    }
052
053    @Override
054    public void actionPerformed(ActionEvent e) {
055        final JTabbedPane about = new JTabbedPane();
056
057        Version version = Version.getInstance();
058
059        JosmTextArea readme = new JosmTextArea();
060        readme.setFont(GuiHelper.getMonospacedFont(readme));
061        readme.setEditable(false);
062        setTextFromResourceFile(readme, "/README");
063        readme.setCaretPosition(0);
064
065        JosmTextArea revision = new JosmTextArea();
066        revision.setFont(GuiHelper.getMonospacedFont(revision));
067        revision.setEditable(false);
068        revision.setText(version.getReleaseAttributes());
069        revision.setCaretPosition(0);
070
071        JosmTextArea contribution = new JosmTextArea();
072        contribution.setEditable(false);
073        setTextFromResourceFile(contribution, "/CONTRIBUTION");
074        contribution.setCaretPosition(0);
075
076        JosmTextArea license = new JosmTextArea();
077        license.setEditable(false);
078        setTextFromResourceFile(license, "/LICENSE");
079        license.setCaretPosition(0);
080
081        JPanel info = new JPanel(new GridBagLayout());
082        final JMultilineLabel label = new JMultilineLabel("<html>" +
083                "<h1>" + "JOSM – " + tr("Java OpenStreetMap Editor") + "</h1>" +
084                "<p style='font-size:75%'></p>" +
085                "<p>" + tr("Version {0}", version.getVersionString()) + "</p>" +
086                "<p style='font-size:50%'></p>" +
087                "<p>" + tr("Last change at {0}", version.getTime()) + "</p>" +
088                "<p style='font-size:50%'></p>" +
089                "<p>" + tr("Java Version {0}", System.getProperty("java.version")) + "</p>" +
090                "<p style='font-size:50%'></p>" +
091                "</html>");
092        info.add(label, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 0, 0, 0));
093        info.add(new JLabel(tr("Homepage")), GBC.std().insets(10, 0, 10, 0));
094        info.add(new UrlLabel(Main.getJOSMWebsite(), 2), GBC.eol().fill(GBC.HORIZONTAL));
095        info.add(GBC.glue(0, 5), GBC.eol());
096
097        about.addTab(tr("Info"), info);
098        about.addTab(tr("Readme"), createScrollPane(readme));
099        about.addTab(tr("Revision"), createScrollPane(revision));
100        about.addTab(tr("Contribution"), createScrollPane(contribution));
101        about.addTab(tr("License"), createScrollPane(license));
102        about.addTab(tr("Plugins"), new JScrollPane(PluginHandler.getInfoPanel()));
103
104        // Intermediate panel to allow proper optionPane resizing
105        JPanel panel = new JPanel(new GridBagLayout());
106        panel.setPreferredSize(new Dimension(890, 300));
107        panel.add(new JLabel("", ImageProvider.get("logo.svg", ImageProvider.ImageSizes.ABOUT_LOGO),
108                JLabel.CENTER), GBC.std().insets(0, 5, 0, 0));
109        panel.add(about, GBC.std().fill());
110
111        GuiHelper.prepareResizeableOptionPane(panel, panel.getPreferredSize());
112        int ret = new ExtendedDialog(Main.parent, tr("About JOSM..."), new String[] {tr("OK"), tr("Report bug")})
113            .setButtonIcons(new String[] {"ok", "bug"})
114            .setContent(panel, false)
115            .showDialog().getValue();
116        if (2 == ret) {
117            Main.main.menu.reportbug.actionPerformed(null);
118        }
119    }
120
121    /**
122     * Reads the contents of the resource file that is described by the {@code filePath}-attribute and puts that text
123     * into the {@link JTextArea} given by the {@code ta}-attribute.
124     * @param ta the {@link JTextArea} to put the files contents into
125     * @param filePath the path where the resource file to read resides
126     */
127    private void setTextFromResourceFile(JTextArea ta, String filePath) {
128        InputStream is = getClass().getResourceAsStream(filePath);
129        if (is == null) {
130            displayErrorMessage(ta, tr("Failed to locate resource ''{0}''.", filePath));
131        } else {
132            try (InputStreamReader reader = new InputStreamReader(is, "UTF-8");
133                 BufferedReader br = new BufferedReader(reader)) {
134                String line;
135                while ((line = br.readLine()) != null) {
136                    ta.append(line+'\n');
137                }
138            } catch (IOException e) {
139                Main.warn(e);
140                displayErrorMessage(ta, tr("Failed to load resource ''{0}'', error is {1}.", filePath, e.toString()));
141            }
142        }
143    }
144
145    private static void displayErrorMessage(JTextArea ta, String msg) {
146        Main.warn(msg);
147        ta.setForeground(new Color(200, 0, 0));
148        ta.setText(msg);
149    }
150
151    private static JScrollPane createScrollPane(JosmTextArea area) {
152        area.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
153        area.setOpaque(false);
154        JScrollPane sp = new JScrollPane(area);
155        sp.setBorder(null);
156        sp.setOpaque(false);
157        return sp;
158    }
159}