Debian Codegraph @

src/com/jidesoft/hints/ListDataIntelliHints.java

raw
1 /*
2 * @(#)ListAutoCompleter.java 7/2/2005
3 *
4 * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
5 */
6 package com.jidesoft.hints;
7
8 import javax.swing.text.JTextComponent;
9 import java.util.AbstractList;
10 import java.util.ArrayList;
11 import java.util.List;
12
13 /**
14 * <code>ListDataIntelliHints</code> is a concrete implementation of {@link com.jidesoft.hints.IntelliHints}. It
15 * provides hints from a known list of data. It is similar to auto complete text field except the list will be filtered
16 * depending on what user types in so far.
17 */
18 public class ListDataIntelliHints<T> extends AbstractListIntelliHints {
19
20 private boolean _caseSensitive = false;
21 private List<T> _completionList;
22
23 public ListDataIntelliHints(JTextComponent comp, List<T> completionList) {
24 super(comp);
25 setCompletionList(completionList);
26 }
27
28 public ListDataIntelliHints(JTextComponent comp, T[] completionList) {
29 super(comp);
30 setCompletionList(completionList);
31 }
32
33 /**
34 * Gets the list of hints.
35 *
36 * @return the list of hints.
37 */
38 public List<T> getCompletionList() {
39 return _completionList;
40 }
41
42 /**
43 * Sets a new list of hints.
44 *
45 * @param completionList a new list of hints.
46 */
47 public void setCompletionList(List<T> completionList) {
48 _completionList = completionList;
49 }
50
51 /**
52 * Sets a new list of hints.
53 *
54 * @param completionList a new array of hints.
55 */
56 public void setCompletionList(T[] completionList) {
57 final T[] list = completionList;
58 _completionList = new AbstractList<T>() {
59 @Override
60 public T get(int index) {
61 return list[index];
62 }
63
64 @Override
65 public int size() {
66 return list.length;
67 }
68 };
69 }
70
71 public boolean updateHints(Object context) {
72 if (context == null) {
73 return false;
74 }
75 List<T> possibleHints = new ArrayList<T>();
76 for (T o : getCompletionList()) {
77 if (compare(context, o)) possibleHints.add(o);
78 }
79
80 Object[] objects = possibleHints.toArray();
81 setListData(objects);
82 return objects.length > 0;
83 }
84
85 /**
86 * Compares the context with the object in the completion list.
87 *
88 * @param context the context returned from {@link #getContext()} method.
89 * @param o the object in the completion list.
90 * @return true if the context matches with the object. Otherwise false.
91 */
92 protected boolean compare(Object context, T o) {
93 boolean match = false;
94 String listEntry = o == null ? "" : o.toString();
95 String s = context.toString();
96 int substringLen = s.length();
97 if (substringLen <= listEntry.length()) {
98 if (!isCaseSensitive()) {
99 if (s.equalsIgnoreCase(listEntry.substring(0, substringLen)))
100 match = true;
101 }
102 else if (listEntry.startsWith(s))
103 match = true;
104 }
105 return match;
106 }
107
108 /**
109 * Checks if it used case sensitive search. By default it's false.
110 *
111 * @return if it's case sensitive.
112 */
113 public boolean isCaseSensitive() {
114 return _caseSensitive;
115 }
116
117 /**
118 * Sets the case sensitive flag. By default, it's false meaning it's a case insensitive search.
119 *
120 * @param caseSensitive true or false.
121 */
122 public void setCaseSensitive(boolean caseSensitive) {
123 _caseSensitive = caseSensitive;
124 }
125
126 }