1 /***
2 * DefaultCompletionModel.java
3 *
4 * This file is part of the creme library.
5 * The creme library intends to ease the development effort of large
6 * applications by providing easy to use support classes.
7 *
8 * Copyright (C) 2002 Denis Bregeon
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 * contact information: dbregeon@sourceforge.net
25 */
26 package org.jcreme.swing;
27
28 import java.beans.PropertyChangeListener;
29 import java.beans.PropertyChangeSupport;
30 import java.text.Format;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.Iterator;
34 import java.util.Vector;
35
36 import org.jcreme.filters.LikeStringFilter;
37 import org.jcreme.filters.StringComparisonOperator;
38
39 /***
40 * This is a default implementation of the CompletionModel interface. When no
41 * completionListElementFormat is provided it uses the toString method to
42 * convert objects into Strings for completion. The default pattern has an empty
43 * prefix ("") and an open suffix ("%").
44 *
45 * @author $Author: dbregeon $
46 * @version $Revision: 1.1 $
47 */
48 public class DefaultCompletionModel implements CompletionModel {
49 /***
50 * The prefix to apply for the completion.
51 */
52 private String completionPrefix = "";
53
54 /***
55 * The suffix to apply for the completion.
56 */
57 private String completionSuffix = "%";
58
59 /***
60 * The possible completion values.
61 */
62 private Collection completionList = null;
63
64 /***
65 * The Format instance to transform objects into Strings.
66 */
67 private Format completionListElementFormat = null;
68
69 /***
70 * Provides the bean - events mechanisms.
71 */
72 private final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
73 this);
74
75 /***
76 * This LikeStringFilter is used to perform completion searches. It is
77 * modified every time the prefix or suffix are changed.
78 */
79 private volatile LikeStringFilter completionMatchFilter = null;
80
81 /***
82 * Creates a new DefaultCompletionModel.
83 *
84 * @param c
85 * the completionList to use.
86 */
87 public DefaultCompletionModel(Collection c) {
88 this.completionList = c;
89 }
90
91 /***
92 * Creates a new DefaultCompletionModel.
93 *
94 * @param array
95 * the completionList to use.
96 */
97 public DefaultCompletionModel(Object[] array) {
98 if (array != null) {
99 this.completionList = Arrays.asList(array);
100 }
101 }
102
103 /***
104 * @see CompletionModel#getCompletionPrefix()
105 */
106 public String getCompletionPrefix() {
107 return this.completionPrefix;
108 }
109
110 /***
111 * @see CompletionModel#getCompletions(java.lang.String)
112 */
113 public Collection getCompletions(String seed) {
114 Vector result = new Vector();
115 if ((this.completionList != null) && (seed != null)) {
116 try {
117 this.completionMatchFilter = new LikeStringFilter(
118 StringComparisonOperator.LIKE, this.completionPrefix
119 + seed + this.completionSuffix);
120 Iterator iter = this.completionList.iterator();
121 Object current = null;
122 String currentString = null;
123 while (iter.hasNext()) {
124 current = iter.next();
125 if (current != null) {
126 if (this.completionListElementFormat != null) {
127 currentString = this.completionListElementFormat
128 .format(current);
129 } else {
130 currentString = current.toString();
131 }
132 if (this.completionMatchFilter.accept(currentString)) {
133 result.add(current);
134 }
135 }
136 }
137
138 } catch (IllegalArgumentException e) {
139 e.printStackTrace();
140 }
141 }
142 return result;
143 }
144
145 /***
146 * @see CompletionModel#getCompletionSuffix()
147 */
148 public String getCompletionSuffix() {
149 return this.completionSuffix;
150 }
151
152 /***
153 * @see CompletionModel#setCompletionListElementFormat(java.text.Format)
154 */
155 public void setCompletionListElementFormat(Format f) {
156 Format oldValue = this.completionListElementFormat;
157 this.completionListElementFormat = f;
158 this.propertyChangeSupport.firePropertyChange(
159 COMPLETION_LIST_ELEMENT_FORMAT_PROPERTY, oldValue,
160 this.completionListElementFormat);
161 }
162
163 /***
164 * @see CompletionModel#setCompletionList(java.util.Collection)
165 */
166 public void setCompletionList(Collection c) {
167 this.completionList = c;
168 }
169
170 /***
171 * @see CompletionModel#setCompletionPrefix(java.lang.String)
172 */
173 public void setCompletionPrefix(String prefix) {
174 this.completionPrefix = prefix;
175 }
176
177 /***
178 * @see CompletionModel#setCompletionSuffix(java.lang.String)
179 */
180 public void setCompletionSuffix(String suffix) {
181 this.completionSuffix = suffix;
182 }
183
184 /***
185 * @see CompletionModel#addPropertyChangeListener(java.beans.PropertyChangeListener)
186 */
187 public void addPropertyChangeListener(PropertyChangeListener l) {
188 this.propertyChangeSupport.addPropertyChangeListener(l);
189 }
190
191 /***
192 * @see CompletionModel#getCompletionListElementFormat()
193 */
194 public Format getCompletionListElementFormat() {
195 return this.completionListElementFormat;
196 }
197
198 /***
199 * @see CompletionModel#removePropertyChangeListener(java.beans.PropertyChangeListener)
200 */
201 public void removePropertyChangeListener(PropertyChangeListener l) {
202 this.propertyChangeSupport.removePropertyChangeListener(l);
203 }
204
205 }