Changeset 26922 in osm


Ignore:
Timestamp:
2011-10-22T00:05:00+02:00 (13 years ago)
Author:
donvip
Message:

tag2link version 0.1

Location:
applications/editors/josm/plugins/tag2link
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/tag2link

    • Property svn:ignore set to
      build
      .settings
      .classpath
      .project
  • applications/editors/josm/plugins/tag2link/build.xml

    r26917 r26922  
    3232    <property name="commit.message" value="Commit message"/>
    3333    <!-- enter the *lowest* JOSM version this plugin is currently compatible with -->
    34     <property name="plugin.main.version" value="4487"/>
     34    <property name="plugin.main.version" value="4536"/>
    3535    <!-- should not be necessary to change the following properties -->
    3636    <property name="josm" location="../../core/dist/josm-custom.jar"/>
     
    9999                <attribute name="Plugin-Class" value="org.openstreetmap.josm.plugins.tag2link.Tag2LinkPlugin"/>
    100100                <attribute name="Plugin-Date" value="${version.entry.commit.date}"/>
    101                 <attribute name="Plugin-Description" value="TODO"/>
    102                 <attribute name="fr_Plugin-Description" value="TODO"/>
     101                <attribute name="Plugin-Description" value="Launch browser to a Web resource about a selected object having known tags, such as Wikipedia"/>
    103102                <attribute name="Plugin-Early" value="false"/>
    104103                <attribute name="Plugin-Icon" value="images/tag2linkv2_24x24.png"/>
  • applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/Tag2LinkPlugin.java

    r26917 r26922  
    2828 * Main class of tag2links plugin.
    2929 * @author Don-vip
    30  * @version 1.0
     30 * @version 0.1
    3131 * History:
    32  * 1.0 ??-Oct-2011 first version
     32 * 0.1 22-Oct-2011 first working prototype
    3333 */
    3434public class Tag2LinkPlugin extends Plugin {
  • applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/Tag2LinkPreferenceSetting.java

    r26917 r26922  
    1616package org.openstreetmap.josm.plugins.tag2link;
    1717
    18 import static org.openstreetmap.josm.tools.I18n.tr;
    19 
    20 import javax.swing.JPanel;
    21 
    2218import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
    2319import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
     
    2723    @Override
    2824    public void addGui(PreferenceTabbedPane gui) {
    29         JPanel p = gui.createPreferenceTab(ICON_48, tr("Tag2Link Preferences"),
    30                 tr("Tag2Link Preferences"), false);
     25        /*JPanel p = gui.createPreferenceTab(ICON_48, tr("Tag2Link Preferences"),
     26                tr("Tag2Link Preferences"), false);*/
    3127        // TODO
    3228    }
  • applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/Tag2LinkRuleChecker.java

    r26921 r26922  
    2424
    2525import org.openstreetmap.josm.data.osm.IPrimitive;
     26import org.openstreetmap.josm.data.osm.Tag;
    2627import org.openstreetmap.josm.plugins.tag2link.data.Link;
    2728import org.openstreetmap.josm.plugins.tag2link.data.Rule;
     
    5354    }
    5455   
     56    private static Collection<Link> processEval(EvalResult eval, Rule rule, Source source) {
     57        Collection<Link> result = new ArrayList<Link>();
     58        if (eval.matches()) {
     59            for (Link link : rule.links) {
     60                Link copy = new Link(link);
     61                copy.name = copy.name.replaceAll("%name%", source.name);
     62                                Matcher m = Pattern.compile("%([^%]*)%").matcher(copy.url);
     63                                while (m.find()) {
     64                                        String arg = m.group(1);
     65                                        String val = findValue(arg, eval.matchingTags);
     66                                        if (val == null && arg.contains(":")) {
     67                                                String[] vars = arg.split(":");
     68                                                for (int i = 0; val == null && i < vars.length-1; i++) {
     69                                                        val = findValue(vars[i], eval.matchingTags);
     70                                                }
     71                                                if (val == null) {
     72                                                        // Default value
     73                                                        val = vars[vars.length-1];
     74                                                }
     75                                        }
     76                                        if (val != null) {
     77                                                try {
     78                                                        // Special hack for Wikipedia that prevents spaces being replaced by "+" characters, but by "_"
     79                                                        if (copy.url.contains("wikipedia.")) {
     80                                                                val = val.replaceAll(" ", "_");
     81                                                        }
     82                                                        // Encode param to be included in the URL, except if it is the URL itself !
     83                                                        if (!m.group().equals(copy.url)) {
     84                                                                val = URLEncoder.encode(val, UTF8_ENCODING);
     85                                                        }
     86                                                        // Finally replace parameter
     87                                                        copy.url = copy.url.replaceFirst(m.group(), val);
     88                                                } catch (UnsupportedEncodingException e) {
     89                                                        e.printStackTrace();
     90                                                }
     91                                        } else {
     92                                                System.err.println("Invalid argument: "+arg);
     93                                        }
     94                                }
     95                result.add(copy);
     96            }
     97        }
     98        return result;
     99    }
     100   
    55101    public static Collection<Link> getLinks(IPrimitive p) {
    56102        Collection<Link> result = new ArrayList<Link>();
    57103        for (Source source : sources) {
    58104            for (Rule rule : source.rules) {
    59                 EvalResult eval = rule.evaluates(p);
    60                 if (eval.matches()) {
    61                     for (Link link : rule.links) {
    62                         Link copy = new Link(link);
    63                         copy.name = copy.name.replaceAll("%name%", source.name);
    64                                                 Matcher m = Pattern.compile("%([^%]*)%").matcher(copy.url);
    65                                                 while (m.find()) {
    66                                                         String arg = m.group(1);
    67                                                         String val = findValue(arg, eval.matchingTags);
    68                                                         if (val == null && arg.contains(":")) {
    69                                                                 String[] vars = arg.split(":");
    70                                                                 for (int i = 0; val == null && i < vars.length-1; i++) {
    71                                                                         val = findValue(vars[i], eval.matchingTags);
    72                                                                 }
    73                                                                 if (val == null) {
    74                                                                         // Default value
    75                                                                         val = vars[vars.length-1];
    76                                                                 }
    77                                                         }
    78                                                         if (val != null) {
    79                                                                 try {
    80                                                                         // Special hack for Wikipedia that prevents spaces being replaced by "+" characters, but by "_"
    81                                                                         if (copy.url.contains("wikipedia.")) {
    82                                                                                 val = val.replaceAll(" ", "_");
    83                                                                         }
    84                                                                         // Encode param to be included in the URL, except if it is the URL itself !
    85                                                                         if (!m.group().equals(copy.url)) {
    86                                                                                 val = URLEncoder.encode(val, UTF8_ENCODING);
    87                                                                         }
    88                                                                         // Finally replace parameter
    89                                                                         copy.url = copy.url.replaceFirst(m.group(), val);
    90                                                                 } catch (UnsupportedEncodingException e) {
    91                                                                         e.printStackTrace();
    92                                                                 }
    93                                                         } else {
    94                                                                 System.err.println("Invalid argument: "+arg);
    95                                                         }
    96                                                 }
    97                         result.add(copy);
    98                     }
    99                 }
     105                result.addAll(processEval(rule.evaluates(p), rule, source));
    100106            }
    101107        }
    102108        return result;
    103109    }
     110
     111        public static Collection<Link> getLinks(Tag tag) {
     112                Collection<Link> result = new ArrayList<Link>();
     113        for (Source source : sources) {
     114            for (Rule rule : source.rules) {
     115                result.addAll(processEval(rule.evaluates(tag), rule, source));
     116            }
     117        }
     118                return result;
     119        }
    104120}
  • applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/data/Rule.java

    r26921 r26922  
    88
    99import org.openstreetmap.josm.data.osm.IPrimitive;
     10import org.openstreetmap.josm.data.osm.Tag;
    1011
    1112public class Rule {
     
    4748    }
    4849   
    49     public EvalResult evaluates(IPrimitive p) {
     50    public EvalResult evaluates(Map<String, String> tags) {
    5051        EvalResult result = new EvalResult();
    51         Map<String, String> tags = p.getKeys();
    5252        for (Condition c : conditions) {
    5353            for (String key : tags.keySet()) {
     
    7373        }
    7474        return result;
     75
    7576    }
     77   
     78    public EvalResult evaluates(IPrimitive p) {
     79        return evaluates(p.getKeys());
     80    }
     81
     82        public EvalResult evaluates(Tag tag) {
     83                Map<String, String> map = new HashMap<String, String>();
     84                map.put(tag.getKey(), tag.getValue());
     85                return evaluates(map);
     86        }
    7687}
  • applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/listeners/MembershipPopupListener.java

    r26921 r26922  
    2727    @Override
    2828    protected IPrimitive getFirstSelectedPrimitive() {
    29         return frame.propertiesDialog.getSelectedMembershipRelations().iterator().next();
     29        return frame.propertiesDialog.getSelectedMembershipRelation();
    3030    }
    3131}
  • applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/listeners/PropertyPopupListener.java

    r26917 r26922  
    1616package org.openstreetmap.josm.plugins.tag2link.listeners;
    1717
     18import javax.swing.JPopupMenu;
    1819import javax.swing.event.PopupMenuEvent;
    1920
     21import org.openstreetmap.josm.data.osm.Tag;
    2022import org.openstreetmap.josm.gui.MapFrame;
     23import org.openstreetmap.josm.plugins.tag2link.Tag2LinkRuleChecker;
     24import org.openstreetmap.josm.plugins.tag2link.data.Link;
    2125
    2226public class PropertyPopupListener extends AbstractPopupListener {
     
    2428    public PropertyPopupListener(MapFrame frame) {
    2529        super(frame);
    26         // TODO Auto-generated constructor stub
    2730    }
    2831
    2932    @Override
    3033    public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
    31         // TODO Auto-generated method stub
    32 
     34        Tag tag = frame.propertiesDialog.getSelectedProperty();
     35        if (tag != null) {
     36            JPopupMenu popup = (JPopupMenu) e.getSource();
     37            for (Link link : Tag2LinkRuleChecker.getLinks(tag)) {
     38                addLink(popup, link);
     39            }
     40        }
    3341    }
    34 
    3542}
Note: See TracChangeset for help on using the changeset viewer.