001: /*
002: * @(#)ListAutoCompleter.java 7/2/2005
003: *
004: * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
005: */
006: package com.jidesoft.hints;
007:
008: import javax.swing.text.JTextComponent;
009: import java.util.AbstractList;
010: import java.util.ArrayList;
011: import java.util.List;
012:
013: /**
014: * <code>ListDataIntelliHints</code> is a concrete implementation of {@link com.jidesoft.hints.IntelliHints}.
015: * It provides hints from a known list of data. It is similar to auto complete text
016: * field except the list will be filtered depending on what user types in so far.
017: */
018: public class ListDataIntelliHints extends AbstractListIntelliHints {
019:
020: private boolean _caseSensitive = false;
021: private List<?> _completionList;
022:
023: public ListDataIntelliHints(JTextComponent comp,
024: List<?> completionList) {
025: super (comp);
026: setCompletionList(completionList);
027: }
028:
029: public ListDataIntelliHints(JTextComponent comp,
030: String[] completionList) {
031: super (comp);
032: setCompletionList(completionList);
033: }
034:
035: /**
036: * Gets the list of hints.
037: *
038: * @return the list of hints.
039: */
040: public List<?> getCompletionList() {
041: return _completionList;
042: }
043:
044: /**
045: * Sets a new list of hints.
046: *
047: * @param completionList
048: */
049: public void setCompletionList(List<?> completionList) {
050: _completionList = completionList;
051: }
052:
053: /**
054: * Sets a new list of hints.
055: *
056: * @param completionList
057: */
058: public void setCompletionList(String[] completionList) {
059: final String[] list = completionList;
060: _completionList = new AbstractList() {
061: @Override
062: public Object get(int index) {
063: return list[index];
064: }
065:
066: @Override
067: public int size() {
068: return list.length;
069: }
070: };
071: }
072:
073: public boolean updateHints(Object context) {
074: if (context == null) {
075: return false;
076: }
077: String s = context.toString();
078: int substringLen = s.length();
079: if (substringLen == 0) {
080: return false;
081: }
082:
083: List<String> possibleStrings = new ArrayList<String>();
084: for (Object o : getCompletionList()) {
085: String listEntry = (String) o;
086: if (substringLen > listEntry.length())
087: continue;
088:
089: if (!isCaseSensitive()) {
090: if (s.equalsIgnoreCase(listEntry.substring(0,
091: substringLen)))
092: possibleStrings.add(listEntry);
093: } else if (listEntry.startsWith(s))
094: possibleStrings.add(listEntry);
095: }
096:
097: Object[] objects = possibleStrings.toArray();
098: setListData(objects);
099: return objects.length > 0;
100: }
101:
102: /**
103: * Checks if it used case sensitive search. By defaul it's false.
104: *
105: * @return if it's case sensitive.
106: */
107: public boolean isCaseSensitive() {
108: return _caseSensitive;
109: }
110:
111: /**
112: * Sets the case sensitive flag. By default, it's false meaning it's a case insensitive search.
113: *
114: * @param caseSensitive
115: */
116: public void setCaseSensitive(boolean caseSensitive) {
117: _caseSensitive = caseSensitive;
118: }
119:
120: }
|