| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
package org.dishevelled.identify; |
| 25 | |
|
| 26 | |
import java.awt.Image; |
| 27 | |
import java.awt.Component; |
| 28 | |
import java.awt.ComponentOrientation; |
| 29 | |
|
| 30 | |
import javax.swing.JTree; |
| 31 | |
import javax.swing.JLabel; |
| 32 | |
import javax.swing.ImageIcon; |
| 33 | |
|
| 34 | |
import javax.swing.tree.DefaultTreeCellRenderer; |
| 35 | |
|
| 36 | |
import org.dishevelled.iconbundle.IconSize; |
| 37 | |
import org.dishevelled.iconbundle.IconState; |
| 38 | |
import org.dishevelled.iconbundle.IconBundle; |
| 39 | |
import org.dishevelled.iconbundle.IconTextDirection; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
public class IdTreeCellRenderer |
| 49 | |
extends DefaultTreeCellRenderer |
| 50 | |
{ |
| 51 | |
|
| 52 | 1 | public static final IconSize DEFAULT_ICON_SIZE = IconSize.DEFAULT_16X16; |
| 53 | |
|
| 54 | |
|
| 55 | |
private IconSize iconSize; |
| 56 | |
|
| 57 | |
|
| 58 | |
private transient ImageIcon imageIcon; |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
public IdTreeCellRenderer() |
| 68 | |
{ |
| 69 | 3 | this(DEFAULT_ICON_SIZE); |
| 70 | 3 | } |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
public IdTreeCellRenderer(final IconSize iconSize) |
| 79 | |
{ |
| 80 | 6 | super(); |
| 81 | 6 | setIconSize(iconSize); |
| 82 | 5 | } |
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
public final IconSize getIconSize() |
| 91 | |
{ |
| 92 | 4 | return iconSize; |
| 93 | |
} |
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
public final void setIconSize(final IconSize iconSize) |
| 101 | |
{ |
| 102 | 8 | if (iconSize == null) |
| 103 | |
{ |
| 104 | 1 | throw new IllegalArgumentException("iconSize must not be null"); |
| 105 | |
} |
| 106 | 7 | this.iconSize = iconSize; |
| 107 | 7 | } |
| 108 | |
|
| 109 | |
|
| 110 | |
public Component getTreeCellRendererComponent(final JTree tree, |
| 111 | |
final Object value, |
| 112 | |
final boolean isSelected, |
| 113 | |
final boolean isExpanded, |
| 114 | |
final boolean isLeaf, |
| 115 | |
final int row, |
| 116 | |
final boolean hasFocus) |
| 117 | |
{ |
| 118 | 55 | JLabel label = (JLabel) super.getTreeCellRendererComponent(tree, value, isSelected, isExpanded, isLeaf, row, hasFocus); |
| 119 | |
|
| 120 | 55 | String name = IdentifyUtils.getNameFor(value); |
| 121 | 55 | label.setText(name); |
| 122 | |
|
| 123 | 55 | IconBundle iconBundle = IdentifyUtils.getIconBundleFor(value); |
| 124 | |
|
| 125 | 55 | if (iconBundle == null) |
| 126 | |
{ |
| 127 | 22 | label.setIcon(null); |
| 128 | |
} |
| 129 | |
else |
| 130 | |
{ |
| 131 | 33 | IconTextDirection textDirection = determineTextDirection(label); |
| 132 | 33 | Image image = iconBundle.getImage(label, textDirection, IconState.NORMAL, iconSize); |
| 133 | |
|
| 134 | 33 | if (imageIcon == null) |
| 135 | |
{ |
| 136 | 1 | imageIcon = new ImageIcon(image); |
| 137 | |
} |
| 138 | |
else |
| 139 | |
{ |
| 140 | 32 | imageIcon.setImage(image); |
| 141 | |
} |
| 142 | 33 | label.setIcon(imageIcon); |
| 143 | |
} |
| 144 | |
|
| 145 | 55 | return label; |
| 146 | |
} |
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
private IconTextDirection determineTextDirection(final JLabel label) |
| 157 | |
{ |
| 158 | 33 | ComponentOrientation componentOrientation = label.getComponentOrientation(); |
| 159 | 33 | return componentOrientation.isLeftToRight() ? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT; |
| 160 | |
} |
| 161 | |
} |