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