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 DefaultEdgeLabelRenderer extends JLabel implements
34 EdgeLabelRenderer, Serializable {
35
36 protected static Border noFocusBorder = new EmptyBorder(0,0,0,0);
37
38 protected Color pickedEdgeLabelColor = Color.black;
39 protected boolean rotateEdgeLabels;
40
41 public DefaultEdgeLabelRenderer(Color pickedEdgeLabelColor) {
42 this(pickedEdgeLabelColor, true);
43 }
44
45
46
47
48 public DefaultEdgeLabelRenderer(Color pickedEdgeLabelColor, boolean rotateEdgeLabels) {
49 super();
50 this.pickedEdgeLabelColor = pickedEdgeLabelColor;
51 this.rotateEdgeLabels = rotateEdgeLabels;
52 setOpaque(true);
53 setBorder(noFocusBorder);
54 }
55
56
57
58
59 public boolean isRotateEdgeLabels() {
60 return rotateEdgeLabels;
61 }
62
63
64
65 public void setRotateEdgeLabels(boolean rotateEdgeLabels) {
66 this.rotateEdgeLabels = rotateEdgeLabels;
67 }
68
69
70
71
72
73
74 @Override
75 public void setForeground(Color c) {
76 super.setForeground(c);
77 }
78
79
80
81
82
83
84
85 @Override
86 public void setBackground(Color c) {
87 super.setBackground(c);
88 }
89
90
91
92
93
94
95
96
97
98 @Override
99 public void updateUI() {
100 super.updateUI();
101 setForeground(null);
102 setBackground(null);
103 }
104
105
106
107
108
109
110
111
112
113
114
115 public <E> Component getEdgeLabelRendererComponent(JComponent vv, Object value,
116 Font font, boolean isSelected, E edge) {
117
118 super.setForeground(vv.getForeground());
119 if(isSelected) setForeground(pickedEdgeLabelColor);
120 super.setBackground(vv.getBackground());
121
122 if(font != null) {
123 setFont(font);
124 } else {
125 setFont(vv.getFont());
126 }
127 setIcon(null);
128 setBorder(noFocusBorder);
129 setValue(value);
130 return this;
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146 @Override
147 public boolean isOpaque() {
148 Color back = getBackground();
149 Component p = getParent();
150 if (p != null) {
151 p = p.getParent();
152 }
153 boolean colorMatch = (back != null) && (p != null) &&
154 back.equals(p.getBackground()) &&
155 p.isOpaque();
156 return !colorMatch && super.isOpaque();
157 }
158
159
160
161
162
163
164 @Override
165 public void validate() {}
166
167
168
169
170
171
172 @Override
173 public void revalidate() {}
174
175
176
177
178
179
180 @Override
181 public void repaint(long tm, int x, int y, int width, int height) {}
182
183
184
185
186
187
188 @Override
189 public void repaint(Rectangle r) { }
190
191
192
193
194
195
196 @Override
197 protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
198
199 if (propertyName=="text") {
200 super.firePropertyChange(propertyName, oldValue, newValue);
201 }
202 }
203
204
205
206
207
208
209 @Override
210 public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { }
211
212
213
214
215
216
217
218
219
220
221 protected void setValue(Object value) {
222 setText((value == null) ? "" : value.toString());
223 }
224 }