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


Ignore:
Timestamp:
2010-09-14T18:21:43+02:00 (14 years ago)
Author:
bastiK
Message:

see #5443 (patch by Christian Müller) - integrate josm tag help with osm wiki (improvements)

File:
1 edited

Legend:

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

    r3525 r3529  
    2020import java.net.HttpURLConnection;
    2121import java.net.URI;
     22import java.net.URLEncoder;
    2223import java.util.ArrayList;
    2324import java.util.Collection;
     
    2829import java.util.List;
    2930import java.util.Map;
     31import java.util.Map.Entry;
     32import java.util.TreeMap;
    3033import java.util.Vector;
    31 import java.util.TreeMap;
    32 import java.util.Map.Entry;
    3334
    3435import javax.swing.AbstractAction;
     
    7071import org.openstreetmap.josm.data.osm.event.DataSetListenerAdapter;
    7172import org.openstreetmap.josm.data.osm.event.DatasetEventManager;
     73import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode;
    7274import org.openstreetmap.josm.data.osm.event.SelectionEventManager;
    73 import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode;
    7475import org.openstreetmap.josm.gui.DefaultNameFormatter;
    7576import org.openstreetmap.josm.gui.ExtendedDialog;
     
    164165
    165166    private DataSetListenerAdapter dataChangedAdapter = new DataSetListenerAdapter(this);
     167    private HelpAction helpAction = new HelpAction();
    166168    private AddAction addAction = new AddAction();
    167169    private Shortcut addActionShortcut = Shortcut.registerShortcut("properties:add", tr("Add Properties"), KeyEvent.VK_B,
     
    543545        propertyData.setColumnIdentifiers(new String[]{tr("Key"),tr("Value")});
    544546        propertyTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
     547        propertyTable.addMouseListener(new PopupMenuLauncher() {
     548            @Override
     549            public void launch(MouseEvent evt) {
     550                Point p = evt.getPoint();
     551                int row = propertyTable.rowAtPoint(p);
     552                if (row > -1) {
     553                    propertyTable.changeSelection(row, 0, false, false);
     554                    JPopupMenu menu = new JPopupMenu();
     555                    menu.add(helpAction);
     556                    menu.show(propertyTable, p.x, p.y-3);
     557                }
     558            }
     559        });
    545560
    546561        propertyTable.getColumnModel().getColumn(1).setCellRenderer(new DefaultTableCellRenderer(){
     
    578593                int row = membershipTable.rowAtPoint(p);
    579594                if (row > -1) {
     595                    membershipTable.changeSelection(row, 0, false, false);
    580596                    JPopupMenu menu = new JPopupMenu();
    581597                    Relation relation = (Relation)membershipData.getValueAt(row, 0);
    582598                    menu.add(new SelectRelationAction(relation, true));
    583599                    menu.add(new SelectRelationAction(relation, false));
     600                    menu.addSeparator();
     601                    menu.add(helpAction);
    584602                    menu.show(membershipTable, p.x, p.y-3);
    585603                }
     
    712730        // -- help action
    713731        //
    714         HelpAction helpAction = new HelpAction();
    715         propertyTable.getSelectionModel().addListSelectionListener(helpAction);
    716732        getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
    717733                KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0), "onHelp");
     
    10341050    }
    10351051
    1036     class HelpAction extends AbstractAction implements ListSelectionListener {
     1052    class HelpAction extends AbstractAction {
    10371053        public HelpAction() {
    1038             putValue(NAME, tr("Help"));
     1054            putValue(NAME, tr("Go to OSM wiki for tag help (F1)"));
    10391055            putValue(SHORT_DESCRIPTION, tr("Launch browser with wiki help to selected object"));
    1040             updateEnabledState();
     1056            putValue(SMALL_ICON, ImageProvider.get("dialogs", "search"));
    10411057        }
    10421058
    10431059        public void actionPerformed(ActionEvent e) {
    1044             if (!isEnabled())
    1045                 return;
    1046 
    10471060            try {
    10481061                String base = new String(Main.pref.get("url.openstreetmap-wiki", "http://wiki.openstreetmap.org/wiki/"));
    10491062                String l = LanguageInfo.getWikiLanguagePrefix();
    10501063                List<URI> uris = new ArrayList<URI>();
    1051 
    10521064                int row;
    10531065                if (propertyTable.getSelectedRowCount() == 1) {
    10541066                    row = propertyTable.getSelectedRow();
    1055                     String key = propertyData.getValueAt(row, 0).toString();
     1067                    String key = URLEncoder.encode(propertyData.getValueAt(row, 0).toString(), "UTF-8");
    10561068                    @SuppressWarnings("unchecked")
    1057                     String val = ((Map<String,Integer>)propertyData.getValueAt(row, 1)).entrySet().iterator().next().getKey();
     1069                    String val = URLEncoder.encode(
     1070                            ((Map<String,Integer>)propertyData.getValueAt(row, 1))
     1071                            .entrySet().iterator().next().getKey(), "UTF-8"
     1072                    );
    10581073
    10591074                    uris.add(new URI(String.format("%s%sTag:%s=%s", base, l, key, val)));
     
    10651080                } else if (membershipTable.getSelectedRowCount() == 1) {
    10661081                    row = membershipTable.getSelectedRow();
    1067                     String type = ((Relation)membershipData.getValueAt(row, 0)).get("type");
    1068                    
     1082                    String type = URLEncoder.encode(
     1083                            ((Relation)membershipData.getValueAt(row, 0)).get("type"), "UTF-8"
     1084                    );
     1085
    10691086                    if (type != null && !type.equals("")) {
    10701087                        uris.add(new URI(String.format("%s%sRelation:%s", base, l, type)));
    10711088                        uris.add(new URI(String.format("%sRelation:%s", base, type)));
    10721089                    }
     1090
    10731091                    uris.add(new URI(String.format("%s%sRelations", base, l)));
    10741092                    uris.add(new URI(String.format("%sRelations", base)));
     1093                } else {
     1094                    // give the generic help page, if more than one element is selected
     1095                    uris.add(new URI(String.format("%s%sMap_Features", base, l)));
     1096                    uris.add(new URI(String.format("%sMap_Features", base)));
    10751097                }
    10761098
    10771099                // find a page that actually exists in the wiki
    1078                 URI uri = null;
    1079                 for (URI u : uris) {
    1080                     System.out.println("INFO: looking for " + u);
    1081                     if (((HttpURLConnection) u.toURL().openConnection()).getResponseCode() == 200) {
    1082                         uri = u;
    1083                         break;
     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;
     1129                        }
    10841130                    }
    1085                 }
    1086                    
    1087                 // browse the help page
    1088                 if (uri != null) {
    1089                     System.out.println("INFO: browsing to url " + uri);
    1090                     OpenBrowser.displayUrl(uri);
    10911131                }
    10921132            } catch (Exception e1) {
    10931133                e1.printStackTrace();
    10941134            }
    1095         }
    1096 
    1097         protected void updateEnabledState() {
    1098             setEnabled(
    1099                     propertyTable.getSelectedRowCount() == 1
    1100                     ^ membershipTable.getSelectedRowCount() == 1
    1101             );
    1102         }
    1103 
    1104         public void valueChanged(ListSelectionEvent e) {
    1105             updateEnabledState();
    11061135        }
    11071136    }
Note: See TracChangeset for help on using the changeset viewer.