001: /*
002: * Copyright 2006 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013:
014: package org.wingx;
015:
016: import java.util.List;
017: import java.util.Map;
018:
019: import org.wings.SDimension;
020: import org.wings.SResourceIcon;
021: import org.wings.STextField;
022: import org.wingx.plaf.SuggestCG;
023:
024: /**
025: * Enhanced STextField that supports the user input by displaying suggestions.
026: * @author Christian Schyma
027: */
028: public class XSuggest extends STextField implements XSuggestDataSource {
029:
030: /**
031: * data source from which suggestions are generated
032: */
033: private XSuggestDataSource dataSource = null;
034:
035: /**
036: * Ajax request timeout in ms
037: */
038: private int timeout = 10000;
039:
040: /**
041: * @see org.wingx.XSuggest#setInputDelay(int delay)
042: */
043: private int inputDelay = 500;
044:
045: /**
046: * an icon to indicate the Ajax activity
047: */
048: private SResourceIcon activityIcon = new SResourceIcon(
049: "org/wings/icons/AjaxActivityIndicatorSmall.gif");
050:
051: /**
052: * @see org.wingx.XSuggest#setSuggestBoxWidth(SDimension dim)
053: */
054: private SDimension suggestBoxWidth = new SDimension(
055: SDimension.AUTO, SDimension.AUTO);
056:
057: {
058: activityIcon.getId(); // hack to externalize
059: }
060:
061: /**
062: * Creates a text field that can display suggestions.
063: */
064: public XSuggest() {
065: }
066:
067: /**
068: * Creates a text field that can display suggestions.
069: * @param text initial content of the text field
070: */
071: public XSuggest(String text) {
072: super (text);
073: }
074:
075: /**
076: * set a new data source from which suggestions are generated
077: * @param source
078: */
079: public void setDataSource(XSuggestDataSource source) {
080: this .dataSource = source;
081: }
082:
083: /**
084: * returns the currently used data source
085: * @see #setDataSource(XSuggestDataSource source)
086: */
087: public XSuggestDataSource getDataSource() {
088: return this .dataSource;
089: }
090:
091: /**
092: * Set Ajax request timeout.
093: * @param timeout in ms
094: */
095: public void setTimeout(int timeout) {
096: int oldTimeout = this .timeout;
097: this .timeout = timeout;
098: reloadIfChange(oldTimeout, timeout);
099: }
100:
101: /**
102: * @see org.wingx.XSuggest#setTimeout(int timeout)
103: */
104: public int getTimeout() {
105: return timeout;
106: }
107:
108: /**
109: * Fast typists can produce a lot of requests in a short time; this delay
110: * limits the requests to a certain time.
111: * @param delay in ms
112: */
113: public void setInputDelay(int delay) {
114: int oldDelay = this .inputDelay;
115: this .inputDelay = delay;
116: reloadIfChange(oldDelay, delay);
117: }
118:
119: /**
120: * @see org.wingx.XSuggest#setInputDelay(int delay)
121: */
122: public int getInputDelay() {
123: return inputDelay;
124: }
125:
126: /**
127: * Configures the width of the suggest box. There are three possibilites:<br>
128: * - <code>SDimension.AUTO</code> automatically set the size in accordance to the content (default)<br>
129: * - <code>SDimension.INHERIT</code> inherits width from the suggest input box<br>
130: * - integer value
131: * @param dim width of suggest box, height is ignored
132: */
133: public void setSuggestBoxWidth(SDimension dim) { // setSuggestBoxWidth inherit, auto, pixel width
134: SDimension oldSuggestBoxWidth = this .suggestBoxWidth;
135: this .suggestBoxWidth = dim;
136: reloadIfChange(oldSuggestBoxWidth, dim);
137: }
138:
139: public SDimension getSuggestBoxWidth() {
140: return this .suggestBoxWidth;
141: }
142:
143: /**
144: * Obtaines the list of suggestions from the data source, if there is one.
145: * @see {org.wingx.XSuggestDataSource.generateSugestions()}
146: * @param lookupText
147: * @return suggestions list with map entries or null, if there is no data source
148: */
149: public List<Map.Entry<String, String>> generateSuggestions(
150: String lookupText) {
151: if (getDataSource() != null) {
152: return getDataSource().generateSuggestions(lookupText);
153: } else {
154: return null;
155: }
156: }
157:
158: @Override
159: public void processLowLevelEvent(String action, String[] values) {
160: String value = values[0];
161: if (value.startsWith("q:")) {
162: String query = value.substring(2);
163: List<Map.Entry<String, String>> suggestions = generateSuggestions(query);
164: update(((SuggestCG) getCG()).getSuggestionsUpdate(this,
165: query, suggestions));
166: } else
167: super.processLowLevelEvent(action, values);
168: }
169: }
|