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.

VAccordion.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.HashMap;
  6. import java.util.HashSet;
  7. import java.util.Iterator;
  8. import java.util.Set;
  9. import com.google.gwt.event.dom.client.ClickEvent;
  10. import com.google.gwt.event.dom.client.ClickHandler;
  11. import com.google.gwt.user.client.DOM;
  12. import com.google.gwt.user.client.Element;
  13. import com.google.gwt.user.client.Event;
  14. import com.google.gwt.user.client.ui.ComplexPanel;
  15. import com.google.gwt.user.client.ui.Widget;
  16. import com.vaadin.terminal.gwt.client.RenderInformation;
  17. import com.vaadin.terminal.gwt.client.UIDL;
  18. import com.vaadin.terminal.gwt.client.Util;
  19. import com.vaadin.terminal.gwt.client.VCaption;
  20. import com.vaadin.terminal.gwt.client.ConnectorMap;
  21. import com.vaadin.terminal.gwt.client.ComponentConnector;
  22. public class VAccordion extends VTabsheetBase {
  23. public static final String CLASSNAME = "v-accordion";
  24. private Set<Widget> widgets = new HashSet<Widget>();
  25. HashMap<StackItem, UIDL> lazyUpdateMap = new HashMap<StackItem, UIDL>();
  26. StackItem openTab = null;
  27. int selectedUIDLItemIndex = -1;
  28. RenderInformation renderInformation = new RenderInformation();
  29. public VAccordion() {
  30. super(CLASSNAME);
  31. }
  32. @Override
  33. protected void renderTab(UIDL tabUidl, int index, boolean selected,
  34. boolean hidden) {
  35. StackItem item;
  36. int itemIndex;
  37. if (getWidgetCount() <= index) {
  38. // Create stackItem and render caption
  39. item = new StackItem(tabUidl);
  40. if (getWidgetCount() == 0) {
  41. item.addStyleDependentName("first");
  42. }
  43. itemIndex = getWidgetCount();
  44. add(item, getElement());
  45. } else {
  46. item = getStackItem(index);
  47. item = moveStackItemIfNeeded(item, index, tabUidl);
  48. itemIndex = index;
  49. }
  50. item.updateCaption(tabUidl);
  51. item.setVisible(!hidden);
  52. if (selected) {
  53. selectedUIDLItemIndex = itemIndex;
  54. }
  55. if (tabUidl.getChildCount() > 0) {
  56. lazyUpdateMap.put(item, tabUidl.getChildUIDL(0));
  57. }
  58. }
  59. /**
  60. * This method tries to find out if a tab has been rendered with a different
  61. * index previously. If this is the case it re-orders the children so the
  62. * same StackItem is used for rendering this time. E.g. if the first tab has
  63. * been removed all tabs which contain cached content must be moved 1 step
  64. * up to preserve the cached content.
  65. *
  66. * @param item
  67. * @param newIndex
  68. * @param tabUidl
  69. * @return
  70. */
  71. private StackItem moveStackItemIfNeeded(StackItem item, int newIndex,
  72. UIDL tabUidl) {
  73. UIDL tabContentUIDL = null;
  74. ComponentConnector tabContent = null;
  75. if (tabUidl.getChildCount() > 0) {
  76. tabContentUIDL = tabUidl.getChildUIDL(0);
  77. tabContent = client.getPaintable(tabContentUIDL);
  78. }
  79. Widget itemWidget = item.getComponent();
  80. if (tabContent != null) {
  81. if (tabContent != itemWidget) {
  82. /*
  83. * This is not the same widget as before, find out if it has
  84. * been moved
  85. */
  86. int oldIndex = -1;
  87. StackItem oldItem = null;
  88. for (int i = 0; i < getWidgetCount(); i++) {
  89. Widget w = getWidget(i);
  90. oldItem = (StackItem) w;
  91. if (tabContent == oldItem.getComponent()) {
  92. oldIndex = i;
  93. break;
  94. }
  95. }
  96. if (oldIndex != -1 && oldIndex > newIndex) {
  97. /*
  98. * The tab has previously been rendered in another position
  99. * so we must move the cached content to correct position.
  100. * We move only items with oldIndex > newIndex to prevent
  101. * moving items already rendered in this update. If for
  102. * instance tabs 1,2,3 are removed and added as 3,2,1 we
  103. * cannot re-use "1" when we get to the third tab.
  104. */
  105. insert(oldItem, getElement(), newIndex, true);
  106. return oldItem;
  107. }
  108. }
  109. } else {
  110. // Tab which has never been loaded. Must assure we use an empty
  111. // StackItem
  112. Widget oldWidget = item.getComponent();
  113. if (oldWidget != null) {
  114. item = new StackItem(tabUidl);
  115. insert(item, getElement(), newIndex, true);
  116. }
  117. }
  118. return item;
  119. }
  120. void open(int itemIndex) {
  121. StackItem item = (StackItem) getWidget(itemIndex);
  122. boolean alreadyOpen = false;
  123. if (openTab != null) {
  124. if (openTab.isOpen()) {
  125. if (openTab == item) {
  126. alreadyOpen = true;
  127. } else {
  128. openTab.close();
  129. }
  130. }
  131. }
  132. if (!alreadyOpen) {
  133. item.open();
  134. activeTabIndex = itemIndex;
  135. openTab = item;
  136. }
  137. // Update the size for the open tab
  138. updateOpenTabSize();
  139. }
  140. void close(StackItem item) {
  141. if (!item.isOpen()) {
  142. return;
  143. }
  144. item.close();
  145. activeTabIndex = -1;
  146. openTab = null;
  147. }
  148. @Override
  149. protected void selectTab(final int index, final UIDL contentUidl) {
  150. StackItem item = getStackItem(index);
  151. if (index != activeTabIndex) {
  152. open(index);
  153. iLayout();
  154. // TODO Check if this is needed
  155. client.runDescendentsLayout(this);
  156. }
  157. item.setContent(contentUidl);
  158. }
  159. public void onSelectTab(StackItem item) {
  160. final int index = getWidgetIndex(item);
  161. if (index != activeTabIndex && !disabled && !readonly
  162. && !disabledTabKeys.contains(tabKeys.get(index))) {
  163. addStyleDependentName("loading");
  164. client.updateVariable(id, "selected", "" + tabKeys.get(index), true);
  165. }
  166. }
  167. /**
  168. * Sets the size of the open tab
  169. */
  170. void updateOpenTabSize() {
  171. if (openTab == null) {
  172. return;
  173. }
  174. // WIDTH
  175. if (!isDynamicWidth()) {
  176. openTab.setWidth("100%");
  177. } else {
  178. openTab.setWidth(null);
  179. }
  180. // HEIGHT
  181. if (!isDynamicHeight()) {
  182. int usedPixels = 0;
  183. for (Widget w : getChildren()) {
  184. StackItem item = (StackItem) w;
  185. if (item == openTab) {
  186. usedPixels += item.getCaptionHeight();
  187. } else {
  188. // This includes the captionNode borders
  189. usedPixels += item.getHeight();
  190. }
  191. }
  192. int offsetHeight = getOffsetHeight();
  193. int spaceForOpenItem = offsetHeight - usedPixels;
  194. if (spaceForOpenItem < 0) {
  195. spaceForOpenItem = 0;
  196. }
  197. openTab.setHeight(spaceForOpenItem);
  198. } else {
  199. openTab.setHeightFromWidget();
  200. }
  201. }
  202. public void iLayout() {
  203. if (openTab == null) {
  204. return;
  205. }
  206. if (isDynamicWidth()) {
  207. int maxWidth = 40;
  208. for (Widget w : getChildren()) {
  209. StackItem si = (StackItem) w;
  210. int captionWidth = si.getCaptionWidth();
  211. if (captionWidth > maxWidth) {
  212. maxWidth = captionWidth;
  213. }
  214. }
  215. int widgetWidth = openTab.getWidgetWidth();
  216. if (widgetWidth > maxWidth) {
  217. maxWidth = widgetWidth;
  218. }
  219. super.setWidth(maxWidth + "px");
  220. openTab.setWidth(maxWidth);
  221. }
  222. Util.runWebkitOverflowAutoFix(openTab.getContainerElement());
  223. }
  224. /**
  225. *
  226. */
  227. protected class StackItem extends ComplexPanel implements ClickHandler {
  228. public void setHeight(int height) {
  229. if (height == -1) {
  230. super.setHeight("");
  231. DOM.setStyleAttribute(content, "height", "0px");
  232. } else {
  233. super.setHeight((height + getCaptionHeight()) + "px");
  234. DOM.setStyleAttribute(content, "height", height + "px");
  235. DOM.setStyleAttribute(content, "top", getCaptionHeight() + "px");
  236. }
  237. }
  238. public Widget getComponent() {
  239. if (getWidgetCount() < 2) {
  240. return null;
  241. }
  242. return getWidget(1);
  243. }
  244. @Override
  245. public void setVisible(boolean visible) {
  246. super.setVisible(visible);
  247. }
  248. public void setHeightFromWidget() {
  249. Widget widget = getChildWidget();
  250. if (widget == null) {
  251. return;
  252. }
  253. int paintableHeight = widget.getElement().getOffsetHeight();
  254. setHeight(paintableHeight);
  255. }
  256. /**
  257. * Returns caption width including padding
  258. *
  259. * @return
  260. */
  261. public int getCaptionWidth() {
  262. if (caption == null) {
  263. return 0;
  264. }
  265. int captionWidth = caption.getRequiredWidth();
  266. int padding = Util.measureHorizontalPaddingAndBorder(
  267. caption.getElement(), 18);
  268. return captionWidth + padding;
  269. }
  270. public void setWidth(int width) {
  271. if (width == -1) {
  272. super.setWidth("");
  273. } else {
  274. super.setWidth(width + "px");
  275. }
  276. }
  277. public int getHeight() {
  278. return getOffsetHeight();
  279. }
  280. public int getCaptionHeight() {
  281. return captionNode.getOffsetHeight();
  282. }
  283. private VCaption caption;
  284. private boolean open = false;
  285. private Element content = DOM.createDiv();
  286. private Element captionNode = DOM.createDiv();
  287. public StackItem(UIDL tabUidl) {
  288. setElement(DOM.createDiv());
  289. caption = new VCaption(client);
  290. caption.addClickHandler(this);
  291. super.add(caption, captionNode);
  292. DOM.appendChild(captionNode, caption.getElement());
  293. DOM.appendChild(getElement(), captionNode);
  294. DOM.appendChild(getElement(), content);
  295. setStyleName(CLASSNAME + "-item");
  296. DOM.setElementProperty(content, "className", CLASSNAME
  297. + "-item-content");
  298. DOM.setElementProperty(captionNode, "className", CLASSNAME
  299. + "-item-caption");
  300. close();
  301. }
  302. @Override
  303. public void onBrowserEvent(Event event) {
  304. onSelectTab(this);
  305. }
  306. public Element getContainerElement() {
  307. return content;
  308. }
  309. public Widget getChildWidget() {
  310. if (getWidgetCount() > 1) {
  311. return getWidget(1);
  312. } else {
  313. return null;
  314. }
  315. }
  316. public void replaceWidget(Widget newWidget) {
  317. if (getWidgetCount() > 1) {
  318. Widget oldWidget = getWidget(1);
  319. ComponentConnector oldPaintable = ConnectorMap.get(client)
  320. .getConnector(oldWidget);
  321. ConnectorMap.get(client).unregisterConnector(oldPaintable);
  322. widgets.remove(oldWidget);
  323. remove(1);
  324. }
  325. add(newWidget, content);
  326. widgets.add(newWidget);
  327. }
  328. public void open() {
  329. open = true;
  330. DOM.setStyleAttribute(content, "top", getCaptionHeight() + "px");
  331. DOM.setStyleAttribute(content, "left", "0px");
  332. DOM.setStyleAttribute(content, "visibility", "");
  333. addStyleDependentName("open");
  334. }
  335. public void hide() {
  336. DOM.setStyleAttribute(content, "visibility", "hidden");
  337. }
  338. public void close() {
  339. DOM.setStyleAttribute(content, "visibility", "hidden");
  340. DOM.setStyleAttribute(content, "top", "-100000px");
  341. DOM.setStyleAttribute(content, "left", "-100000px");
  342. removeStyleDependentName("open");
  343. setHeight(-1);
  344. setWidth("");
  345. open = false;
  346. }
  347. public boolean isOpen() {
  348. return open;
  349. }
  350. public void setContent(UIDL contentUidl) {
  351. final ComponentConnector newPntbl = client.getPaintable(contentUidl);
  352. Widget newWidget = newPntbl.getWidget();
  353. if (getChildWidget() == null) {
  354. add(newWidget, content);
  355. widgets.add(newWidget);
  356. } else if (getChildWidget() != newWidget) {
  357. replaceWidget(newWidget);
  358. }
  359. newPntbl.updateFromUIDL(contentUidl, client);
  360. if (contentUidl.getBooleanAttribute("cached")) {
  361. /*
  362. * The size of a cached, relative sized component must be
  363. * updated to report correct size.
  364. */
  365. client.handleComponentRelativeSize(newPntbl
  366. .getWidget());
  367. }
  368. if (isOpen() && isDynamicHeight()) {
  369. setHeightFromWidget();
  370. }
  371. }
  372. public void onClick(ClickEvent event) {
  373. onSelectTab(this);
  374. }
  375. public void updateCaption(UIDL uidl) {
  376. // TODO need to call this because the caption does not have an owner
  377. caption.updateCaptionWithoutOwner(
  378. uidl,
  379. uidl.getStringAttribute(VTabsheetBasePaintable.ATTRIBUTE_TAB_CAPTION),
  380. uidl.hasAttribute(VTabsheetBasePaintable.ATTRIBUTE_TAB_DISABLED),
  381. uidl.hasAttribute(VTabsheetBasePaintable.ATTRIBUTE_TAB_DESCRIPTION));
  382. }
  383. public int getWidgetWidth() {
  384. return DOM.getFirstChild(content).getOffsetWidth();
  385. }
  386. public boolean contains(ComponentConnector p) {
  387. return (getChildWidget() == p.getWidget());
  388. }
  389. public boolean isCaptionVisible() {
  390. return caption.isVisible();
  391. }
  392. }
  393. @Override
  394. protected void clearPaintables() {
  395. clear();
  396. }
  397. boolean isDynamicWidth() {
  398. ComponentConnector paintable = ConnectorMap.get(client).getConnector(
  399. this);
  400. return paintable.isUndefinedWidth();
  401. }
  402. boolean isDynamicHeight() {
  403. ComponentConnector paintable = ConnectorMap.get(client).getConnector(
  404. this);
  405. return paintable.isUndefinedHeight();
  406. }
  407. @Override
  408. @SuppressWarnings("unchecked")
  409. protected Iterator<Widget> getWidgetIterator() {
  410. return widgets.iterator();
  411. }
  412. @Override
  413. protected int getTabCount() {
  414. return getWidgetCount();
  415. }
  416. @Override
  417. protected void removeTab(int index) {
  418. StackItem item = getStackItem(index);
  419. remove(item);
  420. }
  421. @Override
  422. protected ComponentConnector getTab(int index) {
  423. if (index < getWidgetCount()) {
  424. Widget w = getStackItem(index);
  425. return ConnectorMap.get(client).getConnector(w);
  426. }
  427. return null;
  428. }
  429. StackItem getStackItem(int index) {
  430. return (StackItem) getWidget(index);
  431. }
  432. }