Ignore:
Timestamp:
28.12.2009 00:16:04 (2 years ago)
Author:
Gubaer
Message:

new: Changeset Cache Manager for querying, downloading, browsing, and managing changesets within JOSM. See also Changeset Manager and Changeset Query Dialog

File:
1 edited

Legend:

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

    r2578 r2689  
    2020import org.openstreetmap.josm.data.osm.Relation; 
    2121import org.openstreetmap.josm.data.osm.Way; 
     22import org.openstreetmap.josm.data.osm.history.HistoryNameFormatter; 
     23import org.openstreetmap.josm.data.osm.history.HistoryNode; 
     24import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive; 
     25import org.openstreetmap.josm.data.osm.history.HistoryRelation; 
     26import org.openstreetmap.josm.data.osm.history.HistoryWay; 
    2227 
    2328/** 
     
    2530 * 
    2631 */ 
    27 public class DefaultNameFormatter implements NameFormatter { 
     32public class DefaultNameFormatter implements NameFormatter, HistoryNameFormatter { 
    2833 
    2934    static private DefaultNameFormatter instance; 
     
    238243        return sb.toString(); 
    239244    } 
     245 
     246 
     247    /** 
     248     * Decorates the name of primitive with its id, if the preference 
     249     * <tt>osm-primitives.showid</tt> is set. 
     250     *  
     251     * The id is append to the {@see StringBuilder} passed in in <code>name</code>. 
     252     * 
     253     * @param name  the name without the id 
     254     * @param primitive the primitive 
     255     */ 
     256    protected void decorateNameWithId(StringBuilder name, HistoryOsmPrimitive primitive) { 
     257        if (Main.pref.getBoolean("osm-primitives.showid")) { 
     258            name.append(tr(" [id: {0}]", primitive.getId())); 
     259        } 
     260    } 
     261 
     262 
     263    /** 
     264     * Formats a name for a history node 
     265     * 
     266     * @param node the node 
     267     * @return the name 
     268     */ 
     269    public String format(HistoryNode node) { 
     270        StringBuilder sb = new StringBuilder(); 
     271        String name; 
     272        if (Main.pref.getBoolean("osm-primitives.localize-name", true)) { 
     273            name = node.getLocalName(); 
     274        } else { 
     275            name = node.getName(); 
     276        } 
     277        if (name == null) { 
     278            sb.append(node.getId()); 
     279        } else { 
     280            sb.append(name); 
     281        } 
     282        sb.append(" (") 
     283        .append(node.getCoords().latToString(CoordinateFormat.getDefaultFormat())) 
     284        .append(", ") 
     285        .append(node.getCoords().lonToString(CoordinateFormat.getDefaultFormat())) 
     286        .append(")"); 
     287        decorateNameWithId(sb, node); 
     288        return sb.toString(); 
     289    } 
     290 
     291    /** 
     292     * Formats a name for a way 
     293     * 
     294     * @param way the way 
     295     * @return the name 
     296     */ 
     297    public String format(HistoryWay way) { 
     298        StringBuilder sb = new StringBuilder(); 
     299        String name; 
     300        if (Main.pref.getBoolean("osm-primitives.localize-name", true)) { 
     301            name = way.getLocalName(); 
     302        } else { 
     303            name = way.getName(); 
     304        } 
     305        if (name != null) { 
     306            sb.append(name); 
     307        } 
     308        if (sb.length() == 0 && way.get("ref") != null) { 
     309            sb.append(way.get("ref")); 
     310        } 
     311        if (sb.length() == 0) { 
     312            sb.append( 
     313                    (way.get("highway") != null) ? tr("highway") : 
     314                        (way.get("railway") != null) ? tr("railway") : 
     315                            (way.get("waterway") != null) ? tr("waterway") : 
     316                                (way.get("landuse") != null) ? tr("landuse") : "" 
     317            ); 
     318        } 
     319 
     320        int nodesNo = way.isClosed() ? way.getNumNodes() -1 : way.getNumNodes(); 
     321        String nodes = trn("{0} node", "{0} nodes", nodesNo, nodesNo); 
     322        sb.append((sb.length() > 0) ? " ("+nodes+")" : nodes); 
     323        decorateNameWithId(sb, way); 
     324        return sb.toString(); 
     325    } 
     326 
     327 
     328    /** 
     329     * Formats a name for a {@see HistoryRelation}) 
     330     * 
     331     * @param relation the relation 
     332     * @return the name 
     333     */ 
     334    public String format(HistoryRelation relation) { 
     335        StringBuilder sb = new StringBuilder(); 
     336        if (relation.get("type") != null) { 
     337            sb.append(relation.get("type")); 
     338        } else { 
     339            sb.append(tr("relation")); 
     340        } 
     341        sb.append(" ("); 
     342        String nameTag = null; 
     343        Set<String> namingTags = new HashSet<String>(getNamingtagsForRelations()); 
     344        for (String n : relation.getTags().keySet()) { 
     345            // #3328: "note " and " note" are name tags too 
     346            if (namingTags.contains(n.trim())) { 
     347                if (Main.pref.getBoolean("osm-primitives.localize-name", true)) { 
     348                    nameTag = relation.getLocalName(); 
     349                } else { 
     350                    nameTag = relation.getName(); 
     351                } 
     352                if (nameTag == null) { 
     353                    nameTag = relation.get(n); 
     354                } 
     355            } 
     356            if (nameTag != null) { 
     357                break; 
     358            } 
     359        } 
     360        if (nameTag == null) { 
     361            sb.append(Long.toString(relation.getId())).append(", "); 
     362        } else { 
     363            sb.append("\"").append(nameTag).append("\", "); 
     364        } 
     365 
     366        int mbno = relation.getNumMembers(); 
     367        sb.append(trn("{0} member", "{0} members", mbno, mbno)).append(")"); 
     368 
     369        decorateNameWithId(sb, relation); 
     370        return sb.toString(); 
     371    } 
     372 
     373    /** 
     374     * Builds a default tooltip text for an HistoryOsmPrimitive <code>primitive</code>. 
     375     *  
     376     * @param primitive the primitmive 
     377     * @return the tooltip text 
     378     */ 
     379    public String buildDefaultToolTip(HistoryOsmPrimitive primitive) { 
     380        StringBuilder sb = new StringBuilder(); 
     381        sb.append("<html>"); 
     382        sb.append("<strong>id</strong>=") 
     383        .append(primitive.getId()) 
     384        .append("<br>"); 
     385        ArrayList<String> keyList = new ArrayList<String>(primitive.getTags().keySet()); 
     386        Collections.sort(keyList); 
     387        for (int i = 0; i < keyList.size(); i++) { 
     388            if (i > 0) { 
     389                sb.append("<br>"); 
     390            } 
     391            String key = keyList.get(i); 
     392            sb.append("<strong>") 
     393            .append(key) 
     394            .append("</strong>") 
     395            .append("="); 
     396            String value = primitive.get(key); 
     397            while(value.length() != 0) { 
     398                sb.append(value.substring(0,Math.min(50, value.length()))); 
     399                if (value.length() > 50) { 
     400                    sb.append("<br>"); 
     401                    value = value.substring(50); 
     402                } else { 
     403                    value = ""; 
     404                } 
     405            } 
     406        } 
     407        sb.append("</html>"); 
     408        return sb.toString(); 
     409    } 
    240410} 
Note: See TracChangeset for help on using the changeset viewer.