| 1 | package relcontext;
|
|---|
| 2 |
|
|---|
| 3 | import javax.swing.JLabel;
|
|---|
| 4 |
|
|---|
| 5 | import org.openstreetmap.josm.data.osm.Relation;
|
|---|
| 6 |
|
|---|
| 7 | /**
|
|---|
| 8 | * Renderer for chosen relation.
|
|---|
| 9 | * [Icon] na=wood U
|
|---|
| 10 | * key is 2-letter; type = icon; to the right — symbol of relation topology (closed, lines, broken).
|
|---|
| 11 | *
|
|---|
| 12 | * @author Zverik
|
|---|
| 13 | */
|
|---|
| 14 | public class ChosenRelationComponent extends JLabel implements ChosenRelationListener {
|
|---|
| 15 |
|
|---|
| 16 | //private ChosenRelation chRel;
|
|---|
| 17 |
|
|---|
| 18 | public ChosenRelationComponent(ChosenRelation rel) {
|
|---|
| 19 | super("");
|
|---|
| 20 | /* setBackground(Color.white);
|
|---|
| 21 | setOpaque(true);
|
|---|
| 22 | setBorder(new LineBorder(Color.black, 1, true));*/
|
|---|
| 23 | //this.chRel = rel;
|
|---|
| 24 | rel.addChosenRelationListener(this);
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | public void chosenRelationChanged( Relation oldRelation, Relation newRelation ) {
|
|---|
| 28 | setText(prepareText(newRelation));
|
|---|
| 29 | repaint();
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | private final static String[] TYPE_KEYS = new String[] {
|
|---|
| 33 | "natural", "landuse", "place", "waterway", "leisure", "amenity", "restriction", "public_transport", "route", "enforcement"
|
|---|
| 34 | };
|
|---|
| 35 |
|
|---|
| 36 | private final static String[] NAMING_TAGS = new String[] {
|
|---|
| 37 | "name", "place_name", "ref", "destination", "note"
|
|---|
| 38 | };
|
|---|
| 39 |
|
|---|
| 40 | protected String prepareText( Relation rel ) {
|
|---|
| 41 | if( rel == null )
|
|---|
| 42 | return "";
|
|---|
| 43 |
|
|---|
| 44 | String type = rel.get("type");
|
|---|
| 45 | if( type == null || type.length() == 0 )
|
|---|
| 46 | type = "-";
|
|---|
| 47 |
|
|---|
| 48 | String tag = null;
|
|---|
| 49 | for( int i = 0; i < TYPE_KEYS.length && tag == null; i++ )
|
|---|
| 50 | if( rel.hasKey(TYPE_KEYS[i]))
|
|---|
| 51 | tag = TYPE_KEYS[i];
|
|---|
| 52 | if( tag != null )
|
|---|
| 53 | tag = tag.substring(0, 2) + "=" + rel.get(tag);
|
|---|
| 54 |
|
|---|
| 55 | String name = null;
|
|---|
| 56 | for( int i = 0; i < NAMING_TAGS.length && name == null; i++ )
|
|---|
| 57 | if( rel.hasKey(NAMING_TAGS[i]) )
|
|---|
| 58 | name = rel.get(NAMING_TAGS[i]);
|
|---|
| 59 |
|
|---|
| 60 | StringBuilder sb = new StringBuilder();
|
|---|
| 61 | sb.append(type.substring(0, 1));
|
|---|
| 62 | if( type.equals("boundary") && rel.hasKey("admin_level") )
|
|---|
| 63 | sb.append(rel.get("admin_level"));
|
|---|
| 64 | if( name != null )
|
|---|
| 65 | sb.append(" \"").append(name).append('"');
|
|---|
| 66 | if( tag != null )
|
|---|
| 67 | sb.append("; ").append(tag);
|
|---|
| 68 | sb.append(" (").append(rel.getMembersCount()).append(')');
|
|---|
| 69 |
|
|---|
| 70 | return sb.toString();
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|