Ticket #5443: PropertiesDialog.java.popup-menu-and-fixes.against-r3525.patch

File PropertiesDialog.java.popup-menu-and-fixes.against-r3525.patch, 11.3 KB (added by cmuelle8, 14 years ago)

misc fixes and the popup menu feature for josm newbies

  • src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesDialog.java

     
    1919import java.awt.event.MouseEvent;
    2020import java.net.HttpURLConnection;
    2121import java.net.URI;
     22import java.net.URLEncoder;
    2223import java.util.ArrayList;
    2324import java.util.Collection;
    2425import java.util.Collections;
     
    2627import java.util.HashMap;
    2728import java.util.Iterator;
    2829import java.util.List;
     30import java.util.Locale;
    2931import java.util.Map;
    30 import java.util.Vector;
    31 import java.util.TreeMap;
    3232import java.util.Map.Entry;
     33import java.util.TreeMap;
     34import java.util.Vector;
    3335
    3436import javax.swing.AbstractAction;
    3537import javax.swing.Box;
     
    6971import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
    7072import org.openstreetmap.josm.data.osm.event.DataSetListenerAdapter;
    7173import org.openstreetmap.josm.data.osm.event.DatasetEventManager;
    72 import org.openstreetmap.josm.data.osm.event.SelectionEventManager;
    7374import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode;
     75import org.openstreetmap.josm.data.osm.event.SelectionEventManager;
    7476import org.openstreetmap.josm.gui.DefaultNameFormatter;
    7577import org.openstreetmap.josm.gui.ExtendedDialog;
    7678import org.openstreetmap.josm.gui.MapFrame;
     
    8789import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
    8890import org.openstreetmap.josm.tools.GBC;
    8991import org.openstreetmap.josm.tools.ImageProvider;
    90 import org.openstreetmap.josm.tools.LanguageInfo;
    9192import org.openstreetmap.josm.tools.OpenBrowser;
    9293import org.openstreetmap.josm.tools.Shortcut;
    9394
     
    163164    };
    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,
    168170            Shortcut.GROUP_MNEMONIC);
     
    542544        // setting up the properties table
    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(){
    547562            @Override public Component getTableCellRendererComponent(JTable table, Object value,
     
    577592                Point p = evt.getPoint();
    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                }
    586604            }
     
    711729
    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");
    718734        getActionMap().put("onHelp", helpAction);
     
    10331049        }
    10341050    }
    10351051
    1036     class HelpAction extends AbstractAction implements ListSelectionListener {
     1052    class HelpAction extends AbstractAction {
    10371053        public HelpAction() {
    1038             putValue(NAME, tr("Help"));
    1039             putValue(SHORT_DESCRIPTION, tr("Launch browser with wiki help to selected object"));
    1040             updateEnabledState();
     1054            putValue(NAME, tr("Go to OSM wiki for tag help (F1)"));
     1055            putValue(SHORT_DESCRIPTION, tr("Browse to OSM wiki for tag info"));
     1056            putValue(SMALL_ICON, ImageProvider.get("dialogs", "search"));
    10411057        }
    10421058
     1059        @SuppressWarnings("unchecked")
    10431060        public void actionPerformed(ActionEvent e) {
    1044             if (!isEnabled())
    1045                 return;
    1046 
    10471061            try {
    1048                 String base = new String(Main.pref.get("url.openstreetmap-wiki", "http://wiki.openstreetmap.org/wiki/"));
    1049                 String l = LanguageInfo.getWikiLanguagePrefix();
    1050                 List<URI> uris = new ArrayList<URI>();
     1062                String base = new String("http://wiki.openstreetmap.org/wiki/");
     1063                String l = Locale.getDefault().getCountry() + ":";
     1064                Vector<URI> v = new Vector<URI>();
    10511065
     1066                String uri;
    10521067                int row;
    10531068                if (propertyTable.getSelectedRowCount() == 1) {
    10541069                    row = propertyTable.getSelectedRow();
    1055                     String key = propertyData.getValueAt(row, 0).toString();
    1056                     @SuppressWarnings("unchecked")
    1057                     String val = ((Map<String,Integer>)propertyData.getValueAt(row, 1)).entrySet().iterator().next().getKey();
     1070                    uri = base + l + "Tag:" +
     1071                    URLEncoder.encode(
     1072                            propertyData.getValueAt(row, 0).toString(), "UTF-8"
     1073                    ) + "=" +
     1074                    URLEncoder.encode(
     1075                            ((Map<String,Integer>)propertyData.getValueAt(row, 1))
     1076                            .entrySet().iterator().next().getKey(), "UTF-8"
     1077                    );
    10581078
    1059                     uris.add(new URI(String.format("%s%sTag:%s=%s", base, l, key, val)));
    1060                     uris.add(new URI(String.format("%sTag:%s=%s", base, key, val)));
    1061                     uris.add(new URI(String.format("%s%sKey:%s", base, l, key)));
    1062                     uris.add(new URI(String.format("%sKey:%s", base, key)));
    1063                     uris.add(new URI(String.format("%s%sMap_Features", base, l)));
    1064                     uris.add(new URI(String.format("%sMap_Features", base)));
     1079                    v.add(new URI(uri));
     1080                    v.add(new URI(uri.substring(0, uri.indexOf('=')).replace("Tag:", "Key:")));
     1081                    v.add(new URI(base + l + "Map_Features"));
    10651082                } else if (membershipTable.getSelectedRowCount() == 1) {
    10661083                    row = membershipTable.getSelectedRow();
    1067                     String type = ((Relation)membershipData.getValueAt(row, 0)).get("type");
    1068                    
    1069                     if (type != null && !type.equals("")) {
    1070                         uris.add(new URI(String.format("%s%sRelation:%s", base, l, type)));
    1071                         uris.add(new URI(String.format("%sRelation:%s", base, type)));
    1072                     }
    1073                     uris.add(new URI(String.format("%s%sRelations", base, l)));
    1074                     uris.add(new URI(String.format("%sRelations", base)));
     1084                    uri = base + l + "Relation:" +
     1085                    URLEncoder.encode(
     1086                            ((Relation)membershipData.getValueAt(row, 0)).get("type"), "UTF-8"
     1087                    );
     1088
     1089                    v.add(new URI(uri));
     1090                    v.add(new URI(uri = base + l + "Relations"));
     1091                } else {
     1092                    // give the generic help page, if more than one element is selected
     1093                    v.add(new URI(base + l + "Map_Features"));
     1094                }
     1095
     1096                // try all localized variants.  if they are not available, try default locale variants too.
     1097                for (row = 0; row < v.size(); row++) {
     1098                    v.insertElementAt(new URI(v.get(row).toString().replace(l, "")), ++row);
    10751099                }
    10761100
    10771101                // 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;
     1102                HttpURLConnection conn;
     1103                while (!v.isEmpty()) {
     1104                    conn = (HttpURLConnection) v.firstElement().toURL().openConnection();
     1105
     1106                    if (conn.getResponseCode() != 200) {
     1107                        System.out.println("INFO: " + v.firstElement() + " does not exist");
     1108                        conn.disconnect();
     1109                        v.remove(0);
     1110                    } else {
     1111                        int osize = conn.getContentLength();
     1112                        conn.disconnect();
     1113
     1114                        conn = (HttpURLConnection) new URI(v.firstElement().toString()
     1115                                .replace("=", "%3D") /* do not URLencode whole string! */
     1116                                .replaceFirst("/wiki/", "/w/index.php?redirect=no&title=")
     1117                        ).toURL().openConnection();
     1118
     1119                        /* redirect pages have different content length, but retrieving a "nonredirect"
     1120                         *  page using index.php and the direct-link method gives slightly different
     1121                         *  content lengths, so we have to be fuzzy.. (this is UGLY, recode if u know better)
     1122                         */
     1123                        if (Math.abs(conn.getContentLength()-osize) > 200) {
     1124                            System.out.println("INFO: " + v.firstElement() + " is a mediawiki redirect");
     1125                            v.remove(0);
     1126                        } else {
     1127                            System.out.println("INFO: browsing to " + v.firstElement());
     1128                            OpenBrowser.displayUrl(v.firstElement().toString());
     1129                            v.clear(); // done
     1130                        }
     1131                        conn.disconnect();
    10841132                    }
    10851133                }
    1086                    
    1087                 // browse the help page
    1088                 if (uri != null) {
    1089                     System.out.println("INFO: browsing to url " + uri);
    1090                     OpenBrowser.displayUrl(uri);
    1091                 }
    10921134            } catch (Exception e1) {
    10931135                e1.printStackTrace();
    10941136            }
    10951137        }
    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();
    1106         }
    11071138    }
    11081139
    11091140    static class SelectRelationAction extends AbstractAction {