1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.relation;
|
---|
3 |
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.awt.Component;
|
---|
6 |
|
---|
7 | import javax.swing.ImageIcon;
|
---|
8 | import javax.swing.JLabel;
|
---|
9 | import javax.swing.JTree;
|
---|
10 | import javax.swing.tree.TreeCellRenderer;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
13 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
14 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * This is the cell renderer used in {@see RelationTree}.
|
---|
18 | *
|
---|
19 | *
|
---|
20 | */
|
---|
21 | public class RelationTreeCellRenderer extends JLabel implements TreeCellRenderer {
|
---|
22 | public final static Color BGCOLOR_SELECTED = new Color(143,170,255);
|
---|
23 |
|
---|
24 | /** the relation icon */
|
---|
25 | private ImageIcon icon;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * constructor
|
---|
29 | */
|
---|
30 | public RelationTreeCellRenderer() {
|
---|
31 | icon = ImageProvider.get("data", "relation");
|
---|
32 | setOpaque(true);
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * renders the icon
|
---|
37 | */
|
---|
38 | protected void renderIcon() {
|
---|
39 | setIcon(icon);
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * renders the textual value. Uses the relations names as value
|
---|
44 | *
|
---|
45 | * @param relation the relation
|
---|
46 | */
|
---|
47 | protected void renderValue(Relation relation) {
|
---|
48 | setText(relation.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * renders the background
|
---|
53 | *
|
---|
54 | * @param selected true, if the current node is selected
|
---|
55 | */
|
---|
56 | protected void renderBackground(boolean selected) {
|
---|
57 | Color bgColor = Color.WHITE;
|
---|
58 | if (selected) {
|
---|
59 | bgColor = BGCOLOR_SELECTED;
|
---|
60 | }
|
---|
61 | setBackground(bgColor);
|
---|
62 | }
|
---|
63 |
|
---|
64 | public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
|
---|
65 | boolean leaf, int row, boolean hasFocus) {
|
---|
66 | renderIcon();
|
---|
67 | renderValue((Relation)value);
|
---|
68 | renderBackground(selected);
|
---|
69 | return this;
|
---|
70 | }
|
---|
71 |
|
---|
72 | }
|
---|