1
2
3
4
5
6
7
8
9
10
11 package edu.uci.ics.jung.visualization.renderers;
12
13 import java.awt.Color;
14 import java.awt.Component;
15 import java.awt.Font;
16 import java.awt.Rectangle;
17 import java.io.Serializable;
18
19 import javax.swing.JComponent;
20 import javax.swing.JLabel;
21 import javax.swing.border.Border;
22 import javax.swing.border.EmptyBorder;
23
24
25
26
27
28
29
30
31
32 @SuppressWarnings("serial")
33 public class DefaultVertexLabelRenderer extends JLabel implements
34 VertexLabelRenderer, Serializable {
35
36 protected static Border noFocusBorder = new EmptyBorder(0,0,0,0);
37
38 protected Color pickedVertexLabelColor = Color.black;
39
40
41
42
43 public DefaultVertexLabelRenderer(Color pickedVertexLabelColor) {
44 this.pickedVertexLabelColor = pickedVertexLabelColor;
45 setOpaque(true);
46 setBorder(noFocusBorder);
47 }
48
49
50
51
52
53
54
55 @Override
56 public void setForeground(Color c) {
57 super.setForeground(c);
58 }
59
60
61
62
63
64
65
66 @Override
67 public void setBackground(Color c) {
68 super.setBackground(c);
69 }
70
71
72
73
74
75
76
77
78
79 @Override
80 public void updateUI() {
81 super.updateUI();
82 setForeground(null);
83 setBackground(null);
84 }
85
86
87
88
89
90
91
92
93
94
95
96 public <V> Component getVertexLabelRendererComponent(JComponent vv, Object value,
97 Font font, boolean isSelected, V vertex) {
98
99 super.setForeground(vv.getForeground());
100 if(isSelected) setForeground(pickedVertexLabelColor);
101 super.setBackground(vv.getBackground());
102 if(font != null) {
103 setFont(font);
104 } else {
105 setFont(vv.getFont());
106 }
107 setIcon(null);
108 setBorder(noFocusBorder);
109 setValue(value);
110 return this;
111 }
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126 @Override
127 public boolean isOpaque() {
128 Color back = getBackground();
129 Component p = getParent();
130 if (p != null) {
131 p = p.getParent();
132 }
133 boolean colorMatch = (back != null) && (p != null) &&
134 back.equals(p.getBackground()) &&
135 p.isOpaque();
136 return !colorMatch && super.isOpaque();
137 }
138
139
140
141
142
143
144 @Override
145 public void validate() {}
146
147
148
149
150
151
152 @Override
153 public void revalidate() {}
154
155
156
157
158
159
160 @Override
161 public void repaint(long tm, int x, int y, int width, int height) {}
162
163
164
165
166
167
168 @Override
169 public void repaint(Rectangle r) { }
170
171
172
173
174
175
176 @Override
177 protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
178
179 if (propertyName=="text") {
180 super.firePropertyChange(propertyName, oldValue, newValue);
181 }
182 }
183
184
185
186
187
188
189 @Override
190 public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { }
191
192
193
194
195
196
197
198
199
200
201 protected void setValue(Object value) {
202 setText((value == null) ? "" : value.toString());
203 }
204 }