1 16 package com.google.gwt.user.client.ui.impl; 17 18 import com.google.gwt.user.client.DOM; 19 import com.google.gwt.user.client.Element; 20 import com.google.gwt.user.client.ui.KeyboardListener; 21 import com.google.gwt.user.client.ui.UIObject; 22 import com.google.gwt.user.client.ui.SuggestOracle.Suggestion; 23 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 27 31 public class SuggestPickerImpl extends AbstractItemPickerImpl { 32 33 36 private static final String STYLENAME_DEFAULT = "gwt-SuggestBoxPopup"; 37 private final boolean asHTML; 38 private int startInvisible = Integer.MAX_VALUE; 39 40 46 public SuggestPickerImpl(boolean asHTML) { 47 this.asHTML = asHTML; 48 setStyleName(STYLENAME_DEFAULT); 49 } 50 51 public boolean delegateKeyDown(char keyCode) { 52 if (isAttached()) { 53 switch (keyCode) { 54 case KeyboardListener.KEY_DOWN: 55 shiftSelection(1); 56 return true; 57 case KeyboardListener.KEY_UP: 58 shiftSelection(-1); 59 return true; 60 case KeyboardListener.KEY_ENTER: 61 commitSelection(); 62 return true; 63 } 64 } 65 return false; 66 } 67 68 public int getItemCount() { 69 if (startInvisible == Integer.MAX_VALUE) { 70 return 0; 71 } else { 72 return startInvisible; 73 } 74 } 75 76 81 public final void setItems(Collection suggestions) { 82 setItems(suggestions.iterator()); 83 } 84 85 protected native Element getRow(Element elem, int row); 88 89 void shiftSelection(int shift) { 90 int newSelect = getSelectedIndex() + shift; 91 if (newSelect >= super.getItemCount() || newSelect < 0 92 || newSelect >= startInvisible) { 93 return; 94 } 95 setSelection(getItem(newSelect)); 96 } 97 98 104 private Item ensureItem(int itemIndex) { 105 for (int i = super.getItemCount(); i <= itemIndex; i++) { 106 Item item = new Item(i); 107 addItem(item, true); 108 } 109 return getItem(itemIndex); 110 } 111 112 115 private final void setItems(Iterator suggestions) { 116 int itemCount = 0; 117 118 while (suggestions.hasNext()) { 121 Item item = ensureItem(itemCount); 122 Suggestion suggestion = (Suggestion) suggestions.next(); 123 String display = suggestion.getDisplayString(); 124 if (asHTML) { 125 DOM.setInnerHTML(item.getElement(), display); 126 } else { 127 DOM.setInnerText(item.getElement(), display); 128 } 129 item.setValue(suggestion.getValue()); 130 ++itemCount; 131 } 132 133 if (itemCount == 0) { 134 throw new IllegalStateException ( 135 "Must set at least one item in a SuggestPicker"); 136 } 137 138 int min = Math.min(itemCount, super.getItemCount()); 140 for (int i = startInvisible; i < min; i++) { 141 setVisible(i, true); 142 } 143 144 startInvisible = itemCount; 146 for (int i = itemCount; i < super.getItemCount(); i++) { 147 setVisible(i, false); 148 } 149 } 150 151 157 private void setVisible(int itemIndex, boolean visible) { 158 UIObject.setVisible(getRow(body, itemIndex), visible); 159 } 160 } 161 | Popular Tags |