Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

VTabsheetPanel.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  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.vaadin.client.ui;
  17. import com.google.gwt.dom.client.Element;
  18. import com.google.gwt.dom.client.Style.Position;
  19. import com.google.gwt.dom.client.Style.Unit;
  20. import com.google.gwt.dom.client.Style.Visibility;
  21. import com.google.gwt.user.client.DOM;
  22. import com.google.gwt.user.client.ui.ComplexPanel;
  23. import com.google.gwt.user.client.ui.Widget;
  24. import com.vaadin.client.ui.TouchScrollDelegate.TouchScrollHandler;
  25. /**
  26. * A panel that displays all of its child widgets in a 'deck', where only one
  27. * can be visible at a time. It is used by
  28. * {@link com.vaadin.client.ui.VTabsheet}.
  29. *
  30. * This class has the same basic functionality as the GWT DeckPanel
  31. * {@link com.google.gwt.user.client.ui.DeckPanel}, with the exception that it
  32. * doesn't manipulate the child widgets' width and height attributes.
  33. */
  34. public class VTabsheetPanel extends ComplexPanel {
  35. private Widget visibleWidget;
  36. private final TouchScrollHandler touchScrollHandler;
  37. /**
  38. * Creates an empty tabsheet panel.
  39. */
  40. public VTabsheetPanel() {
  41. setElement(DOM.createDiv());
  42. touchScrollHandler = TouchScrollDelegate.enableTouchScrolling(this);
  43. }
  44. /**
  45. * Adds the specified widget to the deck.
  46. *
  47. * @param w
  48. * the widget to be added
  49. */
  50. @Override
  51. public void add(Widget w) {
  52. Element el = createContainerElement();
  53. DOM.appendChild(getElement(), el);
  54. super.add(w, el);
  55. }
  56. private Element createContainerElement() {
  57. Element el = DOM.createDiv();
  58. el.getStyle().setPosition(Position.ABSOLUTE);
  59. hide(el);
  60. touchScrollHandler.addElement(el);
  61. return el;
  62. }
  63. /**
  64. * Gets the index of the currently-visible widget.
  65. *
  66. * @return the visible widget's index
  67. */
  68. public int getVisibleWidget() {
  69. return getWidgetIndex(visibleWidget);
  70. }
  71. /**
  72. * Inserts a widget before the specified index.
  73. *
  74. * @param w
  75. * the widget to be inserted
  76. * @param beforeIndex
  77. * the index before which it will be inserted
  78. * @throws IndexOutOfBoundsException
  79. * if <code>beforeIndex</code> is out of range
  80. */
  81. public void insert(Widget w, int beforeIndex) {
  82. Element el = createContainerElement();
  83. DOM.insertChild(getElement(), el, beforeIndex);
  84. super.insert(w, el, beforeIndex, false);
  85. }
  86. @Override
  87. public boolean remove(Widget w) {
  88. Element child = w.getElement();
  89. Element parent = null;
  90. if (child != null) {
  91. parent = DOM.getParent(child);
  92. }
  93. final boolean removed = super.remove(w);
  94. if (removed) {
  95. if (visibleWidget == w) {
  96. visibleWidget = null;
  97. }
  98. if (parent != null) {
  99. DOM.removeChild(getElement(), parent);
  100. }
  101. touchScrollHandler.removeElement(parent);
  102. }
  103. return removed;
  104. }
  105. /**
  106. * Shows the widget at the specified index. This causes the currently-
  107. * visible widget to be hidden.
  108. *
  109. * @param index
  110. * the index of the widget to be shown
  111. */
  112. public void showWidget(int index) {
  113. checkIndexBoundsForAccess(index);
  114. Widget newVisible = getWidget(index);
  115. if (visibleWidget != newVisible) {
  116. if (visibleWidget != null) {
  117. hide(DOM.getParent(visibleWidget.getElement()));
  118. }
  119. visibleWidget = newVisible;
  120. touchScrollHandler
  121. .setElements(visibleWidget.getElement().getParentElement());
  122. }
  123. // Always ensure the selected tab is visible. If server prevents a tab
  124. // change we might end up here with visibleWidget == newVisible but its
  125. // parent is still hidden.
  126. unHide(DOM.getParent(visibleWidget.getElement()));
  127. }
  128. private void hide(Element e) {
  129. e.getStyle().setVisibility(Visibility.HIDDEN);
  130. e.getStyle().setTop(-100000, Unit.PX);
  131. e.getStyle().setLeft(-100000, Unit.PX);
  132. }
  133. private void unHide(Element e) {
  134. e.getStyle().setTop(0, Unit.PX);
  135. e.getStyle().setLeft(0, Unit.PX);
  136. e.getStyle().clearVisibility();
  137. }
  138. public void fixVisibleTabSize(int width, int height, int minWidth) {
  139. if (visibleWidget == null) {
  140. return;
  141. }
  142. boolean dynamicHeight = false;
  143. if (height < 0) {
  144. height = visibleWidget.getOffsetHeight();
  145. dynamicHeight = true;
  146. }
  147. if (width < 0) {
  148. width = visibleWidget.getOffsetWidth();
  149. }
  150. if (width < minWidth) {
  151. width = minWidth;
  152. }
  153. Element wrapperDiv = visibleWidget.getElement().getParentElement();
  154. // width first
  155. getElement().getStyle().setPropertyPx("width", width);
  156. wrapperDiv.getStyle().setPropertyPx("width", width);
  157. if (dynamicHeight) {
  158. // height of widget might have changed due wrapping
  159. height = visibleWidget.getOffsetHeight();
  160. }
  161. // v-tabsheet-tabsheetpanel height
  162. getElement().getStyle().setPropertyPx("height", height);
  163. // widget wrapper height
  164. if (dynamicHeight) {
  165. wrapperDiv.getStyle().clearHeight();
  166. } else {
  167. // widget wrapper height
  168. wrapperDiv.getStyle().setPropertyPx("height", height);
  169. }
  170. }
  171. public void replaceComponent(Widget oldComponent, Widget newComponent) {
  172. boolean isVisible = (visibleWidget == oldComponent);
  173. int widgetIndex = getWidgetIndex(oldComponent);
  174. remove(oldComponent);
  175. insert(newComponent, widgetIndex);
  176. if (isVisible) {
  177. showWidget(widgetIndex);
  178. }
  179. }
  180. }