Changeset 5614 in josm


Ignore:
Timestamp:
2012-12-06T22:54:00+01:00 (9 years ago)
Author:
simon04
Message:

see #8255 - suggest roles when adding objects to a relation

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/data/defaultpresets.xml

    r5611 r5614  
    8989  count: how often can the role occur (if not given unlimited number is assumed)
    9090  type: the data types - way,node,relation,closedway (separated by comma)
     91  member_expression: an expression (cf. search dialog) for objects of this role
    9192
    9293 presets: For external files <presets> should have following attributes:
     
    24262427            <roles>
    24272428                <role key="" text="route segment" requisite="required" type="way" />
    2428                 <role key="stop" text="stop position" requisite="optional" type="node" />
    2429                 <role key="stop_exit_only" text="stop position (exit only)" requisite="optional" type="node" />
    2430                 <role key="stop_entry_only" text="stop position (entry only)" requisite="optional" type="node" />
    2431                 <role key="platform" text="platform" requisite="optional" type="node,way,closedway" />
    2432                 <role key="platform_exit_only" text="platform (exit only)" requisite="optional" type="node,way,closedway" />
    2433                 <role key="platform_entry_only" text="platform (entry only)" requisite="optional" type="node,way,closedway" />
     2429                <role key="stop" text="stop position" requisite="optional" type="node" member_expression="public_transport=stop_position" />
     2430                <role key="stop_exit_only" text="stop position (exit only)" requisite="optional" type="node" member_expression="public_transport=stop_position" />
     2431                <role key="stop_entry_only" text="stop position (entry only)" requisite="optional" type="node" member_expression="public_transport=stop_position" />
     2432                <role key="platform" text="platform" requisite="optional" type="node,way,closedway" member_expression="public_transport=platform" />
     2433                <role key="platform_exit_only" text="platform (exit only)" requisite="optional" type="node,way,closedway" member_expression="public_transport=platform" />
     2434                <role key="platform_entry_only" text="platform (entry only)" requisite="optional" type="node,way,closedway" member_expression="public_transport=platform" />
    24342435            </roles>
    24352436        </item>
     
    24472448            </optional>
    24482449            <roles>
    2449                 <role key="" text="route variant/direction (at least 2)" requisite="required" type="relation"/> <!-- FIXME: at least 2 members are required -->
     2450                <role key="" text="route variant/direction (at least 2)" requisite="required" type="relation" member_expression="type=route"/> <!-- FIXME: at least 2 members are required -->
    24502451            </roles>
    24512452        </item>
     
    24642465            </optional>
    24652466            <roles>
    2466                 <role key="stop" text="Stop Position" requisite="optional" type="node"/>
    2467                 <role key="platform" text="Platform" requisite="optional" type="node,way,closedway"/>
    2468                 <role key="" text="Station or amenities" requisite="optional" type="node,way,closedway"/>
     2467                <role key="stop" text="Stop Position" requisite="optional" type="node" member_expression="public_transport=stop_position"/>
     2468                <role key="platform" text="Platform" requisite="optional" type="node,way,closedway" member_expression="public_transport=platform"/>
     2469                <role key="" text="Station or amenities" requisite="optional" type="node,way,closedway" member_expression="amenity|public_transport=station"/>
    24692470            </roles>
    24702471        </item>
     
    59065907            <text key="name" text="Street name" />
    59075908            <roles>
    5908                 <role key="street" text="street" requisite="required" type="way" />
    5909                 <role key="house" text="house" requisite="required" type="node,way,closedway,relation" />
     5909                <role key="street" text="street" requisite="required" type="way" member_expression="highway &quot;addr:street&quot;" />
     5910                <role key="house" text="house" requisite="required" type="node,way,closedway,relation" member_expression="&quot;addr:housenumber&quot;" />
    59105911            </roles>
    59115912        </item>
     
    59635964            </optional>
    59645965            <roles>
    5965                 <role key="" text="waterways (no riverbank)" requisite="optional" type="way" />
    5966                 <role key="main_stream" text="waterways (no riverbank)" requisite="optional" type="way" />
    5967                 <role key="side_stream" text="branch waterways (no riverbank)" requisite="optional" type="way" />
     5966                <role key="" text="waterways (no riverbank)" requisite="optional" type="way" member_expression="waterway -waterway:riverbank" />
     5967                <role key="main_stream" text="waterways (no riverbank)" requisite="optional" type="way" member_expression="waterway -waterway:riverbank" />
     5968                <role key="side_stream" text="branch waterways (no riverbank)" requisite="optional" type="way" member_expression="waterway -waterway:riverbank"/>
    59685969                <role key="spring" text="spring of waterway" requisite="optional" type="node" />
    59695970            </roles>
  • trunk/data/tagging-preset.xsd

    r5585 r5614  
    5757                                <element name="optional" type="tns:optional" minOccurs="0" />
    5858                        </choice>
    59                         <element name="roles" type="tns:roles" minOccurs="0" />
     59                        <element name="roles" type="tns:roles" minOccurs="0" maxOccurs="1" />
    6060                </sequence>
    6161                <attribute name="name" type="string" use="required" />
     
    232232                <attribute name="type" type="string" />
    233233                <attribute name="count" type="integer" />
     234                <attribute name="member_expression" type="string" />
    234235        </complexType>
    235236
  • trunk/src/org/openstreetmap/josm/gui/dialogs/properties/PresetListPanel.java

    r5170 r5614  
    2121import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2222import org.openstreetmap.josm.data.osm.Tag;
    23 import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference;
    2423import org.openstreetmap.josm.gui.tagging.TaggingPreset;
    2524import org.openstreetmap.josm.gui.tagging.TaggingPreset.PresetType;
     
    8685        }
    8786
    88         for (TaggingPreset t : TaggingPresetPreference.taggingPresets) {
    89             if (!t.matches(types, tags, true)) {
    90                 continue;
    91             }
    92 
     87        for (TaggingPreset t : TaggingPreset.getMatchingPresets(types, tags, true)) {
    9388            JLabel lbl = new JLabel(t.getName() + " …");
    9489            lbl.setIcon((Icon) t.getValue(Action.SMALL_ICON));
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/GenericRelationEditor.java

    r5605 r5614  
    6262import org.openstreetmap.josm.gui.SideButton;
    6363import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
     64import org.openstreetmap.josm.gui.dialogs.properties.PresetListPanel;
    6465import org.openstreetmap.josm.gui.dialogs.properties.PresetListPanel.PresetHandler;
    6566import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
     
    115116                WindowGeometry.centerInWindow(Main.parent, new Dimension(700, 650)));
    116117
    117         // init the various models
    118         //
    119         memberTableModel = new MemberTableModel(getLayer());
    120         memberTableModel.register();
    121         selectionTableModel = new SelectionTableModel(getLayer());
    122         selectionTableModel.register();
    123         referrerModel = new ReferringRelationsBrowserModel(relation);
    124 
    125         tagEditorPanel = new TagEditorPanel(new PresetHandler() {
     118        final PresetHandler presetHandler = new PresetHandler() {
    126119
    127120            @Override
     
    136129                return Collections.<OsmPrimitive>singletonList(relation);
    137130            }
    138         });
     131        };
     132
     133        // init the various models
     134        //
     135        memberTableModel = new MemberTableModel(getLayer(), presetHandler);
     136        memberTableModel.register();
     137        selectionTableModel = new SelectionTableModel(getLayer());
     138        selectionTableModel.register();
     139        referrerModel = new ReferringRelationsBrowserModel(relation);
     140
     141        tagEditorPanel = new TagEditorPanel(presetHandler);
    139142
    140143        // populate the models
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/MemberTableModel.java

    r5603 r5614  
    1313import java.util.Collections;
    1414import java.util.Comparator;
     15import java.util.EnumSet;
    1516import java.util.HashMap;
    1617import java.util.HashSet;
     
    4647import org.openstreetmap.josm.data.osm.event.TagsChangedEvent;
    4748import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent;
     49import org.openstreetmap.josm.gui.dialogs.properties.PresetListPanel;
    4850import org.openstreetmap.josm.gui.dialogs.relation.WayConnectionType.Direction;
    4951import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     52import org.openstreetmap.josm.gui.tagging.TaggingPreset;
    5053import org.openstreetmap.josm.gui.widgets.OsmPrimitivesTableModel;
    5154
     
    5962
    6063    private DefaultListSelectionModel listSelectionModel;
    61     private CopyOnWriteArrayList<IMemberModelListener> listeners;
    62     private OsmDataLayer layer;
     64    private final CopyOnWriteArrayList<IMemberModelListener> listeners;
     65    private final OsmDataLayer layer;
     66    private final PresetListPanel.PresetHandler presetHandler;
    6367
    6468    private final int UNCONNECTED = Integer.MIN_VALUE;
     
    7377     * constructor
    7478     */
    75     public MemberTableModel(OsmDataLayer layer) {
     79    public MemberTableModel(OsmDataLayer layer, PresetListPanel.PresetHandler presetHandler) {
    7680        members = new ArrayList<RelationMember>();
    7781        listeners = new CopyOnWriteArrayList<IMemberModelListener>();
    7882        this.layer = layer;
     83        this.presetHandler = presetHandler;
    7984        addTableModelListener(this);
    8085    }
     
    395400
    396401    private void addMembersAtIndex(List<? extends OsmPrimitive> primitives, int index) {
     402        final Collection<TaggingPreset> presets = TaggingPreset.getMatchingPresets(EnumSet.of(TaggingPreset.PresetType.RELATION), presetHandler.getSelection().iterator().next().getKeys(), false);
    397403        if (primitives == null)
    398404            return;
    399405        int idx = index;
    400406        for (OsmPrimitive primitive : primitives) {
    401             RelationMember member = new RelationMember("", primitive);
     407            final String role = presets.isEmpty() ? null : presets.iterator().next().suggestRoleForOsmPrimitive(primitive);
     408            RelationMember member = new RelationMember(role == null ? "" : role, primitive);
    402409            members.add(idx++, member);
    403410        }
  • trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java

    r5606 r5614  
    6767import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    6868import org.openstreetmap.josm.gui.preferences.SourceEntry;
     69import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference;
    6970import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference.PresetPrefHelper;
    7071import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
     
    7677import org.openstreetmap.josm.tools.GBC;
    7778import org.openstreetmap.josm.tools.ImageProvider;
     79import org.openstreetmap.josm.tools.Predicate;
    7880import org.openstreetmap.josm.tools.UrlLabel;
    7981import org.openstreetmap.josm.tools.Utils;
     
    10941096        public String text_context;
    10951097        public String locale_text;
     1098        public Match memberExpression;
    10961099
    10971100        public boolean required = false;
     
    11071110            } else if(!"optional".equals(str))
    11081111                throw new SAXException(tr("Unknown requisite: {0}", str));
     1112        }
     1113
     1114        public void setMember_expression(String member_expression) throws SAXException {
     1115            try {
     1116                this.memberExpression = SearchCompiler.compile(member_expression, true, true);
     1117            } catch (SearchCompiler.ParseError ex) {
     1118                throw new SAXException(tr("Illegal member expression: {0}", ex.getMessage()), ex);
     1119            }
    11091120        }
    11101121
     
    11581169    public static class Roles extends Item {
    11591170
    1160         public List<Role> roles = new LinkedList<Role>();
     1171        public final List<Role> roles = new LinkedList<Role>();
    11611172
    11621173        @Override
     
    12411252    public EnumSet<PresetType> types;
    12421253    public List<Item> data = new LinkedList<Item>();
     1254    public Roles roles;
    12431255    public TemplateEntry nameTemplate;
    12441256    public Match nameTemplateFilter;
     
    14281440                    if (o instanceof Roles) {
    14291441                        all.getLast().data.add((Item) o);
     1442                        if (all.getLast().roles != null) {
     1443                            throw new SAXException(tr("Roles cannot appear more than once"));
     1444                        }
     1445                        all.getLast().roles = (Roles) o;
    14301446                        lastrole = (Roles) o;
    14311447                    } else if (o instanceof Role) {
     
    15721588    }
    15731589
     1590    public String suggestRoleForOsmPrimitive(OsmPrimitive osm) {
     1591        if (roles == null) {
     1592            return null;
     1593        }
     1594        for (Role i : roles.roles) {
     1595            if (i.memberExpression != null && i.memberExpression.match(osm)) {
     1596                return i.key;
     1597            }
     1598        }
     1599        return null;
     1600    }
     1601
    15741602    public void actionPerformed(ActionEvent e) {
    15751603        if (Main.main == null) return;
     
    15901618                r.put(t.getKey(), t.getValue());
    15911619            }
    1592             for(OsmPrimitive osm : Main.main.getCurrentDataSet().getSelected()) {
    1593                 RelationMember rm = new RelationMember("", osm);
     1620            for (OsmPrimitive osm : Main.main.getCurrentDataSet().getSelected()) {
     1621                String role = suggestRoleForOsmPrimitive(osm);
     1622                RelationMember rm = new RelationMember(role == null ? "" : role, osm);
    15941623                r.addMember(rm);
    15951624                members.add(rm);
     
    17731802        return atLeastOnePositiveMatch;
    17741803    }
     1804
     1805    public static Collection<TaggingPreset> getMatchingPresets(final Collection<PresetType> t, final Map<String, String> tags, final boolean onlyShowable) {
     1806        return Utils.filter(TaggingPresetPreference.taggingPresets, new Predicate<TaggingPreset>() {
     1807            @Override
     1808            public boolean evaluate(TaggingPreset object) {
     1809                return object.matches(t, tags, onlyShowable);
     1810            }
     1811        });
     1812    }
    17751813}
Note: See TracChangeset for help on using the changeset viewer.