KickJava   Java API By Example, From Geeks To Geeks.

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


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.Window;
19 import com.google.gwt.user.client.ui.ChangeListener;
20 import com.google.gwt.user.client.ui.HasFocus;
21 import com.google.gwt.user.client.ui.PopupPanel;
22 import com.google.gwt.user.client.ui.UIObject;
23 import com.google.gwt.user.client.ui.Widget;
24
25 /**
26  * Suggestion picker drop-down, used in the implementation of
27  * {@link com.google.gwt.user.client.ui.SuggestBox}.
28  */

29 public class ItemPickerDropDownImpl extends PopupPanel {
30   private final AbstractItemPickerImpl picker;
31   private final HasFocus owner;
32
33   public ItemPickerDropDownImpl(final HasFocus owner, AbstractItemPickerImpl picker) {
34     super(true);
35     setWidget(picker);
36     this.picker = picker;
37     this.owner = owner;
38
39     picker.addChangeListener(new ChangeListener() {
40       public void onChange(Widget sender) {
41         hide();
42       }
43     });
44   }
45
46   /**
47    * Shows the popup, by default <code>show</code> selects the first item and
48    * displays itself under it's owner.
49    */

50   public void show() {
51     showBelow((UIObject) owner);
52   }
53
54   /**
55    * Shows the popup below the given UI object. By default, first item is
56    * selected in the item picker.
57    * <p>
58    * Note, if the popup would not be visible on the browser, than the popup's
59    * position may be adjusted.
60    * </p>
61    *
62    * @param showBelow the <code>UIObject</code> beneath which the popup should
63    * be shown
64    */

65   public void showBelow(UIObject showBelow) {
66     // A drop down with 0 items should never show itself.
67
if (picker.getItemCount() == 0) {
68       hide();
69       return;
70     }
71
72     // Initialize the picker to the first element.
73
picker.setSelectedIndex(0);
74
75     // Show must be called first, as otherwise getOffsetWidth is not correct. As
76
// the adjustment is very fast, the user experience is not effected by this
77
// call.
78
super.show();
79
80     // Calculate left.
81
int left = showBelow.getAbsoluteLeft();
82     int windowRight = Window.getClientWidth() + Window.getScrollLeft();
83     int overshootLeft = Math.max(0, (left + getOffsetWidth()) - windowRight);
84     left = left - overshootLeft;
85
86     // Calculate top.
87
int top = showBelow.getAbsoluteTop() + showBelow.getOffsetHeight();
88     int windowBottom = Window.getScrollTop() + Window.getClientHeight();
89     int overshootTop = Math.max(0, (top + getOffsetHeight()) - windowBottom);
90     top = top - overshootTop;
91
92     // Set the popup position.
93
setPopupPosition(left, top);
94     super.show();
95   }
96 }
97
Popular Tags