KickJava   Java API By Example, From Geeks To Geeks.

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


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
17 package com.google.gwt.user.client.ui.impl;
18
19 import com.google.gwt.user.client.DOM;
20 import com.google.gwt.user.client.Element;
21 import com.google.gwt.user.client.Event;
22 import com.google.gwt.user.client.ui.ChangeListener;
23 import com.google.gwt.user.client.ui.ChangeListenerCollection;
24 import com.google.gwt.user.client.ui.UIObject;
25 import com.google.gwt.user.client.ui.Widget;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29
30 /**
31  * Helpful base implementation of the {@link ItemPicker} interface.
32  */

33 abstract class AbstractItemPickerImpl extends Widget {
34   /**
35    * Selectable item.
36    */

37   class Item extends UIObject {
38     private int index;
39     private Object JavaDoc value;
40
41     /**
42      * Constructor for <code>Item</code>.
43      *
44      * @param index index associated with item
45      */

46     Item(int index) {
47       setElement(DOM.createTD());
48       this.index = index;
49       this.setStyleName(STYLENAME_PRIMARY_ITEM);
50       items.add(index, this);
51     }
52
53     public String JavaDoc toString() {
54       return "value: " + this.getValue() + " index: " + this.getIndex();
55     }
56
57     /**
58      * Gets the index of the item.
59      *
60      * @return the item's index
61      */

62     int getIndex() {
63       return index;
64     }
65
66     AbstractItemPickerImpl getOwner() {
67       return AbstractItemPickerImpl.this;
68     }
69
70     Object JavaDoc getValue() {
71       return value;
72     }
73
74     void setValue(Object JavaDoc value) {
75       this.value = value;
76     }
77   }
78
79   private static final String JavaDoc STYLENAME_DEPENDENT_SELECTED = "-selected";
80   private static final String JavaDoc STYLENAME_PRIMARY_ITEM = "item";
81
82   final Element body;
83   private Element currentTR;
84   private final ArrayList JavaDoc items;
85   private ChangeListenerCollection changeListeners = new ChangeListenerCollection();
86   private Item selectedItem;
87
88   public AbstractItemPickerImpl() {
89     items = new ArrayList JavaDoc();
90     Element table = DOM.createTable();
91     body = DOM.createTBody();
92     DOM.appendChild(table, body);
93     setElement(table);
94     sinkEvents(Event.ONCLICK | Event.ONMOUSEOVER | Event.ONMOUSEOUT);
95     DOM.setElementPropertyInt(table, "cellPadding", 0);
96     DOM.setElementPropertyInt(table, "cellSpacing", 0);
97   }
98
99   public final void addChangeListener(ChangeListener listener) {
100     if (changeListeners == null) {
101       changeListeners = new ChangeListenerCollection();
102     }
103     changeListeners.add(listener);
104   }
105
106   public void commitSelection() {
107     if (selectedItem == null) {
108       throw new IllegalStateException JavaDoc("No element is selected");
109     }
110     changeListeners.fireChange(this);
111   }
112
113   public abstract boolean delegateKeyDown(char keyCode);
114
115   public int getItemCount() {
116     return items.size();
117   }
118
119   public final int getSelectedIndex() {
120     Item item = getSelectedItem();
121     if (item == null) {
122       return -1;
123     }
124     return item.getIndex();
125   }
126
127   public final Object JavaDoc getSelectedValue() {
128     Item item = getSelectedItem();
129     if (item == null) {
130       return null;
131     } else {
132       return item.getValue();
133     }
134   }
135
136   public void onBrowserEvent(Event event) {
137     super.onBrowserEvent(event);
138     Item item = findItem(DOM.eventGetTarget(event));
139     switch (DOM.eventGetType(event)) {
140       case Event.ONCLICK: {
141         commitSelection();
142         break;
143       }
144       case Event.ONMOUSEOVER: {
145         if (item != null) {
146           setSelection(item);
147         }
148         break;
149       }
150       case Event.ONMOUSEOUT: {
151         if (item != null) {
152           setSelection(null);
153         }
154         break;
155       }
156     }
157   }
158
159   public final void removeChangeListener(ChangeListener listener) {
160     this.changeListeners.remove(listener);
161   }
162
163   public abstract void setItems(Collection items);
164
165   public final void setSelectedIndex(int index) {
166     Item item = getItem(index);
167     setSelection(item);
168   }
169
170   void addItem(Item item, boolean vertical) {
171     if (vertical) {
172       currentTR = DOM.createTR();
173       DOM.appendChild(body, currentTR);
174     }
175     DOM.appendChild(currentTR, item.getElement());
176   }
177
178   void clearItems() {
179     items.clear();
180   }
181
182   /**
183    * Gets the ith item.
184    *
185    * @param index index of item
186    * @return the ith item
187    */

188   Item getItem(int index) {
189     return (Item) items.get(index);
190   }
191
192   /**
193    * Gets the currently selected item.
194    *
195    * @return selected item
196    */

197   Item getSelectedItem() {
198     return selectedItem;
199   }
200
201   /**
202    * Sets the current selection.
203    *
204    * @param item item to set
205    */

206   void setSelection(Item item) {
207     if (selectedItem == item) {
208       return;
209     }
210
211     // Remove "selected" style from the item.
212
if (selectedItem != null) {
213       selectedItem.removeStyleName(selectedItem.getStyleName() + STYLENAME_DEPENDENT_SELECTED);
214     }
215
216     // Add the "selected" style to the item.
217
selectedItem = item;
218     if (selectedItem != null) {
219       selectedItem.addStyleName(selectedItem.getStyleName() + STYLENAME_DEPENDENT_SELECTED);
220     }
221   }
222
223   /**
224    * Shifts the current selection by the given amount, unless that would make
225    * the selection invalid.
226    *
227    * @param shift the amount to shift the current selection by
228    */

229   void shiftSelection(int shift) {
230     int newIndex = getSelectedIndex() + shift;
231     if (newIndex < 0 || newIndex >= getItemCount()) {
232       return;
233     } else {
234       Item item = getItem(newIndex);
235       setSelection(item);
236     }
237   }
238
239   private Item findItem(Element hItem) {
240     for (int i = 0; i < items.size(); ++i) {
241       Item item = (Item) items.get(i);
242       if (DOM.isOrHasChild(item.getElement(), hItem)) {
243         return item;
244       }
245     }
246
247     return null;
248   }
249 }
250
Popular Tags