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

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