Changeset 1350 in josm for trunk/src


Ignore:
Timestamp:
2009-01-31T14:39:28+01:00 (15 years ago)
Author:
stoecker
Message:

speedup start, fix #2106, patch by xeen and Michel Marti

Location:
trunk/src/org/openstreetmap/josm/gui
Files:
2 edited

Legend:

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

    r1290 r1350  
    77import java.io.IOException;
    88import java.util.ArrayList;
     9import java.util.concurrent.Executors;
     10import java.util.concurrent.ExecutorService;
     11import java.util.concurrent.Callable;
     12import java.util.concurrent.Future;
    913import java.util.regex.Matcher;
    1014import java.util.regex.Pattern;
     
    1216import java.awt.BorderLayout;
    1317import java.awt.Component;
     18import java.awt.EventQueue;
    1419
    1520import javax.swing.JScrollPane;
     
    4146                OpenBrowser.displayUrl(e.getDescription());
    4247            }
     48        }
     49    }
     50   
     51    public class readMOTD implements Callable<String> {
     52        private boolean isLocalized;
     53        private boolean isHelp;
     54        private String urlLoc;
     55        private String urlIntl;
     56        private String urlBase;
     57       
     58        readMOTD(boolean isLocalized, String urlBase, String urlLoc, String urlIntl, boolean isHelp) {
     59          this.isLocalized = isLocalized;
     60          this.urlBase = urlBase;
     61          this.urlLoc = urlLoc;
     62          this.urlIntl = urlIntl;
     63          this.isHelp = isHelp;
     64        }
     65
     66        public String call() {
     67            WikiReader wr = new WikiReader(urlBase);
     68            String content = "";
     69            try {
     70                // If we hit a non-localized link here, we already know there's no translated version available
     71                String message = isLocalized ? wr.read(urlLoc) : "";
     72                // Look for non-localized version
     73                if (message.equals(""))
     74                    message = wr.read(urlIntl);
     75
     76                if (!message.equals(""))
     77                    if(isHelp)
     78                        content += message;
     79                    else
     80                        content += "<ul><li>"+ message.substring(8)+"</li></ul>";
     81            } catch (IOException ioe) {
     82                try {
     83                    if(isHelp)
     84                        content += wr.read(urlIntl);
     85                    else
     86                        content += "<ul><li>"+wr.read(urlIntl).substring(8)+"</li></ul>";
     87                } catch (IOException ioe2) {
     88                }
     89            }
     90           
     91            return content;
    4392        }
    4493    }
     
    72121        matcher.reset();
    73122
    74         ArrayList<Object> links = new ArrayList<Object>();
     123        ArrayList<String[]> links = new ArrayList<String[]>();
    75124        String linksList="";
    76125        while (matcher.find()) {
     
    82131            linksList += matcher.group(1)+matcher.group(2)+matcher.group(3)+": ";
    83132        }
    84 
     133       
     134        // We cannot use Main.worker here because it's single-threaded and
     135        // setting it to multi-threading will cause problems elsewhere
     136        ExecutorService slave = Executors.newCachedThreadPool();
     137       
     138        ArrayList<Future<String>> linkContent = new ArrayList<Future<String>>();
    85139        for(int i=0; i < links.size(); i++) {
    86140            String[] obj = (String[])links.get(i);
     
    90144
    91145            // Prefer localized over non-localized links, if they're otherwise the same
    92             if(!isLocalized && linksList.indexOf(condition+obj[1]+languageCode+" ") >= 0)
     146            if(!isLocalized && linksList.indexOf(condition + obj[1] + languageCode + " ") >= 0)
    93147                continue;
    94148
     
    107161
    108162            if(!included) continue;
    109 
    110             Boolean isHelp = targetVersion == 1;
    111 
     163           
     164            boolean isHelp = targetVersion == 1;
    112165            String urlStart = baseurl + "/wiki/";
    113166            String urlEnd = "MessageOfTheDay" + condition + targetVersion + (isHelp ? "" : "?format=txt");
    114 
     167            String urlLoc = urlStart + languageCode + urlEnd;
     168            String urlIntl = urlStart + urlEnd;
     169           
     170            // This adds all links to the worker which will download them concurrently
     171            linkContent.add(slave.submit(new readMOTD(isLocalized, baseurl, urlLoc, urlIntl, isHelp)));
     172        }
     173       
     174        for(int i=0; i < linkContent.size(); i++) {
    115175            try {
    116                 // If we hit a non-localized link here, we already know there's no translated version available
    117                 String message = isLocalized ? wr.read(urlStart + languageCode + urlEnd) : "";
    118                 // Look for non-localized version
    119                 if (message.equals(""))
    120                     message = wr.read(urlStart + urlEnd);
    121 
    122                 if (!message.equals(""))
    123                     if(isHelp)
    124                         content += message;
    125                     else
    126                         content += "<ul><li>"+ message.substring(8)+"</li></ul>";
    127             } catch (IOException ioe) {
    128                 try {
    129                     if(isHelp)
    130                         content += wr.read(urlStart + urlEnd);
    131                     else
    132                         content += "<ul><li>"+wr.read(urlStart + urlEnd).substring(8)+"</li></ul>";
    133                 } catch (IOException ioe2) {
    134                 }
    135             }
    136         }
    137 
     176                content += linkContent.get(i).get();
     177            } catch (Exception e) {}
     178        }
     179       
    138180        content = "<html>\n"+
    139181            "<style type=\"text/css\">\n"+
     
    148190    public GettingStarted() {
    149191        super(new BorderLayout());
    150         assignContent();
    151 
     192        final LinkGeneral lg = new LinkGeneral(tr("Download \"Message of the day\""));
     193        JScrollPane scroller = new JScrollPane(lg);
    152194        // panel.add(GBC.glue(0,1), GBC.eol());
    153195        //panel.setMinimumSize(new Dimension(400, 600));
    154196        Component linkGeneral = new LinkGeneral(content);
    155         JScrollPane scroller = new JScrollPane(linkGeneral);
    156197        scroller.setViewportBorder(new EmptyBorder(10,100,10,100));
    157198        add(scroller, BorderLayout.CENTER);
    158199
     200        // Asynchronously get MOTD to speed-up JOSM startup
     201        Thread t = new Thread(new Runnable() {
     202            public void run() {
     203                assignContent();
     204                EventQueue.invokeLater(new Runnable() {
     205                    public void run() {
     206                       lg.setText(content);
     207                       lg.moveCaretPosition(0);
     208                    }
     209                });
     210            }
     211        }, "MOTD-Loader");
     212        t.setDaemon(true);
     213        t.start();
     214
    159215        new FileDrop(linkGeneral);
    160216    }
  • trunk/src/org/openstreetmap/josm/gui/MapStatus.java

    r1258 r1350  
    2222import java.awt.event.MouseEvent;
    2323import java.awt.event.MouseMotionListener;
     24import java.awt.event.MouseListener;
    2425import java.lang.reflect.InvocationTargetException;
    2526import java.text.DecimalFormat;
     
    345346        return "Statusline";
    346347    }
     348   
     349    @Override
     350    public void addMouseListener(MouseListener ml) {
     351        //super.addMouseListener(ml);
     352        lonText.addMouseListener(ml);
     353        latText.addMouseListener(ml);
     354    }
    347355
    348356    public void setHelpText(String t) {
     
    360368        distText.setText(dist < 0 ? "--" : text);
    361369    }
    362 
    363370}
Note: See TracChangeset for help on using the changeset viewer.