Ignore:
Timestamp:
2012-04-05T15:27:00+02:00 (12 years ago)
Author:
simon04
Message:

JOSM/wikipedia: add wikipedia tag to selected object

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaToggleDialog.java

    r28194 r28198  
    77import java.awt.event.MouseAdapter;
    88import java.awt.event.MouseEvent;
     9import java.io.UnsupportedEncodingException;
    910import java.net.URL;
     11import java.net.URLDecoder;
    1012import java.util.Arrays;
    1113import java.util.Collections;
     
    2325import javax.xml.xpath.XPathFactory;
    2426import org.openstreetmap.josm.Main;
     27import org.openstreetmap.josm.command.ChangePropertyCommand;
    2528import org.openstreetmap.josm.data.coor.LatLon;
     29import org.openstreetmap.josm.data.osm.Tag;
    2630import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
    2731import org.openstreetmap.josm.data.preferences.StringProperty;
     
    4044        createLayout(list, true, Arrays.asList(
    4145                new SideButton(new WikipediaDownloadAction()),
     46                new SideButton(new AddWikipediaTagAction()),
    4247                new SideButton(new OpenWikipediaArticleAction()),
    4348                new SideButton(new WikipediaSettingsAction(), false)));
     
    8186            this.description = description;
    8287            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));
    83119        }
    84120
     
    153189        public void actionPerformed(ActionEvent e) {
    154190            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);
    161195                }
    162196            }
     
    182216        }
    183217    }
     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    }
    184239}
Note: See TracChangeset for help on using the changeset viewer.