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 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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;
  17. import java.util.ArrayList;
  18. import java.util.HashSet;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import java.util.Set;
  22. import com.google.gwt.user.client.DOM;
  23. import com.google.gwt.user.client.ui.ComplexPanel;
  24. import com.google.gwt.user.client.ui.HasEnabled;
  25. import com.google.gwt.user.client.ui.Widget;
  26. import com.vaadin.client.ApplicationConnection;
  27. import com.vaadin.client.ComponentConnector;
  28. import com.vaadin.client.ConnectorMap;
  29. import com.vaadin.shared.ui.tabsheet.TabState;
  30. /**
  31. * Base class for a multi-view widget such as TabSheet or Accordion.
  32. *
  33. * @author Vaadin Ltd.
  34. */
  35. public abstract class VTabsheetBase extends ComplexPanel implements HasEnabled {
  36. /** For internal use only. May be removed or replaced in the future. */
  37. protected ApplicationConnection client;
  38. /** For internal use only. May be removed or replaced in the future. */
  39. protected final List<String> tabKeys = new ArrayList<>();
  40. /** For internal use only. May be removed or replaced in the future. */
  41. protected Set<String> disabledTabKeys = new HashSet<>();
  42. /** For internal use only. May be removed or replaced in the future. */
  43. protected int activeTabIndex = 0;
  44. /** For internal use only. May be removed or replaced in the future. */
  45. protected boolean disabled;
  46. /** For internal use only. May be removed or replaced in the future. */
  47. protected boolean readonly;
  48. /** For internal use only. May be removed or replaced in the future. */
  49. protected AbstractComponentConnector connector;
  50. private boolean tabCaptionsAsHtml = false;
  51. /**
  52. * Constructs a multi-view widget with the given classname.
  53. *
  54. * @param classname
  55. * the style name to set
  56. */
  57. @SuppressWarnings("deprecation")
  58. public VTabsheetBase(String classname) {
  59. setElement(DOM.createDiv());
  60. setStyleName(classname);
  61. }
  62. /**
  63. * @return a list of currently shown Widgets
  64. */
  65. public abstract Iterator<Widget> getWidgetIterator();
  66. /**
  67. * Clears current tabs and contents.
  68. *
  69. * @deprecated This method is not called by the framework code anymore.
  70. */
  71. @Deprecated
  72. protected abstract void clearPaintables();
  73. /**
  74. * Implement in extending classes. This method should render needed elements
  75. * and set the visibility of the tab according to the 'visible' parameter.
  76. * This method should not update the selection, the connector should handle
  77. * that separately.
  78. *
  79. * @param tabState
  80. * shared state of a single tab
  81. * @param index
  82. * the index of that tab
  83. */
  84. public abstract void renderTab(TabState tabState, int index);
  85. /**
  86. * Implement in extending classes. This method should return the number of
  87. * tabs currently rendered.
  88. *
  89. * @return the number of currently rendered tabs
  90. */
  91. public abstract int getTabCount();
  92. /**
  93. * Implement in extending classes. This method should return the connector
  94. * corresponding to the given index.
  95. *
  96. * @param index
  97. * the index of the tab whose connector to find
  98. * @return the connector of the queried tab, or {@code null} if not found
  99. */
  100. public abstract ComponentConnector getTab(int index);
  101. /**
  102. * Implement in extending classes. This method should remove the rendered
  103. * tab with the specified index.
  104. *
  105. * @param index
  106. * the index of the tab to remove
  107. */
  108. public abstract void removeTab(int index);
  109. /**
  110. * Returns whether the width of the widget is undefined.
  111. *
  112. * @since 7.2
  113. * @return {@code true} if width of the widget is determined by its content,
  114. * {@code false} otherwise
  115. */
  116. protected boolean isDynamicWidth() {
  117. return getConnectorForWidget(this).isUndefinedWidth();
  118. }
  119. /**
  120. * Returns whether the height of the widget is undefined.
  121. *
  122. * @since 7.2
  123. * @return {@code true} if height of the widget is determined by its
  124. * content, {@code false} otherwise
  125. */
  126. protected boolean isDynamicHeight() {
  127. return getConnectorForWidget(this).isUndefinedHeight();
  128. }
  129. /**
  130. * Sets the connector that should be notified of events etc.
  131. *
  132. * For internal use only. This method may be removed or replaced in the
  133. * future.
  134. *
  135. * @since 7.2
  136. * @param connector
  137. * the connector of this widget
  138. */
  139. public void setConnector(AbstractComponentConnector connector) {
  140. this.connector = connector;
  141. }
  142. /** For internal use only. May be removed or replaced in the future. */
  143. public void clearTabKeys() {
  144. tabKeys.clear();
  145. disabledTabKeys.clear();
  146. }
  147. /**
  148. * For internal use only. May be removed or replaced in the future.
  149. *
  150. * @param key
  151. * an internal key that corresponds with a tab
  152. * @param disabled
  153. * {@code true} if the tab should be disabled, {@code false}
  154. * otherwise
  155. */
  156. public void addTabKey(String key, boolean disabled) {
  157. tabKeys.add(key);
  158. if (disabled) {
  159. disabledTabKeys.add(key);
  160. }
  161. }
  162. /**
  163. * For internal use only. May be removed or replaced in the future.
  164. *
  165. * @param client
  166. * the current application connection instance
  167. */
  168. public void setClient(ApplicationConnection client) {
  169. this.client = client;
  170. }
  171. /**
  172. * For internal use only. May be removed or replaced in the future.
  173. *
  174. * @param activeTabIndex
  175. * the index of the currently active tab
  176. */
  177. public void setActiveTabIndex(int activeTabIndex) {
  178. this.activeTabIndex = activeTabIndex;
  179. }
  180. /** For internal use only. May be removed or replaced in the future. */
  181. @Override
  182. public void setEnabled(boolean enabled) {
  183. disabled = !enabled;
  184. }
  185. /**
  186. * For internal use only. May be removed or replaced in the future.
  187. *
  188. * @param readonly
  189. * {@code true} if this widget should be read-only, {@code false}
  190. * otherwise
  191. */
  192. public void setReadonly(boolean readonly) {
  193. this.readonly = readonly;
  194. }
  195. /**
  196. * For internal use only. May be removed or replaced in the future.
  197. *
  198. * @param widget
  199. * the widget whose connector to find
  200. * @return the connector
  201. */
  202. protected ComponentConnector getConnectorForWidget(Widget widget) {
  203. return ConnectorMap.get(client).getConnector(widget);
  204. }
  205. /**
  206. * For internal use only. May be removed or replaced in the future.
  207. *
  208. * @param index
  209. * the index of the tab to select
  210. */
  211. public abstract void selectTab(int index);
  212. @Override
  213. public boolean isEnabled() {
  214. return !disabled;
  215. }
  216. /**
  217. * Sets whether the caption is rendered as HTML.
  218. * <p>
  219. * The default is false, i.e. render tab captions as plain text
  220. * <p>
  221. * This value is delegated from the TabsheetState.
  222. *
  223. * @since 7.4
  224. * @param tabCaptionsAsHtml
  225. * {@code true} if the captions are rendered as HTML,
  226. * {@code false} if rendered as plain text
  227. */
  228. public void setTabCaptionsAsHtml(boolean tabCaptionsAsHtml) {
  229. this.tabCaptionsAsHtml = tabCaptionsAsHtml;
  230. }
  231. /**
  232. * Checks whether captions are rendered as HTML
  233. *
  234. * The default is false, i.e. render tab captions as plain text
  235. *
  236. * @since 7.4
  237. * @return true if the captions are rendered as HTML, false if rendered as
  238. * plain text
  239. */
  240. public boolean isTabCaptionsAsHtml() {
  241. return tabCaptionsAsHtml;
  242. }
  243. }