1 /* 2 * Copyright 2008 Google Inc. 3 * 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 6 * the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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 14 * the License. 15 */ 16 package com.google.gwt.sample.showcase.client.content.lists; 17 18 import com.google.gwt.core.client.GWT; 19 import com.google.gwt.core.client.RunAsyncCallback; 20 import com.google.gwt.i18n.client.Constants; 21 import com.google.gwt.sample.showcase.client.ContentWidget; 22 import com.google.gwt.sample.showcase.client.ShowcaseAnnotations.ShowcaseData; 23 import com.google.gwt.sample.showcase.client.ShowcaseAnnotations.ShowcaseSource; 24 import com.google.gwt.sample.showcase.client.ShowcaseAnnotations.ShowcaseStyle; 25 import com.google.gwt.user.client.rpc.AsyncCallback; 26 import com.google.gwt.user.client.ui.HTML; 27 import com.google.gwt.user.client.ui.MultiWordSuggestOracle; 28 import com.google.gwt.user.client.ui.SuggestBox; 29 import com.google.gwt.user.client.ui.VerticalPanel; 30 import com.google.gwt.user.client.ui.Widget; 31 32 /** 33 * Example file. 34 */ 35 @ShowcaseStyle({ 36 ".gwt-SuggestBox", ".gwt-SuggestBoxPopup", "html>body .gwt-SuggestBoxPopup", 37 "* html .gwt-SuggestBoxPopup"}) 38 public class CwSuggestBox extends ContentWidget { 39 40 /** 41 * The constants used in this Content Widget. 42 */ 43 @ShowcaseSource 44 public static interface CwConstants extends Constants { cwSuggestBoxDescription()45 String cwSuggestBoxDescription(); 46 cwSuggestBoxLabel()47 String cwSuggestBoxLabel(); 48 cwSuggestBoxName()49 String cwSuggestBoxName(); 50 cwSuggestBoxWords()51 String[] cwSuggestBoxWords(); 52 } 53 54 /** 55 * An instance of the constants. 56 */ 57 @ShowcaseData 58 private final CwConstants constants; 59 60 /** 61 * Constructor. 62 * 63 * @param constants the constants 64 */ CwSuggestBox(CwConstants constants)65 public CwSuggestBox(CwConstants constants) { 66 super(constants.cwSuggestBoxName(), constants.cwSuggestBoxDescription(), 67 true); 68 this.constants = constants; 69 } 70 71 /** 72 * Initialize this example. 73 */ 74 @ShowcaseSource 75 @Override onInitialize()76 public Widget onInitialize() { 77 // Define the oracle that finds suggestions 78 MultiWordSuggestOracle oracle = new MultiWordSuggestOracle(); 79 String[] words = constants.cwSuggestBoxWords(); 80 for (int i = 0; i < words.length; ++i) { 81 oracle.add(words[i]); 82 } 83 84 // Create the suggest box 85 final SuggestBox suggestBox = new SuggestBox(oracle); 86 suggestBox.ensureDebugId("cwSuggestBox"); 87 VerticalPanel suggestPanel = new VerticalPanel(); 88 suggestPanel.add(new HTML(constants.cwSuggestBoxLabel())); 89 suggestPanel.add(suggestBox); 90 91 // Return the panel 92 return suggestPanel; 93 } 94 95 @Override asyncOnInitialize(final AsyncCallback<Widget> callback)96 protected void asyncOnInitialize(final AsyncCallback<Widget> callback) { 97 GWT.runAsync(CwSuggestBox.class, new RunAsyncCallback() { 98 99 public void onFailure(Throwable caught) { 100 callback.onFailure(caught); 101 } 102 103 public void onSuccess() { 104 callback.onSuccess(onInitialize()); 105 } 106 }); 107 } 108 } 109