?? pcombobox.java
字號:
/*
* PSwing Utilities -- Nifty Swing Widgets
* Copyright (C) 2002 Pallas Technology
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Pallas Technology
* 1170 HOWELL MILL RD NW
* SUITE 306
* ATLANTA GEORGIA 30318
*
* PHONE 404.983.0623
* EMAIL info@pallastechnology.com
*
* www.pallastechnology.com
**************************************************************************
* $Archive: SwingTools$
* $FileName: PComboBox.java$
* $FileID: 3$
*
* Last change:
* $AuthorName: Rob MacGrogan$
* $Date: 8/1/03 1:22 PM$
* $VerID: 86$
* $Comment: Debug array index error when PComboBox contains no items.$
**************************************************************************/
package com.pallas.swing.pcombobox;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
/**
* Title: $FileName: PComboBox.java$
* @version $VerNum: 11$
* @author $AuthorName: Rob MacGrogan$<br><br>
*
* $Description: Nifty Swing combobox that functions like the
* standard non-web browser combo box people are used to
* seeing.$<br>
* $KeyWordsOff: $<br><br>
*
* Nifty Swing combobox that functions like the standard non-web browser combo box
* people are used to seeing. User types the viewable string of the item they want
* to select and the closest match will be selected, instead of the default Java
* functionality which only looks at the first letter of each string in the list.
*/
public class PComboBox extends JComboBox implements PCombo {
private Vector itemIndex = new Vector();
private Hashtable listLocationIndex = new Hashtable();
private FullWordComboKeySelectionModel mgr = null;
private boolean allowNewEntries = false;
/**
* Constructor for PComboBox.
* @param aModel
*/
public PComboBox(ComboBoxModel aModel) {
super(aModel);
//initializeComponent();
}
/**
* Constructor for PComboBox.
* @param items
*/
public PComboBox(Object[] items) {
super(items);
//initializeComponent();
}
/**
* Constructor for PComboBox.
* @param items
*/
public PComboBox(Vector items) {
super(items);
//initializeComponent();
}
/**
* Constructor for PComboBox.
*/
public PComboBox() {
super();
//initializeComponent();
}
public Enumeration getUpperCaseContents(){
return listLocationIndex.keys();
}
/**
* Must be called after all items have been added to combo box, or after a new
* item is added. This indexes all items String values for fast
* searching and matching.<br><br>
*
* PComboBox WILL NOT WORK IF YOU DO NOT CALL THIS METHOD.
*/
public synchronized void buildIndex(){
ComboBoxModel model = getModel();
itemIndex = new Vector();
listLocationIndex = new Hashtable();
for (int i = 0; i < model.getSize(); i++){
Object o = model.getElementAt(i);
itemIndex.add(o);
listLocationIndex.put(o.toString().toUpperCase(), new Integer(i));
}
Collections.sort(itemIndex, new ToStringComparator());
}
public boolean containsDisplayString(String s){
int i = search(s);
boolean contains = false;
if (i >= 0){
contains = true;
}
return contains;
}
/**
* Selects the first item in the list where itemInList.equals(item).
* Returns the itemInList.
*/
public Object setSelectedItemByValue(Object item){
//Brute force search for now.
Iterator itr = itemIndex.iterator();
Object foundItem = null;
while(itr.hasNext() && foundItem == null){
Object o = itr.next();
if (o.equals(item)){
foundItem = o;
}
}
setSelectedItem(foundItem);
return foundItem;
}
/**
* Returns the index of the first item in the list that matches
* the passed in string. A match is made if the first <code>x</code>
* letters of the item match the search string, where <code>x</code>
* is the length of the search string.
*/
public int search(String search){
int listLocation = -1;
if (itemIndex.size() > 0){
String s = search.toUpperCase();
//System.out.println("[C] search for: " + s);
int iBeginSearchRange = 0;
int iEndSearchRange = itemIndex.size() - 1;
//First, let's check index 0.
Object oZ = itemIndex.get(0);
int compZ = compareStrings(s, oZ.toString());
if (compZ == 0){
//Index 0 matches.
listLocation = 0;
}
else{
while (iEndSearchRange != iBeginSearchRange){
int iRange = (iEndSearchRange - iBeginSearchRange) + 1;
int iCheckPoint = iBeginSearchRange + (iRange / 2);
//System.out.println("[C] begin: " + iBeginSearchRange);
//System.out.println("[C] end: " + iEndSearchRange);
//System.out.println("[C] check: " + iCheckPoint);
Object o = itemIndex.get(iCheckPoint);
//System.out.println("[C] checking: " + o);
int comp = compareStrings(s, o.toString());
//System.out.println("[C] comp: " + comp);
if (comp == 0){
//We've got a likely match.
listLocation = iCheckPoint;
//This will bump us out of the loop.
iEndSearchRange = iBeginSearchRange;
}
else if (comp == 1){
//search value must be after checkpoint.
iBeginSearchRange = iCheckPoint;
}
else{
//search value is before checkpoint.
iEndSearchRange = iCheckPoint - 1;
}
}//end while
}//end else index 0 does not match.
if (listLocation != -1){
listLocation = getBestMatchIndex(s, itemIndex.get(listLocation).toString(), listLocation);
}
}
return listLocation;
}
/**
* Double checks list for an exact match of search.
*/
private int getBestMatchIndex(String search, String item, int listLocation){
int bestMatchIndex = -1;
String searchItem = item.toUpperCase();
if (search.length() < searchItem.length() &&
listLocationIndex.containsKey(search)){
//For now, just look for exact match of search.
Integer intLoc = (Integer)listLocationIndex.get(search);
bestMatchIndex = intLoc.intValue();
}
else{
//listLocation is it.
Integer intLoc = (Integer)listLocationIndex.get(itemIndex.get(listLocation).toString().toUpperCase());
bestMatchIndex = intLoc.intValue();
}
return bestMatchIndex;
}
/**
* Returns -1, 0, or 1
*/
private int compareStrings(String search, String item){
//System.out.println("[C] item: " + item);
int comp = 0;
String sBeginItem = item;
if (item.length() >= search.length()){
sBeginItem = item.substring(0, search.length());
}
if (sBeginItem.equalsIgnoreCase(search)){
comp = 0;
}
else if (ToStringComparator.firstStringSortsBeforeSecond(search, sBeginItem, false)){
comp = -1;
}
else{
comp = 1;
}
return comp;
}
/**
* Initializes the PComboBox.
*/
protected void initializeComponent(){
super.setEditable(true);
//getEditor();
mgr = new FullWordComboKeySelectionModel(this);
setKeySelectionManager(mgr);
getEditor().getEditorComponent().addFocusListener(new PComboFocusListener(mgr));
}
public void updateUI(){
super.updateUI();
//Update key selection.
initializeComponent();
}
/**
* This method does nothing. PComboBox always uses FullWordComboKeySelectionModel.
*/
public void setKeySelectionManager(JComboBox.KeySelectionManager manager){
}
/**
* This method does nothing. PComboBox is always editable.
*/
public void setEditable(boolean editable){
//Do nothing.
}
/**
* Returns the editor component of the PComboBox.
*/
public JTextField getTextField(){
return (JTextField)(getEditor().getEditorComponent());
}
/**
* Selects the item with the specified display string. If the
* display string does not occur in the combo box, nothing happens.
*/
public void setSelectionByDisplay(String display){
display = display.toUpperCase();
Integer index = (Integer)listLocationIndex.get(display);
if (index != null){
int iIndex = index.intValue();
setSelectedIndex(iIndex);
}
}
/**
* Returns the allowNewEntries.
* @return boolean
*/
public boolean isAllowNewEntries() {
return allowNewEntries;
}
/**
* Sets the allowNewEntries.
* @param allowNewEntries The allowNewEntries to set
*/
public void setAllowNewEntries(boolean allowNewEntries) {
this.allowNewEntries = allowNewEntries;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -