001: /*
002: * Stingray Software Objective Blend
003: * Copyright (C) 1997 Stingray Software, Inc.
004: * All Rights Reserved
005: *
006: * This source code is only intended as a supplement to
007: * the Stingray Objective Blend product. See the Objective
008: * Blend html help documentation for detailed information regarding
009: * using OB classes.
010: *
011: * Author : Kerry Smith
012: * Description : SearchableListBox.java - list box
013: *
014: * CHANGELOG:
015: * 7/09/96 LFW Created
016: * 3/12/97 JDK1.1
017: *
018: */
019:
020: /**
021: * SearchableListBox provides the ability to dynamically search for a particular
022: * element of the listbox. By invoking some of its data members, you can perform
023: * partial key searches (that is, if you had a very long listbox with many data members
024: * that is pre-sorted, you can enter in the first few characters of the search string to
025: * be able to quickly jump to the location you wish to fine tune your search in). The
026: * SearchableListBox derives from the SortableListBox since it uses many of the routines
027: * of sorting and depends on a presorted list to be able to search for items.
028: * @see ob.listbox.SortableListBox
029: * @see ob.listbox.ListBox
030: */package ob.listbox;
031:
032: import java.awt.event.*;
033: import com.sun.portal.log.common.PortalLogger;
034:
035: public class SearchableListBox extends ob.listbox.SortableListBox {
036: String m_strCurrentSearchString = "";
037:
038: /**
039: * constructor
040: * @param bMultipleSelections whether multiple selections are allowed
041: */
042: public SearchableListBox(boolean bMultipleSelections) {
043: super (bMultipleSelections);
044: }
045:
046: /**
047: * default constructor
048: */
049: public SearchableListBox() {
050: this (false);
051: }
052:
053: /**
054: * sets the string to search on
055: * @param newSearch the text to search for as type String
056: */
057: public void setSearchString(String newSearch) {
058: m_strCurrentSearchString = newSearch;
059: }
060:
061: /**
062: * search for a particular piece of text. If list is not sorted, will automatically
063: * sort the list.
064: * @param strSearch string to search on
065: * @return the first ListItem (row) instance which matches the search criterea, as type int
066: */
067:
068: public int search(String strSearch) {
069: if (m_nColumnSorted != 0)
070: sort();
071:
072: if (strSearch == null || strSearch.length() == 0)
073: return 0;
074: int nSearchLength = strSearch.length();
075: int maximumLocation = m_arrItems.size() - 1;
076: int minimumLocation = 0;
077:
078: while (maximumLocation > minimumLocation) {
079: int middle = (maximumLocation + minimumLocation) / 2;
080: ListItem currentItem = (ListItem) m_arrItems
081: .elementAt(middle);
082: String currentString = (currentItem.getText())
083: .toUpperCase();
084:
085: if (currentString.startsWith(strSearch.toUpperCase())) {
086: int tempMiddle = middle;
087: while (currentString
088: .startsWith(strSearch.toUpperCase())
089: && tempMiddle >= 0) {
090: if (--tempMiddle != -1)
091: currentString = ((ListItem) m_arrItems
092: .elementAt(tempMiddle)).getText()
093: .toUpperCase();
094: }
095:
096: return tempMiddle + 1;
097: } else if (currentString.compareTo(strSearch.toUpperCase()) > 0)
098: maximumLocation = middle - 1;
099: else
100: minimumLocation = middle + 1;
101: }
102:
103: if ((((ListItem) m_arrItems.elementAt(minimumLocation))
104: .getText()).toUpperCase().startsWith(
105: strSearch.toUpperCase())) { //System.out.println("here");
106: return minimumLocation;
107: } else
108: return search(strSearch
109: .substring(0, strSearch.length() - 1)); //0;
110: }
111:
112: protected void changeItemText() {
113: super .changeItemText();
114: m_nColumnSorted = -1;
115: }
116:
117: protected void processKeyEvent(KeyEvent e) {
118:
119: int key = e.getKeyCode();
120:
121: String keyChar = e.getKeyText(key);
122: if (e.getID() == KeyEvent.KEY_PRESSED) {
123: if (!e.isActionKey() && key != KeyEvent.VK_BACK_SPACE
124: && keyChar.length() == 1) {
125: //System.out.println(keyChar);
126: m_strCurrentSearchString = m_strCurrentSearchString
127: .concat(keyChar);
128: int searchIndex = search(m_strCurrentSearchString);
129: //System.out.println(searchIndex + " search index");
130: // select(searchIndex);
131: prev = searchIndex;
132: selected = new int[0];
133: select(searchIndex, false);
134: scrollToView(searchIndex);
135: update();
136: processItemEvent(new ItemEvent(this ,
137: ItemEvent.ITEM_STATE_CHANGED,
138: getItem(searchIndex), ItemEvent.SELECTED));
139:
140: } else if (key == KeyEvent.VK_BACK_SPACE) {
141: int nIndex = m_strCurrentSearchString.length() - 1;
142: if (nIndex < 0)
143: nIndex = 0;
144:
145: m_strCurrentSearchString = m_strCurrentSearchString
146: .substring(0, nIndex);
147: int searchIndex = search(m_strCurrentSearchString);
148: prev = searchIndex;
149: selected = new int[0];
150: select(searchIndex);
151: scrollToView(searchIndex);
152: update();
153: processItemEvent(new ItemEvent(this,
154: ItemEvent.ITEM_STATE_CHANGED,
155: getItem(searchIndex), ItemEvent.SELECTED));
156: }
157:
158: }
159:
160: super.processKeyEvent(e);
161: }
162:
163: }
|