Changeset 3592 in josm for trunk/src


Ignore:
Timestamp:
2010-10-05T20:34:31+02:00 (14 years ago)
Author:
bastiK
Message:

fixed #5465 (based on patch by cmuelle8) - properties dialog tag-wiki-lookup does not have timeout (potentially blocks)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesDialog.java

    r3529 r3592  
    10591059        public void actionPerformed(ActionEvent e) {
    10601060            try {
    1061                 String base = new String(Main.pref.get("url.openstreetmap-wiki", "http://wiki.openstreetmap.org/wiki/"));
    1062                 String l = LanguageInfo.getWikiLanguagePrefix();
    1063                 List<URI> uris = new ArrayList<URI>();
     1061                String base = Main.pref.get("url.openstreetmap-wiki", "http://wiki.openstreetmap.org/wiki/");
     1062                String lang = LanguageInfo.getWikiLanguagePrefix();
     1063                final List<URI> uris = new ArrayList<URI>();
    10641064                int row;
    10651065                if (propertyTable.getSelectedRowCount() == 1) {
    10661066                    row = propertyTable.getSelectedRow();
    10671067                    String key = URLEncoder.encode(propertyData.getValueAt(row, 0).toString(), "UTF-8");
    1068                     @SuppressWarnings("unchecked")
    10691068                    String val = URLEncoder.encode(
    10701069                            ((Map<String,Integer>)propertyData.getValueAt(row, 1))
     
    10721071                    );
    10731072
    1074                     uris.add(new URI(String.format("%s%sTag:%s=%s", base, l, key, val)));
     1073                    uris.add(new URI(String.format("%s%sTag:%s=%s", base, lang, key, val)));
    10751074                    uris.add(new URI(String.format("%sTag:%s=%s", base, key, val)));
    1076                     uris.add(new URI(String.format("%s%sKey:%s", base, l, key)));
     1075                    uris.add(new URI(String.format("%s%sKey:%s", base, lang, key)));
    10771076                    uris.add(new URI(String.format("%sKey:%s", base, key)));
    1078                     uris.add(new URI(String.format("%s%sMap_Features", base, l)));
     1077                    uris.add(new URI(String.format("%s%sMap_Features", base, lang)));
    10791078                    uris.add(new URI(String.format("%sMap_Features", base)));
    10801079                } else if (membershipTable.getSelectedRowCount() == 1) {
     
    10851084
    10861085                    if (type != null && !type.equals("")) {
    1087                         uris.add(new URI(String.format("%s%sRelation:%s", base, l, type)));
     1086                        uris.add(new URI(String.format("%s%sRelation:%s", base, lang, type)));
    10881087                        uris.add(new URI(String.format("%sRelation:%s", base, type)));
    10891088                    }
    10901089
    1091                     uris.add(new URI(String.format("%s%sRelations", base, l)));
     1090                    uris.add(new URI(String.format("%s%sRelations", base, lang)));
    10921091                    uris.add(new URI(String.format("%sRelations", base)));
    10931092                } else {
    10941093                    // give the generic help page, if more than one element is selected
    1095                     uris.add(new URI(String.format("%s%sMap_Features", base, l)));
     1094                    uris.add(new URI(String.format("%s%sMap_Features", base, lang)));
    10961095                    uris.add(new URI(String.format("%sMap_Features", base)));
    10971096                }
    10981097
    1099                 // find a page that actually exists in the wiki
    1100                 HttpURLConnection conn;
    1101                 for(URI u : uris) {
    1102                     conn = (HttpURLConnection) u.toURL().openConnection();
    1103 
    1104                     if (conn.getResponseCode() != 200) {
    1105                         System.out.println("INFO: " + u + " does not exist");
    1106                         conn.disconnect();
    1107                     } else {
    1108                         int osize = conn.getContentLength();
    1109                         conn.disconnect();
    1110 
    1111                         conn = (HttpURLConnection) new URI(u.toString()
    1112                                 .replace("=", "%3D") /* do not URLencode whole string! */
    1113                                 .replaceFirst("/wiki/", "/w/index.php?redirect=no&title=")
    1114                         ).toURL().openConnection();
    1115 
    1116                         /* redirect pages have different content length, but retrieving a "nonredirect"
    1117                          *  page using index.php and the direct-link method gives slightly different
    1118                          *  content lengths, so we have to be fuzzy.. (this is UGLY, recode if u know better)
    1119                          */
    1120                         if (Math.abs(conn.getContentLength()-osize) > 200) {
    1121                             System.out.println("INFO: " + u + " is a mediawiki redirect");
    1122                             conn.disconnect();
    1123                         } else {
    1124                             System.out.println("INFO: browsing to " + u);
    1125                             conn.disconnect();
    1126 
    1127                             OpenBrowser.displayUrl(u.toString());
    1128                             break;
     1098                Main.worker.execute(new Runnable(){
     1099                    public void run() {
     1100                        try {
     1101                            // find a page that actually exists in the wiki
     1102                            HttpURLConnection conn;
     1103                            for (URI u : uris) {
     1104                                conn = (HttpURLConnection) u.toURL().openConnection();
     1105                                conn.setConnectTimeout(5000);
     1106
     1107                                if (conn.getResponseCode() != 200) {
     1108                                    System.out.println("INFO: " + u + " does not exist");
     1109                                    conn.disconnect();
     1110                                } else {
     1111                                    int osize = conn.getContentLength();
     1112                                    conn.disconnect();
     1113
     1114                                    conn = (HttpURLConnection) new URI(u.toString()
     1115                                            .replace("=", "%3D") /* do not URLencode whole string! */
     1116                                            .replaceFirst("/wiki/", "/w/index.php?redirect=no&title=")
     1117                                    ).toURL().openConnection();
     1118                                    conn.setConnectTimeout(5000);
     1119
     1120                                    /* redirect pages have different content length, but retrieving a "nonredirect"
     1121                                     *  page using index.php and the direct-link method gives slightly different
     1122                                     *  content lengths, so we have to be fuzzy.. (this is UGLY, recode if u know better)
     1123                                     */
     1124                                    if (Math.abs(conn.getContentLength() - osize) > 200) {
     1125                                        System.out.println("INFO: " + u + " is a mediawiki redirect");
     1126                                        conn.disconnect();
     1127                                    } else {
     1128                                        System.out.println("INFO: browsing to " + u);
     1129                                        conn.disconnect();
     1130
     1131                                        OpenBrowser.displayUrl(u.toString());
     1132                                        break;
     1133                                    }
     1134                                }
     1135                            }
     1136                        } catch (Exception e) {
     1137                            e.printStackTrace();
    11291138                        }
    11301139                    }
    1131                 }
     1140                });
    11321141            } catch (Exception e1) {
    11331142                e1.printStackTrace();
Note: See TracChangeset for help on using the changeset viewer.