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 , 14 years ago) |
---|
-
src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesDialog.java
19 19 import java.awt.event.MouseEvent; 20 20 import java.net.HttpURLConnection; 21 21 import java.net.URI; 22 import java.net.URLEncoder; 22 23 import java.util.ArrayList; 23 24 import java.util.Collection; 24 25 import java.util.Collections; … … 26 27 import java.util.HashMap; 27 28 import java.util.Iterator; 28 29 import java.util.List; 30 import java.util.Locale; 29 31 import java.util.Map; 30 import java.util.Vector;31 import java.util.TreeMap;32 32 import java.util.Map.Entry; 33 import java.util.TreeMap; 34 import java.util.Vector; 33 35 34 36 import javax.swing.AbstractAction; 35 37 import javax.swing.Box; … … 69 71 import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent; 70 72 import org.openstreetmap.josm.data.osm.event.DataSetListenerAdapter; 71 73 import org.openstreetmap.josm.data.osm.event.DatasetEventManager; 72 import org.openstreetmap.josm.data.osm.event.SelectionEventManager;73 74 import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode; 75 import org.openstreetmap.josm.data.osm.event.SelectionEventManager; 74 76 import org.openstreetmap.josm.gui.DefaultNameFormatter; 75 77 import org.openstreetmap.josm.gui.ExtendedDialog; 76 78 import org.openstreetmap.josm.gui.MapFrame; … … 87 89 import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher; 88 90 import org.openstreetmap.josm.tools.GBC; 89 91 import org.openstreetmap.josm.tools.ImageProvider; 90 import org.openstreetmap.josm.tools.LanguageInfo;91 92 import org.openstreetmap.josm.tools.OpenBrowser; 92 93 import org.openstreetmap.josm.tools.Shortcut; 93 94 … … 163 164 }; 164 165 165 166 private DataSetListenerAdapter dataChangedAdapter = new DataSetListenerAdapter(this); 167 private HelpAction helpAction = new HelpAction(); 166 168 private AddAction addAction = new AddAction(); 167 169 private Shortcut addActionShortcut = Shortcut.registerShortcut("properties:add", tr("Add Properties"), KeyEvent.VK_B, 168 170 Shortcut.GROUP_MNEMONIC); … … 542 544 // setting up the properties table 543 545 propertyData.setColumnIdentifiers(new String[]{tr("Key"),tr("Value")}); 544 546 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 }); 545 560 546 561 propertyTable.getColumnModel().getColumn(1).setCellRenderer(new DefaultTableCellRenderer(){ 547 562 @Override public Component getTableCellRendererComponent(JTable table, Object value, … … 577 592 Point p = evt.getPoint(); 578 593 int row = membershipTable.rowAtPoint(p); 579 594 if (row > -1) { 595 membershipTable.changeSelection(row, 0, false, false); 580 596 JPopupMenu menu = new JPopupMenu(); 581 597 Relation relation = (Relation)membershipData.getValueAt(row, 0); 582 598 menu.add(new SelectRelationAction(relation, true)); 583 599 menu.add(new SelectRelationAction(relation, false)); 600 menu.addSeparator(); 601 menu.add(helpAction); 584 602 menu.show(membershipTable, p.x, p.y-3); 585 603 } 586 604 } … … 711 729 712 730 // -- help action 713 731 // 714 HelpAction helpAction = new HelpAction();715 propertyTable.getSelectionModel().addListSelectionListener(helpAction);716 732 getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put( 717 733 KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0), "onHelp"); 718 734 getActionMap().put("onHelp", helpAction); … … 1033 1049 } 1034 1050 } 1035 1051 1036 class HelpAction extends AbstractAction implements ListSelectionListener{1052 class HelpAction extends AbstractAction { 1037 1053 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")); 1041 1057 } 1042 1058 1059 @SuppressWarnings("unchecked") 1043 1060 public void actionPerformed(ActionEvent e) { 1044 if (!isEnabled())1045 return;1046 1047 1061 try { 1048 String base = new String( Main.pref.get("url.openstreetmap-wiki", "http://wiki.openstreetmap.org/wiki/"));1049 String l = L anguageInfo.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>(); 1051 1065 1066 String uri; 1052 1067 int row; 1053 1068 if (propertyTable.getSelectedRowCount() == 1) { 1054 1069 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 ); 1058 1078 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")); 1065 1082 } else if (membershipTable.getSelectedRowCount() == 1) { 1066 1083 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); 1075 1099 } 1076 1100 1077 1101 // 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(); 1084 1132 } 1085 1133 } 1086 1087 // browse the help page1088 if (uri != null) {1089 System.out.println("INFO: browsing to url " + uri);1090 OpenBrowser.displayUrl(uri);1091 }1092 1134 } catch (Exception e1) { 1093 1135 e1.printStackTrace(); 1094 1136 } 1095 1137 } 1096 1097 protected void updateEnabledState() {1098 setEnabled(1099 propertyTable.getSelectedRowCount() == 11100 ^ membershipTable.getSelectedRowCount() == 11101 );1102 }1103 1104 public void valueChanged(ListSelectionEvent e) {1105 updateEnabledState();1106 }1107 1138 } 1108 1139 1109 1140 static class SelectRelationAction extends AbstractAction {