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.

IAccordion.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package com.itmill.toolkit.terminal.gwt.client.ui;
  2. import java.util.ArrayList;
  3. import java.util.HashSet;
  4. import java.util.Iterator;
  5. import java.util.Set;
  6. import com.google.gwt.user.client.Command;
  7. import com.google.gwt.user.client.DOM;
  8. import com.google.gwt.user.client.DeferredCommand;
  9. import com.google.gwt.user.client.Element;
  10. import com.google.gwt.user.client.ui.ClickListener;
  11. import com.google.gwt.user.client.ui.ComplexPanel;
  12. import com.google.gwt.user.client.ui.Widget;
  13. import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
  14. import com.itmill.toolkit.terminal.gwt.client.BrowserInfo;
  15. import com.itmill.toolkit.terminal.gwt.client.ContainerResizedListener;
  16. import com.itmill.toolkit.terminal.gwt.client.ICaption;
  17. import com.itmill.toolkit.terminal.gwt.client.Paintable;
  18. import com.itmill.toolkit.terminal.gwt.client.UIDL;
  19. import com.itmill.toolkit.terminal.gwt.client.Util;
  20. public class IAccordion extends ITabsheetBase implements
  21. ContainerResizedListener {
  22. public static final String CLASSNAME = "i-accordion";
  23. private ArrayList stack = new ArrayList();
  24. private Set paintables = new HashSet();
  25. private String height;
  26. public IAccordion() {
  27. super(CLASSNAME);
  28. // IE6 needs this to calculate offsetHeight correctly
  29. if (BrowserInfo.get().isIE6()) {
  30. DOM.setStyleAttribute(getElement(), "zoom", "1");
  31. }
  32. }
  33. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  34. super.updateFromUIDL(uidl, client);
  35. iLayout();
  36. }
  37. private StackItem getSelectedStack() {
  38. if (stack.size() == 0) {
  39. return null;
  40. }
  41. return (StackItem) stack.get(activeTabIndex);
  42. }
  43. protected void renderTab(UIDL tabUidl, int index, boolean selected) {
  44. StackItem item;
  45. if (stack.size() <= index) {
  46. item = new StackItem(tabUidl);
  47. if (stack.size() == 0) {
  48. item.addStyleDependentName("first");
  49. }
  50. stack.add(item);
  51. add(item, getElement());
  52. } else {
  53. item = (StackItem) stack.get(index);
  54. item.updateCaption(tabUidl);
  55. }
  56. if (selected) {
  57. item.open();
  58. item.setContent(tabUidl.getChildUIDL(0));
  59. } else if (tabUidl.getChildCount() > 0) {
  60. item.setContent(tabUidl.getChildUIDL(0));
  61. }
  62. }
  63. protected void selectTab(final int index, final UIDL contentUidl) {
  64. StackItem item = (StackItem) stack.get(index);
  65. if (index != activeTabIndex) {
  66. activeTabIndex = index;
  67. item.open();
  68. iLayout();
  69. }
  70. item.setContent(contentUidl);
  71. }
  72. public void onSelectTab(StackItem item) {
  73. final int index = stack.indexOf(item);
  74. if (index != activeTabIndex && !disabled && !readonly
  75. && !disabledTabKeys.contains(tabKeys.get(index))) {
  76. if (getSelectedStack() != null) {
  77. getSelectedStack().close();
  78. }
  79. addStyleDependentName("loading");
  80. // run updating variables in deferred command to bypass some FF
  81. // optimization issues
  82. DeferredCommand.addCommand(new Command() {
  83. public void execute() {
  84. client.updateVariable(id, "selected", ""
  85. + tabKeys.get(index), true);
  86. }
  87. });
  88. }
  89. }
  90. public void setWidth(String width) {
  91. if (width.equals("100%")) {
  92. super.setWidth("");
  93. } else {
  94. super.setWidth(width);
  95. }
  96. }
  97. public void setHeight(String height) {
  98. this.height = height;
  99. }
  100. private void iLayout() {
  101. iLayout(-1, -1);
  102. }
  103. public void iLayout(int availableWidth, int availableHeight) {
  104. StackItem item = getSelectedStack();
  105. if (item == null) {
  106. return;
  107. }
  108. if (height != null && !height.equals("")) {
  109. int usedPixels = 0;
  110. for (Iterator iterator = stack.iterator(); iterator.hasNext();) {
  111. StackItem si = (StackItem) iterator.next();
  112. if (si != item) {
  113. usedPixels += si.getOffsetHeight();
  114. }
  115. }
  116. // Calculate target height
  117. super.setHeight(height);
  118. int offsetHeight = getOffsetHeight();
  119. int spaceForOpenItem = offsetHeight - usedPixels;
  120. if (spaceForOpenItem > 0) {
  121. item.setHeight(spaceForOpenItem + "px");
  122. }
  123. } else {
  124. super.setHeight("");
  125. item.setHeight("");
  126. }
  127. Util.runDescendentsLayout(item);
  128. }
  129. /**
  130. *
  131. */
  132. protected class StackItem extends ComplexPanel implements ClickListener {
  133. public void setHeight(String height) {
  134. super.setHeight(height);
  135. if (!"".equals(height)) {
  136. int offsetHeight = getOffsetHeight();
  137. int captionHeight = DOM.getElementPropertyInt(captionNode,
  138. "offsetHeight");
  139. int contentSpace = offsetHeight - captionHeight;
  140. if (contentSpace > 0) {
  141. DOM.setStyleAttribute(content, "height", contentSpace
  142. + "px");
  143. }
  144. } else {
  145. DOM.setStyleAttribute(content, "height", "");
  146. }
  147. }
  148. private ICaption caption;
  149. private boolean open = false;
  150. private Element content = DOM.createDiv();
  151. private Element captionNode = DOM.createDiv();
  152. private Paintable paintable;
  153. public StackItem(UIDL tabUidl) {
  154. setElement(DOM.createDiv());
  155. caption = new ICaption(null, client);
  156. caption.addClickListener(this);
  157. super.add(caption, captionNode);
  158. DOM.appendChild(captionNode, caption.getElement());
  159. DOM.appendChild(getElement(), captionNode);
  160. DOM.appendChild(getElement(), content);
  161. setStylePrimaryName(CLASSNAME + "-item");
  162. DOM.setElementProperty(content, "className", CLASSNAME
  163. + "-item-content");
  164. DOM.setElementProperty(captionNode, "className", CLASSNAME
  165. + "-item-caption");
  166. DOM.setStyleAttribute(content, "overflow", "auto");
  167. // Force 'hasLayout' in IE6 (prevents layout problems)
  168. if (BrowserInfo.get().isIE6()) {
  169. DOM.setStyleAttribute(content, "zoom", "1");
  170. DOM.setStyleAttribute(getElement(), "overflow", "hidden");
  171. }
  172. close();
  173. updateCaption(tabUidl);
  174. }
  175. public Element getContainerElement() {
  176. return content;
  177. }
  178. public Widget getPaintable() {
  179. if (getWidgetCount() > 1) {
  180. return getWidget(1);
  181. } else {
  182. return null;
  183. }
  184. }
  185. public void open() {
  186. open = true;
  187. if (getPaintable() != null) {
  188. remove(getPaintable());
  189. }
  190. DOM.setStyleAttribute(content, "visibility", "");
  191. DOM.setStyleAttribute(content, "position", "");
  192. DOM.setStyleAttribute(content, "top", "");
  193. addStyleDependentName("open");
  194. if (getPaintable() != null) {
  195. add(getPaintable(), content);
  196. }
  197. }
  198. public void close() {
  199. open = false;
  200. DOM.setStyleAttribute(content, "visibility", "hidden");
  201. DOM.setStyleAttribute(content, "position", "absolute");
  202. DOM.setStyleAttribute(content, "top", "0");
  203. removeStyleDependentName("open");
  204. setHeight(""); // only open StackItem may contain height
  205. }
  206. public boolean isOpen() {
  207. return open;
  208. }
  209. public void setContent(UIDL contentUidl) {
  210. final Paintable newPntbl = client.getPaintable(contentUidl);
  211. if (getPaintable() == null) {
  212. add((Widget) newPntbl, content);
  213. paintables.add(newPntbl);
  214. } else if (getPaintable() != newPntbl) {
  215. client.unregisterPaintable((Paintable) getWidget(1));
  216. paintables.remove(getWidget(1));
  217. remove(1);
  218. add((Widget) newPntbl, content);
  219. paintables.add(newPntbl);
  220. }
  221. paintable = newPntbl;
  222. paintable.updateFromUIDL(contentUidl, client);
  223. }
  224. public void onClick(Widget sender) {
  225. onSelectTab(this);
  226. }
  227. public void updateCaption(UIDL uidl) {
  228. caption.updateCaption(uidl);
  229. }
  230. }
  231. protected void clearPaintables() {
  232. stack.clear();
  233. clear();
  234. }
  235. protected Iterator getPaintableIterator() {
  236. return paintables.iterator();
  237. }
  238. public boolean hasChildComponent(Widget component) {
  239. if (paintables.contains(component)) {
  240. return true;
  241. } else {
  242. return false;
  243. }
  244. }
  245. public void replaceChildComponent(Widget oldComponent, Widget newComponent) {
  246. // TODO Auto-generated method stub
  247. }
  248. public void updateCaption(Paintable component, UIDL uidl) {
  249. for (Iterator iterator = stack.iterator(); iterator.hasNext();) {
  250. StackItem si = (StackItem) iterator.next();
  251. if (si.getPaintable() == component) {
  252. si.updateCaption(uidl);
  253. return;
  254. }
  255. }
  256. }
  257. public boolean childComponentSizesUpdated() {
  258. // TODO Auto-generated method stub
  259. return false;
  260. }
  261. }