1 /***
2 Copyright (C) 2005 The Java Community
3
4 This program is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free Software
6 Foundation; either version 2 of the License, or (at your option) any later
7 version.
8
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along with
14 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 Place, Suite 330, Boston, MA 02111-1307 USA.
16 */
17 package org.bejug.javacareers.common.ajax;
18
19
20 /***
21 * Utility methods for the AJAX Text component (and possibly other components
22 * we'll develop next)
23 *
24 * @author Tor Norbye
25 */
26 public class Utilities {
27 /***
28 * Creates a new instance of Utilities
29 */
30 private Utilities() {
31 }
32
33 /***
34 * Perform a case insensitive binary search for the key in the data string.
35 * Not an exact binary search since we return the index of the closest match
36 * rather than returning an actual match.
37 *
38 * @param data an array of Strings containing the data.
39 * @param key a String containing the key to search.
40 * @param ignoreCase whether to ignore the case or not.
41 * @return an int representing the closest index.
42 */
43 private static int findClosestIndex(String[] data, String key, boolean ignoreCase) {
44
45 int low = 0;
46 int high = data.length - 1;
47 int middle = -1;
48
49 while (high > low) {
50 middle = (low + high) / 2;
51
52 int result;
53
54
55
56
57 if (ignoreCase) {
58 result = key.compareToIgnoreCase(data[middle]);
59 } else {
60 result = key.compareTo(data[middle]);
61 }
62
63 if (result == 0) {
64 return middle;
65 } else if (result < 0) {
66 high = middle;
67 } else if (low != middle) {
68 low = middle;
69 } else {
70 return high;
71 }
72 }
73
74 return middle;
75 }
76
77 /***
78 * Return (via the result object) a short completion list from the given data using
79 * the given prefix
80 *
81 * @param sortedData A sorted array of Strings we want to pick completion results
82 * from
83 * @param prefix A prefix of some of the strings in the sortedData that the
84 * user has typed so far
85 * @param result A result object that will be populated with completion results
86 * by this method
87 */
88 public static void addMatchingItems(String[] sortedData, String prefix, CompletionResult result) {
89 int index;
90
91 if (prefix.length() > 0) {
92
93
94 boolean caseInsensitive = true;
95 index = findClosestIndex(sortedData, prefix, caseInsensitive);
96 } else {
97
98
99 index = 0;
100 }
101
102 if (index == -1) {
103
104
105
106
107
108 index = 0;
109 }
110
111 for (int i = 0; i < PhaseListener.MAX_RESULTS_RETURNED; i++) {
112 if (index >= sortedData.length) {
113 break;
114 }
115
116 result.addItem(sortedData[index]);
117 index++;
118 }
119 }
120 }
121
122 /***
123 * $Log: Utilities.java,v $
124 * Revision 1.3 2005/08/10 12:07:42 bavo_jcs
125 * Optimized imports according to checkstyle
126 *
127 * Revision 1.2 2005/08/08 12:07:54 bme_jcs
128 * resolved checkstyle errors
129 *
130 * Revision 1.1 2005/07/13 11:42:08 bavo_jcs
131 * Ajax rename and test-cleanup
132 *
133 * Revision 1.1 2005/07/13 11:16:53 bavo_jcs
134 * Moved Ajax files
135 *
136 * Revision 1.1 2005/07/06 14:15:30 bavo_jcs
137 * Ajax integration
138 *
139 */