Changeset 28198 in osm for applications/editors/josm/plugins/wikipedia
- Timestamp:
- 2012-04-05T15:27:00+02:00 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaToggleDialog.java
r28194 r28198 7 7 import java.awt.event.MouseAdapter; 8 8 import java.awt.event.MouseEvent; 9 import java.io.UnsupportedEncodingException; 9 10 import java.net.URL; 11 import java.net.URLDecoder; 10 12 import java.util.Arrays; 11 13 import java.util.Collections; … … 23 25 import javax.xml.xpath.XPathFactory; 24 26 import org.openstreetmap.josm.Main; 27 import org.openstreetmap.josm.command.ChangePropertyCommand; 25 28 import org.openstreetmap.josm.data.coor.LatLon; 29 import org.openstreetmap.josm.data.osm.Tag; 26 30 import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor; 27 31 import org.openstreetmap.josm.data.preferences.StringProperty; … … 40 44 createLayout(list, true, Arrays.asList( 41 45 new SideButton(new WikipediaDownloadAction()), 46 new SideButton(new AddWikipediaTagAction()), 42 47 new SideButton(new OpenWikipediaArticleAction()), 43 48 new SideButton(new WikipediaSettingsAction(), false))); … … 81 86 this.description = description; 82 87 this.coordinate = coordinate; 88 } 89 90 public String getHrefFromDescription() { 91 final Matcher m = Pattern.compile(".*href=\"(.+?)\".*").matcher(description); 92 if (m.matches()) { 93 return m.group(1); 94 } else { 95 System.err.println("Could not parse URL from: " + description); 96 return null; 97 } 98 } 99 100 public Tag createWikipediaTag() { 101 // get URL from description 102 String url = getHrefFromDescription(); 103 if (url == null) { 104 return null; 105 } 106 // decode URL for nicer value 107 try { 108 url = URLDecoder.decode(url, "UTF-8"); 109 } catch (UnsupportedEncodingException ex) { 110 throw new IllegalStateException(ex); 111 } 112 // extract Wikipedia language and 113 final Matcher m = Pattern.compile("https?://(\\w*)\\.wikipedia\\.org/wiki/(.*)").matcher(url); 114 if (!m.matches()) { 115 System.err.println("Could not extract Wikipedia tag from: " + url); 116 return null; 117 } 118 return new Tag("wikipedia:" + m.group(1), m.group(2)); 83 119 } 84 120 … … 153 189 public void actionPerformed(ActionEvent e) { 154 190 if (list.getSelectedValue() != null) { 155 Matcher m = Pattern.compile(".*href=\"([^\"]*)\".*").matcher(list.getSelectedValue().description); 156 if (m.matches()) { 157 System.out.println("Wikipedia: opening " + m.group(1)); 158 OpenBrowser.displayUrl(m.group(1)); 159 } else { 160 System.err.println("No match: " + list.getSelectedValue().description); 191 String url = list.getSelectedValue().getHrefFromDescription(); 192 if (url != null) { 193 System.out.println("Wikipedia: opening " + url); 194 OpenBrowser.displayUrl(url); 161 195 } 162 196 } … … 182 216 } 183 217 } 218 219 class AddWikipediaTagAction extends AbstractAction { 220 221 public AddWikipediaTagAction() { 222 super(tr("Add Tag"), ImageProvider.get("pastetags")); 223 putValue(SHORT_DESCRIPTION, tr("Adds a ''wikipedia'' tag corresponding to this article to the selected objects")); 224 } 225 226 @Override 227 public void actionPerformed(ActionEvent e) { 228 if (list.getSelectedValue() != null) { 229 Tag tag = list.getSelectedValue().createWikipediaTag(); 230 if (tag != null) { 231 ChangePropertyCommand cmd = new ChangePropertyCommand( 232 Main.main.getCurrentDataSet().getSelected(), 233 tag.getKey(), tag.getValue()); 234 Main.main.undoRedo.add(cmd); 235 } 236 } 237 } 238 } 184 239 }
Note:
See TracChangeset
for help on using the changeset viewer.