Ignore:
Timestamp:
2011-09-17T10:59:32+02:00 (13 years ago)
Author:
jttt
Message:

Custom primitive name formatters via tagging presets

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/DefaultNameFormatter.java

    r4324 r4431  
    1919import org.openstreetmap.josm.data.coor.CoordinateFormat;
    2020import org.openstreetmap.josm.data.osm.Changeset;
    21 import org.openstreetmap.josm.data.osm.INode;
    2221import org.openstreetmap.josm.data.osm.IPrimitive;
    2322import org.openstreetmap.josm.data.osm.IRelation;
    24 import org.openstreetmap.josm.data.osm.IWay;
    2523import org.openstreetmap.josm.data.osm.NameFormatter;
    2624import org.openstreetmap.josm.data.osm.Node;
    27 import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2825import org.openstreetmap.josm.data.osm.OsmUtils;
    2926import org.openstreetmap.josm.data.osm.Relation;
    30 import org.openstreetmap.josm.data.osm.RelationMember;
    3127import org.openstreetmap.josm.data.osm.Way;
    3228import org.openstreetmap.josm.data.osm.history.HistoryNameFormatter;
     
    3531import org.openstreetmap.josm.data.osm.history.HistoryRelation;
    3632import org.openstreetmap.josm.data.osm.history.HistoryWay;
     33import org.openstreetmap.josm.gui.tagging.TaggingPreset;
     34import org.openstreetmap.josm.tools.TaggingPresetNameTemplateList;
    3735
    3836/**
     
    5755        return instance;
    5856    }
    59    
     57
    6058    /**
    6159     * Registers a format hook. Adds the hook at the first position of the format hooks.
     
    103101            namingTagsForRelations = new ArrayList<String>(
    104102                    Main.pref.getCollection("relation.nameOrder", Arrays.asList(DEFAULT_NAMING_TAGS_FOR_RELATIONS))
    105             );
     103                    );
    106104        }
    107105        return namingTagsForRelations;
     
    116114     * @return the decorated name
    117115     */
    118     protected String decorateNameWithId(String name, IPrimitive primitive) {
    119         if (Main.pref.getBoolean("osm-primitives.showid"))
    120             if (Main.pref.getBoolean("osm-primitives.showid.new-primitives"))
    121                 return name + tr(" [id: {0}]", primitive.getUniqueId());
    122             else
    123                 return name + tr(" [id: {0}]", primitive.getId());
    124         else
    125             return name;
     116    protected void decorateNameWithId(StringBuilder name, IPrimitive primitive) {
     117        if (Main.pref.getBoolean("osm-primitives.showid")) {
     118            if (Main.pref.getBoolean("osm-primitives.showid.new-primitives")) {
     119                name.append(tr(" [id: {0}]", primitive.getUniqueId()));
     120            } else {
     121                name.append(tr(" [id: {0}]", primitive.getId()));
     122            }
     123        }
    126124    }
    127125
     
    132130     * @return the name
    133131     */
    134     public String format(INode node) {
    135         String name = "";
     132    public String format(Node node) {
     133        StringBuilder name = new StringBuilder();
    136134        if (node.isIncomplete()) {
    137             name = tr("incomplete");
    138         } else {
    139             if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
    140                 name = node.getLocalName();
     135            name.append(tr("incomplete"));
     136        } else {
     137            TaggingPreset preset = TaggingPresetNameTemplateList.getInstance().findPresetTemplate(node);
     138            if (preset == null) {
     139                String n;
     140                if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
     141                    n = node.getLocalName();
     142                } else {
     143                    n = node.getName();
     144                }
     145                if(n == null)
     146                {
     147                    String s;
     148                    if((s = node.get("addr:housename")) != null) {
     149                        /* I18n: name of house as parameter */
     150                        n = tr("House {0}", s);
     151                    }
     152                    if(n == null && (s = node.get("addr:housenumber")) != null) {
     153                        String t = node.get("addr:street");
     154                        if(t != null) {
     155                            /* I18n: house number, street as parameter, number should remain
     156                        before street for better visibility */
     157                            n =  tr("House number {0} at {1}", s, t);
     158                        }
     159                        else {
     160                            /* I18n: house number as parameter */
     161                            n = tr("House number {0}", s);
     162                        }
     163                    }
     164                }
     165
     166                if (n == null) {
     167                    n = node.isNew() ? tr("node") : ""+ node.getId();
     168                }
     169                name.append(n);
    141170            } else {
    142                 name = node.getName();
    143             }
    144             if(name == null)
    145             {
    146                 String s;
    147                 if((s = node.get("addr:housename")) != null) {
    148                     /* I18n: name of house as parameter */
    149                     name = tr("House {0}", s);
    150                 }
    151                 if(name == null && (s = node.get("addr:housenumber")) != null) {
    152                     String t = node.get("addr:street");
    153                     if(t != null) {
    154                         /* I18n: house number, street as parameter, number should remain
    155                         before street for better visibility */
    156                         name =  tr("House number {0} at {1}", s, t);
    157                     }
    158                     else {
    159                         /* I18n: house number as parameter */
    160                         name = tr("House number {0}", s);
    161                     }
    162                 }
    163             }
    164 
    165             if (name == null) {
    166                 name = node.isNew() ? tr("node") : ""+ node.getId();
    167             }
    168             name += " \u200E(" + node.getCoor().latToString(CoordinateFormat.getDefaultFormat()) + ", " + node.getCoor().lonToString(CoordinateFormat.getDefaultFormat()) + ")";
    169         }
    170         name = decorateNameWithId(name, node);
    171 
     171                preset.nameTemplate.appendText(name, node);
     172            }
     173            name.append(" \u200E(").append(node.getCoor().latToString(CoordinateFormat.getDefaultFormat())).append(", ").append(node.getCoor().lonToString(CoordinateFormat.getDefaultFormat())).append(")");
     174        }
     175        decorateNameWithId(name, node);
     176
     177
     178        String result = name.toString();
    172179        for (NameFormatterHook hook: formatHooks) {
    173             String hookResult = hook.checkFormat(node, name);
    174             if (hookResult != null) {
     180            String hookResult = hook.checkFormat(node, result);
     181            if (hookResult != null)
    175182                return hookResult;
    176             }
    177         }
    178 
    179         return name;
     183        }
     184
     185        return result;
    180186    }
    181187
     
    198204     * @return the name
    199205     */
    200     public String format(IWay way) {
    201         String name = "";
     206    public String format(Way way) {
     207        StringBuilder name = new StringBuilder();
    202208        if (way.isIncomplete()) {
    203             name = tr("incomplete");
    204         } else {
    205             if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
    206                 name = way.getLocalName();
     209            name.append(tr("incomplete"));
     210        } else {
     211            TaggingPreset preset = TaggingPresetNameTemplateList.getInstance().findPresetTemplate(way);
     212            if (preset == null) {
     213                String n;
     214                if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
     215                    n = way.getLocalName();
     216                } else {
     217                    n = way.getName();
     218                }
     219                if (n == null) {
     220                    n = way.get("ref");
     221                }
     222                if (n == null) {
     223                    n =
     224                            (way.get("highway") != null) ? tr("highway") :
     225                                (way.get("railway") != null) ? tr("railway") :
     226                                    (way.get("waterway") != null) ? tr("waterway") :
     227                                        (way.get("landuse") != null) ? tr("landuse") : null;
     228                }
     229                if(n == null)
     230                {
     231                    String s;
     232                    if((s = way.get("addr:housename")) != null) {
     233                        /* I18n: name of house as parameter */
     234                        n = tr("House {0}", s);
     235                    }
     236                    if(n == null && (s = way.get("addr:housenumber")) != null) {
     237                        String t = way.get("addr:street");
     238                        if(t != null) {
     239                            /* I18n: house number, street as parameter, number should remain
     240                        before street for better visibility */
     241                            n =  tr("House number {0} at {1}", s, t);
     242                        }
     243                        else {
     244                            /* I18n: house number as parameter */
     245                            n = tr("House number {0}", s);
     246                        }
     247                    }
     248                }
     249                if(n == null || n.length() == 0) {
     250                    n = String.valueOf(way.getId());
     251                }
     252
     253                name.append(n);
    207254            } else {
    208                 name = way.getName();
    209             }
    210             if (name == null) {
    211                 name = way.get("ref");
    212             }
    213             if (name == null) {
    214                 name =
    215                     (way.get("highway") != null) ? tr("highway") :
    216                         (way.get("railway") != null) ? tr("railway") :
    217                             (way.get("waterway") != null) ? tr("waterway") :
    218                                 (way.get("landuse") != null) ? tr("landuse") : null;
    219             }
    220             if(name == null)
    221             {
    222                 String s;
    223                 if((s = way.get("addr:housename")) != null) {
    224                     /* I18n: name of house as parameter */
    225                     name = tr("House {0}", s);
    226                 }
    227                 if(name == null && (s = way.get("addr:housenumber")) != null) {
    228                     String t = way.get("addr:street");
    229                     if(t != null) {
    230                         /* I18n: house number, street as parameter, number should remain
    231                         before street for better visibility */
    232                         name =  tr("House number {0} at {1}", s, t);
    233                     }
    234                     else {
    235                         /* I18n: house number as parameter */
    236                         name = tr("House number {0}", s);
    237                     }
    238                 }
     255                preset.nameTemplate.appendText(name, way);
    239256            }
    240257
     
    242259            if (nodesNo > 1 && way.isClosed()) {
    243260                nodesNo--;
    244             }
    245             if(name == null || name.length() == 0) {
    246                 name = String.valueOf(way.getId());
    247261            }
    248262            /* note: length == 0 should no longer happen, but leave the bracket code
     
    250264            /* I18n: count of nodes as parameter */
    251265            String nodes = trn("{0} node", "{0} nodes", nodesNo, nodesNo);
    252             name += (name.length() > 0) ? " ("+nodes+")" : nodes;
    253         }
    254         name = decorateNameWithId(name, way);
    255        
     266            name.append(" (").append(nodes).append(")");
     267        }
     268        decorateNameWithId(name, way);
     269
     270        String result = name.toString();
    256271        for (NameFormatterHook hook: formatHooks) {
    257             String hookResult = hook.checkFormat(way, name);
    258             if (hookResult != null) {
     272            String hookResult = hook.checkFormat(way, result);
     273            if (hookResult != null)
    259274                return hookResult;
    260             }
    261         }
    262 
    263         return name;
     275        }
     276
     277        return result;
    264278    }
    265279
     
    282296     * @return the name
    283297     */
    284     public String format(IRelation relation) {
    285         String name;
     298    public String format(Relation relation) {
     299        StringBuilder name = new StringBuilder();
    286300        if (relation.isIncomplete()) {
    287             name = tr("incomplete");
    288         } else {
    289             name = getRelationTypeName(relation);
     301            name.append(tr("incomplete"));
     302        } else {
     303            TaggingPreset preset = TaggingPresetNameTemplateList.getInstance().findPresetTemplate(relation);
     304
     305            formatRelationNameAndType(relation, name, preset);
     306
     307            int mbno = relation.getMembersCount();
     308            name.append(trn("{0} member", "{0} members", mbno, mbno));
     309
     310            if (relation.hasIncompleteMembers()) {
     311                name.append(", ").append(tr("incomplete"));
     312            }
     313
     314            name.append(")");
     315        }
     316        decorateNameWithId(name, relation);
     317
     318        String result = name.toString();
     319        for (NameFormatterHook hook: formatHooks) {
     320            String hookResult = hook.checkFormat(relation, result);
     321            if (hookResult != null)
     322                return hookResult;
     323        }
     324
     325        return result;
     326    }
     327
     328    private void formatRelationNameAndType(Relation relation, StringBuilder result, TaggingPreset preset) {
     329        if (preset == null) {
     330            result.append(getRelationTypeName(relation));
    290331            String relationName = getRelationName(relation);
    291332            if (relationName == null) {
     
    294335                relationName = "\"" + relationName + "\"";
    295336            }
    296             name += " (" + relationName + ", ";
    297 
    298             int mbno = relation.getMembersCount();
    299             name += trn("{0} member", "{0} members", mbno, mbno);
    300 
    301             if (relation instanceof Relation) {
    302                 if (((Relation) relation).hasIncompleteMembers()) {
    303                     name += ", "+tr("incomplete");
    304                 }
    305             }
    306 
    307             name += ")";
    308         }
    309         name = decorateNameWithId(name, relation);
    310 
    311         for (NameFormatterHook hook: formatHooks) {
    312             String hookResult = hook.checkFormat(relation, name);
    313             if (hookResult != null) {
    314                 return hookResult;
    315             }
    316         }
    317 
    318         return name;
     337            result.append(" (").append(relationName).append(", ");
     338        } else {
     339            preset.nameTemplate.appendText(result, relation);
     340            result.append("(");
     341        }
    319342    }
    320343
     
    322345        @Override
    323346        public int compare(Relation r1, Relation r2) {
    324             String type1 = getRelationTypeName(r1);
    325             String type2 = getRelationTypeName(r2);
    326 
    327             int comp = type1.compareTo(type2);
    328             if (comp != 0)
    329                 return comp;
    330 
    331             String name1 = getRelationName(r1);
    332             String name2 = getRelationName(r2);
    333 
    334             if (name1 == null && name2 == null)
    335                 return (r1.getUniqueId() > r2.getUniqueId())?1:-1;
    336             else if (name1 == null)
    337                 return -1;
    338             else if (name2 == null)
    339                 return 1;
    340             else if (!name1.isEmpty() && !name2.isEmpty() && Character.isDigit(name1.charAt(0)) && Character.isDigit(name2.charAt(0))) {
    341                 //Compare numerically
    342                 String ln1 = getLeadingNumber(name1);
    343                 String ln2 = getLeadingNumber(name2);
    344 
    345                 comp = Long.valueOf(ln1).compareTo(Long.valueOf(ln2));
    346                 if (comp != 0)
    347                     return comp;
    348 
    349                 // put 1 before 0001
    350                 comp = ln1.compareTo(ln2);
    351                 if (comp != 0)
    352                     return comp;
    353 
    354                 comp = name1.substring(ln1.length()).compareTo(name2.substring(ln2.length()));
     347            //TODO This doesn't work correctly with formatHooks
     348
     349            TaggingPreset preset1 = TaggingPresetNameTemplateList.getInstance().findPresetTemplate(r1);
     350            TaggingPreset preset2 = TaggingPresetNameTemplateList.getInstance().findPresetTemplate(r2);
     351
     352            if (preset1 != null || preset2 != null) {
     353                StringBuilder name1 = new StringBuilder();
     354                formatRelationNameAndType(r1, name1, preset1);
     355                StringBuilder name2 = new StringBuilder();
     356                formatRelationNameAndType(r2, name2, preset2);
     357
     358                int comp = name1.toString().compareTo(name2.toString());
    355359                if (comp != 0)
    356360                    return comp;
    357361            } else {
    358                 comp = name1.compareToIgnoreCase(name2);
     362
     363                String type1 = getRelationTypeName(r1);
     364                String type2 = getRelationTypeName(r2);
     365
     366                int comp = type1.compareTo(type2);
    359367                if (comp != 0)
    360368                    return comp;
    361             }
     369
     370                String name1 = getRelationName(r1);
     371                String name2 = getRelationName(r2);
     372
     373                if (name1 == null && name2 == null)
     374                    return (r1.getUniqueId() > r2.getUniqueId())?1:-1;
     375                else if (name1 == null)
     376                    return -1;
     377                else if (name2 == null)
     378                    return 1;
     379                else if (!name1.isEmpty() && !name2.isEmpty() && Character.isDigit(name1.charAt(0)) && Character.isDigit(name2.charAt(0))) {
     380                    //Compare numerically
     381                    String ln1 = getLeadingNumber(name1);
     382                    String ln2 = getLeadingNumber(name2);
     383
     384                    comp = Long.valueOf(ln1).compareTo(Long.valueOf(ln2));
     385                    if (comp != 0)
     386                        return comp;
     387
     388                    // put 1 before 0001
     389                    comp = ln1.compareTo(ln2);
     390                    if (comp != 0)
     391                        return comp;
     392
     393                    comp = name1.substring(ln1.length()).compareTo(name2.substring(ln2.length()));
     394                    if (comp != 0)
     395                        return comp;
     396                } else {
     397                    comp = name1.compareToIgnoreCase(name2);
     398                    if (comp != 0)
     399                        return comp;
     400                }
     401            }
     402
    362403
    363404            if (r1.getMembersCount() != r2.getMembersCount())
    364405                return (r1.getMembersCount() > r2.getMembersCount())?1:-1;
    365406
    366             comp = Boolean.valueOf(r1.hasIncompleteMembers()).compareTo(Boolean.valueOf(r2.hasIncompleteMembers()));
     407            int comp = Boolean.valueOf(r1.hasIncompleteMembers()).compareTo(Boolean.valueOf(r2.hasIncompleteMembers()));
    367408            if (comp != 0)
    368409                return comp;
     
    408449            name += "["+admin_level+"]";
    409450        }
    410        
     451
    411452        for (NameFormatterHook hook: formatHooks) {
    412453            String hookResult = hook.checkRelationTypeName(relation, name);
    413             if (hookResult != null) {
     454            if (hookResult != null)
    414455                return hookResult;
    415             }
    416456        }
    417457
     
    562602                            (way.get("waterway") != null) ? tr("waterway") :
    563603                                (way.get("landuse") != null) ? tr("landuse") : ""
    564             );
     604                    );
    565605        }
    566606
Note: See TracChangeset for help on using the changeset viewer.