KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > user > client > ui > DeckPanel


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;
17
18 import com.google.gwt.user.client.DOM;
19 import com.google.gwt.user.client.Element;
20
21 /**
22  * A panel that displays all of its child widgets in a 'deck', where only one
23  * can be visible at a time. It is used by
24  * {@link com.google.gwt.user.client.ui.TabPanel}.
25  *
26  * <p>
27  * Once a widget has been added to a DeckPanel, its visibility, width, and
28  * height attributes will be manipulated. When the widget is removed from
29  * the DeckPanel, it will be visible, and its width and height attributes
30  * will be cleared.
31  * </p>
32  */

33 public class DeckPanel extends ComplexPanel {
34
35   private Widget visibleWidget;
36
37   /**
38    * Creates an empty deck panel.
39    */

40   public DeckPanel() {
41     setElement(DOM.createDiv());
42   }
43
44   /**
45    * Adds the specified widget to the deck.
46    *
47    * @param w the widget to be added
48    */

49   public void add(Widget w) {
50     insert(w, getWidgetCount());
51   }
52
53   /**
54    * Gets the index of the currently-visible widget.
55    *
56    * @return the visible widget's index
57    */

58   public int getVisibleWidget() {
59     return getWidgetIndex(visibleWidget);
60   }
61
62   /**
63    * Inserts a widget before the specified index.
64    *
65    * @param w the widget to be inserted
66    * @param beforeIndex the index before which it will be inserted
67    * @throws IndexOutOfBoundsException if <code>beforeIndex</code> is out of
68    * range
69    */

70   public void insert(Widget w, int beforeIndex) {
71     super.insert(w, getElement(), beforeIndex);
72
73     Element child = w.getElement();
74     DOM.setStyleAttribute(child, "width", "100%");
75     DOM.setStyleAttribute(child, "height", "100%");
76     w.setVisible(false);
77   }
78
79   public boolean remove(Widget w) {
80     if (!super.remove(w)) {
81       return false;
82     }
83
84     if (visibleWidget == w) {
85       visibleWidget = null;
86     }
87
88     return true;
89   }
90
91   /**
92    * Shows the widget at the specified index. This causes the currently- visible
93    * widget to be hidden.
94    *
95    * @param index the index of the widget to be shown
96    */

97   public void showWidget(int index) {
98     checkIndex(index);
99
100     if (visibleWidget != null) {
101       visibleWidget.setVisible(false);
102     }
103     visibleWidget = getWidget(index);
104     visibleWidget.setVisible(true);
105   }
106
107   /**
108    * Calls the superclass' <code>disown(Widget)</code> method, makes the widget
109    * visible, and clears the widget's width and height attributes. This is done
110    * so that any changes to the visibility, height, or width of the widget
111    * that were done by the panel are undone when the widget is disowned from
112    * the panel.
113    *
114    * @param w the widget to be disowned
115    */

116   protected void disown(Widget w) {
117     super.disown(w);
118     DOM.setStyleAttribute(w.getElement(), "width", "");
119     DOM.setStyleAttribute(w.getElement(), "height", "");
120     w.setVisible(true);
121   }
122
123   private void checkIndex(int index) {
124     if ((index < 0) || (index >= getWidgetCount())) {
125       throw new IndexOutOfBoundsException JavaDoc();
126     }
127   }
128 }
129
Popular Tags