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.

VGridLayout.java 40KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.HashSet;
  8. import java.util.Iterator;
  9. import java.util.LinkedList;
  10. import java.util.List;
  11. import java.util.Set;
  12. import com.google.gwt.dom.client.DivElement;
  13. import com.google.gwt.dom.client.Document;
  14. import com.google.gwt.event.dom.client.DomEvent.Type;
  15. import com.google.gwt.event.shared.EventHandler;
  16. import com.google.gwt.event.shared.HandlerRegistration;
  17. import com.google.gwt.user.client.Element;
  18. import com.google.gwt.user.client.ui.AbsolutePanel;
  19. import com.google.gwt.user.client.ui.SimplePanel;
  20. import com.google.gwt.user.client.ui.Widget;
  21. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  22. import com.vaadin.terminal.gwt.client.Container;
  23. import com.vaadin.terminal.gwt.client.EventId;
  24. import com.vaadin.terminal.gwt.client.RenderSpace;
  25. import com.vaadin.terminal.gwt.client.StyleConstants;
  26. import com.vaadin.terminal.gwt.client.UIDL;
  27. import com.vaadin.terminal.gwt.client.Util;
  28. import com.vaadin.terminal.gwt.client.VPaintableMap;
  29. import com.vaadin.terminal.gwt.client.VPaintableWidget;
  30. import com.vaadin.terminal.gwt.client.ui.layout.CellBasedLayout;
  31. import com.vaadin.terminal.gwt.client.ui.layout.ChildComponentContainer;
  32. public class VGridLayout extends SimplePanel implements VPaintableWidget,
  33. Container {
  34. public static final String CLASSNAME = "v-gridlayout";
  35. private DivElement margin = Document.get().createDivElement();
  36. private final AbsolutePanel canvas = new AbsolutePanel();
  37. private ApplicationConnection client;
  38. protected HashMap<Widget, ChildComponentContainer> widgetToComponentContainer = new HashMap<Widget, ChildComponentContainer>();
  39. private HashMap<Widget, Cell> widgetToCell = new HashMap<Widget, Cell>();
  40. private int spacingPixelsHorizontal;
  41. private int spacingPixelsVertical;
  42. private int[] columnWidths;
  43. private int[] rowHeights;
  44. private String height;
  45. private String width;
  46. private int[] colExpandRatioArray;
  47. private int[] rowExpandRatioArray;
  48. private int[] minColumnWidths;
  49. private int[] minRowHeights;
  50. private boolean rendering;
  51. private HashMap<Widget, ChildComponentContainer> nonRenderedWidgets;
  52. private boolean sizeChangedDuringRendering = false;
  53. private LayoutClickEventHandler clickEventHandler = new LayoutClickEventHandler(
  54. this, EventId.LAYOUT_CLICK) {
  55. @Override
  56. protected VPaintableWidget getChildComponent(Element element) {
  57. return getComponent(element);
  58. }
  59. @Override
  60. protected <H extends EventHandler> HandlerRegistration registerHandler(
  61. H handler, Type<H> type) {
  62. return addDomHandler(handler, type);
  63. }
  64. };
  65. public VGridLayout() {
  66. super();
  67. getElement().appendChild(margin);
  68. setStyleName(CLASSNAME);
  69. setWidget(canvas);
  70. }
  71. @Override
  72. protected Element getContainerElement() {
  73. return margin.cast();
  74. }
  75. /**
  76. * Returns the column widths measured in pixels
  77. *
  78. * @return
  79. */
  80. protected int[] getColumnWidths() {
  81. return columnWidths;
  82. }
  83. /**
  84. * Returns the row heights measured in pixels
  85. *
  86. * @return
  87. */
  88. protected int[] getRowHeights() {
  89. return rowHeights;
  90. }
  91. /**
  92. * Returns the spacing between the cells horizontally in pixels
  93. *
  94. * @return
  95. */
  96. protected int getHorizontalSpacing() {
  97. return spacingPixelsHorizontal;
  98. }
  99. /**
  100. * Returns the spacing between the cells vertically in pixels
  101. *
  102. * @return
  103. */
  104. protected int getVerticalSpacing() {
  105. return spacingPixelsVertical;
  106. }
  107. @SuppressWarnings("unchecked")
  108. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  109. rendering = true;
  110. this.client = client;
  111. if (client.updateComponent(this, uidl, true)) {
  112. rendering = false;
  113. return;
  114. }
  115. clickEventHandler.handleEventHandlerRegistration(client);
  116. canvas.setWidth("0px");
  117. handleMargins(uidl);
  118. detectSpacing(uidl);
  119. int cols = uidl.getIntAttribute("w");
  120. int rows = uidl.getIntAttribute("h");
  121. columnWidths = new int[cols];
  122. rowHeights = new int[rows];
  123. if (cells == null) {
  124. cells = new Cell[cols][rows];
  125. } else if (cells.length != cols || cells[0].length != rows) {
  126. Cell[][] newCells = new Cell[cols][rows];
  127. for (int i = 0; i < cells.length; i++) {
  128. for (int j = 0; j < cells[i].length; j++) {
  129. if (i < cols && j < rows) {
  130. newCells[i][j] = cells[i][j];
  131. }
  132. }
  133. }
  134. cells = newCells;
  135. }
  136. nonRenderedWidgets = (HashMap<Widget, ChildComponentContainer>) widgetToComponentContainer
  137. .clone();
  138. final int[] alignments = uidl.getIntArrayAttribute("alignments");
  139. int alignmentIndex = 0;
  140. LinkedList<Cell> pendingCells = new LinkedList<Cell>();
  141. LinkedList<Cell> relativeHeighted = new LinkedList<Cell>();
  142. for (final Iterator<?> i = uidl.getChildIterator(); i.hasNext();) {
  143. final UIDL r = (UIDL) i.next();
  144. if ("gr".equals(r.getTag())) {
  145. for (final Iterator<?> j = r.getChildIterator(); j.hasNext();) {
  146. final UIDL c = (UIDL) j.next();
  147. if ("gc".equals(c.getTag())) {
  148. Cell cell = getCell(c);
  149. if (cell.hasContent()) {
  150. boolean rendered = cell.renderIfNoRelativeWidth();
  151. cell.alignment = alignments[alignmentIndex++];
  152. if (!rendered) {
  153. pendingCells.add(cell);
  154. }
  155. if (cell.colspan > 1) {
  156. storeColSpannedCell(cell);
  157. } else if (rendered) {
  158. // strore non-colspanned widths to columnWidth
  159. // array
  160. if (columnWidths[cell.col] < cell.getWidth()) {
  161. columnWidths[cell.col] = cell.getWidth();
  162. }
  163. }
  164. if (cell.hasRelativeHeight()) {
  165. relativeHeighted.add(cell);
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }
  172. colExpandRatioArray = uidl.getIntArrayAttribute("colExpand");
  173. rowExpandRatioArray = uidl.getIntArrayAttribute("rowExpand");
  174. distributeColSpanWidths();
  175. minColumnWidths = cloneArray(columnWidths);
  176. expandColumns();
  177. renderRemainingComponentsWithNoRelativeHeight(pendingCells);
  178. detectRowHeights();
  179. expandRows();
  180. renderRemainingComponents(pendingCells);
  181. for (Cell cell : relativeHeighted) {
  182. // rendering done above so cell.cc should not be null
  183. Widget widget2 = cell.cc.getWidget();
  184. client.handleComponentRelativeSize(widget2);
  185. cell.cc.updateWidgetSize();
  186. }
  187. layoutCells();
  188. // clean non rendered components
  189. for (Widget w : nonRenderedWidgets.keySet()) {
  190. ChildComponentContainer childComponentContainer = widgetToComponentContainer
  191. .get(w);
  192. widgetToCell.remove(w);
  193. widgetToComponentContainer.remove(w);
  194. childComponentContainer.removeFromParent();
  195. VPaintableMap paintableMap = VPaintableMap.get(client);
  196. paintableMap.unregisterPaintable(paintableMap.getPaintable(w));
  197. }
  198. nonRenderedWidgets = null;
  199. rendering = false;
  200. sizeChangedDuringRendering = false;
  201. }
  202. private static int[] cloneArray(int[] toBeCloned) {
  203. int[] clone = new int[toBeCloned.length];
  204. for (int i = 0; i < clone.length; i++) {
  205. clone[i] = toBeCloned[i] * 1;
  206. }
  207. return clone;
  208. }
  209. private void expandRows() {
  210. if (!"".equals(height)) {
  211. int usedSpace = minRowHeights[0];
  212. for (int i = 1; i < minRowHeights.length; i++) {
  213. usedSpace += spacingPixelsVertical + minRowHeights[i];
  214. }
  215. int availableSpace = getOffsetHeight() - marginTopAndBottom;
  216. int excessSpace = availableSpace - usedSpace;
  217. int distributed = 0;
  218. if (excessSpace > 0) {
  219. for (int i = 0; i < rowHeights.length; i++) {
  220. int ew = excessSpace * rowExpandRatioArray[i] / 1000;
  221. rowHeights[i] = minRowHeights[i] + ew;
  222. distributed += ew;
  223. }
  224. excessSpace -= distributed;
  225. int c = 0;
  226. while (excessSpace > 0) {
  227. rowHeights[c % rowHeights.length]++;
  228. excessSpace--;
  229. c++;
  230. }
  231. }
  232. }
  233. }
  234. @Override
  235. public void setHeight(String height) {
  236. super.setHeight(height);
  237. if (!height.equals(this.height)) {
  238. this.height = height;
  239. if (rendering) {
  240. sizeChangedDuringRendering = true;
  241. } else {
  242. expandRows();
  243. layoutCells();
  244. for (Widget w : widgetToCell.keySet()) {
  245. client.handleComponentRelativeSize(w);
  246. }
  247. }
  248. }
  249. }
  250. @Override
  251. public void setWidth(String width) {
  252. super.setWidth(width);
  253. if (!width.equals(this.width)) {
  254. this.width = width;
  255. if (rendering) {
  256. sizeChangedDuringRendering = true;
  257. } else {
  258. int[] oldWidths = cloneArray(columnWidths);
  259. expandColumns();
  260. boolean heightChanged = false;
  261. HashSet<Integer> dirtyRows = null;
  262. for (int i = 0; i < oldWidths.length; i++) {
  263. if (columnWidths[i] != oldWidths[i]) {
  264. Cell[] column = cells[i];
  265. for (int j = 0; j < column.length; j++) {
  266. Cell c = column[j];
  267. if (c != null && c.cc != null
  268. && c.widthCanAffectHeight()) {
  269. c.cc.setContainerSize(c.getAvailableWidth(),
  270. c.getAvailableHeight());
  271. client.handleComponentRelativeSize(c.cc
  272. .getWidget());
  273. c.cc.updateWidgetSize();
  274. int newHeight = c.getHeight();
  275. if (columnWidths[i] < oldWidths[i]
  276. && newHeight > minRowHeights[j]
  277. && c.rowspan == 1) {
  278. /*
  279. * The width of this column was reduced and
  280. * this affected the height. The height is
  281. * now greater than the previously
  282. * calculated minHeight for the row.
  283. */
  284. minRowHeights[j] = newHeight;
  285. if (newHeight > rowHeights[j]) {
  286. /*
  287. * The new height is greater than the
  288. * previously calculated rowHeight -> we
  289. * need to recalculate heights later on
  290. */
  291. rowHeights[j] = newHeight;
  292. heightChanged = true;
  293. }
  294. } else if (newHeight < minRowHeights[j]) {
  295. /*
  296. * The new height of the component is less
  297. * than the previously calculated min row
  298. * height. The min row height may be
  299. * affected and must thus be recalculated
  300. */
  301. if (dirtyRows == null) {
  302. dirtyRows = new HashSet<Integer>();
  303. }
  304. dirtyRows.add(j);
  305. }
  306. }
  307. }
  308. }
  309. }
  310. if (dirtyRows != null) {
  311. /* flag indicating that there is a potential row shrinking */
  312. boolean rowMayShrink = false;
  313. for (Integer rowIndex : dirtyRows) {
  314. int oldMinimum = minRowHeights[rowIndex];
  315. int newMinimum = 0;
  316. for (int colIndex = 0; colIndex < columnWidths.length; colIndex++) {
  317. Cell cell = cells[colIndex][rowIndex];
  318. if (cell != null && !cell.hasRelativeHeight()
  319. && cell.getHeight() > newMinimum) {
  320. newMinimum = cell.getHeight();
  321. }
  322. }
  323. if (newMinimum < oldMinimum) {
  324. minRowHeights[rowIndex] = rowHeights[rowIndex] = newMinimum;
  325. rowMayShrink = true;
  326. }
  327. }
  328. if (rowMayShrink) {
  329. distributeRowSpanHeights();
  330. minRowHeights = cloneArray(rowHeights);
  331. heightChanged = true;
  332. }
  333. }
  334. layoutCells();
  335. for (Widget w : widgetToCell.keySet()) {
  336. client.handleComponentRelativeSize(w);
  337. }
  338. if (heightChanged && "".equals(height)) {
  339. Util.notifyParentOfSizeChange(this, false);
  340. }
  341. }
  342. }
  343. }
  344. private void expandColumns() {
  345. if (!"".equals(width)) {
  346. int usedSpace = minColumnWidths[0];
  347. for (int i = 1; i < minColumnWidths.length; i++) {
  348. usedSpace += spacingPixelsHorizontal + minColumnWidths[i];
  349. }
  350. canvas.setWidth("");
  351. int availableSpace = canvas.getOffsetWidth();
  352. int excessSpace = availableSpace - usedSpace;
  353. int distributed = 0;
  354. if (excessSpace > 0) {
  355. for (int i = 0; i < columnWidths.length; i++) {
  356. int ew = excessSpace * colExpandRatioArray[i] / 1000;
  357. columnWidths[i] = minColumnWidths[i] + ew;
  358. distributed += ew;
  359. }
  360. excessSpace -= distributed;
  361. int c = 0;
  362. while (excessSpace > 0) {
  363. columnWidths[c % columnWidths.length]++;
  364. excessSpace--;
  365. c++;
  366. }
  367. }
  368. }
  369. }
  370. private void layoutCells() {
  371. int x = 0;
  372. int y = 0;
  373. for (int i = 0; i < cells.length; i++) {
  374. y = 0;
  375. for (int j = 0; j < cells[i].length; j++) {
  376. Cell cell = cells[i][j];
  377. if (cell != null) {
  378. cell.layout(x, y);
  379. }
  380. y += rowHeights[j] + spacingPixelsVertical;
  381. }
  382. x += columnWidths[i] + spacingPixelsHorizontal;
  383. }
  384. if (isUndefinedWidth()) {
  385. canvas.setWidth((x - spacingPixelsHorizontal) + "px");
  386. } else {
  387. // main element defines width
  388. canvas.setWidth("");
  389. }
  390. int canvasHeight;
  391. if (isUndefinedHeight()) {
  392. canvasHeight = y - spacingPixelsVertical;
  393. } else {
  394. canvasHeight = getOffsetHeight() - marginTopAndBottom;
  395. if (canvasHeight < 0) {
  396. canvasHeight = 0;
  397. }
  398. }
  399. canvas.setHeight(canvasHeight + "px");
  400. }
  401. private boolean isUndefinedHeight() {
  402. return "".equals(height);
  403. }
  404. private boolean isUndefinedWidth() {
  405. return "".equals(width);
  406. }
  407. private void renderRemainingComponents(LinkedList<Cell> pendingCells) {
  408. for (Cell cell : pendingCells) {
  409. cell.render();
  410. }
  411. }
  412. private void detectRowHeights() {
  413. // collect min rowheight from non-rowspanned cells
  414. for (int i = 0; i < cells.length; i++) {
  415. for (int j = 0; j < cells[i].length; j++) {
  416. Cell cell = cells[i][j];
  417. if (cell != null) {
  418. /*
  419. * Setting fixing container width may in some situations
  420. * affect height. Example: Label with wrapping text without
  421. * or with relative width.
  422. */
  423. if (cell.cc != null && cell.widthCanAffectHeight()) {
  424. cell.cc.setWidth(cell.getAvailableWidth() + "px");
  425. cell.cc.updateWidgetSize();
  426. }
  427. if (cell.rowspan == 1) {
  428. if (!cell.hasRelativeHeight()
  429. && rowHeights[j] < cell.getHeight()) {
  430. rowHeights[j] = cell.getHeight();
  431. }
  432. } else {
  433. storeRowSpannedCell(cell);
  434. }
  435. }
  436. }
  437. }
  438. distributeRowSpanHeights();
  439. minRowHeights = cloneArray(rowHeights);
  440. }
  441. private void storeRowSpannedCell(Cell cell) {
  442. SpanList l = null;
  443. for (SpanList list : rowSpans) {
  444. if (list.span < cell.rowspan) {
  445. continue;
  446. } else {
  447. // insert before this
  448. l = list;
  449. break;
  450. }
  451. }
  452. if (l == null) {
  453. l = new SpanList(cell.rowspan);
  454. rowSpans.add(l);
  455. } else if (l.span != cell.rowspan) {
  456. SpanList newL = new SpanList(cell.rowspan);
  457. rowSpans.add(rowSpans.indexOf(l), newL);
  458. l = newL;
  459. }
  460. l.cells.add(cell);
  461. }
  462. private void renderRemainingComponentsWithNoRelativeHeight(
  463. LinkedList<Cell> pendingCells) {
  464. for (Iterator<Cell> iterator = pendingCells.iterator(); iterator
  465. .hasNext();) {
  466. Cell cell = iterator.next();
  467. if (!cell.hasRelativeHeight()) {
  468. cell.render();
  469. iterator.remove();
  470. }
  471. }
  472. }
  473. /**
  474. * Iterates colspanned cells, ensures cols have enough space to accommodate
  475. * them
  476. */
  477. private void distributeColSpanWidths() {
  478. for (SpanList list : colSpans) {
  479. for (Cell cell : list.cells) {
  480. // cells with relative content may return non 0 here if on
  481. // subsequent renders
  482. int width = cell.hasRelativeWidth() ? 0 : cell.getWidth();
  483. distributeSpanSize(columnWidths, cell.col, cell.colspan,
  484. spacingPixelsHorizontal, width, colExpandRatioArray);
  485. }
  486. }
  487. }
  488. /**
  489. * Iterates rowspanned cells, ensures rows have enough space to accommodate
  490. * them
  491. */
  492. private void distributeRowSpanHeights() {
  493. for (SpanList list : rowSpans) {
  494. for (Cell cell : list.cells) {
  495. // cells with relative content may return non 0 here if on
  496. // subsequent renders
  497. int height = cell.hasRelativeHeight() ? 0 : cell.getHeight();
  498. distributeSpanSize(rowHeights, cell.row, cell.rowspan,
  499. spacingPixelsVertical, height, rowExpandRatioArray);
  500. }
  501. }
  502. }
  503. private static void distributeSpanSize(int[] dimensions,
  504. int spanStartIndex, int spanSize, int spacingSize, int size,
  505. int[] expansionRatios) {
  506. int allocated = dimensions[spanStartIndex];
  507. for (int i = 1; i < spanSize; i++) {
  508. allocated += spacingSize + dimensions[spanStartIndex + i];
  509. }
  510. if (allocated < size) {
  511. // dimensions needs to be expanded due spanned cell
  512. int neededExtraSpace = size - allocated;
  513. int allocatedExtraSpace = 0;
  514. // Divide space according to expansion ratios if any span has a
  515. // ratio
  516. int totalExpansion = 0;
  517. for (int i = 0; i < spanSize; i++) {
  518. int itemIndex = spanStartIndex + i;
  519. totalExpansion += expansionRatios[itemIndex];
  520. }
  521. for (int i = 0; i < spanSize; i++) {
  522. int itemIndex = spanStartIndex + i;
  523. int expansion;
  524. if (totalExpansion == 0) {
  525. // Divide equally among all cells if there are no
  526. // expansion ratios
  527. expansion = neededExtraSpace / spanSize;
  528. } else {
  529. expansion = neededExtraSpace * expansionRatios[itemIndex]
  530. / totalExpansion;
  531. }
  532. dimensions[itemIndex] += expansion;
  533. allocatedExtraSpace += expansion;
  534. }
  535. // We might still miss a couple of pixels because of
  536. // rounding errors...
  537. if (neededExtraSpace > allocatedExtraSpace) {
  538. for (int i = 0; i < spanSize; i++) {
  539. // Add one pixel to every cell until we have
  540. // compensated for any rounding error
  541. int itemIndex = spanStartIndex + i;
  542. dimensions[itemIndex] += 1;
  543. allocatedExtraSpace += 1;
  544. if (neededExtraSpace == allocatedExtraSpace) {
  545. break;
  546. }
  547. }
  548. }
  549. }
  550. }
  551. private LinkedList<SpanList> colSpans = new LinkedList<SpanList>();
  552. private LinkedList<SpanList> rowSpans = new LinkedList<SpanList>();
  553. private int marginTopAndBottom;
  554. private class SpanList {
  555. final int span;
  556. List<Cell> cells = new LinkedList<Cell>();
  557. public SpanList(int span) {
  558. this.span = span;
  559. }
  560. }
  561. private void storeColSpannedCell(Cell cell) {
  562. SpanList l = null;
  563. for (SpanList list : colSpans) {
  564. if (list.span < cell.colspan) {
  565. continue;
  566. } else {
  567. // insert before this
  568. l = list;
  569. break;
  570. }
  571. }
  572. if (l == null) {
  573. l = new SpanList(cell.colspan);
  574. colSpans.add(l);
  575. } else if (l.span != cell.colspan) {
  576. SpanList newL = new SpanList(cell.colspan);
  577. colSpans.add(colSpans.indexOf(l), newL);
  578. l = newL;
  579. }
  580. l.cells.add(cell);
  581. }
  582. private void detectSpacing(UIDL uidl) {
  583. DivElement spacingmeter = Document.get().createDivElement();
  584. spacingmeter.setClassName(CLASSNAME + "-" + "spacing-"
  585. + (uidl.getBooleanAttribute("spacing") ? "on" : "off"));
  586. spacingmeter.getStyle().setProperty("width", "0");
  587. spacingmeter.getStyle().setProperty("height", "0");
  588. canvas.getElement().appendChild(spacingmeter);
  589. spacingPixelsHorizontal = spacingmeter.getOffsetWidth();
  590. spacingPixelsVertical = spacingmeter.getOffsetHeight();
  591. canvas.getElement().removeChild(spacingmeter);
  592. }
  593. private void handleMargins(UIDL uidl) {
  594. final VMarginInfo margins = new VMarginInfo(
  595. uidl.getIntAttribute("margins"));
  596. String styles = CLASSNAME + "-margin";
  597. if (margins.hasTop()) {
  598. styles += " " + CLASSNAME + "-" + StyleConstants.MARGIN_TOP;
  599. }
  600. if (margins.hasRight()) {
  601. styles += " " + CLASSNAME + "-" + StyleConstants.MARGIN_RIGHT;
  602. }
  603. if (margins.hasBottom()) {
  604. styles += " " + CLASSNAME + "-" + StyleConstants.MARGIN_BOTTOM;
  605. }
  606. if (margins.hasLeft()) {
  607. styles += " " + CLASSNAME + "-" + StyleConstants.MARGIN_LEFT;
  608. }
  609. margin.setClassName(styles);
  610. marginTopAndBottom = margin.getOffsetHeight()
  611. - canvas.getOffsetHeight();
  612. }
  613. public boolean hasChildComponent(Widget component) {
  614. return widgetToCell.containsKey(component);
  615. }
  616. public void replaceChildComponent(Widget oldComponent, Widget newComponent) {
  617. ChildComponentContainer componentContainer = widgetToComponentContainer
  618. .remove(oldComponent);
  619. if (componentContainer == null) {
  620. return;
  621. }
  622. componentContainer.setPaintable(VPaintableMap.get(client).getPaintable(
  623. newComponent));
  624. widgetToComponentContainer.put(newComponent, componentContainer);
  625. widgetToCell.put(newComponent, widgetToCell.get(oldComponent));
  626. }
  627. public void updateCaption(VPaintableWidget paintable, UIDL uidl) {
  628. Widget widget = paintable.getWidgetForPaintable();
  629. ChildComponentContainer cc = widgetToComponentContainer.get(widget);
  630. if (cc != null) {
  631. cc.updateCaption(uidl, client);
  632. }
  633. if (!rendering) {
  634. // ensure rel size details are updated
  635. widgetToCell.get(widget).updateRelSizeStatus(uidl);
  636. /*
  637. * This was a component-only update and the possible size change
  638. * must be propagated to the layout
  639. */
  640. client.captionSizeUpdated(widget);
  641. }
  642. }
  643. public boolean requestLayout(final Set<Widget> changedChildren) {
  644. boolean needsLayout = false;
  645. boolean reDistributeColSpanWidths = false;
  646. boolean reDistributeRowSpanHeights = false;
  647. int offsetHeight = canvas.getOffsetHeight();
  648. int offsetWidth = canvas.getOffsetWidth();
  649. if ("".equals(width) || "".equals(height)) {
  650. needsLayout = true;
  651. }
  652. ArrayList<Integer> dirtyColumns = new ArrayList<Integer>();
  653. ArrayList<Integer> dirtyRows = new ArrayList<Integer>();
  654. for (Widget widget : changedChildren) {
  655. Cell cell = widgetToCell.get(widget);
  656. if (!cell.hasRelativeHeight() || !cell.hasRelativeWidth()) {
  657. // cell sizes will only stay still if only relatively
  658. // sized components
  659. // check if changed child affects min col widths
  660. assert cell.cc != null;
  661. cell.cc.setWidth("");
  662. cell.cc.setHeight("");
  663. cell.cc.updateWidgetSize();
  664. /*
  665. * If this is the result of an caption icon onload event the
  666. * caption size may have changed
  667. */
  668. cell.cc.updateCaptionSize();
  669. int width = cell.getWidth();
  670. int allocated = columnWidths[cell.col];
  671. for (int i = 1; i < cell.colspan; i++) {
  672. allocated += spacingPixelsHorizontal
  673. + columnWidths[cell.col + i];
  674. }
  675. if (allocated < width) {
  676. needsLayout = true;
  677. if (cell.colspan == 1) {
  678. // do simple column width expansion
  679. columnWidths[cell.col] = minColumnWidths[cell.col] = width;
  680. } else {
  681. // mark that col span expansion is needed
  682. reDistributeColSpanWidths = true;
  683. }
  684. } else if (allocated != width) {
  685. // size is smaller thant allocated, column might
  686. // shrink
  687. dirtyColumns.add(cell.col);
  688. }
  689. int height = cell.getHeight();
  690. allocated = rowHeights[cell.row];
  691. for (int i = 1; i < cell.rowspan; i++) {
  692. allocated += spacingPixelsVertical
  693. + rowHeights[cell.row + i];
  694. }
  695. if (allocated < height) {
  696. needsLayout = true;
  697. if (cell.rowspan == 1) {
  698. // do simple row expansion
  699. rowHeights[cell.row] = minRowHeights[cell.row] = height;
  700. } else {
  701. // mark that row span expansion is needed
  702. reDistributeRowSpanHeights = true;
  703. }
  704. } else if (allocated != height) {
  705. // size is smaller than allocated, row might shrink
  706. dirtyRows.add(cell.row);
  707. }
  708. }
  709. }
  710. if (dirtyColumns.size() > 0) {
  711. for (Integer colIndex : dirtyColumns) {
  712. int colW = 0;
  713. for (int i = 0; i < rowHeights.length; i++) {
  714. Cell cell = cells[colIndex][i];
  715. if (cell != null && cell.getChildUIDL() != null
  716. && !cell.hasRelativeWidth() && cell.colspan == 1) {
  717. int width = cell.getWidth();
  718. if (width > colW) {
  719. colW = width;
  720. }
  721. }
  722. }
  723. minColumnWidths[colIndex] = colW;
  724. }
  725. needsLayout = true;
  726. // ensure colspanned columns have enough space
  727. columnWidths = cloneArray(minColumnWidths);
  728. distributeColSpanWidths();
  729. reDistributeColSpanWidths = false;
  730. }
  731. if (reDistributeColSpanWidths) {
  732. distributeColSpanWidths();
  733. }
  734. if (dirtyRows.size() > 0) {
  735. needsLayout = true;
  736. for (Integer rowIndex : dirtyRows) {
  737. // recalculate min row height
  738. int rowH = minRowHeights[rowIndex] = 0;
  739. // loop all columns on row rowIndex
  740. for (int i = 0; i < columnWidths.length; i++) {
  741. Cell cell = cells[i][rowIndex];
  742. if (cell != null && cell.getChildUIDL() != null
  743. && !cell.hasRelativeHeight() && cell.rowspan == 1) {
  744. int h = cell.getHeight();
  745. if (h > rowH) {
  746. rowH = h;
  747. }
  748. }
  749. }
  750. minRowHeights[rowIndex] = rowH;
  751. }
  752. // TODO could check only some row spans
  753. rowHeights = cloneArray(minRowHeights);
  754. distributeRowSpanHeights();
  755. reDistributeRowSpanHeights = false;
  756. }
  757. if (reDistributeRowSpanHeights) {
  758. distributeRowSpanHeights();
  759. }
  760. if (needsLayout) {
  761. expandColumns();
  762. expandRows();
  763. layoutCells();
  764. // loop all relative sized components and update their size
  765. for (int i = 0; i < cells.length; i++) {
  766. for (int j = 0; j < cells[i].length; j++) {
  767. Cell cell = cells[i][j];
  768. if (cell != null
  769. && cell.cc != null
  770. && (cell.hasRelativeHeight() || cell
  771. .hasRelativeWidth())) {
  772. client.handleComponentRelativeSize(cell.cc.getWidget());
  773. }
  774. }
  775. }
  776. }
  777. if (canvas.getOffsetHeight() != offsetHeight
  778. || canvas.getOffsetWidth() != offsetWidth) {
  779. return false;
  780. } else {
  781. return true;
  782. }
  783. }
  784. public RenderSpace getAllocatedSpace(Widget child) {
  785. Cell cell = widgetToCell.get(child);
  786. assert cell != null;
  787. return cell.getAllocatedSpace();
  788. }
  789. private Cell[][] cells;
  790. /**
  791. * Private helper class.
  792. */
  793. private class Cell {
  794. private boolean relHeight = false;
  795. private boolean relWidth = false;
  796. private boolean widthCanAffectHeight = false;
  797. public Cell(UIDL c) {
  798. row = c.getIntAttribute("y");
  799. col = c.getIntAttribute("x");
  800. setUidl(c);
  801. }
  802. public boolean widthCanAffectHeight() {
  803. return widthCanAffectHeight;
  804. }
  805. public boolean hasRelativeHeight() {
  806. return relHeight;
  807. }
  808. public RenderSpace getAllocatedSpace() {
  809. return new RenderSpace(getAvailableWidth()
  810. - cc.getCaptionWidthAfterComponent(), getAvailableHeight()
  811. - cc.getCaptionHeightAboveComponent());
  812. }
  813. public boolean hasContent() {
  814. return childUidl != null;
  815. }
  816. /**
  817. * @return total of spanned cols
  818. */
  819. private int getAvailableWidth() {
  820. int width = columnWidths[col];
  821. for (int i = 1; i < colspan; i++) {
  822. width += spacingPixelsHorizontal + columnWidths[col + i];
  823. }
  824. return width;
  825. }
  826. /**
  827. * @return total of spanned rows
  828. */
  829. private int getAvailableHeight() {
  830. int height = rowHeights[row];
  831. for (int i = 1; i < rowspan; i++) {
  832. height += spacingPixelsVertical + rowHeights[row + i];
  833. }
  834. return height;
  835. }
  836. public void layout(int x, int y) {
  837. if (cc != null && cc.isAttached()) {
  838. canvas.setWidgetPosition(cc, x, y);
  839. cc.setContainerSize(getAvailableWidth(), getAvailableHeight());
  840. cc.setAlignment(new AlignmentInfo(alignment));
  841. cc.updateAlignments(getAvailableWidth(), getAvailableHeight());
  842. }
  843. }
  844. public int getWidth() {
  845. if (cc != null) {
  846. int w = cc.getWidgetSize().getWidth()
  847. + cc.getCaptionWidthAfterComponent();
  848. return w;
  849. } else {
  850. return 0;
  851. }
  852. }
  853. public int getHeight() {
  854. if (cc != null) {
  855. return cc.getWidgetSize().getHeight()
  856. + cc.getCaptionHeightAboveComponent();
  857. } else {
  858. return 0;
  859. }
  860. }
  861. public boolean renderIfNoRelativeWidth() {
  862. if (childUidl == null) {
  863. return false;
  864. }
  865. if (!hasRelativeWidth()) {
  866. render();
  867. return true;
  868. } else {
  869. return false;
  870. }
  871. }
  872. protected boolean hasRelativeWidth() {
  873. return relWidth;
  874. }
  875. protected void render() {
  876. assert childUidl != null;
  877. VPaintableWidget paintable = client.getPaintable(childUidl);
  878. Widget w = paintable.getWidgetForPaintable();
  879. assert paintable != null;
  880. if (cc == null || cc.getWidget() != w) {
  881. if (widgetToComponentContainer.containsKey(w)) {
  882. // Component moving from one place to another
  883. cc = widgetToComponentContainer.get(w);
  884. cc.setWidth("");
  885. cc.setHeight("");
  886. /*
  887. * Widget might not be set if moving from another component
  888. * and this layout has been hidden when moving out, see
  889. * #5372
  890. */
  891. cc.setPaintable(paintable);
  892. } else {
  893. // A new component
  894. cc = new ChildComponentContainer(paintable,
  895. CellBasedLayout.ORIENTATION_VERTICAL);
  896. widgetToComponentContainer.put(w, cc);
  897. cc.setWidth("");
  898. canvas.add(cc, 0, 0);
  899. }
  900. widgetToCell.put(w, this);
  901. }
  902. cc.renderChild(childUidl, client, -1);
  903. if (sizeChangedDuringRendering && Util.isCached(childUidl)) {
  904. client.handleComponentRelativeSize(cc.getWidget());
  905. }
  906. cc.updateWidgetSize();
  907. nonRenderedWidgets.remove(w);
  908. }
  909. public UIDL getChildUIDL() {
  910. return childUidl;
  911. }
  912. final int row;
  913. final int col;
  914. int colspan = 1;
  915. int rowspan = 1;
  916. UIDL childUidl;
  917. int alignment;
  918. // may be null after setUidl() if content has vanished or changed, set
  919. // in render()
  920. ChildComponentContainer cc;
  921. public void setUidl(UIDL c) {
  922. // Set cell width
  923. colspan = c.hasAttribute("w") ? c.getIntAttribute("w") : 1;
  924. // Set cell height
  925. rowspan = c.hasAttribute("h") ? c.getIntAttribute("h") : 1;
  926. // ensure we will lose reference to old cells, now overlapped by
  927. // this cell
  928. for (int i = 0; i < colspan; i++) {
  929. for (int j = 0; j < rowspan; j++) {
  930. if (i > 0 || j > 0) {
  931. cells[col + i][row + j] = null;
  932. }
  933. }
  934. }
  935. c = c.getChildUIDL(0); // we are interested about childUidl
  936. if (childUidl != null) {
  937. if (c == null) {
  938. // content has vanished, old content will be removed from
  939. // canvas later during the render phase
  940. cc = null;
  941. } else if (cc != null
  942. && cc.getWidget() != client.getPaintable(c)
  943. .getWidgetForPaintable()) {
  944. // content has changed
  945. cc = null;
  946. VPaintableWidget paintable = client.getPaintable(c);
  947. Widget w = paintable.getWidgetForPaintable();
  948. if (widgetToComponentContainer.containsKey(w)) {
  949. // cc exist for this component (moved) use that for this
  950. // cell
  951. cc = widgetToComponentContainer.get(w);
  952. cc.setWidth("");
  953. cc.setHeight("");
  954. widgetToCell.put(w, this);
  955. }
  956. }
  957. }
  958. childUidl = c;
  959. updateRelSizeStatus(c);
  960. }
  961. protected void updateRelSizeStatus(UIDL uidl) {
  962. if (uidl != null && !uidl.getBooleanAttribute("cached")) {
  963. if (uidl.hasAttribute("height")
  964. && uidl.getStringAttribute("height").contains("%")) {
  965. relHeight = true;
  966. } else {
  967. relHeight = false;
  968. }
  969. if (uidl.hasAttribute("width")) {
  970. widthCanAffectHeight = relWidth = uidl.getStringAttribute(
  971. "width").contains("%");
  972. if (uidl.hasAttribute("height")) {
  973. widthCanAffectHeight = false;
  974. }
  975. } else {
  976. widthCanAffectHeight = !uidl.hasAttribute("height");
  977. relWidth = false;
  978. }
  979. }
  980. }
  981. }
  982. private Cell getCell(UIDL c) {
  983. int row = c.getIntAttribute("y");
  984. int col = c.getIntAttribute("x");
  985. Cell cell = cells[col][row];
  986. if (cell == null) {
  987. cell = new Cell(c);
  988. cells[col][row] = cell;
  989. } else {
  990. cell.setUidl(c);
  991. }
  992. return cell;
  993. }
  994. /**
  995. * Returns the deepest nested child component which contains "element". The
  996. * child component is also returned if "element" is part of its caption.
  997. *
  998. * @param element
  999. * An element that is a nested sub element of the root element in
  1000. * this layout
  1001. * @return The Paintable which the element is a part of. Null if the element
  1002. * belongs to the layout and not to a child.
  1003. */
  1004. private VPaintableWidget getComponent(Element element) {
  1005. return Util.getPaintableForElement(client, this, element);
  1006. }
  1007. public Widget getWidgetForPaintable() {
  1008. return this;
  1009. }
  1010. }