001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.user.client.ui;
017:
018: import com.google.gwt.user.client.DOM;
019: import com.google.gwt.user.client.Element;
020:
021: /**
022: * A panel that displays all of its child widgets in a 'deck', where only one
023: * can be visible at a time. It is used by
024: * {@link com.google.gwt.user.client.ui.TabPanel}.
025: *
026: * <p>
027: * Once a widget has been added to a DeckPanel, its visibility, width, and
028: * height attributes will be manipulated. When the widget is removed from the
029: * DeckPanel, it will be visible, and its width and height attributes will be
030: * cleared.
031: * </p>
032: */
033: public class DeckPanel extends ComplexPanel {
034:
035: private Widget visibleWidget;
036:
037: /**
038: * Creates an empty deck panel.
039: */
040: public DeckPanel() {
041: setElement(DOM.createDiv());
042: }
043:
044: /**
045: * Adds the specified widget to the deck.
046: *
047: * @param w the widget to be added
048: */
049: @Override
050: public void add(Widget w) {
051: super .add(w, getElement());
052: initChildWidget(w);
053: }
054:
055: /**
056: * Gets the index of the currently-visible widget.
057: *
058: * @return the visible widget's index
059: */
060: public int getVisibleWidget() {
061: return getWidgetIndex(visibleWidget);
062: }
063:
064: /**
065: * Inserts a widget before the specified index.
066: *
067: * @param w the widget to be inserted
068: * @param beforeIndex the index before which it will be inserted
069: * @throws IndexOutOfBoundsException if <code>beforeIndex</code> is out of
070: * range
071: */
072: public void insert(Widget w, int beforeIndex) {
073: super .insert(w, getElement(), beforeIndex, true);
074: initChildWidget(w);
075: }
076:
077: @Override
078: public boolean remove(Widget w) {
079: boolean removed = super .remove(w);
080: if (removed) {
081: resetChildWidget(w);
082:
083: if (visibleWidget == w) {
084: visibleWidget = null;
085: }
086: }
087: return removed;
088: }
089:
090: /**
091: * Shows the widget at the specified index. This causes the currently- visible
092: * widget to be hidden.
093: *
094: * @param index the index of the widget to be shown
095: */
096: public void showWidget(int index) {
097: checkIndexBoundsForAccess(index);
098:
099: if (visibleWidget != null) {
100: visibleWidget.setVisible(false);
101: }
102: visibleWidget = getWidget(index);
103: visibleWidget.setVisible(true);
104: }
105:
106: /**
107: * Make the widget invisible, and set its width and height to full.
108: */
109: private void initChildWidget(Widget w) {
110: Element child = w.getElement();
111: DOM.setStyleAttribute(child, "width", "100%");
112: DOM.setStyleAttribute(child, "height", "100%");
113: w.setVisible(false);
114: }
115:
116: /**
117: * Make the widget visible, and clear the widget's width and height
118: * attributes. This is done so that any changes to the visibility, height, or
119: * width of the widget that were done by the panel are undone.
120: */
121: private void resetChildWidget(Widget w) {
122: DOM.setStyleAttribute(w.getElement(), "width", "");
123: DOM.setStyleAttribute(w.getElement(), "height", "");
124: w.setVisible(true);
125: }
126: }
|