📄 splittedexplicativejcomboboxpanel.java
字号:
/* * File: SplittedExplicativeJComboBoxPanel.java * Project: MPI Linguistic Application * Date: 02 May 2007 * * Copyright (C) 2001-2007 Max Planck Institute for Psycholinguistics * * This program 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 2 of the License, or * (at your option) any later version. * * This program 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 this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *//* This program 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 2 of the License, or * (at your option) any later version. * * This program 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 this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package mpi.util.gui;import mpi.util.CVEntry;import mpi.util.DescriptedObject;import java.awt.BorderLayout;import java.awt.Component;import java.awt.event.InputEvent;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Set;import java.util.Vector;import javax.swing.ComboBoxEditor;import javax.swing.JComboBox;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JPanel;import javax.swing.ListCellRenderer;import javax.swing.ToolTipManager;/** * SplittedExplicativeJComboBoxPanel is a Panel which splits a long Vector of similar Strings into * two depending JComboBoxes. The beginnings (before splitting char) of the strings are put in the * first ComboBox, the second ComboBox actually contains the full items, however displays only the * string-endings. One might consider it as a primitive form of automatic completion. The second * box is a modyfied ExplicativeComboBox, e.g. it contains the CVentries as Items and presents * ToolTips. If an Item doesn't contain the split-character, it is automatically used in the * first ComboBox. If there are no corresponding items in the second, than it is ignored. If no * Item contains the split-character, all Items are shown in a single ExplicativeJComboBox. * * @author Alexander Klassmann * @version 2.0 24/10/2002 */public class SplittedExplicativeJComboBoxPanel extends JPanel implements ItemListener { /** Holds value of property DOCUMENT ME! */ private final HashMap hash; /** Holds value of property DOCUMENT ME! */ private final JComboBox categoryComboBox = new ExplicativeJComboBox(); /** Holds value of property DOCUMENT ME! */ private final char splitChar; private ComboBoxEditor comboBoxEditor; private JComboBox itemComboBox; // placeholder for one of the two above boxes private SteppedComboBox featureComboBox = null; private boolean itemBoxEditable; /** * DOCUMENT ME! * * @param items Vector of mpi.vocabs.CVentries * @param splitChar character which defines the position to where to split entries */ public SplittedExplicativeJComboBoxPanel(List items, char splitChar) { this(items, splitChar, 50); } /** * DOCUMENT ME! * * @param items Vector of mpi.vocabs.CVentries * @param splitChar max number of entries in second panel * @param limit character which defines the position to where to split entries */ public SplittedExplicativeJComboBoxPanel(List items, char splitChar, int limit) { this.splitChar = splitChar; setLayout(new BorderLayout()); hash = splitItems(items, splitChar); //Use original vector, if no split items or all items belong to different categories Set set; if (hash.size() == items.size()) { set = (Set) new HashSet(items); } else { Iterator it = hash.keySet().iterator(); while (it.hasNext()) { Object key = it.next(); Vector vector = (Vector) hash.get(key); if (vector.size() > 2) { vector.insertElementAt(new EmptyDescriptedObject( "All Elements", key.toString() + "(*)"), 0); } } setVectorSizeLimit(hash, limit); set = (Set) new HashSet(hash.keySet()).clone(); } //Adding the elements in sorted order to the categoryBox while (set.size() > 0) { Iterator it = set.iterator(); Object object1 = it.next(); while (it.hasNext()) { Object object2 = it.next(); if (object1.toString().compareTo(object2.toString()) > 0) { object1 = object2; } } categoryComboBox.addItem(object1); set.remove(object1); } categoryComboBox.setSelectedIndex(0); categoryComboBox.setMaximumRowCount(15); // larger than swing default if (hash.size() != items.size()) { featureComboBox = new SteppedComboBox(); updateFeatureComboBox(); add(categoryComboBox, BorderLayout.WEST); featureComboBox.setMaximumRowCount(15); //larger than swing default categoryComboBox.addItemListener(this); ToolTipManager.sharedInstance().registerComponent(featureComboBox); comboBoxEditor = new RestrictedComboBoxEditor(splitChar, ',', ')', '*'); featureComboBox.setEditor(comboBoxEditor); featureComboBox.setRenderer(new MyListCellRenderer('(')); itemComboBox = featureComboBox; } else { itemComboBox = categoryComboBox; } add(itemComboBox, BorderLayout.CENTER); if (itemBoxEditable) { itemComboBox.setToolTipText("Enable editing with right mouse key"); } itemComboBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent ie) { if ((itemBoxEditable) && (ie.getStateChange() == ItemEvent.SELECTED) && (itemComboBox.getSelectedIndex() != -1)) { itemComboBox.setEditable(false); itemComboBox.setToolTipText( "Enable editing with right mouse key"); } } }); itemComboBox.getComponent(0).addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent me) { if ((itemBoxEditable) && (me.getModifiers() == InputEvent.BUTTON3_MASK)) { itemComboBox.setEditable(true); itemComboBox.setToolTipText( "Use DELETE key to change entry"); } } }); } /** * Sets the possibility of editing the Items in the ComboBox. However the ComboBox is turned * editable only after a right mouse click. * * @param b DOCUMENT ME! */ public void setEditable(boolean b) { itemBoxEditable = b; } /** * DOCUMENT ME! * * @param o DOCUMENT ME! */ public void setSelectedItem(Object o) { if (featureComboBox == null) { for (int i = 0; i < categoryComboBox.getItemCount(); i++) { if (categoryComboBox.getItemAt(i).toString().equals(o.toString())) { categoryComboBox.setSelectedIndex(i); break; } } } else { String s = o.toString(); int catIndex = s.indexOf(splitChar); if (catIndex == -1) { System.out.println("String " + s + " not found!"); return; } for (int i = 0; i < categoryComboBox.getItemCount(); i++) { if (categoryComboBox.getItemAt(i).toString().startsWith(s.substring( 0, catIndex))) { categoryComboBox.setSelectedIndex(i); updateFeatureComboBox(); if (s.indexOf('*') == -1) { for (int j = 0; j < featureComboBox.getItemCount(); j++) { if (featureComboBox.getItemAt(j).toString() .endsWith(s.substring( catIndex))) { featureComboBox.setSelectedIndex(j); return; } } } else { featureComboBox.setEditable(true); featureComboBox.setSelectedIndex(-1); //remove additional brackets inserted for reg-ex search int leftBracket = s.indexOf('['); int rightBracket = s.indexOf(']'); if ((leftBracket == -1) || (rightBracket == -1)) { comboBoxEditor.setItem(o); } else { comboBoxEditor.setItem(s.substring(0, leftBracket) + s.substring(rightBracket + 1)); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -