📄 splittedexplicativejcomboboxpanel.java
字号:
return; } } } } } /** * DOCUMENT ME! * * @return DOCUMENT ME! */ public Object getSelectedItem() { //this is short and it works, but it is still a logical bad work-around, //as getSelectedItem never should return null return ((featureComboBox != null) && (featureComboBox.getSelectedIndex() == -1)) ? comboBoxEditor.getItem() : itemComboBox.getSelectedItem(); } // implementation of java.awt.event.ItemListener interface // only if featureBox exists, is catBox listener public void itemStateChanged(ItemEvent event) { if (event.getStateChange() == ItemEvent.SELECTED) { updateFeatureComboBox(); } } /** * This method breaks up those vectors in the HashMap which exceed a given limit. For a given * key, it builds new (sub)keys, each corresponding to a value vector with maximal 'limit' * elements. It tries to split the original vector in peaces of equal size. * * @param hash hash with strings as keys and vectors as values * @param limit limit Maximum allowed size for each vector */ private static void setVectorSizeLimit(HashMap hash, int limit) { for (Iterator it = ((Set) new HashSet(hash.keySet()).clone()).iterator(); it.hasNext();) { Object key = it.next(); Vector vector = (Vector) hash.get(key); float frac = vector.size() / (float) limit; if (frac > 1) { //adjust limit in order to make partitions equally big int alimit = (int) (((float) vector.size() / ((int) (frac + 0.99999))) + 0.99999); hash.remove(key); String first; String last; String previousFirst = ""; for (int i = 0; i < frac; i++) { int end = ((alimit * (i + 1)) < vector.size()) ? (alimit * (i + 1)) : vector.size(); Vector localVector = new Vector(vector.subList(alimit * i, end)); first = localVector.firstElement().toString(); last = localVector.lastElement().toString(); //set into category box an abbreviation of the first element of the chunk //those abbreviations should have the minimal number of letters necessary to //distinguish between them if (i > 0) { int diff = 1; while (first.substring(0, diff).equals(previousFirst.substring( 0, diff))) diff++; Object subKey = first.substring(0, diff) + "-" + last.substring(diff - 1, diff); //if key is CVentry create subkey with same CVentry-Value (i.e. Tooltiptext) if (key instanceof CVEntry) { subKey = new CVEntry((String) subKey, ((CVEntry) key).getDescription()); } hash.put(subKey, localVector); } else { hash.put(key, localVector); } previousFirst = first; } } } } /** * This method uses only the toString() method. The items can be arbitrary objects. It uses the * frontpart of the string (before the separating character) as key in a HashMap. Each item * with the same frontpart is put into a Vector belonging to that key. If the item-string * doesn't contain the split character, the key "null" is used. * * @param items Vector with arbitrary items * @param splitChar splitChar Character that is used to split * * @return HashMap HashMap with frontparts as keys and Vectors of corresponding items as * values */ private static HashMap splitItems(List items, char splitChar) { HashMap hash = new HashMap(); Vector nonSplit = new Vector(); for (int i = 0; i < items.size(); i++) { String cat = items.get(i).toString(); int splitIndex = cat.indexOf(splitChar); if (splitIndex >= 0) { cat = cat.substring(0, splitIndex); if (hash.containsKey(cat)) { ((Vector) hash.get(cat)).add(items.get(i)); } else { Vector vector = new Vector(); vector.add(items.get(i)); hash.put(cat, vector); } } else { nonSplit.add(items.get(i)); } } for (int i = 0; i < nonSplit.size(); i++) { Object key = nonSplit.elementAt(i); String keyString = key.toString(); //CGN has also "empty" DescriptedObjects, containing only the category name //and a general description of that category //the corresponding String-Keys are thus replaced by those DescriptedObjects if (hash.containsKey(keyString)) { Object value = hash.get(keyString); hash.remove(keyString); hash.put(key, value); } else { Vector vector = new Vector(); vector.add(key); hash.put(key, vector); } } return hash; } private void updateFeatureComboBox() { if (featureComboBox.getItemCount() > 0) { featureComboBox.removeAllItems(); } Vector vector = (Vector) hash.get(categoryComboBox.getSelectedItem()); for (int i = 0; i < vector.size(); i++) { featureComboBox.addItem(vector.elementAt(i)); } featureComboBox.setPopupWidth(featureComboBox.getPreferredSize().width); } /** * This renderer displays only the backpart of the string of CVentries */ public class MyListCellRenderer implements ListCellRenderer { /** Holds value of property DOCUMENT ME! */ private final JLabel label = new JLabel(); /** Holds value of property DOCUMENT ME! */ private final char splitChar; /** * Creates a new MyListCellRenderer instance * * @param splitChar DOCUMENT ME! */ MyListCellRenderer(char splitChar) { this.splitChar = splitChar; setOpaque(true); } /** * DOCUMENT ME! * * @param list DOCUMENT ME! * @param value DOCUMENT ME! * @param index DOCUMENT ME! * @param isSelected DOCUMENT ME! * @param celHasFocus DOCUMENT ME! * * @return DOCUMENT ME! */ public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean celHasFocus) { if (isSelected) { label.setBackground(list.getSelectionBackground()); label.setForeground(list.getSelectionForeground()); } else { label.setBackground(list.getBackground()); label.setForeground(list.getForeground()); } if (value instanceof DescriptedObject) { DescriptedObject descrobj = (DescriptedObject) value; if (isSelected) { list.setToolTipText(descrobj.getDescription()); } String string = value.toString(); int splitindex = string.indexOf(splitChar); string = (splitindex >= 0) ? string.substring(splitindex) : ""; label.setText(string); return label; } else { return comboBoxEditor.getEditorComponent(); } } } /** * DOCUMENT ME! $Id: SplittedExplicativeJComboBoxPanel.java,v 1.5 2005/05/24 16:42:51 klasal * Exp $ * * @author $Author: klasal $ * @version $Revision: 1.4 $ */ private class EmptyDescriptedObject implements DescriptedObject { /** Holds value of property DOCUMENT ME! */ private final String description; /** Holds value of property DOCUMENT ME! */ private final String string; /** * Creates a new EmptyDescriptedObject instance * * @param description DOCUMENT ME! * @param string DOCUMENT ME! */ public EmptyDescriptedObject(String description, String string) { this.string = string; this.description = description; } /** * DOCUMENT ME! * * @return DOCUMENT ME! */ public String getDescription() { return description; } /** * DOCUMENT ME! * * @return DOCUMENT ME! */ public String toString() { return string; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -