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.Image; 010import java.awt.event.ActionEvent; 011import java.awt.event.KeyEvent; 012import java.io.BufferedReader; 013import java.io.IOException; 014import java.io.InputStream; 015import java.io.InputStreamReader; 016 017import javax.swing.BorderFactory; 018import javax.swing.ImageIcon; 019import javax.swing.JLabel; 020import javax.swing.JPanel; 021import javax.swing.JScrollPane; 022import javax.swing.JTabbedPane; 023import javax.swing.JTextArea; 024 025import org.openstreetmap.josm.Main; 026import org.openstreetmap.josm.data.Version; 027import org.openstreetmap.josm.gui.ExtendedDialog; 028import org.openstreetmap.josm.gui.util.GuiHelper; 029import org.openstreetmap.josm.gui.widgets.JMultilineLabel; 030import org.openstreetmap.josm.gui.widgets.JosmTextArea; 031import org.openstreetmap.josm.gui.widgets.UrlLabel; 032import org.openstreetmap.josm.plugins.PluginHandler; 033import org.openstreetmap.josm.tools.GBC; 034import org.openstreetmap.josm.tools.ImageProvider; 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.setEditable(false); 063 setTextFromResourceFile(readme, "/README"); 064 readme.setCaretPosition(0); 065 066 JosmTextArea revision = new JosmTextArea(); 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("", new ImageIcon(ImageProvider.get("logo.svg").getImage().getScaledInstance(256, 258, Image.SCALE_SMOOTH)), 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}