Debian Codegraph @

src/dr/app/bss/DisabledItemsComboBox.java

raw symbols SCIP
1 /*
2 * DisabledItemsComboBox.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 }// END: Constructor
47
48 public void addItem(Object object, boolean isDisabled) {
49 super.addItem(object);
50 if (isDisabled) {
51 disabledItems.add(getItemCount() - 1);
52 }
53 }// END: addItem
54
55 @Override
56 public void removeAllItems() {
57 super.removeAllItems();
58 disabledItems = new HashSet<Object>();
59 }// END: removeAllItems
60
61 @Override
62 public void removeItemAt(final int index) {
63 super.removeItemAt(index);
64 disabledItems.remove(index);
65 }// END: removeItemAt
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 }// END: removeItem
78
79 @Override
80 public void setSelectedIndex(int index) {
81
82 if (!disabledItems.contains(index)) {
83 super.setSelectedIndex(index);
84 }
85
86 }// END: setSelectedIndex
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 }// END: getListCellRendererComponent
123
124 }// END: DisabledItemsRenderer
125
126 }// END: class