Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

IAccordion.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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.ICaption;
  16. import com.itmill.toolkit.terminal.gwt.client.ContainerResizedListener;
  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. public void iLayout() {
  101. StackItem item = getSelectedStack();
  102. if (item == null) {
  103. return;
  104. }
  105. if (height != null && height != "") {
  106. // Detach visible widget from document flow for a while to calculate
  107. // used height correctly
  108. Widget w = item.getPaintable();
  109. String originalPositioning = "";
  110. if (w != null) {
  111. originalPositioning = DOM.getStyleAttribute(w.getElement(),
  112. "position");
  113. DOM.setStyleAttribute(w.getElement(), "visibility", "hidden");
  114. DOM.setStyleAttribute(w.getElement(), "position", "absolute");
  115. }
  116. DOM.setStyleAttribute(item.getContainerElement(), "height", "0");
  117. // Calculate target height
  118. super.setHeight(height);
  119. int targetHeight = DOM.getElementPropertyInt(DOM
  120. .getParent(getElement()), "offsetHeight");
  121. super.setHeight("");
  122. // Calculate used height
  123. int usedHeight = getOffsetHeight();
  124. int h = targetHeight - usedHeight;
  125. if (h < 0) {
  126. h = 0;
  127. }
  128. DOM.setStyleAttribute(item.getContainerElement(), "height", h
  129. + "px");
  130. // Put widget back into normal flow
  131. if (w != null) {
  132. DOM.setStyleAttribute(w.getElement(), "position",
  133. originalPositioning);
  134. DOM.setStyleAttribute(w.getElement(), "visibility", "");
  135. }
  136. } else {
  137. DOM.setStyleAttribute(item.getContainerElement(), "height", "");
  138. }
  139. Util.runDescendentsLayout(this);
  140. }
  141. /**
  142. * TODO Caption widget not properly attached
  143. */
  144. protected class StackItem extends ComplexPanel implements ClickListener {
  145. private ICaption caption;
  146. private boolean open = false;
  147. private Element content = DOM.createDiv();
  148. private Element captionNode = DOM.createDiv();
  149. private Paintable paintable;
  150. public StackItem(UIDL tabUidl) {
  151. setElement(DOM.createDiv());
  152. caption = new ICaption(null, client);
  153. caption.addClickListener(this);
  154. super.add(caption, captionNode);
  155. DOM.appendChild(captionNode, caption.getElement());
  156. DOM.appendChild(getElement(), captionNode);
  157. DOM.appendChild(getElement(), content);
  158. setStylePrimaryName(CLASSNAME + "-item");
  159. DOM.setElementProperty(content, "className", CLASSNAME
  160. + "-item-content");
  161. DOM.setElementProperty(captionNode, "className", CLASSNAME
  162. + "-item-caption");
  163. DOM.setStyleAttribute(content, "overflow", "auto");
  164. // Force 'hasLayout' in IE6 (prevents layout problems)
  165. if (BrowserInfo.get().isIE6()) {
  166. DOM.setStyleAttribute(content, "zoom", "1");
  167. }
  168. close();
  169. updateCaption(tabUidl);
  170. }
  171. public Element getContainerElement() {
  172. return content;
  173. }
  174. public Widget getPaintable() {
  175. if (getWidgetCount() > 1) {
  176. return getWidget(1);
  177. } else {
  178. return null;
  179. }
  180. }
  181. public void open() {
  182. open = true;
  183. if (getPaintable() != null) {
  184. remove(getPaintable());
  185. }
  186. DOM.setStyleAttribute(content, "visibility", "");
  187. DOM.setStyleAttribute(content, "position", "");
  188. addStyleDependentName("open");
  189. if (getPaintable() != null) {
  190. add(getPaintable(), content);
  191. }
  192. }
  193. public void close() {
  194. open = false;
  195. DOM.setStyleAttribute(content, "visibility", "hidden");
  196. DOM.setStyleAttribute(content, "position", "absolute");
  197. removeStyleDependentName("open");
  198. }
  199. public boolean isOpen() {
  200. return open;
  201. }
  202. public void setContent(UIDL contentUidl) {
  203. final Paintable newPntbl = client.getPaintable(contentUidl);
  204. if (getPaintable() == null) {
  205. add((Widget) newPntbl, content);
  206. paintables.add(newPntbl);
  207. } else if (getPaintable() != newPntbl) {
  208. client.unregisterPaintable((Paintable) getWidget(1));
  209. paintables.remove(getWidget(1));
  210. remove(1);
  211. add((Widget) newPntbl, content);
  212. paintables.add(newPntbl);
  213. }
  214. paintable = newPntbl;
  215. paintable.updateFromUIDL(contentUidl, client);
  216. }
  217. public void onClick(Widget sender) {
  218. onSelectTab(this);
  219. }
  220. public void updateCaption(UIDL uidl) {
  221. caption.updateCaption(uidl);
  222. }
  223. }
  224. protected void clearPaintables() {
  225. stack.clear();
  226. clear();
  227. }
  228. protected Iterator getPaintableIterator() {
  229. return paintables.iterator();
  230. }
  231. public boolean hasChildComponent(Widget component) {
  232. if (paintables.contains(component)) {
  233. return true;
  234. } else {
  235. return false;
  236. }
  237. }
  238. public void replaceChildComponent(Widget oldComponent, Widget newComponent) {
  239. // TODO Auto-generated method stub
  240. }
  241. public void updateCaption(Paintable component, UIDL uidl) {
  242. for (Iterator iterator = stack.iterator(); iterator.hasNext();) {
  243. StackItem si = (StackItem) iterator.next();
  244. if (si.getPaintable() == component) {
  245. si.updateCaption(uidl);
  246. return;
  247. }
  248. }
  249. }
  250. }