Ticket #12112: patch0.patch

File patch0.patch, 10.7 KB (added by floscher, 4 years ago)
  • src/org/openstreetmap/josm/actions/AboutAction.java

    diff --git a/src/org/openstreetmap/josm/actions/AboutAction.java b/src/org/openstreetmap/josm/actions/AboutAction.java
    index 064b101..0eabc16 100644
    a b package org.openstreetmap.josm.actions; 
    33
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.awt.Color;
    67import java.awt.Dimension;
    78import java.awt.GridBagLayout;
    89import java.awt.Image;
    910import java.awt.event.ActionEvent;
    1011import java.awt.event.KeyEvent;
     12import java.io.BufferedReader;
     13import java.io.IOException;
     14import java.io.InputStream;
     15import java.io.InputStreamReader;
    1116
    1217import javax.swing.BorderFactory;
    1318import javax.swing.ImageIcon;
    import javax.swing.JOptionPane; 
    1621import javax.swing.JPanel;
    1722import javax.swing.JScrollPane;
    1823import javax.swing.JTabbedPane;
     24import javax.swing.JTextArea;
    1925
    2026import org.openstreetmap.josm.Main;
    2127import org.openstreetmap.josm.data.Version;
    public class AboutAction extends JosmAction { 
    5258    public void actionPerformed(ActionEvent e) {
    5359        final JTabbedPane about = new JTabbedPane();
    5460
    55         Version version = Version.getInstance();
    56 
    5761        JosmTextArea readme = new JosmTextArea();
    5862        readme.setEditable(false);
    59         readme.setText(Version.loadResourceFile(Main.class.getResource("/README")));
     63        setTextFromResourceFile(readme, "/README");
    6064        readme.setCaretPosition(0);
    6165
    6266        JosmTextArea revision = new JosmTextArea();
    6367        revision.setEditable(false);
    64         revision.setText(version.getReleaseAttributes());
     68        revision.setText(Version.getInstance().getReleaseAttributes());
    6569        revision.setCaretPosition(0);
    6670
    6771        JosmTextArea contribution = new JosmTextArea();
    6872        contribution.setEditable(false);
    69         contribution.setText(Version.loadResourceFile(Main.class.getResource("/CONTRIBUTION")));
     73        setTextFromResourceFile(contribution, "/CONTRIBUTION");
    7074        contribution.setCaretPosition(0);
    7175
    7276        JosmTextArea license = new JosmTextArea();
    7377        license.setEditable(false);
    74         license.setText(Version.loadResourceFile(Main.class.getResource("/LICENSE")));
     78        setTextFromResourceFile(license, "/LICENSE");
    7579        license.setCaretPosition(0);
    7680
    7781        JPanel info = new JPanel(new GridBagLayout());
    7882        final JMultilineLabel label = new JMultilineLabel("<html>" +
    7983                "<h1>" + "JOSM – " + tr("Java OpenStreetMap Editor") + "</h1>" +
    8084                "<p style='font-size:75%'></p>" +
    81                 "<p>" + tr("Version {0}", version.getVersionString()) + "</p>" +
     85                "<p>" + tr("Version {0}", Version.getInstance().getVersionString()) + "</p>" +
    8286                "<p style='font-size:50%'></p>" +
    83                 "<p>" + tr("Last change at {0}", version.getTime()) + "</p>" +
     87                "<p>" + tr("Last change at {0}", Version.getInstance().getTime()) + "</p>" +
    8488                "<p style='font-size:50%'></p>" +
    8589                "<p>" + tr("Java Version {0}", System.getProperty("java.version")) + "</p>" +
    8690                "<p style='font-size:50%'></p>" +
    public class AboutAction extends JosmAction { 
    110114                new ImageIcon(ImageProvider.get("logo.svg").getImage().getScaledInstance(256, 258, Image.SCALE_SMOOTH)));
    111115    }
    112116
     117    /**
     118     * Reads the contents of the resource file that is described by the {@code filePath}-attribute and puts that text
     119     * into the {@link JTextArea} given by the {@code ta}-attribute.
     120     * @param ta the {@link JTextArea} to put the files contents into
     121     * @param filePath the path where the resource file to read resides
     122     */
     123    private void setTextFromResourceFile(JTextArea ta, String filePath) {
     124        InputStream is = getClass().getResourceAsStream(filePath);
     125        if (is == null) {
     126            Main.warn(tr("Failed to locate resource ''{0}''.", filePath));
     127            ta.setForeground(new Color(200, 0, 0));
     128            ta.setText(tr("Failed to locate resource ''{0}''.", filePath));
     129        } else {
     130            try {
     131                BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
     132                String line;
     133                while ((line = br.readLine()) != null) {
     134                    ta.append(line+'\n');
     135                }
     136                br.close();
     137            } catch (IOException e) {
     138                Main.warn(tr("Failed to load resource ''{0}'', error is {1}.", filePath, e.toString()));
     139                Main.warn(e);
     140                ta.setForeground(new Color(200, 0, 0));
     141                ta.setText(tr("Failed to load resource ''{0}'', error is {1}.", filePath, e.toString()));
     142            }
     143        }
     144    }
     145
    113146    private static JScrollPane createScrollPane(JosmTextArea area) {
    114147        area.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    115148        area.setOpaque(false);
  • src/org/openstreetmap/josm/data/Version.java

    diff --git a/src/org/openstreetmap/josm/data/Version.java b/src/org/openstreetmap/josm/data/Version.java
    index 252a554..88a74aa 100644
    a b package org.openstreetmap.josm.data; 
    33
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.io.BufferedReader;
    76import java.io.IOException;
    8 import java.net.URL;
    9 import java.util.HashMap;
    10 import java.util.Map;
     7import java.io.InputStream;
    118import java.util.Map.Entry;
    12 import java.util.regex.Matcher;
    13 import java.util.regex.Pattern;
     9import java.util.Properties;
    1410
    1511import org.openstreetmap.josm.Main;
    1612import org.openstreetmap.josm.tools.LanguageInfo;
    17 import org.openstreetmap.josm.tools.Utils;
    1813
    1914/**
    2015 * Provides basic information about the currently used JOSM build.
    public class Version { 
    2823    private static Version instance;
    2924
    3025    /**
    31      * Load the specified resource as string.
    32      *
    33      * @param resource the resource url to load
    34      * @return  the content of the resource file; null, if an error occurred
    35      */
    36     public static String loadResourceFile(URL resource) {
    37         if (resource == null) return null;
    38         String s = null;
    39         try {
    40             StringBuilder sb = new StringBuilder();
    41             try (BufferedReader in = Utils.openURLReader(resource)) {
    42                 for (String line = in.readLine(); line != null; line = in.readLine()) {
    43                     sb.append(line).append('\n');
    44                 }
    45             }
    46             s = sb.toString();
    47         } catch (IOException e) {
    48             Main.error(tr("Failed to load resource ''{0}'', error is {1}.", resource.toString(), e.toString()));
    49             Main.error(e);
    50         }
    51         return s;
    52     }
    53 
    54     /**
    5526     * Replies the unique instance of the version information
    5627     *
    5728     * @return the unique instance of the version information
    public class Version { 
    7041    private String buildName;
    7142    private boolean isLocalBuild;
    7243
    73     protected Map<String, String> parseManifestStyleFormattedString(String content) {
    74         Map<String, String> properties = new HashMap<>();
    75         if (content == null) return properties;
    76         Pattern p = Pattern.compile("^([^:]+):(.*)$");
    77         for (String line: content.split("\n")) {
    78             if (line == null || line.trim().isEmpty()) {
    79                 continue;
    80             }
    81             if (line.matches("^\\s*#.*$")) {
    82                 continue;
    83             }
    84             Matcher m = p.matcher(line);
    85             if (m.matches()) {
    86                 properties.put(m.group(1), m.group(2));
    87             }
    88         }
    89         return properties;
    90     }
    91 
    9244    /**
    9345     * Initializes the version infos from the revision resource file
    9446     *
    95      * @param revisionInfo the revision info loaded from a revision resource file
     47     * @param revisionInfo the revision info from a revision resource file as InputStream
    9648     */
    97     protected void initFromRevisionInfo(String revisionInfo) {
     49    protected void initFromRevisionInfo(InputStream revisionInfo) {
    9850        if (revisionInfo == null) {
    9951            this.releaseDescription = tr("UNKNOWN");
    10052            this.version = JOSM_UNKNOWN_VERSION;
    public class Version { 
    10254            return;
    10355        }
    10456
    105         Map<String, String> properties = parseManifestStyleFormattedString(revisionInfo);
    106         String value = properties.get("Revision");
     57        Properties properties = new Properties();
     58        try {
     59            properties.load(revisionInfo);
     60            revisionInfo.close();
     61        } catch (IOException e) {
     62            Main.warn(tr("Error reading revision info from revision file"));
     63        }
     64        String value = properties.getProperty("Revision");
    10765        if (value != null) {
    10866            value = value.trim();
    10967            try {
    public class Version { 
    11876
    11977        // the last changed data
    12078        //
    121         time = properties.get("Last Changed Date");
     79        time = properties.getProperty("Last Changed Date");
    12280        if (time == null) {
    123             time = properties.get("Build-Date");
     81            time = properties.getProperty("Build-Date");
    12482        }
    12583
    12684        // is this a local build ?
    12785        //
    12886        isLocalBuild = false;
    129         value = properties.get("Is-Local-Build");
     87        value = properties.getProperty("Is-Local-Build");
    13088        if (value != null && "true".equalsIgnoreCase(value.trim()))  {
    13189            isLocalBuild = true;
    13290        }
    public class Version { 
    13492        // is this a specific build ?
    13593        //
    13694        buildName = null;
    137         value = properties.get("Build-Name");
     95        value = properties.getProperty("Build-Name");
    13896        if (value != null && !value.trim().isEmpty())  {
    13997            buildName = value.trim();
    14098        }
    public class Version { 
    142100        // the revision info
    143101        //
    144102        StringBuilder sb = new StringBuilder();
    145         for (Entry<String, String> property: properties.entrySet()) {
     103        for (Entry<Object, Object> property: properties.entrySet()) {
    146104            sb.append(property.getKey()).append(':').append(property.getValue()).append('\n');
    147105        }
    148106        releaseDescription = sb.toString();
    public class Version { 
    152110     * Initializes version info
    153111     */
    154112    public void init() {
    155         URL u = Main.class.getResource("/REVISION");
    156         if (u == null) {
     113        InputStream stream = Main.class.getResourceAsStream("/REVISION");
     114        if (stream == null) {
    157115            Main.warn(tr("The revision file ''/REVISION'' is missing."));
    158116            version = 0;
    159117            releaseDescription = "";
    160118            return;
    161119        }
    162         initFromRevisionInfo(loadResourceFile(u));
     120        initFromRevisionInfo(stream);
    163121    }
    164122
    165123    /**