You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ITabsheetPanel.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.terminal.gwt.client.ui;
  5. import com.google.gwt.user.client.DOM;
  6. import com.google.gwt.user.client.Element;
  7. import com.google.gwt.user.client.ui.ComplexPanel;
  8. import com.google.gwt.user.client.ui.Widget;
  9. /**
  10. * A panel that displays all of its child widgets in a 'deck', where only one
  11. * can be visible at a time. It is used by
  12. * {@link com.itmill.toolkit.terminal.gwt.client.ui.ITabsheet}.
  13. *
  14. * This class has the same basic functionality as the GWT DeckPanel
  15. * {@link com.google.gwt.user.client.ui.DeckPanel}, with the exception that it
  16. * doesn't manipulate the child widgets' width and height attributes.
  17. */
  18. public class ITabsheetPanel extends ComplexPanel {
  19. private Widget visibleWidget;
  20. private int contentHeight;
  21. /**
  22. * Creates an empty tabsheet panel.
  23. */
  24. public ITabsheetPanel() {
  25. setElement(DOM.createDiv());
  26. }
  27. /**
  28. * Adds the specified widget to the deck.
  29. *
  30. * @param w
  31. * the widget to be added
  32. */
  33. public void add(Widget w) {
  34. Element el = createContainerElement();
  35. DOM.appendChild(getElement(), el);
  36. super.add(w, el);
  37. }
  38. private Element createContainerElement() {
  39. Element el = DOM.createDiv();
  40. hide(el);
  41. return el;
  42. }
  43. /**
  44. * Gets the index of the currently-visible widget.
  45. *
  46. * @return the visible widget's index
  47. */
  48. public int getVisibleWidget() {
  49. return getWidgetIndex(visibleWidget);
  50. }
  51. /**
  52. * Inserts a widget before the specified index.
  53. *
  54. * @param w
  55. * the widget to be inserted
  56. * @param beforeIndex
  57. * the index before which it will be inserted
  58. * @throws IndexOutOfBoundsException
  59. * if <code>beforeIndex</code> is out of range
  60. */
  61. public void insert(Widget w, int beforeIndex) {
  62. Element el = createContainerElement();
  63. DOM.insertChild(getElement(), el, beforeIndex);
  64. super.insert(w, el, beforeIndex, false);
  65. }
  66. public boolean remove(Widget w) {
  67. final int index = getWidgetIndex(w);
  68. final boolean removed = super.remove(w);
  69. if (removed) {
  70. if (visibleWidget == w) {
  71. visibleWidget = null;
  72. }
  73. Element child = DOM.getChild(getElement(), index);
  74. DOM.removeChild(getElement(), child);
  75. unHide(child);
  76. }
  77. return removed;
  78. }
  79. /**
  80. * Shows the widget at the specified index. This causes the currently-
  81. * visible widget to be hidden.
  82. *
  83. * @param index
  84. * the index of the widget to be shown
  85. */
  86. public void showWidget(int index) {
  87. checkIndexBoundsForAccess(index);
  88. Widget newVisible = getWidget(index);
  89. if (visibleWidget != newVisible) {
  90. if (visibleWidget != null) {
  91. hide(DOM.getParent(visibleWidget.getElement()));
  92. }
  93. visibleWidget = newVisible;
  94. unHide(DOM.getParent(visibleWidget.getElement()));
  95. }
  96. }
  97. private void hide(Element e) {
  98. DOM.setStyleAttribute(e, "width", "0px");
  99. DOM.setStyleAttribute(e, "height", "0px");
  100. DOM.setStyleAttribute(e, "overflow", "hidden");
  101. DOM.setStyleAttribute(e, "visibility", "hidden");
  102. DOM.setStyleAttribute(e, "position", "absolute");
  103. DOM.setStyleAttribute(e, "top", "0px");
  104. DOM.setStyleAttribute(e, "left", "0px");
  105. }
  106. private void unHide(Element e) {
  107. DOM.setStyleAttribute(e, "marginLeft", "0px");
  108. DOM.setStyleAttribute(e, "marginTop", "0px");
  109. DOM.setStyleAttribute(e, "position", "");
  110. DOM.setStyleAttribute(e, "top", "");
  111. DOM.setStyleAttribute(e, "left", "");
  112. DOM.setStyleAttribute(e, "visibility", "");
  113. DOM.setStyleAttribute(e, "width", "");
  114. DOM.setStyleAttribute(e, "height", "");
  115. DOM.setStyleAttribute(e, "overflow", "");
  116. }
  117. }