1 | |
|
2 | |
|
3 | |
|
4 | |
package org.homeunix.thecave.buddi.view.swing; |
5 | |
|
6 | |
import java.awt.Component; |
7 | |
import java.lang.reflect.Field; |
8 | |
|
9 | |
import javax.swing.JComponent; |
10 | |
import javax.swing.JList; |
11 | |
|
12 | |
import org.homeunix.thecave.buddi.i18n.BuddiKeys; |
13 | |
import org.homeunix.thecave.buddi.model.Source; |
14 | |
import org.homeunix.thecave.buddi.model.swing.SourceComboBoxModel; |
15 | |
import org.homeunix.thecave.buddi.plugin.api.util.TextFormatter; |
16 | |
|
17 | |
public class SourceListCellRenderer extends MaxLengthListCellRenderer { |
18 | |
public static final long serialVersionUID = 0; |
19 | |
private final String nullLabel; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
public SourceListCellRenderer(String nullLabel, JComponent component) { |
29 | 2433 | super(component); |
30 | 2433 | this.nullLabel = nullLabel; |
31 | 2433 | } |
32 | |
|
33 | |
|
34 | |
@Override |
35 | |
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
36 | |
|
37 | 32298 | Object newValue = ""; |
38 | 32298 | if (value instanceof Source) |
39 | 13651 | newValue = ((Source) value).getFullName(); |
40 | 18647 | else if (BuddiKeys.SPLITS.toString().equals(value)) |
41 | 2717 | newValue = TextFormatter.getTranslation(value.toString()); |
42 | 15930 | else if (index == -1) |
43 | 14308 | newValue = nullLabel; |
44 | |
else |
45 | 1622 | newValue = value; |
46 | |
|
47 | |
|
48 | 32298 | super.getListCellRendererComponent(list, newValue, index, isSelected, cellHasFocus); |
49 | |
|
50 | |
|
51 | 32298 | if (value instanceof Source){ |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | 13651 | Source source = (Source) value; |
57 | 13651 | if (source.isDeleted()){ |
58 | 0 | this.setText("<strike>" + this.getText() + "</strike>"); |
59 | |
} |
60 | |
try { |
61 | 13651 | Field dataModelField = JList.class.getDeclaredField("dataModel"); |
62 | 13651 | dataModelField.setAccessible(true); |
63 | 13651 | Object dataModel = dataModelField.get(list); |
64 | 13651 | if (dataModel instanceof SourceComboBoxModel){ |
65 | 13651 | SourceComboBoxModel model = (SourceComboBoxModel) dataModel; |
66 | 13651 | if (model.getLevel(source) > 0){ |
67 | 0 | StringBuilder sb = new StringBuilder(); |
68 | 0 | for (int i = 0; i < model.getLevel(source); i++){ |
69 | 0 | sb.append(" "); |
70 | |
} |
71 | 0 | this.setText(sb.toString() + this.getText()); |
72 | |
} |
73 | |
} |
74 | |
} |
75 | 0 | catch (Throwable t){ |
76 | 0 | t.printStackTrace(); |
77 | |
|
78 | 13651 | } |
79 | 13651 | this.setText("<html>" + this.getText() + "</html>"); |
80 | 13651 | } |
81 | 18647 | else if (!BuddiKeys.SPLITS.toString().equals(value)) |
82 | 15930 | this.setText("<html><font color='gray'>" + this.getText() + "</font></html>"); |
83 | |
|
84 | 32298 | return this; |
85 | |
} |
86 | |
} |