2 * Copyright 2011, The gwtquery team.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
16 package com.google.gwt.query.client.plugins.widgets;
18 import static com.google.gwt.query.client.GQuery.$;
20 import com.google.gwt.dom.client.Element;
21 import com.google.gwt.query.client.GQuery;
22 import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
23 import com.google.gwt.user.client.ui.SuggestBox;
24 import com.google.gwt.user.client.ui.SuggestOracle;
27 * Factory used to create a {@link SuggestBox} widget.
30 public class SuggestBoxWidgetFactory implements WidgetFactory<SuggestBox> {
33 * Options used to create a {@link SuggestBox}
36 public static class SuggestBoxOptions {
38 private String suggestionsSelector;
39 private SuggestOracle suggestOracle;
41 public SuggestBoxOptions() {
45 public SuggestBoxOptions(String suggestionsSelector) {
46 this.suggestionsSelector = suggestionsSelector;
49 public SuggestBoxOptions(SuggestOracle suggestOracle) {
50 this.suggestOracle = suggestOracle;
53 public SuggestOracle getSuggestOracle() {
57 public String getSuggestionsSelector() {
58 return suggestionsSelector;
62 * Set the css selector to use for selecting elements playing roles of the
63 * list items. The value of the items will be the inner texts of the
66 * If this options is null, the inner text of the direct children of the
67 * element will be used as suggestions list.
69 * This options is used if the element is not a <i>select</i> element.
71 * @param optionsSelector
73 public void setSuggestionsSelector(String suggestionsSelector) {
74 this.suggestionsSelector = suggestionsSelector;
77 public void setSuggestOracle(SuggestOracle suggestOracle) {
78 this.suggestOracle = suggestOracle;
81 private void initDefault() {
82 suggestionsSelector = null;
87 private SuggestBoxOptions options;
89 public SuggestBoxWidgetFactory(SuggestBoxOptions options) {
90 this.options = options;
93 public SuggestBox create(Element e) {
95 SuggestOracle suggestOracle = createOracle(e);
97 if ($(e).filter("input[type='text']").length() > 0) {
98 return SuggestBox.wrap(suggestOracle, e);
101 SuggestBox sbox = new SuggestBox(suggestOracle);
102 WidgetsUtils.replaceOrAppend(e, sbox);
107 private SuggestOracle createOracle(Element e) {
108 if (options.getSuggestOracle() != null) {
109 return options.getSuggestOracle();
112 MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
114 if (options.getSuggestionsSelector() != null) {
115 GQuery suggestions = $(options.getSuggestionsSelector(), e);
116 for (Element suggestion : suggestions.elements()) {
117 oracle.add(suggestion.getInnerText());