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.

VGridLayoutPaintable.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.HashSet;
  6. import java.util.Iterator;
  7. import com.google.gwt.core.client.GWT;
  8. import com.google.gwt.event.dom.client.DomEvent.Type;
  9. import com.google.gwt.event.shared.EventHandler;
  10. import com.google.gwt.event.shared.HandlerRegistration;
  11. import com.google.gwt.user.client.Element;
  12. import com.google.gwt.user.client.ui.Widget;
  13. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  14. import com.vaadin.terminal.gwt.client.DirectionalManagedLayout;
  15. import com.vaadin.terminal.gwt.client.EventId;
  16. import com.vaadin.terminal.gwt.client.UIDL;
  17. import com.vaadin.terminal.gwt.client.VCaption;
  18. import com.vaadin.terminal.gwt.client.ConnectorMap;
  19. import com.vaadin.terminal.gwt.client.ComponentConnector;
  20. import com.vaadin.terminal.gwt.client.ui.VGridLayout.Cell;
  21. import com.vaadin.terminal.gwt.client.ui.layout.VLayoutSlot;
  22. public class VGridLayoutPaintable extends AbstractComponentContainerConnector
  23. implements DirectionalManagedLayout {
  24. private LayoutClickEventHandler clickEventHandler = new LayoutClickEventHandler(
  25. this, EventId.LAYOUT_CLICK) {
  26. @Override
  27. protected ComponentConnector getChildComponent(Element element) {
  28. return getWidget().getComponent(element);
  29. }
  30. @Override
  31. protected <H extends EventHandler> HandlerRegistration registerHandler(
  32. H handler, Type<H> type) {
  33. return getWidget().addDomHandler(handler, type);
  34. }
  35. };
  36. @Override
  37. public void init() {
  38. getLayoutManager().registerDependency(this,
  39. getWidget().spacingMeasureElement);
  40. }
  41. @Override
  42. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  43. VGridLayout layout = getWidget();
  44. layout.client = client;
  45. super.updateFromUIDL(uidl, client);
  46. if (!isRealUpdate(uidl)) {
  47. return;
  48. }
  49. clickEventHandler.handleEventHandlerRegistration(client);
  50. int cols = uidl.getIntAttribute("w");
  51. int rows = uidl.getIntAttribute("h");
  52. layout.columnWidths = new int[cols];
  53. layout.rowHeights = new int[rows];
  54. if (layout.cells == null) {
  55. layout.cells = new Cell[cols][rows];
  56. } else if (layout.cells.length != cols
  57. || layout.cells[0].length != rows) {
  58. Cell[][] newCells = new Cell[cols][rows];
  59. for (int i = 0; i < layout.cells.length; i++) {
  60. for (int j = 0; j < layout.cells[i].length; j++) {
  61. if (i < cols && j < rows) {
  62. newCells[i][j] = layout.cells[i][j];
  63. }
  64. }
  65. }
  66. layout.cells = newCells;
  67. }
  68. final int[] alignments = uidl.getIntArrayAttribute("alignments");
  69. int alignmentIndex = 0;
  70. HashSet<Widget> nonRenderedWidgets = new HashSet<Widget>(
  71. layout.widgetToCell.keySet());
  72. for (final Iterator<?> i = uidl.getChildIterator(); i.hasNext();) {
  73. final UIDL r = (UIDL) i.next();
  74. if ("gr".equals(r.getTag())) {
  75. for (final Iterator<?> j = r.getChildIterator(); j.hasNext();) {
  76. final UIDL c = (UIDL) j.next();
  77. if ("gc".equals(c.getTag())) {
  78. Cell cell = layout.getCell(c);
  79. if (cell.hasContent()) {
  80. cell.setAlignment(new AlignmentInfo(
  81. alignments[alignmentIndex++]));
  82. nonRenderedWidgets.remove(cell.slot.getWidget());
  83. }
  84. }
  85. }
  86. }
  87. }
  88. layout.colExpandRatioArray = uidl.getIntArrayAttribute("colExpand");
  89. layout.rowExpandRatioArray = uidl.getIntArrayAttribute("rowExpand");
  90. // clean non rendered components
  91. for (Widget w : nonRenderedWidgets) {
  92. Cell cell = layout.widgetToCell.remove(w);
  93. cell.slot.setCaption(null);
  94. if (w.getParent() == layout) {
  95. w.removeFromParent();
  96. ConnectorMap paintableMap = ConnectorMap.get(client);
  97. paintableMap.unregisterConnector(paintableMap.getConnector(w));
  98. }
  99. cell.slot.getWrapperElement().removeFromParent();
  100. }
  101. int bitMask = uidl.getIntAttribute("margins");
  102. layout.updateMarginStyleNames(new VMarginInfo(bitMask));
  103. layout.updateSpacingStyleName(uidl.getBooleanAttribute("spacing"));
  104. getLayoutManager().setNeedsUpdate(this);
  105. }
  106. public void updateCaption(ComponentConnector paintable, UIDL uidl) {
  107. VGridLayout layout = getWidget();
  108. if (VCaption.isNeeded(uidl, paintable.getState())) {
  109. Cell cell = layout.widgetToCell.get(paintable
  110. .getWidget());
  111. VLayoutSlot layoutSlot = cell.slot;
  112. VCaption caption = layoutSlot.getCaption();
  113. if (caption == null) {
  114. caption = new VCaption(paintable, getConnection());
  115. Widget widget = paintable.getWidget();
  116. layout.setCaption(widget, caption);
  117. }
  118. caption.updateCaption(uidl);
  119. } else {
  120. layout.setCaption(paintable.getWidget(), null);
  121. }
  122. }
  123. @Override
  124. public VGridLayout getWidget() {
  125. return (VGridLayout) super.getWidget();
  126. }
  127. @Override
  128. protected Widget createWidget() {
  129. return GWT.create(VGridLayout.class);
  130. }
  131. public void layoutVertically() {
  132. getWidget().updateHeight();
  133. }
  134. public void layoutHorizontally() {
  135. getWidget().updateWidth();
  136. }
  137. }