1 | |
|
2 | |
|
3 | |
|
4 | |
package org.homeunix.thecave.buddi.view.swing; |
5 | |
|
6 | |
import java.awt.Component; |
7 | |
|
8 | |
import javax.swing.DefaultListCellRenderer; |
9 | |
import javax.swing.JComponent; |
10 | |
import javax.swing.JList; |
11 | |
|
12 | |
import org.homeunix.thecave.buddi.util.Formatter; |
13 | |
|
14 | |
public class MaxLengthListCellRenderer extends DefaultListCellRenderer { |
15 | |
public static final long serialVersionUID = 0; |
16 | |
|
17 | |
private final JComponent component; |
18 | |
|
19 | 3598 | private int computedLength = -1; |
20 | |
|
21 | 3598 | public MaxLengthListCellRenderer(JComponent component) { |
22 | 3598 | this.component = component; |
23 | 3598 | } |
24 | |
|
25 | |
@Override |
26 | |
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
27 | 32298 | super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
28 | |
|
29 | 32298 | if (value != null){ |
30 | 32298 | String s = value.toString(); |
31 | 32298 | if (index == -1 && s.length() > computedLength && component != null && component.getGraphics() != null) |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 4988 | this.setText(Formatter.getStringToLength(s, component.getWidth() - 55, component.getGraphics().getFontMetrics())); |
38 | |
else |
39 | 27310 | this.setText(s); |
40 | 32298 | } |
41 | |
else { |
42 | 0 | this.setText(" "); |
43 | |
} |
44 | |
|
45 | 32298 | return this; |
46 | |
} |
47 | |
} |