Changeset 1450 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2009-02-28T20:25:46+01:00 (15 years ago)
Author:
stoecker
Message:

fix #1848 - version in start screen. patch by xeen

Location:
trunk/src/org/openstreetmap/josm
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/GettingStarted.java

    r1420 r1450  
    2525
    2626import org.openstreetmap.josm.Main;
     27import org.openstreetmap.josm.io.CacheCustomContent;
    2728import org.openstreetmap.josm.tools.OpenBrowser;
    2829import org.openstreetmap.josm.tools.WikiReader;
     
    3031
    3132public class GettingStarted extends JPanel {
    32 
    33     static private String content = "";
     33    private String content = "";
    3434    static private String styles = "<style type=\"text/css\">\n"+
    3535            "body { font-family: sans-serif; font-weight: bold; }\n"+
     
    5252    }
    5353
    54     public class readMOTD implements Callable<String> {
     54    /**
     55     * This class encapsulates the "reading URL" task and can be executed in background and in
     56     * parallel. Since the MOTD is many separate pages this speeds things up quite a lot. If no
     57     * localized version is available, it automatically falls back to the international one.
     58     */
     59    private class readMOTD implements Callable<String> {
    5560        private boolean isLocalized;
    5661        private boolean isHelp;
     
    5964        private String urlBase;
    6065
    61         readMOTD(boolean isLocalized, String urlBase, String urlLoc, String urlIntl, boolean isHelp) {
     66        /**
     67         * Read a MOTD page
     68         * @param isLocalized If true, tries to get localized version as defined in urlLoc
     69         * @param urlBase Base URL (i.e. http://www.openstreetmap.de/wiki/)
     70         * @param urlLoc Part to append to base URL to receive localized version
     71         * @param urlIntl Part to append to base URL to receive international version
     72         * @param makeList If true, the URL's contents will be wrapped in a list (<ul><li>)
     73         */
     74        readMOTD(boolean isLocalized, String urlBase, String urlLoc, String urlIntl, boolean makeList) {
    6275          this.isLocalized = isLocalized;
    6376          this.urlBase = urlBase;
    6477          this.urlLoc = urlLoc;
    6578          this.urlIntl = urlIntl;
    66           this.isHelp = isHelp;
    67         }
    68 
     79          this.isHelp = makeList;
     80        }
     81
     82        /*
     83         * Does the actual work
     84         * @see java.util.concurrent.Callable#call()
     85         */
    6986        public String call() {
    7087            WikiReader wr = new WikiReader(urlBase);
    7188            String content = "";
    7289            try {
    73                 // If we hit a non-localized link here, we already know there's no translated version available
     90                // If we hit a non-localized link here, we already know there's no translated
     91                // version available
    7492                String message = isLocalized ? wr.read(urlLoc) : "";
    7593                // Look for non-localized version
     
    8199                        content += message;
    82100                    else
    83                         content += "<ul><li>"+ message.substring(8).replaceAll("\n *\\* +","</li><li>")+"</li></ul>";
     101                        content += "<ul><li>"+ message.substring(8)
     102                                            .replaceAll("\n *\\* +","</li><li>")+"</li></ul>";
    84103            } catch (IOException ioe) {
    85104                try {
     
    87106                        content += wr.read(urlIntl);
    88107                    else
    89                         content += "<ul><li>"+wr.read(urlIntl).substring(8).replaceAll("\n *\\* +","</li><li>")+"</li></ul>";
     108                        content += "<ul><li>"+wr.read(urlIntl).substring(8)
     109                                            .replaceAll("\n *\\* +","</li><li>")+"</li></ul>";
    90110                } catch (IOException ioe2) {
    91111                }
     
    96116    }
    97117
    98     private void assignContent() {
    99         if (content.length() > 0 && Main.pref.getBoolean("help.displaymotd", true)) return;
    100 
    101         String baseurl = Main.pref.get("help.baseurl", "http://josm.openstreetmap.de");
    102         WikiReader wr = new WikiReader(baseurl);
    103         String motdcontent = "";
    104         try {
    105             motdcontent = wr.read(baseurl + "/wiki/MessageOfTheDay?format=txt");
    106         } catch (IOException ioe) {
    107             motdcontent = "<html>" + styles + "<body><h1>" +
    108                 "JOSM - " + tr("Java OpenStreetMap Editor") +
    109                 "</h1>\n<h2 align=\"center\">(" +
    110                 tr ("Message of the day not available") +
    111                 ")</h2>";
    112         }
    113 
    114         int myVersion = AboutAction.getVersionNumber();
    115         String languageCode = Main.getLanguageCodeU();
    116 
    117         // Finds wiki links like (underscores inserted for readability): [wiki:LANGCODE:messageoftheday_CONDITON_REVISION LANGCODE]
    118         // Langcode usually consists of two letters describing the language and may be omitted
    119         // Condition may be one of the following: >  <  <=  =>
    120         // Revision is the JOSM version
    121         Pattern versionPattern = Pattern.compile("\\[wiki:(?:[A-Z]+:)?MessageOfTheDay(\\>\\=|\\<\\=|\\<|\\>)([0-9]+)\\s*([A-Z]*)\\]", Pattern.CASE_INSENSITIVE);
    122         // 1=condition, 2=targetVersion, 3=lang
    123         Matcher matcher = versionPattern.matcher(motdcontent);
    124         matcher.reset();
    125 
    126         ArrayList<String[]> links = new ArrayList<String[]>();
    127         String linksList="";
    128         while (matcher.find()) {
    129             // Discards all but the selected locale and non-localized links
    130             if(!(matcher.group(3)+":").equals(languageCode) && !matcher.group(3).equals(""))
    131                 continue;
    132 
    133             links.add(new String[] {matcher.group(1), matcher.group(2), matcher.group(3)});
    134             linksList += matcher.group(1)+matcher.group(2)+matcher.group(3)+": ";
    135         }
    136 
    137         // We cannot use Main.worker here because it's single-threaded and
    138         // setting it to multi-threading will cause problems elsewhere
    139         ExecutorService slave = Executors.newCachedThreadPool();
    140 
    141         ArrayList<Future<String>> linkContent = new ArrayList<Future<String>>();
    142         for(int i=0; i < links.size(); i++) {
    143             String[] obj = links.get(i);
    144             int targetVersion = Integer.parseInt(obj[1]);
    145             String condition = obj[0];
    146             Boolean isLocalized = !obj[2].equals("");
    147 
    148             // Prefer localized over non-localized links, if they're otherwise the same
    149             if(!isLocalized && linksList.indexOf(condition + obj[1] + languageCode + " ") >= 0)
    150                 continue;
    151 
    152             boolean included = false;
    153 
    154             if(myVersion == 0)
    155               included = true;
    156             else if(condition.equals(">="))
    157               included=myVersion >= targetVersion;
    158             else if(condition.equals(">"))
    159               included = myVersion > targetVersion;
    160             else if(condition.equals("<"))
    161               included=myVersion < targetVersion;
    162             else
    163               included = myVersion <= targetVersion;
    164 
    165             if(!included) continue;
    166 
    167             boolean isHelp = targetVersion == 1;
    168             String urlStart = baseurl + "/wiki/";
    169             String urlEnd = "MessageOfTheDay" + condition + targetVersion + (isHelp ? "" : "?format=txt");
    170             String urlLoc = urlStart + languageCode + urlEnd;
    171             String urlIntl = urlStart + urlEnd;
    172 
    173             // This adds all links to the worker which will download them concurrently
    174             linkContent.add(slave.submit(new readMOTD(isLocalized, baseurl, urlLoc, urlIntl, isHelp)));
    175         }
    176 
    177         for(int i=0; i < linkContent.size(); i++) {
     118    /**
     119     * Grabs current MOTD from cache or webpage and parses it.
     120     */
     121    private class assignContent extends CacheCustomContent {
     122        public assignContent() {
     123            super("motd.html", CacheCustomContent.INTERVAL_DAILY);
     124        }
     125
     126        final private int myVersion = AboutAction.getVersionNumber();
     127
     128        /**
     129         * This function gets executed whenever the cached files need updating
     130         * @see org.openstreetmap.josm.io.CacheCustomContent#updateData()
     131         */
     132        protected byte[] updateData() {
     133            String motd = "";
     134            String baseurl = Main.pref.get("help.baseurl", "http://josm.openstreetmap.de");
     135            WikiReader wr = new WikiReader(baseurl);
     136            String motdcontent = "";
    178137            try {
    179                 content += linkContent.get(i).get();
    180             } catch (Exception e) {}
    181         }
    182        
    183         linkContent.clear();
    184         try {
    185             slave.shutdown();
    186         } catch(SecurityException x) {}
    187        
    188 
    189         content = "<html>\n"+
    190             styles +
    191             "<h1>JOSM - " + tr("Java OpenStreetMap Editor") + "</h1>\n"+
    192             content+"\n"+
    193             "</html>";
     138                motdcontent = wr.read(baseurl + "/wiki/MessageOfTheDay?format=txt");
     139            } catch (IOException ioe) {
     140                motdcontent = "<html>" + styles + "<body><h1>" +
     141                    "JOSM - " + tr("Java OpenStreetMap Editor") +
     142                    "</h1>\n<h2 align=\"center\">(" +
     143                    tr ("Message of the day not available") +
     144                    ")</h2>";
     145            }
     146
     147            String languageCode = Main.getLanguageCodeU();
     148
     149            // Finds wiki links like (underscores inserted for readability):
     150            // [wiki:LANGCODE:messageoftheday_CONDITON_REVISION LANGCODE]
     151            // Langcode usually consists of two letters describing the language and may be omitted
     152            // Condition may be one of the following: >  <  <=  =>
     153            // Revision is the JOSM version
     154            Pattern versionPattern = Pattern.compile(
     155                    "\\[wiki:(?:[A-Z]+:)?MessageOfTheDay(\\>\\=|\\<\\=|\\<|\\>)([0-9]+)\\s*([A-Z]*)\\]",
     156                    Pattern.CASE_INSENSITIVE);
     157            // 1=condition, 2=targetVersion, 3=lang
     158            Matcher matcher = versionPattern.matcher(motdcontent);
     159            matcher.reset();
     160
     161            ArrayList<String[]> links = new ArrayList<String[]>();
     162            String linksList="";
     163            while (matcher.find()) {
     164                // Discards all but the selected locale and non-localized links
     165                if(!(matcher.group(3)+":").equals(languageCode) && !matcher.group(3).equals(""))
     166                    continue;
     167
     168                links.add(new String[] {matcher.group(1), matcher.group(2), matcher.group(3)});
     169                linksList += matcher.group(1)+matcher.group(2)+matcher.group(3)+": ";
     170            }
     171
     172            // We cannot use Main.worker here because it's single-threaded and
     173            // setting it to multi-threading will cause problems elsewhere
     174            ExecutorService slave = Executors.newCachedThreadPool();
     175
     176            ArrayList<Future<String>> linkContents = new ArrayList<Future<String>>();
     177            for(int i=0; i < links.size(); i++) {
     178                String[] obj = links.get(i);
     179                int targetVersion = Integer.parseInt(obj[1]);
     180                String condition = obj[0];
     181                Boolean isLocalized = !obj[2].equals("");
     182
     183                // Prefer localized over non-localized links, if they're otherwise the same
     184                if(!isLocalized && linksList.indexOf(condition + obj[1] + languageCode + " ") >= 0)
     185                    continue;
     186
     187                boolean included = false;
     188
     189                if(myVersion == 0)
     190                  included = true;
     191                else if(condition.equals(">="))
     192                  included=myVersion >= targetVersion;
     193                else if(condition.equals(">"))
     194                  included = myVersion > targetVersion;
     195                else if(condition.equals("<"))
     196                  included=myVersion < targetVersion;
     197                else
     198                  included = myVersion <= targetVersion;
     199
     200                if(!included) continue;
     201
     202                boolean isHelp = targetVersion == 1;
     203                String urlStart = baseurl + "/wiki/";
     204                String urlEnd = "MessageOfTheDay" + condition + targetVersion
     205                                    + (isHelp ? "" : "?format=txt");
     206                String urlLoc = urlStart + languageCode + urlEnd;
     207                String urlIntl = urlStart + urlEnd;
     208
     209                // This adds all links to the worker which will download them concurrently
     210                linkContents.add(slave.submit(new readMOTD(isLocalized, baseurl, urlLoc, urlIntl, isHelp)));
     211            }
     212            // Gets newest version numbers
     213            linkContents.add(slave.submit(new readMOTD(false, baseurl, "",
     214                    baseurl + "/version?format=txt", true)));
     215
     216            for(int i=0; i < linkContents.size()-1; i++) {
     217                try {
     218                    motd += linkContents.get(i).get();
     219                } catch (Exception e) {}
     220            }
     221
     222            motd = "<html>"
     223                + styles
     224                + "<h1>JOSM - "
     225                + tr("Java OpenStreetMap Editor")
     226                + "</h1>"
     227                + motd.replace("</html>", "")
     228                + getVersionNumber(linkContents.get(linkContents.size()-1))
     229                + "</html>";
     230
     231            linkContents.clear();
     232            try {
     233                slave.shutdown();
     234            } catch(SecurityException x) {}
     235
     236            // Save this to prefs in case JOSM is updated so MOTD can be refreshed
     237            Main.pref.putInteger("cache.motd.html.version", myVersion);
     238
     239            return motd.getBytes();
     240        }
     241
     242        /**
     243         * Additionally check if JOSM has been updated and refresh MOTD
     244         */
     245        @Override
     246        protected boolean isCacheValid() {
     247            // We assume a default of myVersion because it only kicks in in two cases:
     248            // 1. Not yet written - but so isn't the interval variable, so it gets updated anyway
     249            // 2. Cannot be written (e.g. while developing). Obviously we don't want to update
     250            //    everytime because of something we can't read.
     251            return Main.pref.getInteger("cache.motd.html.version", myVersion) == myVersion;
     252        }
     253
     254        /**
     255         * Tries to read the version number from a given Future<String>
     256         * @param Future<String> that contains the version page
     257         * @return String with HTML Code
     258         */
     259        private String getVersionNumber(Future<String> linkContent) {
     260            try {
     261                String str = linkContent.get();
     262                Matcher m = Pattern.compile(".*josm-tested\\.jar: *(\\d+).*", Pattern.DOTALL).matcher(str);
     263                m.matches();
     264                int curVersion = Integer.parseInt(m.group(1));
     265                m = Pattern.compile(".*josm-latest\\.jar: *(\\d+).*", Pattern.DOTALL).matcher(str);
     266                m.matches();
     267                int latest = Integer.parseInt(m.group(1));
     268                return "<div style=\"text-align:right;font-size:small;font-weight:normal;\">"
     269                + "<b>"
     270                + (curVersion > myVersion ? tr("Update available") + " &#151; ": "")
     271                + tr("Version Details:") + "</b> "
     272                + tr("Yours: {2}; Current: {0}; <font style=\"font-size:x-small\">"
     273                + "(latest untested: {1} &#150; not recommended)</font>",
     274                curVersion, latest, myVersion)
     275                + "</div>";
     276            } catch(Exception e) {e.printStackTrace();}
     277
     278            return "";
     279        }
    194280    }
    195281
     282    /**
     283     * Initializes getting the MOTD as well as enabling the FileDrop Listener.
     284     * Displays a message while the MOTD is downloading.
     285     */
    196286    public GettingStarted() {
    197287        super(new BorderLayout());
     
    212302        Thread t = new Thread(new Runnable() {
    213303            public void run() {
    214                 assignContent();
     304                if (content.length() == 0 && Main.pref.getBoolean("help.displaymotd", true))
     305                    content = new assignContent().updateIfRequiredString();
     306
    215307                EventQueue.invokeLater(new Runnable() {
    216308                    public void run() {
Note: See TracChangeset for help on using the changeset viewer.