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.

IExpandLayout.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package com.itmill.toolkit.terminal.gwt.client.ui;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import com.google.gwt.user.client.DOM;
  5. import com.google.gwt.user.client.Element;
  6. import com.google.gwt.user.client.ui.Widget;
  7. import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
  8. import com.itmill.toolkit.terminal.gwt.client.ContainerResizedListener;
  9. import com.itmill.toolkit.terminal.gwt.client.Paintable;
  10. import com.itmill.toolkit.terminal.gwt.client.UIDL;
  11. import com.itmill.toolkit.terminal.gwt.client.Util;
  12. /**
  13. * TODO make this work horizontally
  14. *
  15. * @author IT Mill Ltd
  16. */
  17. public class IExpandLayout extends IOrderedLayout implements ContainerResizedListener {
  18. private Widget expandedWidget;
  19. private UIDL expandedWidgetUidl;
  20. public IExpandLayout() {
  21. super(IOrderedLayout.ORIENTATION_VERTICAL);
  22. }
  23. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  24. this.client = client;
  25. // Ensure correct implementation
  26. if (client.updateComponent(this, uidl, false))
  27. return;
  28. String h = uidl.getStringAttribute("height");
  29. setHeight(h);
  30. String w = uidl.getStringAttribute("width");
  31. setWidth(w);
  32. ArrayList uidlWidgets = new ArrayList();
  33. for (Iterator it = uidl.getChildIterator(); it.hasNext();) {
  34. UIDL cellUidl = (UIDL) it.next();
  35. Widget child = client.getWidget(cellUidl.getChildUIDL(0));
  36. uidlWidgets.add(child);
  37. if (cellUidl.hasAttribute("expanded")) {
  38. expandedWidget = child;
  39. expandedWidgetUidl = cellUidl.getChildUIDL(0);
  40. }
  41. }
  42. ArrayList oldWidgets = getPaintables();
  43. Iterator oldIt = oldWidgets.iterator();
  44. Iterator newIt = uidlWidgets.iterator();
  45. Iterator newUidl = uidl.getChildIterator();
  46. Widget oldChild = null;
  47. while (newIt.hasNext()) {
  48. Widget child = (Widget) newIt.next();
  49. UIDL childUidl = ((UIDL) newUidl.next()).getChildUIDL(0);
  50. if (oldChild == null && oldIt.hasNext()) {
  51. // search for next old Paintable which still exists in layout
  52. // and delete others
  53. while (oldIt.hasNext()) {
  54. oldChild = (Widget) oldIt.next();
  55. // now oldChild is an instance of Paintable
  56. if (uidlWidgets.contains(oldChild))
  57. break;
  58. else {
  59. removePaintable((Paintable) oldChild);
  60. oldChild = null;
  61. }
  62. }
  63. }
  64. if (oldChild == null) {
  65. // we are adding components to layout
  66. add(child);
  67. } else if (child == oldChild) {
  68. // child already attached and updated
  69. oldChild = null;
  70. } else if (hasChildComponent(child)) {
  71. // current child has been moved, re-insert before current
  72. // oldChild
  73. // TODO this might be optimized by moving only container element
  74. // to correct position
  75. removeCaption(child);
  76. int index = getWidgetIndex(oldChild);
  77. if (componentToCaption.containsKey(oldChild))
  78. index--;
  79. remove(child);
  80. this.insert(child, index);
  81. } else {
  82. // insert new child before old one
  83. int index = getWidgetIndex(oldChild);
  84. insert(child, index);
  85. }
  86. if (child != expandedWidget)
  87. ((Paintable) child).updateFromUIDL(childUidl, client);
  88. }
  89. // remove possibly remaining old Paintable object which were not updated
  90. while (oldIt.hasNext()) {
  91. oldChild = (Widget) oldIt.next();
  92. Paintable p = (Paintable) oldChild;
  93. if (!uidlWidgets.contains(p))
  94. removePaintable(p);
  95. }
  96. iLayout();
  97. /*
  98. * Expanded widget is updated after layout function so it has its
  99. * container fixed at the moment of updateFromUIDL.
  100. */
  101. ((Paintable) expandedWidget).updateFromUIDL(expandedWidgetUidl, client);
  102. }
  103. public void iLayout() {
  104. // ApplicationConnection.getConsole().log("EL layouting...");
  105. Element expandedElement = DOM.getParent(expandedWidget.getElement());
  106. String origDisplay = DOM.getStyleAttribute(expandedElement, "display");
  107. DOM.setStyleAttribute(expandedElement, "display", "none");
  108. // add temp element to make some measurements
  109. Element meter = createWidgetWrappper();
  110. DOM.setStyleAttribute(meter, "overflow", "hidden");
  111. DOM.setStyleAttribute(meter, "height", "1px");
  112. DOM.appendChild(childContainer, meter);
  113. int usedSpace = DOM.getElementPropertyInt(meter, "offsetTop")
  114. - DOM.getElementPropertyInt(DOM.getFirstChild(childContainer),
  115. "offsetTop");
  116. // ApplicationConnection.getConsole().log("EL h" + getOffsetHeight());
  117. // ApplicationConnection.getConsole().log("EL h" + getOffsetHeight());
  118. int freeSpace = getOffsetHeight() - usedSpace;
  119. DOM.setStyleAttribute(expandedElement,
  120. "height", freeSpace + "px");
  121. DOM.setStyleAttribute(expandedElement, "display", origDisplay);
  122. DOM.removeChild(childContainer, meter);
  123. // TODO save previous size and only propagate if really changed
  124. Util.runAnchestorsLayout(this);
  125. }
  126. }