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 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.ui;
  17. import java.util.HashMap;
  18. import java.util.LinkedList;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.Set;
  22. import com.google.gwt.dom.client.DivElement;
  23. import com.google.gwt.dom.client.Document;
  24. import com.google.gwt.dom.client.Element;
  25. import com.google.gwt.dom.client.Style;
  26. import com.google.gwt.dom.client.Style.Position;
  27. import com.google.gwt.dom.client.Style.Unit;
  28. import com.google.gwt.user.client.DOM;
  29. import com.google.gwt.user.client.ui.ComplexPanel;
  30. import com.google.gwt.user.client.ui.Widget;
  31. import com.vaadin.client.ApplicationConnection;
  32. import com.vaadin.client.ComponentConnector;
  33. import com.vaadin.client.ConnectorMap;
  34. import com.vaadin.client.LayoutManager;
  35. import com.vaadin.client.StyleConstants;
  36. import com.vaadin.client.Util;
  37. import com.vaadin.client.VCaption;
  38. import com.vaadin.client.ui.gridlayout.GridLayoutConnector;
  39. import com.vaadin.client.ui.layout.ComponentConnectorLayoutSlot;
  40. import com.vaadin.client.ui.layout.VLayoutSlot;
  41. import com.vaadin.shared.ui.AlignmentInfo;
  42. import com.vaadin.shared.ui.MarginInfo;
  43. import com.vaadin.shared.ui.gridlayout.GridLayoutState.ChildComponentData;
  44. public class VGridLayout extends ComplexPanel {
  45. public static final String CLASSNAME = "v-gridlayout";
  46. /** For internal use only. May be removed or replaced in the future. */
  47. public ApplicationConnection client;
  48. /** For internal use only. May be removed or replaced in the future. */
  49. public Map<Widget, Cell> widgetToCell = new HashMap<>();
  50. /** For internal use only. May be removed or replaced in the future. */
  51. public int[] columnWidths;
  52. /** For internal use only. May be removed or replaced in the future. */
  53. public int[] rowHeights;
  54. /** For internal use only. May be removed or replaced in the future. */
  55. public float[] colExpandRatioArray;
  56. /** For internal use only. May be removed or replaced in the future. */
  57. public float[] rowExpandRatioArray;
  58. int[] minColumnWidths;
  59. private int[] minRowHeights;
  60. /** For internal use only. May be removed or replaced in the future. */
  61. public DivElement spacingMeasureElement;
  62. public Set<Integer> explicitRowRatios;
  63. public Set<Integer> explicitColRatios;
  64. public boolean hideEmptyRowsAndColumns = false;
  65. public VGridLayout() {
  66. super();
  67. setElement(Document.get().createDivElement());
  68. spacingMeasureElement = Document.get().createDivElement();
  69. Style spacingStyle = spacingMeasureElement.getStyle();
  70. spacingStyle.setPosition(Position.ABSOLUTE);
  71. getElement().appendChild(spacingMeasureElement);
  72. setStyleName(CLASSNAME);
  73. addStyleName(StyleConstants.UI_LAYOUT);
  74. publishJSHelpers(getElement());
  75. }
  76. private native void publishJSHelpers(Element root)
  77. /*-{
  78. var self = this;
  79. root.getRowCount = $entry(function () {
  80. return self.@VGridLayout::getRowCount()();
  81. });
  82. root.getColumnCount = $entry(function () {
  83. return self.@VGridLayout::getColumnCount()();
  84. });
  85. root.getCell = $entry(function (row,column) {
  86. return self.@VGridLayout::getCellElement(*)(row, column);
  87. });
  88. }-*/;
  89. private GridLayoutConnector getConnector() {
  90. return (GridLayoutConnector) ConnectorMap.get(client)
  91. .getConnector(this);
  92. }
  93. /**
  94. * Returns the column widths measured in pixels.
  95. *
  96. * @return
  97. */
  98. protected int[] getColumnWidths() {
  99. return columnWidths;
  100. }
  101. /**
  102. * Returns the row heights measured in pixels.
  103. *
  104. * @return
  105. */
  106. protected int[] getRowHeights() {
  107. return rowHeights;
  108. }
  109. /**
  110. * Returns the spacing between the cells horizontally in pixels.
  111. *
  112. * @return
  113. */
  114. protected int getHorizontalSpacing() {
  115. return LayoutManager.get(client).getOuterWidth(spacingMeasureElement);
  116. }
  117. /**
  118. * Returns the spacing between the cells vertically in pixels.
  119. *
  120. * @return
  121. */
  122. protected int getVerticalSpacing() {
  123. return LayoutManager.get(client).getOuterHeight(spacingMeasureElement);
  124. }
  125. static int[] cloneArray(int[] toBeCloned) {
  126. int[] clone = new int[toBeCloned.length];
  127. for (int i = 0; i < clone.length; i++) {
  128. clone[i] = toBeCloned[i] * 1;
  129. }
  130. return clone;
  131. }
  132. void expandRows() {
  133. if (!isUndefinedHeight()) {
  134. int usedSpace = calcRowUsedSpace();
  135. float[] actualExpandRatio = calcRowExpandRatio();
  136. // Round down to avoid problems with fractions (100.1px available ->
  137. // can use 100, not 101)
  138. int availableSpace = (int) LayoutManager.get(client)
  139. .getInnerHeightDouble(getElement());
  140. int excessSpace = availableSpace - usedSpace;
  141. int distributed = 0;
  142. if (excessSpace > 0) {
  143. float expandRatioSum = 0;
  144. for (int i = 0; i < rowHeights.length; i++) {
  145. expandRatioSum += actualExpandRatio[i];
  146. }
  147. for (int i = 0; i < rowHeights.length; i++) {
  148. int ew = (int) (excessSpace * actualExpandRatio[i]
  149. / expandRatioSum);
  150. rowHeights[i] = minRowHeights[i] + ew;
  151. distributed += ew;
  152. }
  153. excessSpace -= distributed;
  154. int c = 0;
  155. while (excessSpace > 0) {
  156. rowHeights[c % rowHeights.length]++;
  157. excessSpace--;
  158. c++;
  159. }
  160. }
  161. }
  162. }
  163. private float[] calcRowExpandRatio() {
  164. float[] actualExpandRatio = new float[minRowHeights.length];
  165. for (int i = 0; i < minRowHeights.length; i++) {
  166. if (hiddenEmptyRow(i)) {
  167. actualExpandRatio[i] = 0;
  168. } else {
  169. actualExpandRatio[i] = rowExpandRatioArray[i];
  170. }
  171. }
  172. return actualExpandRatio;
  173. }
  174. /**
  175. * Checks if it is ok to hide (or ignore) the given row.
  176. *
  177. * @param rowIndex
  178. * the row to check
  179. * @return true, if the row should be interpreted as non-existant (hides
  180. * extra spacing)
  181. */
  182. private boolean hiddenEmptyRow(int rowIndex) {
  183. return hideEmptyRowsAndColumns && !rowHasComponentsOrRowSpan(rowIndex)
  184. && !explicitRowRatios.contains(rowIndex);
  185. }
  186. /**
  187. * Checks if it is ok to hide (or ignore) the given column.
  188. *
  189. * @param columnIndex
  190. * the column to check
  191. * @return true, if the column should be interpreted as non-existant (hides
  192. * extra spacing)
  193. */
  194. private boolean hiddenEmptyColumn(int columnIndex) {
  195. return hideEmptyRowsAndColumns
  196. && !colHasComponentsOrColSpan(columnIndex)
  197. && !explicitColRatios.contains(columnIndex);
  198. }
  199. private int calcRowUsedSpace() {
  200. int usedSpace = 0;
  201. int verticalSpacing = getVerticalSpacing();
  202. boolean visibleFound = false;
  203. for (int i = 0; i < minRowHeights.length; i++) {
  204. if (minRowHeights[i] > 0 || !hiddenEmptyRow(i)) {
  205. if (visibleFound) {
  206. // only include spacing if there already is a visible row
  207. // before this one
  208. usedSpace += verticalSpacing + minRowHeights[i];
  209. } else {
  210. usedSpace += minRowHeights[i];
  211. visibleFound = true;
  212. }
  213. }
  214. }
  215. return usedSpace;
  216. }
  217. void expandColumns() {
  218. if (!isUndefinedWidth()) {
  219. int usedSpace = calcColumnUsedSpace();
  220. float[] actualExpandRatio = calcColumnExpandRatio();
  221. // Round down to avoid problems with fractions (100.1px available ->
  222. // can use 100, not 101)
  223. int availableSpace = (int) LayoutManager.get(client)
  224. .getInnerWidthDouble(getElement());
  225. int excessSpace = availableSpace - usedSpace;
  226. int distributed = 0;
  227. if (excessSpace > 0) {
  228. float expandRatioSum = 0;
  229. for (int i = 0; i < columnWidths.length; i++) {
  230. expandRatioSum += actualExpandRatio[i];
  231. }
  232. for (int i = 0; i < columnWidths.length; i++) {
  233. int ew = (int) (excessSpace * actualExpandRatio[i]
  234. / expandRatioSum);
  235. columnWidths[i] = minColumnWidths[i] + ew;
  236. distributed += ew;
  237. }
  238. excessSpace -= distributed;
  239. int c = 0;
  240. while (excessSpace > 0) {
  241. columnWidths[c % columnWidths.length]++;
  242. excessSpace--;
  243. c++;
  244. }
  245. }
  246. }
  247. }
  248. /**
  249. * Calculates column expand ratio.
  250. */
  251. private float[] calcColumnExpandRatio() {
  252. float[] actualExpandRatio = new float[minColumnWidths.length];
  253. for (int i = 0; i < minColumnWidths.length; i++) {
  254. if (!hiddenEmptyColumn(i)) {
  255. actualExpandRatio[i] = colExpandRatioArray[i];
  256. } else {
  257. actualExpandRatio[i] = 0;
  258. }
  259. }
  260. return actualExpandRatio;
  261. }
  262. /**
  263. * Calculates column used space
  264. */
  265. private int calcColumnUsedSpace() {
  266. int usedSpace = 0;
  267. int horizontalSpacing = getHorizontalSpacing();
  268. boolean visibleFound = false;
  269. for (int i = 0; i < minColumnWidths.length; i++) {
  270. if (minColumnWidths[i] > 0 || !hiddenEmptyColumn(i)) {
  271. if (visibleFound) {
  272. // only include spacing if there already is a visible column
  273. // before this one
  274. usedSpace += horizontalSpacing + minColumnWidths[i];
  275. } else {
  276. usedSpace += minColumnWidths[i];
  277. visibleFound = true;
  278. }
  279. }
  280. }
  281. return usedSpace;
  282. }
  283. private boolean rowHasComponentsOrRowSpan(int i) {
  284. for (Cell cell : widgetToCell.values()) {
  285. if (cell.row == i) {
  286. return true;
  287. }
  288. }
  289. for (SpanList l : rowSpans) {
  290. for (Cell cell : l.cells) {
  291. if (cell.row <= i && i < cell.row + cell.rowspan) {
  292. return true;
  293. }
  294. }
  295. }
  296. return false;
  297. }
  298. private boolean colHasComponentsOrColSpan(int i) {
  299. for (Cell cell : widgetToCell.values()) {
  300. if (cell.col == i) {
  301. return true;
  302. }
  303. }
  304. for (SpanList l : colSpans) {
  305. for (Cell cell : l.cells) {
  306. if (cell.col <= i && i < cell.col + cell.colspan) {
  307. return true;
  308. }
  309. }
  310. }
  311. return false;
  312. }
  313. /** For internal use only. May be removed or replaced in the future. */
  314. public void updateHeight() {
  315. // Detect minimum heights & calculate spans
  316. detectRowHeights();
  317. // Expand
  318. expandRows();
  319. // Position
  320. layoutCellsVertically();
  321. }
  322. /** For internal use only. May be removed or replaced in the future. */
  323. public void updateWidth() {
  324. // Detect widths & calculate spans
  325. detectColWidths();
  326. // Expand
  327. expandColumns();
  328. // Position
  329. layoutCellsHorizontally();
  330. }
  331. void layoutCellsVertically() {
  332. int verticalSpacing = getVerticalSpacing();
  333. LayoutManager layoutManager = LayoutManager.get(client);
  334. Element element = getElement();
  335. int paddingTop = layoutManager.getPaddingTop(element);
  336. int paddingBottom = layoutManager.getPaddingBottom(element);
  337. int y = paddingTop;
  338. for (int column = 0; column < cells.length; column++) {
  339. // Ensure IE10 does not optimize this out by
  340. // adding something to evaluate on the RHS
  341. // #11303
  342. y = paddingTop + 1 - 1;
  343. for (int row = 0; row < cells[column].length; row++) {
  344. Cell cell = cells[column][row];
  345. if (cell != null) {
  346. int reservedMargin;
  347. if (cell.rowspan + row >= cells[column].length) {
  348. // Make room for layout padding for cells reaching the
  349. // bottom of the layout
  350. reservedMargin = paddingBottom;
  351. } else {
  352. reservedMargin = 0;
  353. }
  354. cell.layoutVertically(y, reservedMargin);
  355. }
  356. if (!hideEmptyRowsAndColumns || rowHasComponentsOrRowSpan(row)
  357. || rowHeights[row] > 0) {
  358. y += rowHeights[row] + verticalSpacing;
  359. }
  360. }
  361. }
  362. if (isUndefinedHeight()) {
  363. int outerHeight = y - verticalSpacing
  364. + layoutManager.getPaddingBottom(element)
  365. + layoutManager.getBorderHeight(element);
  366. element.getStyle().setHeight(outerHeight, Unit.PX);
  367. getConnector().getLayoutManager().reportOuterHeight(getConnector(),
  368. outerHeight);
  369. }
  370. }
  371. void layoutCellsHorizontally() {
  372. LayoutManager layoutManager = LayoutManager.get(client);
  373. Element element = getElement();
  374. int x = layoutManager.getPaddingLeft(element);
  375. int paddingRight = layoutManager.getPaddingRight(element);
  376. int horizontalSpacing = getHorizontalSpacing();
  377. for (int i = 0; i < cells.length; i++) {
  378. for (int j = 0; j < cells[i].length; j++) {
  379. Cell cell = cells[i][j];
  380. if (cell != null) {
  381. int reservedMargin;
  382. // Make room for layout padding for cells reaching the
  383. // right edge of the layout
  384. if (i + cell.colspan >= cells.length) {
  385. reservedMargin = paddingRight;
  386. } else {
  387. reservedMargin = 0;
  388. }
  389. cell.layoutHorizontally(x, reservedMargin);
  390. }
  391. }
  392. if (!hideEmptyRowsAndColumns || colHasComponentsOrColSpan(i)
  393. || columnWidths[i] > 0) {
  394. x += columnWidths[i] + horizontalSpacing;
  395. }
  396. }
  397. if (isUndefinedWidth()) {
  398. int outerWidth = x - horizontalSpacing
  399. + layoutManager.getPaddingRight(element)
  400. + layoutManager.getBorderWidth(element);
  401. element.getStyle().setWidth(outerWidth, Unit.PX);
  402. getConnector().getLayoutManager().reportOuterWidth(getConnector(),
  403. outerWidth);
  404. }
  405. }
  406. private boolean isUndefinedHeight() {
  407. return getConnector().isUndefinedHeight();
  408. }
  409. private boolean isUndefinedWidth() {
  410. return getConnector().isUndefinedWidth();
  411. }
  412. private void detectRowHeights() {
  413. for (int i = 0; i < rowHeights.length; i++) {
  414. rowHeights[i] = 0;
  415. }
  416. // collect min rowheight from non-rowspanned cells
  417. for (int i = 0; i < cells.length; i++) {
  418. for (int j = 0; j < cells[i].length; j++) {
  419. Cell cell = cells[i][j];
  420. if (cell != null) {
  421. if (cell.rowspan == 1) {
  422. if (!cell.hasRelativeHeight()
  423. && rowHeights[j] < cell.getHeight()) {
  424. rowHeights[j] = cell.getHeight();
  425. }
  426. } else {
  427. storeRowSpannedCell(cell);
  428. }
  429. }
  430. }
  431. }
  432. distributeRowSpanHeights();
  433. minRowHeights = cloneArray(rowHeights);
  434. }
  435. private void detectColWidths() {
  436. // collect min colwidths from non-colspanned cells
  437. for (int i = 0; i < columnWidths.length; i++) {
  438. columnWidths[i] = 0;
  439. }
  440. for (int i = 0; i < cells.length; i++) {
  441. for (int j = 0; j < cells[i].length; j++) {
  442. Cell cell = cells[i][j];
  443. if (cell != null) {
  444. if (cell.colspan == 1) {
  445. if (!cell.hasRelativeWidth()
  446. && columnWidths[i] < cell.getWidth()) {
  447. columnWidths[i] = cell.getWidth();
  448. }
  449. } else {
  450. storeColSpannedCell(cell);
  451. }
  452. }
  453. }
  454. }
  455. distributeColSpanWidths();
  456. minColumnWidths = cloneArray(columnWidths);
  457. }
  458. private void storeRowSpannedCell(Cell cell) {
  459. SpanList l = null;
  460. for (SpanList list : rowSpans) {
  461. if (list.span < cell.rowspan) {
  462. continue;
  463. } else {
  464. // insert before this
  465. l = list;
  466. break;
  467. }
  468. }
  469. if (l == null) {
  470. l = new SpanList(cell.rowspan);
  471. rowSpans.add(l);
  472. } else if (l.span != cell.rowspan) {
  473. SpanList newL = new SpanList(cell.rowspan);
  474. rowSpans.add(rowSpans.indexOf(l), newL);
  475. l = newL;
  476. }
  477. l.cells.add(cell);
  478. }
  479. /**
  480. * Iterates colspanned cells, ensures cols have enough space to accommodate
  481. * them
  482. */
  483. void distributeColSpanWidths() {
  484. for (SpanList list : colSpans) {
  485. for (Cell cell : list.cells) {
  486. // cells with relative content may return non 0 here if on
  487. // subsequent renders
  488. int width = cell.hasRelativeWidth() ? 0 : cell.getWidth();
  489. distributeSpanSize(columnWidths, cell.col, cell.colspan,
  490. getHorizontalSpacing(), width, colExpandRatioArray);
  491. }
  492. }
  493. }
  494. /**
  495. * Iterates rowspanned cells, ensures rows have enough space to accommodate
  496. * them
  497. */
  498. private void distributeRowSpanHeights() {
  499. for (SpanList list : rowSpans) {
  500. for (Cell cell : list.cells) {
  501. // cells with relative content may return non 0 here if on
  502. // subsequent renders
  503. int height = cell.hasRelativeHeight() ? 0 : cell.getHeight();
  504. distributeSpanSize(rowHeights, cell.row, cell.rowspan,
  505. getVerticalSpacing(), height, rowExpandRatioArray);
  506. }
  507. }
  508. }
  509. private static void distributeSpanSize(int[] dimensions, int spanStartIndex,
  510. int spanSize, int spacingSize, int size, float[] expansionRatios) {
  511. int allocated = dimensions[spanStartIndex];
  512. for (int i = 1; i < spanSize; i++) {
  513. allocated += spacingSize + dimensions[spanStartIndex + i];
  514. }
  515. if (allocated < size) {
  516. // dimensions needs to be expanded due spanned cell
  517. int neededExtraSpace = size - allocated;
  518. int allocatedExtraSpace = 0;
  519. // Divide space according to expansion ratios if any span has a
  520. // ratio
  521. int totalExpansion = 0;
  522. for (int i = 0; i < spanSize; i++) {
  523. int itemIndex = spanStartIndex + i;
  524. totalExpansion += expansionRatios[itemIndex];
  525. }
  526. for (int i = 0; i < spanSize; i++) {
  527. int itemIndex = spanStartIndex + i;
  528. int expansion;
  529. if (totalExpansion == 0) {
  530. // Divide equally among all cells if there are no
  531. // expansion ratios
  532. expansion = neededExtraSpace / spanSize;
  533. } else {
  534. expansion = (int) (neededExtraSpace
  535. * expansionRatios[itemIndex] / totalExpansion);
  536. }
  537. dimensions[itemIndex] += expansion;
  538. allocatedExtraSpace += expansion;
  539. }
  540. // We might still miss a couple of pixels because of
  541. // rounding errors...
  542. if (neededExtraSpace > allocatedExtraSpace) {
  543. for (int i = 0; i < spanSize; i++) {
  544. // Add one pixel to every cell until we have
  545. // compensated for any rounding error
  546. int itemIndex = spanStartIndex + i;
  547. dimensions[itemIndex] += 1;
  548. allocatedExtraSpace += 1;
  549. if (neededExtraSpace == allocatedExtraSpace) {
  550. break;
  551. }
  552. }
  553. }
  554. }
  555. }
  556. private LinkedList<SpanList> colSpans = new LinkedList<>();
  557. private LinkedList<SpanList> rowSpans = new LinkedList<>();
  558. private class SpanList {
  559. final int span;
  560. List<Cell> cells = new LinkedList<>();
  561. public SpanList(int span) {
  562. this.span = span;
  563. }
  564. }
  565. void storeColSpannedCell(Cell cell) {
  566. SpanList l = null;
  567. for (SpanList list : colSpans) {
  568. if (list.span < cell.colspan) {
  569. continue;
  570. } else {
  571. // insert before this
  572. l = list;
  573. break;
  574. }
  575. }
  576. if (l == null) {
  577. l = new SpanList(cell.colspan);
  578. colSpans.add(l);
  579. } else if (l.span != cell.colspan) {
  580. SpanList newL = new SpanList(cell.colspan);
  581. colSpans.add(colSpans.indexOf(l), newL);
  582. l = newL;
  583. }
  584. l.cells.add(cell);
  585. }
  586. Cell[][] cells;
  587. private int rowCount;
  588. private int columnCount;
  589. /**
  590. * Private helper class.
  591. */
  592. /** For internal use only. May be removed or replaced in the future. */
  593. public class Cell {
  594. public Cell(int row, int col) {
  595. this.row = row;
  596. this.col = col;
  597. }
  598. public boolean hasRelativeHeight() {
  599. if (slot != null) {
  600. return slot.getChild().isRelativeHeight();
  601. } else {
  602. return true;
  603. }
  604. }
  605. /**
  606. * @return total of spanned cols
  607. */
  608. private int getAvailableWidth() {
  609. int width = columnWidths[col];
  610. for (int i = 1; i < colspan; i++) {
  611. width += getHorizontalSpacing() + columnWidths[col + i];
  612. }
  613. return width;
  614. }
  615. /**
  616. * @return total of spanned rows
  617. */
  618. private int getAvailableHeight() {
  619. int height = rowHeights[row];
  620. for (int i = 1; i < rowspan; i++) {
  621. height += getVerticalSpacing() + rowHeights[row + i];
  622. }
  623. return height;
  624. }
  625. public void layoutHorizontally(int x, int marginRight) {
  626. if (slot != null) {
  627. slot.positionHorizontally(x, getAvailableWidth(), marginRight);
  628. }
  629. }
  630. public void layoutVertically(int y, int marginBottom) {
  631. if (slot != null) {
  632. slot.positionVertically(y, getAvailableHeight(), marginBottom);
  633. }
  634. }
  635. public int getWidth() {
  636. if (slot != null) {
  637. return slot.getUsedWidth();
  638. } else {
  639. return 0;
  640. }
  641. }
  642. public int getHeight() {
  643. if (slot != null) {
  644. return slot.getUsedHeight();
  645. } else {
  646. return 0;
  647. }
  648. }
  649. protected boolean hasRelativeWidth() {
  650. if (slot != null) {
  651. return slot.getChild().isRelativeWidth();
  652. } else {
  653. return true;
  654. }
  655. }
  656. private int row;
  657. private int col;
  658. int colspan = 1;
  659. int rowspan = 1;
  660. private AlignmentInfo alignment;
  661. /** For internal use only. May be removed or replaced in the future. */
  662. public ComponentConnectorLayoutSlot slot;
  663. public void updateCell(ChildComponentData childComponentData) {
  664. if (row != childComponentData.row1
  665. || col != childComponentData.column1) {
  666. // cell has been moved, update matrix
  667. if (col < cells.length && cells.length != 0
  668. && row < cells[0].length && cells[col][row] == this) {
  669. // Remove old reference if still relevant
  670. cells[col][row] = null;
  671. }
  672. row = childComponentData.row1;
  673. col = childComponentData.column1;
  674. cells[col][row] = this;
  675. }
  676. // Set cell width
  677. colspan = 1 + childComponentData.column2
  678. - childComponentData.column1;
  679. // Set cell height
  680. rowspan = 1 + childComponentData.row2 - childComponentData.row1;
  681. setAlignment(new AlignmentInfo(childComponentData.alignment));
  682. }
  683. public void setComponent(ComponentConnector component,
  684. List<ComponentConnector> ordering) {
  685. if (slot == null || slot.getChild() != component) {
  686. slot = new ComponentConnectorLayoutSlot(CLASSNAME, component,
  687. getConnector());
  688. slot.setAlignment(alignment);
  689. if (component.isRelativeWidth()) {
  690. slot.getWrapperElement().getStyle().setWidth(100, Unit.PCT);
  691. }
  692. Element slotWrapper = slot.getWrapperElement();
  693. int childIndex = ordering.indexOf(component);
  694. // insert new slot by proper index
  695. // do not break default focus order
  696. com.google.gwt.user.client.Element element = getElement();
  697. if (childIndex == ordering.size()) {
  698. element.appendChild(slotWrapper);
  699. } else if (childIndex == 0) {
  700. element.insertAfter(slotWrapper, spacingMeasureElement);
  701. } else {
  702. // here we use childIndex - 1 + 1(spacingMeasureElement)
  703. Element previousSlot = (Element) element
  704. .getChild(childIndex);
  705. element.insertAfter(slotWrapper, previousSlot);
  706. }
  707. Widget widget = component.getWidget();
  708. insert(widget, slotWrapper, getWidgetCount(), false);
  709. Cell oldCell = widgetToCell.put(widget, this);
  710. if (oldCell != null) {
  711. oldCell.slot.getWrapperElement().removeFromParent();
  712. oldCell.slot = null;
  713. }
  714. }
  715. }
  716. public void setAlignment(AlignmentInfo alignmentInfo) {
  717. alignment = alignmentInfo;
  718. if (slot != null) {
  719. slot.setAlignment(alignmentInfo);
  720. }
  721. }
  722. }
  723. /** For internal use only. May be removed or replaced in the future. */
  724. public Cell getCell(int row, int col) {
  725. return cells[col][row];
  726. }
  727. private Element getCellElement(int row, int col) {
  728. if (row < 0 || row >= getRowCount() || col < 0
  729. || col >= getColumnCount()) {
  730. return null;
  731. }
  732. Cell cell = cells[col][row];
  733. if (cell == null || cell.slot == null) {
  734. return null;
  735. }
  736. return cell.slot.getWrapperElement();
  737. }
  738. /**
  739. * Creates a new Cell with the given coordinates.
  740. * <p>
  741. * For internal use only. May be removed or replaced in the future.
  742. *
  743. * @param row
  744. * @param col
  745. * @return
  746. */
  747. public Cell createNewCell(int row, int col) {
  748. Cell cell = new Cell(row, col);
  749. cells[col][row] = cell;
  750. return cell;
  751. }
  752. /**
  753. * Returns the deepest nested child component which contains "element". The
  754. * child component is also returned if "element" is part of its caption.
  755. * <p>
  756. * For internal use only. May be removed or replaced in the future.
  757. *
  758. * @param element
  759. * An element that is a nested sub element of the root element in
  760. * this layout
  761. * @return The Paintable which the element is a part of. Null if the element
  762. * belongs to the layout and not to a child.
  763. * @deprecated As of 7.2, call or override {@link #getComponent(Element)}
  764. * instead
  765. */
  766. @Deprecated
  767. public ComponentConnector getComponent(
  768. com.google.gwt.user.client.Element element) {
  769. return Util.getConnectorForElement(client, this, element);
  770. }
  771. /**
  772. * Returns the deepest nested child component which contains "element". The
  773. * child component is also returned if "element" is part of its caption.
  774. * <p>
  775. * For internal use only. May be removed or replaced in the future.
  776. *
  777. * @param element
  778. * An element that is a nested sub element of the root element in
  779. * this layout
  780. * @return The Paintable which the element is a part of. Null if the element
  781. * belongs to the layout and not to a child.
  782. *
  783. * @since 7.2
  784. */
  785. public ComponentConnector getComponent(Element element) {
  786. return getComponent(DOM.asOld(element));
  787. }
  788. /** For internal use only. May be removed or replaced in the future. */
  789. public void setCaption(Widget widget, VCaption caption) {
  790. VLayoutSlot slot = widgetToCell.get(widget).slot;
  791. if (caption != null) {
  792. // Logical attach.
  793. getChildren().add(caption);
  794. }
  795. // Physical attach if not null, also removes old caption
  796. slot.setCaption(caption);
  797. if (caption != null) {
  798. // Adopt.
  799. adopt(caption);
  800. }
  801. }
  802. private void togglePrefixedStyleName(String name, boolean enabled) {
  803. if (enabled) {
  804. addStyleDependentName(name);
  805. } else {
  806. removeStyleDependentName(name);
  807. }
  808. }
  809. /** For internal use only. May be removed or replaced in the future. */
  810. public void updateMarginStyleNames(MarginInfo marginInfo) {
  811. togglePrefixedStyleName("margin-top", marginInfo.hasTop());
  812. togglePrefixedStyleName("margin-right", marginInfo.hasRight());
  813. togglePrefixedStyleName("margin-bottom", marginInfo.hasBottom());
  814. togglePrefixedStyleName("margin-left", marginInfo.hasLeft());
  815. }
  816. /** For internal use only. May be removed or replaced in the future. */
  817. public void updateSpacingStyleName(boolean spacingEnabled) {
  818. String styleName = getStylePrimaryName();
  819. if (spacingEnabled) {
  820. spacingMeasureElement.addClassName(styleName + "-spacing-on");
  821. spacingMeasureElement.removeClassName(styleName + "-spacing-off");
  822. } else {
  823. spacingMeasureElement.removeClassName(styleName + "-spacing-on");
  824. spacingMeasureElement.addClassName(styleName + "-spacing-off");
  825. }
  826. }
  827. public void setSize(int rows, int cols) {
  828. rowCount = rows;
  829. columnCount = cols;
  830. if (cells == null) {
  831. cells = new Cell[cols][rows];
  832. } else if (cells.length != cols || cells[0].length != rows) {
  833. Cell[][] newCells = new Cell[cols][rows];
  834. for (int i = 0; i < cells.length; i++) {
  835. for (int j = 0; j < cells[i].length; j++) {
  836. if (i < cols && j < rows) {
  837. newCells[i][j] = cells[i][j];
  838. }
  839. }
  840. }
  841. cells = newCells;
  842. }
  843. }
  844. private int getRowCount() {
  845. return rowCount;
  846. }
  847. private int getColumnCount() {
  848. return columnCount;
  849. }
  850. @Override
  851. public boolean remove(Widget w) {
  852. boolean removed = super.remove(w);
  853. if (removed) {
  854. Cell cell = widgetToCell.remove(w);
  855. if (cell != null) {
  856. cell.slot.setCaption(null);
  857. cell.slot.getWrapperElement().removeFromParent();
  858. cell.slot = null;
  859. Style style = w.getElement().getStyle();
  860. style.clearTop();
  861. style.clearLeft();
  862. style.clearPosition();
  863. if (cells.length < cell.col && cells.length != 0
  864. && cells[0].length < cell.row
  865. && cells[cell.col][cell.row] == cell) {
  866. cells[cell.col][cell.row] = null;
  867. }
  868. }
  869. }
  870. return removed;
  871. }
  872. }