Ticket #5443: PropertiesDialog.java.httpurl-corrected.patch
| File PropertiesDialog.java.httpurl-corrected.patch, 5.6 KB (added by , 16 years ago) |
|---|
-
src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesDialog.java
6 6 7 7 import java.awt.BorderLayout; 8 8 import java.awt.Component; 9 import java.awt.Desktop; 10 import java.awt.Dialog.ModalityType; 9 11 import java.awt.Font; 10 12 import java.awt.GridBagLayout; 11 13 import java.awt.Point; 12 import java.awt.Dialog.ModalityType;13 14 import java.awt.event.ActionEvent; 14 15 import java.awt.event.ActionListener; 15 16 import java.awt.event.FocusAdapter; … … 17 18 import java.awt.event.KeyEvent; 18 19 import java.awt.event.MouseAdapter; 19 20 import java.awt.event.MouseEvent; 21 import java.net.HttpURLConnection; 22 import java.net.URI; 20 23 import java.util.ArrayList; 21 24 import java.util.Collection; 22 25 import java.util.Collections; … … 24 27 import java.util.HashMap; 25 28 import java.util.Iterator; 26 29 import java.util.List; 30 import java.util.Locale; 27 31 import java.util.Map; 32 import java.util.Map.Entry; 28 33 import java.util.TreeMap; 29 34 import java.util.Vector; 30 import java.util.Map.Entry;31 35 32 36 import javax.swing.AbstractAction; 33 37 import javax.swing.Box; … … 67 71 import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent; 68 72 import org.openstreetmap.josm.data.osm.event.DataSetListenerAdapter; 69 73 import org.openstreetmap.josm.data.osm.event.DatasetEventManager; 70 import org.openstreetmap.josm.data.osm.event.SelectionEventManager;71 74 import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode; 75 import org.openstreetmap.josm.data.osm.event.SelectionEventManager; 72 76 import org.openstreetmap.josm.gui.DefaultNameFormatter; 73 77 import org.openstreetmap.josm.gui.ExtendedDialog; 74 78 import org.openstreetmap.josm.gui.MapFrame; … … 704 708 getActionMap().put("delete", deleteAction); 705 709 buttonPanel.add(this.btnDel); 706 710 add(buttonPanel, BorderLayout.SOUTH); 711 712 // -- help action 713 // 714 HelpAction helpAction = new HelpAction(); 715 propertyTable.getSelectionModel().addListSelectionListener(helpAction); 716 getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put( 717 KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0), "onHelp"); 718 getActionMap().put("onHelp", helpAction); 707 719 } 708 720 709 721 @Override public void setVisible(boolean b) { … … 1021 1033 } 1022 1034 } 1023 1035 1036 class HelpAction extends AbstractAction implements ListSelectionListener { 1037 public HelpAction() { 1038 putValue(NAME, tr("Help")); 1039 putValue(SHORT_DESCRIPTION, tr("Launch browser with wiki help to selected object")); 1040 updateEnabledState(); 1041 } 1042 1043 @SuppressWarnings("unchecked") 1044 public void actionPerformed(ActionEvent e) { 1045 if (!isEnabled()) 1046 return; 1047 1048 try { 1049 String base = new String("http://wiki.openstreetmap.org/wiki/"); 1050 String l = Locale.getDefault().getCountry() + ":"; 1051 Vector<URI> v = new Vector<URI>(); 1052 1053 String uri; 1054 int row; 1055 if (propertyTable.getSelectedRowCount() == 1) { 1056 row = propertyTable.getSelectedRow(); 1057 uri = base + l + "Tag:" + 1058 propertyData.getValueAt(row, 0).toString() + "=" + 1059 ((Map<String,Integer>)propertyData.getValueAt(row, 1)).entrySet().iterator().next().getKey(); 1060 1061 v.add(new URI(uri)); 1062 v.add(new URI(uri.substring(0, uri.indexOf('=')).replace("Tag:", "Key:"))); 1063 v.add(new URI(base + l + "Map_Features")); 1064 } else if (membershipTable.getSelectedRowCount() == 1) { 1065 row = membershipTable.getSelectedRow(); 1066 uri = base + l + "Relation:" + ((Relation)membershipData.getValueAt(row, 0)).get("type"); 1067 1068 v.add(new URI(uri)); 1069 v.add(new URI(uri = base + l + "Relations")); 1070 } 1071 1072 // try all localized variants and if they are not available, try default as well 1073 for (row = 0; row < v.size(); row++) { 1074 v.insertElementAt(new URI(v.get(row).toString().replace(l, "")), ++row); 1075 } 1076 // find a page that actually exists in the wiki 1077 while ( !v.isEmpty() && 1078 ((HttpURLConnection) v.firstElement().toURL().openConnection()) 1079 .getResponseCode() != 200) { 1080 System.out.println("INFO: looking for " + v.firstElement()); 1081 v.remove(0); 1082 } 1083 1084 // browse the help page 1085 if (!v.isEmpty()) { 1086 System.out.println("INFO: browsing to url " + v.firstElement()); 1087 Desktop.getDesktop().browse(v.firstElement()); 1088 } 1089 } catch (Exception e1) { 1090 e1.printStackTrace(); 1091 } 1092 } 1093 1094 protected void updateEnabledState() { 1095 setEnabled( 1096 propertyTable.getSelectedRowCount() == 1 1097 ^ membershipTable.getSelectedRowCount() == 1 1098 ); 1099 } 1100 1101 public void valueChanged(ListSelectionEvent e) { 1102 updateEnabledState(); 1103 } 1104 } 1105 1024 1106 static class SelectRelationAction extends AbstractAction { 1025 1107 boolean selectionmode; 1026 1108 Relation relation;
