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.

TabsheetBaseConnector.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright 2000-2021 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.tabsheet;
  17. import java.util.ArrayList;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import com.google.gwt.user.client.ui.Widget;
  21. import com.vaadin.client.ComponentConnector;
  22. import com.vaadin.client.communication.StateChangeEvent;
  23. import com.vaadin.client.ui.AbstractComponentContainerConnector;
  24. import com.vaadin.client.ui.VTabsheetBase;
  25. import com.vaadin.shared.ui.tabsheet.TabState;
  26. import com.vaadin.shared.ui.tabsheet.TabsheetState;
  27. /**
  28. * An abstract connector class for components that share features with a
  29. * TabSheet.
  30. *
  31. * @author Vaadin Ltd
  32. */
  33. public abstract class TabsheetBaseConnector
  34. extends AbstractComponentContainerConnector {
  35. /*
  36. * (non-Javadoc)
  37. *
  38. * @see com.vaadin.client.ui.AbstractConnector#init()
  39. */
  40. @Override
  41. protected void init() {
  42. super.init();
  43. getWidget().setClient(getConnection());
  44. }
  45. /*
  46. * (non-Javadoc)
  47. *
  48. * @see
  49. * com.vaadin.client.ui.AbstractComponentConnector#onStateChanged(com.vaadin
  50. * .client.communication.StateChangeEvent)
  51. */
  52. @Override
  53. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  54. super.onStateChanged(stateChangeEvent);
  55. VTabsheetBase widget = getWidget();
  56. // Update member references
  57. widget.setEnabled(isEnabled());
  58. // Widgets in the TabSheet before update (should be max 1)
  59. List<Widget> oldWidgets = new ArrayList<>();
  60. for (Iterator<Widget> iterator = widget.getWidgetIterator(); iterator
  61. .hasNext();) {
  62. Widget child = iterator.next();
  63. // filter out any current widgets (should be max 1)
  64. boolean found = false;
  65. for (ComponentConnector childComponent : getChildComponents()) {
  66. if (childComponent.getWidget().equals(child)) {
  67. found = true;
  68. break;
  69. }
  70. }
  71. if (!found) {
  72. oldWidgets.add(child);
  73. }
  74. }
  75. // Clear previous values
  76. widget.clearTabKeys();
  77. int index = 0;
  78. for (TabState tab : getState().tabs) {
  79. final String key = tab.key;
  80. final boolean selected = key.equals(getState().selected);
  81. widget.addTabKey(key, !tab.enabled && tab.visible);
  82. if (selected) {
  83. widget.setActiveTabIndex(index);
  84. }
  85. widget.renderTab(tab, index);
  86. if (selected) {
  87. widget.selectTab(index);
  88. }
  89. index++;
  90. }
  91. int tabCount = widget.getTabCount();
  92. while (tabCount-- > index) {
  93. widget.removeTab(index);
  94. }
  95. // Detach any old tab widget, should be max 1
  96. for (Widget oldWidget : oldWidgets) {
  97. if (oldWidget.isAttached()) {
  98. oldWidget.removeFromParent();
  99. }
  100. }
  101. }
  102. @Override
  103. public VTabsheetBase getWidget() {
  104. return (VTabsheetBase) super.getWidget();
  105. }
  106. @Override
  107. public TabsheetState getState() {
  108. return (TabsheetState) super.getState();
  109. }
  110. }