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.setEditable(false);
061        setTextFromResourceFile(readme, "/README");
062        readme.setCaretPosition(0);
063
064        JosmTextArea revision = new JosmTextArea();
065        revision.setEditable(false);
066        revision.setText(version.getReleaseAttributes());
067        revision.setCaretPosition(0);
068
069        JosmTextArea contribution = new JosmTextArea();
070        contribution.setEditable(false);
071        setTextFromResourceFile(contribution, "/CONTRIBUTION");
072        contribution.setCaretPosition(0);
073
074        JosmTextArea license = new JosmTextArea();
075        license.setEditable(false);
076        setTextFromResourceFile(license, "/LICENSE");
077        license.setCaretPosition(0);
078
079        JPanel info = new JPanel(new GridBagLayout());
080        final JMultilineLabel label = new JMultilineLabel("<html>" +
081                "<h1>" + "JOSM – " + tr("Java OpenStreetMap Editor") + "</h1>" +
082                "<p style='font-size:75%'></p>" +
083                "<p>" + tr("Version {0}", version.getVersionString()) + "</p>" +
084                "<p style='font-size:50%'></p>" +
085                "<p>" + tr("Last change at {0}", version.getTime()) + "</p>" +
086                "<p style='font-size:50%'></p>" +
087                "<p>" + tr("Java Version {0}", System.getProperty("java.version")) + "</p>" +
088                "<p style='font-size:50%'></p>" +
089                "</html>");
090        info.add(label, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 0, 0, 0));
091        info.add(new JLabel(tr("Homepage")), GBC.std().insets(10, 0, 10, 0));
092        info.add(new UrlLabel(Main.getJOSMWebsite(), 2), GBC.eol().fill(GBC.HORIZONTAL));
093        info.add(GBC.glue(0, 5), GBC.eol());
094
095        about.addTab(tr("Info"), info);
096        about.addTab(tr("Readme"), createScrollPane(readme));
097        about.addTab(tr("Revision"), createScrollPane(revision));
098        about.addTab(tr("Contribution"), createScrollPane(contribution));
099        about.addTab(tr("License"), createScrollPane(license));
100        about.addTab(tr("Plugins"), new JScrollPane(PluginHandler.getInfoPanel()));
101
102        // Intermediate panel to allow proper optionPane resizing
103        JPanel panel = new JPanel(new GridBagLayout());
104        panel.setPreferredSize(new Dimension(890, 300));
105        panel.add(new JLabel("", ImageProvider.get("logo.svg", ImageProvider.ImageSizes.ABOUT_LOGO),
106                JLabel.CENTER), GBC.std().insets(0, 5, 0, 0));
107        panel.add(about, GBC.std().fill());
108
109        GuiHelper.prepareResizeableOptionPane(panel, panel.getPreferredSize());
110        int ret = new ExtendedDialog(Main.parent, tr("About JOSM..."), new String[] {tr("OK"), tr("Report bug")})
111            .setButtonIcons(new String[] {"ok", "bug"})
112            .setContent(panel, false)
113            .showDialog().getValue();
114        if (2 == ret) {
115            Main.main.menu.reportbug.actionPerformed(null);
116        }
117    }
118
119    /**
120     * Reads the contents of the resource file that is described by the {@code filePath}-attribute and puts that text
121     * into the {@link JTextArea} given by the {@code ta}-attribute.
122     * @param ta the {@link JTextArea} to put the files contents into
123     * @param filePath the path where the resource file to read resides
124     */
125    private void setTextFromResourceFile(JTextArea ta, String filePath) {
126        InputStream is = getClass().getResourceAsStream(filePath);
127        if (is == null) {
128            displayErrorMessage(ta, tr("Failed to locate resource ''{0}''.", filePath));
129        } else {
130            try (InputStreamReader reader = new InputStreamReader(is, "UTF-8");
131                 BufferedReader br = new BufferedReader(reader)) {
132                String line;
133                while ((line = br.readLine()) != null) {
134                    ta.append(line+'\n');
135                }
136            } catch (IOException e) {
137                Main.warn(e);
138                displayErrorMessage(ta, tr("Failed to load resource ''{0}'', error is {1}.", filePath, e.toString()));
139            }
140        }
141    }
142
143    private static void displayErrorMessage(JTextArea ta, String msg) {
144        Main.warn(msg);
145        ta.setForeground(new Color(200, 0, 0));
146        ta.setText(msg);
147    }
148
149    private static JScrollPane createScrollPane(JosmTextArea area) {
150        area.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
151        area.setOpaque(false);
152        JScrollPane sp = new JScrollPane(area);
153        sp.setBorder(null);
154        sp.setOpaque(false);
155        return sp;
156    }
157}