aboutsummaryrefslogtreecommitdiffstats
path: root/client/src/com/vaadin/client/ui/VTabsheetPanel.java
blob: 6bd63cdbd332c19c87cebaa29037227fdea1171e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * Copyright 2000-2013 Vaadin Ltd.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.vaadin.client.ui;

import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.client.ui.TouchScrollDelegate.TouchScrollHandler;

/**
 * A panel that displays all of its child widgets in a 'deck', where only one
 * can be visible at a time. It is used by
 * {@link com.vaadin.client.ui.VTabsheet}.
 * 
 * This class has the same basic functionality as the GWT DeckPanel
 * {@link com.google.gwt.user.client.ui.DeckPanel}, with the exception that it
 * doesn't manipulate the child widgets' width and height attributes.
 */
public class VTabsheetPanel extends ComplexPanel {

    private Widget visibleWidget;

    private final TouchScrollHandler touchScrollHandler;

    /**
     * Creates an empty tabsheet panel.
     */
    public VTabsheetPanel() {
        setElement(DOM.createDiv());
        touchScrollHandler = TouchScrollDelegate.enableTouchScrolling(this);
    }

    /**
     * Adds the specified widget to the deck.
     * 
     * @param w
     *            the widget to be added
     */
    @Override
    public void add(Widget w) {
        Element el = createContainerElement();
        DOM.appendChild(getElement(), el);
        super.add(w, el);
    }

    private Element createContainerElement() {
        Element el = DOM.createDiv();
        DOM.setStyleAttribute(el, "position", "absolute");
        hide(el);
        touchScrollHandler.addElement(el);
        return el;
    }

    /**
     * Gets the index of the currently-visible widget.
     * 
     * @return the visible widget's index
     */
    public int getVisibleWidget() {
        return getWidgetIndex(visibleWidget);
    }

    /**
     * Inserts a widget before the specified index.
     * 
     * @param w
     *            the widget to be inserted
     * @param beforeIndex
     *            the index before which it will be inserted
     * @throws IndexOutOfBoundsException
     *             if <code>beforeIndex</code> is out of range
     */
    public void insert(Widget w, int beforeIndex) {
        Element el = createContainerElement();
        DOM.insertChild(getElement(), el, beforeIndex);
        super.insert(w, el, beforeIndex, false);
    }

    @Override
    public boolean remove(Widget w) {
        Element child = w.getElement();
        Element parent = null;
        if (child != null) {
            parent = DOM.getParent(child);
        }
        final boolean removed = super.remove(w);
        if (removed) {
            if (visibleWidget == w) {
                visibleWidget = null;
            }
            if (parent != null) {
                DOM.removeChild(getElement(), parent);
            }
            touchScrollHandler.removeElement(parent);
        }
        return removed;
    }

    /**
     * Shows the widget at the specified index. This causes the currently-
     * visible widget to be hidden.
     * 
     * @param index
     *            the index of the widget to be shown
     */
    public void showWidget(int index) {
        checkIndexBoundsForAccess(index);
        Widget newVisible = getWidget(index);
        if (visibleWidget != newVisible) {
            if (visibleWidget != null) {
                hide(DOM.getParent(visibleWidget.getElement()));
            }
            visibleWidget = newVisible;
            touchScrollHandler.setElements(visibleWidget.getElement()
                    .getParentElement());
        }
        // Always ensure the selected tab is visible. If server prevents a tab
        // change we might end up here with visibleWidget == newVisible but its
        // parent is still hidden.
        unHide(DOM.getParent(visibleWidget.getElement()));
    }

    private void hide(Element e) {
        DOM.setStyleAttribute(e, "visibility", "hidden");
        DOM.setStyleAttribute(e, "top", "-100000px");
        DOM.setStyleAttribute(e, "left", "-100000px");
    }

    private void unHide(Element e) {
        DOM.setStyleAttribute(e, "top", "0px");
        DOM.setStyleAttribute(e, "left", "0px");
        DOM.setStyleAttribute(e, "visibility", "");
    }

    public void fixVisibleTabSize(int width, int height, int minWidth) {
        if (visibleWidget == null) {
            return;
        }

        boolean dynamicHeight = false;

        if (height < 0) {
            height = visibleWidget.getOffsetHeight();
            dynamicHeight = true;
        }
        if (width < 0) {
            width = visibleWidget.getOffsetWidth();
        }
        if (width < minWidth) {
            width = minWidth;
        }

        Element wrapperDiv = visibleWidget.getElement().getParentElement();

        // width first
        getElement().getStyle().setPropertyPx("width", width);
        wrapperDiv.getStyle().setPropertyPx("width", width);

        if (dynamicHeight) {
            // height of widget might have changed due wrapping
            height = visibleWidget.getOffsetHeight();
        }
        // v-tabsheet-tabsheetpanel height
        getElement().getStyle().setPropertyPx("height", height);

        // widget wrapper height
        if (dynamicHeight) {
            wrapperDiv.getStyle().clearHeight();
        } else {
            // widget wrapper height
            wrapperDiv.getStyle().setPropertyPx("height", height);
        }
    }

    public void replaceComponent(Widget oldComponent, Widget newComponent) {
        boolean isVisible = (visibleWidget == oldComponent);
        int widgetIndex = getWidgetIndex(oldComponent);
        remove(oldComponent);
        insert(newComponent, widgetIndex);
        if (isVisible) {
            showWidget(widgetIndex);
        }
    }
}