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