| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.app.bss; |
| 27 |
|
| 28 |
import java.awt.Component; |
| 29 |
import java.util.HashSet; |
| 30 |
import java.util.Set; |
| 31 |
|
| 32 |
import javax.swing.JComboBox; |
| 33 |
import javax.swing.JList; |
| 34 |
import javax.swing.UIManager; |
| 35 |
import javax.swing.plaf.basic.BasicComboBoxRenderer; |
| 36 |
|
| 37 |
@SuppressWarnings("serial") |
| 38 |
public class DisabledItemsComboBox extends JComboBox { |
| 39 |
|
| 40 |
private Set<Object> disabledItems = new HashSet<Object>(); |
| 41 |
|
| 42 |
@SuppressWarnings("unchecked") |
| 43 |
public DisabledItemsComboBox() { |
| 44 |
super(); |
| 45 |
super.setRenderer(new DisabledItemsRenderer()); |
| 46 |
} |
| 47 |
|
| 48 |
public void addItem(Object object, boolean isDisabled) { |
| 49 |
super.addItem(object); |
| 50 |
if (isDisabled) { |
| 51 |
disabledItems.add(getItemCount() - 1); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
@Override |
| 56 |
public void removeAllItems() { |
| 57 |
super.removeAllItems(); |
| 58 |
disabledItems = new HashSet<Object>(); |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public void removeItemAt(final int index) { |
| 63 |
super.removeItemAt(index); |
| 64 |
disabledItems.remove(index); |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
public void removeItem(final Object anObject) { |
| 69 |
for (int i = 0; i < getItemCount(); i++) { |
| 70 |
|
| 71 |
if (getItemAt(i) == anObject) { |
| 72 |
disabledItems.remove(i); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
super.removeItem(anObject); |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
public void setSelectedIndex(int index) { |
| 81 |
|
| 82 |
if (!disabledItems.contains(index)) { |
| 83 |
super.setSelectedIndex(index); |
| 84 |
} |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
private class DisabledItemsRenderer extends BasicComboBoxRenderer { |
| 89 |
|
| 90 |
@Override |
| 91 |
public Component getListCellRendererComponent(@SuppressWarnings("rawtypes") JList list, Object value, |
| 92 |
int index, boolean isSelected, boolean cellHasFocus) { |
| 93 |
|
| 94 |
if (isSelected) { |
| 95 |
|
| 96 |
setBackground(list.getSelectionBackground()); |
| 97 |
setForeground(list.getSelectionForeground()); |
| 98 |
|
| 99 |
} else { |
| 100 |
|
| 101 |
setBackground(list.getBackground()); |
| 102 |
setForeground(list.getForeground()); |
| 103 |
} |
| 104 |
|
| 105 |
if (disabledItems.contains(index)) { |
| 106 |
|
| 107 |
setFocusable(false); |
| 108 |
setEnabled(false); |
| 109 |
setBackground(list.getBackground()); |
| 110 |
setForeground(UIManager.getColor("Label.disabledForeground")); |
| 111 |
|
| 112 |
} else { |
| 113 |
|
| 114 |
setFocusable(true); |
| 115 |
setEnabled(true); |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
setText((value == null) ? "" : value.toString()); |
| 120 |
|
| 121 |
return this; |
| 122 |
} |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
} |