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