KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > user > client > ui > impl > SuggestPickerImpl


1 /*
2  * Copyright 2007 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.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 JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 /**
28  * Suggestion picker, used in the implementation of
29  * {@link com.google.gwt.user.client.ui.SuggestBox}.
30  */

31 public class SuggestPickerImpl extends AbstractItemPickerImpl {
32
33   /**
34    * Default style for the picker.
35    */

36   private static final String JavaDoc STYLENAME_DEFAULT = "gwt-SuggestBoxPopup";
37   private final boolean asHTML;
38   private int startInvisible = Integer.MAX_VALUE;
39
40   /**
41    * Constructor for <code>SuggestPickerImpl</code>.
42    *
43    * @param asHTML flag used to indicate how to treat {@link Suggestion} display
44    * strings
45    */

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   /**
77    * Sets the suggestions associated with this picker.
78    *
79    * @param suggestions suggestions for this picker
80    */

81   public final void setItems(Collection JavaDoc suggestions) {
82     setItems(suggestions.iterator());
83   }
84
85   protected native Element getRow(Element elem, int row)/*-{
86    return elem.rows[row];
87    }-*/
;
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   /**
99    * Ensures the existence of the given item and returns it.
100    *
101    * @param itemIndex item index to ensure
102    * @return associated item
103    */

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   /**
113    * Sets the suggestions associated with this picker.
114    */

115   private final void setItems(Iterator JavaDoc suggestions) {
116     int itemCount = 0;
117
118     // Ensure all needed items exist and set each item's html to the given
119
// suggestion.
120
while (suggestions.hasNext()) {
121       Item item = ensureItem(itemCount);
122       Suggestion suggestion = (Suggestion) suggestions.next();
123       String JavaDoc 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 JavaDoc(
135         "Must set at least one item in a SuggestPicker");
136     }
137
138     // Render visible all needed cells.
139
int min = Math.min(itemCount, super.getItemCount());
140     for (int i = startInvisible; i < min; i++) {
141       setVisible(i, true);
142     }
143
144     // Render invisible all useless cells.
145
startInvisible = itemCount;
146     for (int i = itemCount; i < super.getItemCount(); i++) {
147       setVisible(i, false);
148     }
149   }
150
151   /**
152    * Sets whether the given item is visible.
153    *
154    * @param itemIndex item index
155    * @param visible visible boolean
156    */

157   private void setVisible(int itemIndex, boolean visible) {
158     UIObject.setVisible(getRow(body, itemIndex), visible);
159   }
160 }
161
Popular Tags