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.

VTabsheetBase.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.ArrayList;
  6. import java.util.HashSet;
  7. import java.util.Iterator;
  8. import java.util.Set;
  9. import com.google.gwt.user.client.DOM;
  10. import com.google.gwt.user.client.ui.ComplexPanel;
  11. import com.google.gwt.user.client.ui.Widget;
  12. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  13. import com.vaadin.terminal.gwt.client.Container;
  14. import com.vaadin.terminal.gwt.client.UIDL;
  15. import com.vaadin.terminal.gwt.client.VPaintableMap;
  16. import com.vaadin.terminal.gwt.client.VPaintableWidget;
  17. abstract class VTabsheetBase extends ComplexPanel implements Container {
  18. String id;
  19. ApplicationConnection client;
  20. protected final ArrayList<String> tabKeys = new ArrayList<String>();
  21. protected int activeTabIndex = 0;
  22. protected boolean disabled;
  23. protected boolean readonly;
  24. protected Set<String> disabledTabKeys = new HashSet<String>();
  25. protected boolean cachedUpdate = false;
  26. public VTabsheetBase(String classname) {
  27. setElement(DOM.createDiv());
  28. setStyleName(classname);
  29. }
  30. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  31. this.client = client;
  32. // Ensure correct implementation
  33. cachedUpdate = client.updateComponent(this, uidl, true);
  34. if (cachedUpdate) {
  35. return;
  36. }
  37. // Update member references
  38. id = uidl.getId();
  39. disabled = uidl.hasAttribute("disabled");
  40. // Render content
  41. final UIDL tabs = uidl.getChildUIDL(0);
  42. // Paintables in the TabSheet before update
  43. ArrayList<Widget> oldWidgets = new ArrayList<Widget>();
  44. for (Iterator<Widget> iterator = getWidgetIterator(); iterator
  45. .hasNext();) {
  46. oldWidgets.add(iterator.next());
  47. }
  48. // Clear previous values
  49. tabKeys.clear();
  50. disabledTabKeys.clear();
  51. int index = 0;
  52. for (final Iterator<Object> it = tabs.getChildIterator(); it.hasNext();) {
  53. final UIDL tab = (UIDL) it.next();
  54. final String key = tab.getStringAttribute("key");
  55. final boolean selected = tab.getBooleanAttribute("selected");
  56. final boolean hidden = tab.getBooleanAttribute("hidden");
  57. if (tab.getBooleanAttribute("disabled")) {
  58. disabledTabKeys.add(key);
  59. }
  60. tabKeys.add(key);
  61. if (selected) {
  62. activeTabIndex = index;
  63. }
  64. renderTab(tab, index, selected, hidden);
  65. index++;
  66. }
  67. int tabCount = getTabCount();
  68. while (tabCount-- > index) {
  69. removeTab(index);
  70. }
  71. for (int i = 0; i < getTabCount(); i++) {
  72. VPaintableWidget p = getTab(i);
  73. // During the initial rendering the paintable might be null (this is
  74. // weird...)
  75. if (p != null) {
  76. oldWidgets.remove(p.getWidgetForPaintable());
  77. }
  78. }
  79. // Perform unregister for any paintables removed during update
  80. for (Iterator<Widget> iterator = oldWidgets.iterator(); iterator
  81. .hasNext();) {
  82. Widget oldWidget = iterator.next();
  83. VPaintableWidget oldPaintable = VPaintableMap.get(client)
  84. .getPaintable(oldWidget);
  85. if (oldWidget.isAttached()) {
  86. oldWidget.removeFromParent();
  87. }
  88. VPaintableMap.get(client).unregisterPaintable(oldPaintable);
  89. }
  90. }
  91. /**
  92. * @return a list of currently shown Paintables
  93. *
  94. * Apparently can be something else than Paintable as
  95. * {@link #updateFromUIDL(UIDL, ApplicationConnection)} checks if
  96. * instanceof Paintable. Therefore set to <Object>
  97. */
  98. abstract protected Iterator<Widget> getWidgetIterator();
  99. /**
  100. * Clears current tabs and contents
  101. */
  102. abstract protected void clearPaintables();
  103. /**
  104. * Implement in extending classes. This method should render needed elements
  105. * and set the visibility of the tab according to the 'selected' parameter.
  106. */
  107. protected abstract void renderTab(final UIDL tabUidl, int index,
  108. boolean selected, boolean hidden);
  109. /**
  110. * Implement in extending classes. This method should render any previously
  111. * non-cached content and set the activeTabIndex property to the specified
  112. * index.
  113. */
  114. protected abstract void selectTab(int index, final UIDL contentUidl);
  115. /**
  116. * Implement in extending classes. This method should return the number of
  117. * tabs currently rendered.
  118. */
  119. protected abstract int getTabCount();
  120. /**
  121. * Implement in extending classes. This method should return the Paintable
  122. * corresponding to the given index.
  123. */
  124. protected abstract VPaintableWidget getTab(int index);
  125. /**
  126. * Implement in extending classes. This method should remove the rendered
  127. * tab with the specified index.
  128. */
  129. protected abstract void removeTab(int index);
  130. }