Parent: [a7a9ff] (diff)

Download this file

SortedComboBox.java    146 lines (124 with data), 4.9 kB

  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
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright 2013-2014 TECO - Karlsruhe Institute of Technology.
*
* This file is part of TACET.
*
* TACET is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TACET is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TACET. If not, see <http://www.gnu.org/licenses/>.
*/
package squirrel.view;
import java.awt.Component;
import java.awt.Font;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import squirrel.model.MetaClassifier;
import weka.classifiers.Classifier;
public class SortedComboBox<E> extends JComboBox<E> {
private List<String> list;
public SortedComboBox() {
super();
this.setModel(new MyComboBoxModel());
this.setRenderer(new MyComboRenderer(this));
list = new ArrayList<String>();
}
public void addClassifier(E item) {
if (item instanceof MetaClassifier) {
MetaClassifier c = (MetaClassifier) item;
if (c.getType() != null) {
String type = c.getType();
String name = c.getName();
if (!list.contains(type)) {
list.add(type);
addItem((E) type);
list.add(name);
addItem((E) c);
} else {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).equals(type)) {
list.add(i + 1, name);
insertItemAt((E) c, i + 1);
break;
}
}
}
// } else {
// String name = c.getName();
// if (!list.contains(name)) {
// list.add(0, name);
// insertItemAt((E) c, 0);
// }
}
}
}
class MyComboBoxModel extends DefaultComboBoxModel {
@Override
public void setSelectedItem(Object anObject) {
if (anObject != null) {
if (anObject instanceof MetaClassifier) {
super.setSelectedItem(anObject);
}
} else {
super.setSelectedItem(anObject);
}
}
}
class MyComboRenderer extends BasicComboBoxRenderer {
private static final long serialVersionUID = 1L;
private JComboBox comboBox;
final DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();
private int row;
MyComboRenderer(JComboBox fontsBox) {
comboBox = fontsBox;
}
// private void manItemInCombo() {
// if (comboBox.getItemCount() > 0) {
// final Object comp = comboBox.getUI().getAccessibleChild(comboBox, 0);
// if ((comp instanceof JPopupMenu)) {
// final JList list = new JList(comboBox.getModel());
// final JPopupMenu popup = (JPopupMenu) comp;
// final JScrollPane scrollPane = (JScrollPane) popup.getComponent(0);
// final JViewport viewport = scrollPane.getViewport();
// final Rectangle rect = popup.getVisibleRect();
// final Point pt = viewport.getViewPosition();
// row = list.locationToIndex(pt);
// }
// }
// }
@Override
public Component getListCellRendererComponent(JList list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
// if (list.getModel().getSize() > 0) {
// manItemInCombo();
// }
// final JLabel renderer = (JLabel)
// defaultRenderer.getListCellRendererComponent(list, value, row,
// isSelected, cellHasFocus);
// final Object fntObj = value;
// final String fontFamilyName = (String) fntObj;
if (value instanceof MetaClassifier) {
MetaClassifier c = (MetaClassifier) value;
setText(" " + c.getName());
setFont(new Font("Courier", Font.PLAIN, 12));
} else {
setFont(new Font("Courier", Font.BOLD, 12));
}
return this;
}
}
}