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.

AbsoluteLayout.java 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. /*
  2. * Copyright 2000-2016 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.ui;
  17. import java.io.Serializable;
  18. import java.util.HashMap;
  19. import java.util.Iterator;
  20. import java.util.LinkedHashMap;
  21. import java.util.Map;
  22. import org.jsoup.nodes.Attributes;
  23. import org.jsoup.nodes.Element;
  24. import org.jsoup.nodes.Node;
  25. import com.vaadin.event.LayoutEvents.LayoutClickEvent;
  26. import com.vaadin.event.LayoutEvents.LayoutClickListener;
  27. import com.vaadin.event.LayoutEvents.LayoutClickNotifier;
  28. import com.vaadin.server.Sizeable;
  29. import com.vaadin.shared.Connector;
  30. import com.vaadin.shared.EventId;
  31. import com.vaadin.shared.MouseEventDetails;
  32. import com.vaadin.shared.ui.absolutelayout.AbsoluteLayoutServerRpc;
  33. import com.vaadin.shared.ui.absolutelayout.AbsoluteLayoutState;
  34. import com.vaadin.ui.declarative.DesignAttributeHandler;
  35. import com.vaadin.ui.declarative.DesignContext;
  36. /**
  37. * AbsoluteLayout is a layout implementation that mimics html absolute
  38. * positioning.
  39. *
  40. */
  41. @SuppressWarnings("serial")
  42. public class AbsoluteLayout extends AbstractLayout
  43. implements LayoutClickNotifier {
  44. // constants for design attributes
  45. private static final String ATTR_TOP = ":top";
  46. private static final String ATTR_RIGHT = ":right";
  47. private static final String ATTR_BOTTOM = ":bottom";
  48. private static final String ATTR_LEFT = ":left";
  49. private static final String ATTR_Z_INDEX = ":z-index";
  50. private AbsoluteLayoutServerRpc rpc = new AbsoluteLayoutServerRpc() {
  51. @Override
  52. public void layoutClick(MouseEventDetails mouseDetails,
  53. Connector clickedConnector) {
  54. fireEvent(LayoutClickEvent.createEvent(AbsoluteLayout.this,
  55. mouseDetails, clickedConnector));
  56. }
  57. };
  58. // Maps each component to a position
  59. private LinkedHashMap<Component, ComponentPosition> componentToCoordinates = new LinkedHashMap<Component, ComponentPosition>();
  60. /**
  61. * Creates an AbsoluteLayout with full size.
  62. */
  63. public AbsoluteLayout() {
  64. registerRpc(rpc);
  65. setSizeFull();
  66. }
  67. @Override
  68. protected AbsoluteLayoutState getState() {
  69. return (AbsoluteLayoutState) super.getState();
  70. }
  71. /**
  72. * Gets an iterator for going through all components enclosed in the
  73. * absolute layout.
  74. */
  75. @Override
  76. public Iterator<Component> iterator() {
  77. return componentToCoordinates.keySet().iterator();
  78. }
  79. /**
  80. * Gets the number of contained components. Consistent with the iterator
  81. * returned by {@link #getComponentIterator()}.
  82. *
  83. * @return the number of contained components
  84. */
  85. @Override
  86. public int getComponentCount() {
  87. return componentToCoordinates.size();
  88. }
  89. /**
  90. * Replaces one component with another one. The new component inherits the
  91. * old components position.
  92. */
  93. @Override
  94. public void replaceComponent(Component oldComponent,
  95. Component newComponent) {
  96. ComponentPosition position = getPosition(oldComponent);
  97. removeComponent(oldComponent);
  98. addComponent(newComponent, position);
  99. }
  100. /*
  101. * (non-Javadoc)
  102. *
  103. * @see com.vaadin.ui.AbstractComponentContainer#addComponent(com.vaadin.ui.
  104. * Component )
  105. */
  106. @Override
  107. public void addComponent(Component c) {
  108. addComponent(c, new ComponentPosition());
  109. }
  110. /**
  111. * Adds a component to the layout. The component can be positioned by
  112. * providing a string formatted in CSS-format.
  113. * <p>
  114. * For example the string "top:10px;left:10px" will position the component
  115. * 10 pixels from the left and 10 pixels from the top. The identifiers:
  116. * "top","left","right" and "bottom" can be used to specify the position.
  117. * </p>
  118. *
  119. * @param c
  120. * The component to add to the layout
  121. * @param cssPosition
  122. * The css position string
  123. */
  124. public void addComponent(Component c, String cssPosition) {
  125. ComponentPosition position = new ComponentPosition();
  126. position.setCSSString(cssPosition);
  127. addComponent(c, position);
  128. }
  129. /**
  130. * Adds the component using the given position. Ensures the position is only
  131. * set if the component is added correctly.
  132. *
  133. * @param c
  134. * The component to add
  135. * @param position
  136. * The position info for the component. Must not be null.
  137. * @throws IllegalArgumentException
  138. * If adding the component failed
  139. */
  140. private void addComponent(Component c, ComponentPosition position)
  141. throws IllegalArgumentException {
  142. if (equals(c.getParent())) {
  143. removeComponent(c);
  144. }
  145. /*
  146. * Create position instance and add it to componentToCoordinates map. We
  147. * need to do this before we call addComponent so the attachListeners
  148. * can access this position. #6368
  149. */
  150. internalSetPosition(c, position);
  151. try {
  152. super.addComponent(c);
  153. } catch (IllegalArgumentException e) {
  154. internalRemoveComponent(c);
  155. throw e;
  156. }
  157. }
  158. /**
  159. * Removes the component from all internal data structures. Does not
  160. * actually remove the component from the layout (this is assumed to have
  161. * been done by the caller).
  162. *
  163. * @param c
  164. * The component to remove
  165. */
  166. private void internalRemoveComponent(Component c) {
  167. componentToCoordinates.remove(c);
  168. }
  169. @Override
  170. public void beforeClientResponse(boolean initial) {
  171. super.beforeClientResponse(initial);
  172. // This could be in internalRemoveComponent and internalSetComponent if
  173. // Map<Connector,String> was supported. We cannot get the child
  174. // connectorId unless the component is attached to the application so
  175. // the String->String map cannot be populated in internal* either.
  176. Map<String, String> connectorToPosition = new HashMap<String, String>();
  177. for (Iterator<Component> ci = getComponentIterator(); ci.hasNext();) {
  178. Component c = ci.next();
  179. connectorToPosition.put(c.getConnectorId(),
  180. getPosition(c).getCSSString());
  181. }
  182. getState().connectorToCssPosition = connectorToPosition;
  183. }
  184. /*
  185. * (non-Javadoc)
  186. *
  187. * @see
  188. * com.vaadin.ui.AbstractComponentContainer#removeComponent(com.vaadin.ui
  189. * .Component)
  190. */
  191. @Override
  192. public void removeComponent(Component c) {
  193. internalRemoveComponent(c);
  194. super.removeComponent(c);
  195. }
  196. /**
  197. * Gets the position of a component in the layout. Returns null if component
  198. * is not attached to the layout.
  199. * <p>
  200. * Note that you cannot update the position by updating this object. Call
  201. * {@link #setPosition(Component, ComponentPosition)} with the updated
  202. * {@link ComponentPosition} object.
  203. * </p>
  204. *
  205. * @param component
  206. * The component which position is needed
  207. * @return An instance of ComponentPosition containing the position of the
  208. * component, or null if the component is not enclosed in the
  209. * layout.
  210. */
  211. public ComponentPosition getPosition(Component component) {
  212. return componentToCoordinates.get(component);
  213. }
  214. /**
  215. * Sets the position of a component in the layout.
  216. *
  217. * @param component
  218. * @param position
  219. */
  220. public void setPosition(Component component, ComponentPosition position) {
  221. if (!componentToCoordinates.containsKey(component)) {
  222. throw new IllegalArgumentException(
  223. "Component must be a child of this layout");
  224. }
  225. internalSetPosition(component, position);
  226. }
  227. /**
  228. * Updates the position for a component. Caller must ensure component is a
  229. * child of this layout.
  230. *
  231. * @param component
  232. * The component. Must be a child for this layout. Not enforced.
  233. * @param position
  234. * New position. Must not be null.
  235. */
  236. private void internalSetPosition(Component component,
  237. ComponentPosition position) {
  238. componentToCoordinates.put(component, position);
  239. markAsDirty();
  240. }
  241. /**
  242. * The CompontPosition class represents a components position within the
  243. * absolute layout. It contains the attributes for left, right, top and
  244. * bottom and the units used to specify them.
  245. */
  246. public class ComponentPosition implements Serializable {
  247. private int zIndex = -1;
  248. private Float topValue = null;
  249. private Float rightValue = null;
  250. private Float bottomValue = null;
  251. private Float leftValue = null;
  252. private Unit topUnits = Unit.PIXELS;
  253. private Unit rightUnits = Unit.PIXELS;
  254. private Unit bottomUnits = Unit.PIXELS;
  255. private Unit leftUnits = Unit.PIXELS;
  256. /**
  257. * Sets the position attributes using CSS syntax. Attributes not
  258. * included in the string are reset to their unset states.
  259. *
  260. * <code><pre>
  261. * setCSSString("top:10px;left:20%;z-index:16;");
  262. * </pre></code>
  263. *
  264. * @param css
  265. */
  266. public void setCSSString(String css) {
  267. topValue = rightValue = bottomValue = leftValue = null;
  268. topUnits = rightUnits = bottomUnits = leftUnits = Unit.PIXELS;
  269. zIndex = -1;
  270. if (css == null) {
  271. return;
  272. }
  273. String[] cssProperties = css.split(";");
  274. for (int i = 0; i < cssProperties.length; i++) {
  275. String[] keyValuePair = cssProperties[i].split(":");
  276. String key = keyValuePair[0].trim();
  277. if (key.equals("")) {
  278. continue;
  279. }
  280. if (key.equals("z-index")) {
  281. zIndex = Integer.parseInt(keyValuePair[1].trim());
  282. } else {
  283. String value;
  284. if (keyValuePair.length > 1) {
  285. value = keyValuePair[1].trim();
  286. } else {
  287. value = "";
  288. }
  289. String symbol = value.replaceAll("[0-9\\.\\-]+", "");
  290. if (!symbol.equals("")) {
  291. value = value.substring(0, value.indexOf(symbol))
  292. .trim();
  293. }
  294. float v = Float.parseFloat(value);
  295. Unit unit = Unit.getUnitFromSymbol(symbol);
  296. if (key.equals("top")) {
  297. topValue = v;
  298. topUnits = unit;
  299. } else if (key.equals("right")) {
  300. rightValue = v;
  301. rightUnits = unit;
  302. } else if (key.equals("bottom")) {
  303. bottomValue = v;
  304. bottomUnits = unit;
  305. } else if (key.equals("left")) {
  306. leftValue = v;
  307. leftUnits = unit;
  308. }
  309. }
  310. }
  311. markAsDirty();
  312. }
  313. /**
  314. * Converts the internal values into a valid CSS string.
  315. *
  316. * @return A valid CSS string
  317. */
  318. public String getCSSString() {
  319. String s = "";
  320. if (topValue != null) {
  321. s += "top:" + topValue + topUnits.getSymbol() + ";";
  322. }
  323. if (rightValue != null) {
  324. s += "right:" + rightValue + rightUnits.getSymbol() + ";";
  325. }
  326. if (bottomValue != null) {
  327. s += "bottom:" + bottomValue + bottomUnits.getSymbol() + ";";
  328. }
  329. if (leftValue != null) {
  330. s += "left:" + leftValue + leftUnits.getSymbol() + ";";
  331. }
  332. if (zIndex >= 0) {
  333. s += "z-index:" + zIndex + ";";
  334. }
  335. return s;
  336. }
  337. /**
  338. * Sets the 'top' attribute; distance from the top of the component to
  339. * the top edge of the layout.
  340. *
  341. * @param topValue
  342. * The value of the 'top' attribute
  343. * @param topUnits
  344. * The unit of the 'top' attribute. See UNIT_SYMBOLS for a
  345. * description of the available units.
  346. */
  347. public void setTop(Float topValue, Unit topUnits) {
  348. this.topValue = topValue;
  349. this.topUnits = topUnits;
  350. markAsDirty();
  351. }
  352. /**
  353. * Sets the 'right' attribute; distance from the right of the component
  354. * to the right edge of the layout.
  355. *
  356. * @param rightValue
  357. * The value of the 'right' attribute
  358. * @param rightUnits
  359. * The unit of the 'right' attribute. See UNIT_SYMBOLS for a
  360. * description of the available units.
  361. */
  362. public void setRight(Float rightValue, Unit rightUnits) {
  363. this.rightValue = rightValue;
  364. this.rightUnits = rightUnits;
  365. markAsDirty();
  366. }
  367. /**
  368. * Sets the 'bottom' attribute; distance from the bottom of the
  369. * component to the bottom edge of the layout.
  370. *
  371. * @param bottomValue
  372. * The value of the 'bottom' attribute
  373. * @param units
  374. * The unit of the 'bottom' attribute. See UNIT_SYMBOLS for a
  375. * description of the available units.
  376. */
  377. public void setBottom(Float bottomValue, Unit bottomUnits) {
  378. this.bottomValue = bottomValue;
  379. this.bottomUnits = bottomUnits;
  380. markAsDirty();
  381. }
  382. /**
  383. * Sets the 'left' attribute; distance from the left of the component to
  384. * the left edge of the layout.
  385. *
  386. * @param leftValue
  387. * The value of the 'left' attribute
  388. * @param units
  389. * The unit of the 'left' attribute. See UNIT_SYMBOLS for a
  390. * description of the available units.
  391. */
  392. public void setLeft(Float leftValue, Unit leftUnits) {
  393. this.leftValue = leftValue;
  394. this.leftUnits = leftUnits;
  395. markAsDirty();
  396. }
  397. /**
  398. * Sets the 'z-index' attribute; the visual stacking order
  399. *
  400. * @param zIndex
  401. * The z-index for the component.
  402. */
  403. public void setZIndex(int zIndex) {
  404. this.zIndex = zIndex;
  405. markAsDirty();
  406. }
  407. /**
  408. * Sets the value of the 'top' attribute; distance from the top of the
  409. * component to the top edge of the layout.
  410. *
  411. * @param topValue
  412. * The value of the 'left' attribute
  413. */
  414. public void setTopValue(Float topValue) {
  415. this.topValue = topValue;
  416. markAsDirty();
  417. }
  418. /**
  419. * Gets the 'top' attributes value in current units.
  420. *
  421. * @see #getTopUnits()
  422. * @return The value of the 'top' attribute, null if not set
  423. */
  424. public Float getTopValue() {
  425. return topValue;
  426. }
  427. /**
  428. * Gets the 'right' attributes value in current units.
  429. *
  430. * @return The value of the 'right' attribute, null if not set
  431. * @see #getRightUnits()
  432. */
  433. public Float getRightValue() {
  434. return rightValue;
  435. }
  436. /**
  437. * Sets the 'right' attribute value (distance from the right of the
  438. * component to the right edge of the layout). Currently active units
  439. * are maintained.
  440. *
  441. * @param rightValue
  442. * The value of the 'right' attribute
  443. * @see #setRightUnits(int)
  444. */
  445. public void setRightValue(Float rightValue) {
  446. this.rightValue = rightValue;
  447. markAsDirty();
  448. }
  449. /**
  450. * Gets the 'bottom' attributes value using current units.
  451. *
  452. * @return The value of the 'bottom' attribute, null if not set
  453. * @see #getBottomUnits()
  454. */
  455. public Float getBottomValue() {
  456. return bottomValue;
  457. }
  458. /**
  459. * Sets the 'bottom' attribute value (distance from the bottom of the
  460. * component to the bottom edge of the layout). Currently active units
  461. * are maintained.
  462. *
  463. * @param bottomValue
  464. * The value of the 'bottom' attribute
  465. * @see #setBottomUnits(int)
  466. */
  467. public void setBottomValue(Float bottomValue) {
  468. this.bottomValue = bottomValue;
  469. markAsDirty();
  470. }
  471. /**
  472. * Gets the 'left' attributes value using current units.
  473. *
  474. * @return The value of the 'left' attribute, null if not set
  475. * @see #getLeftUnits()
  476. */
  477. public Float getLeftValue() {
  478. return leftValue;
  479. }
  480. /**
  481. * Sets the 'left' attribute value (distance from the left of the
  482. * component to the left edge of the layout). Currently active units are
  483. * maintained.
  484. *
  485. * @param leftValue
  486. * The value of the 'left' CSS-attribute
  487. * @see #setLeftUnits(int)
  488. */
  489. public void setLeftValue(Float leftValue) {
  490. this.leftValue = leftValue;
  491. markAsDirty();
  492. }
  493. /**
  494. * Gets the unit for the 'top' attribute
  495. *
  496. * @return See {@link Sizeable} UNIT_SYMBOLS for a description of the
  497. * available units.
  498. */
  499. public Unit getTopUnits() {
  500. return topUnits;
  501. }
  502. /**
  503. * Sets the unit for the 'top' attribute
  504. *
  505. * @param topUnits
  506. * See {@link Sizeable} UNIT_SYMBOLS for a description of the
  507. * available units.
  508. */
  509. public void setTopUnits(Unit topUnits) {
  510. this.topUnits = topUnits;
  511. markAsDirty();
  512. }
  513. /**
  514. * Gets the unit for the 'right' attribute
  515. *
  516. * @return See {@link Sizeable} UNIT_SYMBOLS for a description of the
  517. * available units.
  518. */
  519. public Unit getRightUnits() {
  520. return rightUnits;
  521. }
  522. /**
  523. * Sets the unit for the 'right' attribute
  524. *
  525. * @param rightUnits
  526. * See {@link Sizeable} UNIT_SYMBOLS for a description of the
  527. * available units.
  528. */
  529. public void setRightUnits(Unit rightUnits) {
  530. this.rightUnits = rightUnits;
  531. markAsDirty();
  532. }
  533. /**
  534. * Gets the unit for the 'bottom' attribute
  535. *
  536. * @return See {@link Sizeable} UNIT_SYMBOLS for a description of the
  537. * available units.
  538. */
  539. public Unit getBottomUnits() {
  540. return bottomUnits;
  541. }
  542. /**
  543. * Sets the unit for the 'bottom' attribute
  544. *
  545. * @param bottomUnits
  546. * See {@link Sizeable} UNIT_SYMBOLS for a description of the
  547. * available units.
  548. */
  549. public void setBottomUnits(Unit bottomUnits) {
  550. this.bottomUnits = bottomUnits;
  551. markAsDirty();
  552. }
  553. /**
  554. * Gets the unit for the 'left' attribute
  555. *
  556. * @return See {@link Sizeable} UNIT_SYMBOLS for a description of the
  557. * available units.
  558. */
  559. public Unit getLeftUnits() {
  560. return leftUnits;
  561. }
  562. /**
  563. * Sets the unit for the 'left' attribute
  564. *
  565. * @param leftUnits
  566. * See {@link Sizeable} UNIT_SYMBOLS for a description of the
  567. * available units.
  568. */
  569. public void setLeftUnits(Unit leftUnits) {
  570. this.leftUnits = leftUnits;
  571. markAsDirty();
  572. }
  573. /**
  574. * Gets the 'z-index' attribute.
  575. *
  576. * @return the zIndex The z-index attribute
  577. */
  578. public int getZIndex() {
  579. return zIndex;
  580. }
  581. /*
  582. * (non-Javadoc)
  583. *
  584. * @see java.lang.Object#toString()
  585. */
  586. @Override
  587. public String toString() {
  588. return getCSSString();
  589. }
  590. }
  591. @Override
  592. public void addLayoutClickListener(LayoutClickListener listener) {
  593. addListener(EventId.LAYOUT_CLICK_EVENT_IDENTIFIER,
  594. LayoutClickEvent.class, listener,
  595. LayoutClickListener.clickMethod);
  596. }
  597. /**
  598. * @deprecated As of 7.0, replaced by
  599. * {@link #addLayoutClickListener(LayoutClickListener)}
  600. **/
  601. @Override
  602. @Deprecated
  603. public void addListener(LayoutClickListener listener) {
  604. addLayoutClickListener(listener);
  605. }
  606. @Override
  607. public void removeLayoutClickListener(LayoutClickListener listener) {
  608. removeListener(EventId.LAYOUT_CLICK_EVENT_IDENTIFIER,
  609. LayoutClickEvent.class, listener);
  610. }
  611. /**
  612. * @deprecated As of 7.0, replaced by
  613. * {@link #removeLayoutClickListener(LayoutClickListener)}
  614. **/
  615. @Override
  616. @Deprecated
  617. public void removeListener(LayoutClickListener listener) {
  618. removeLayoutClickListener(listener);
  619. }
  620. /*
  621. * (non-Javadoc)
  622. *
  623. * @see com.vaadin.ui.AbstractComponent#readDesign(org.jsoup.nodes .Node,
  624. * com.vaadin.ui.declarative.DesignContext)
  625. */
  626. @Override
  627. public void readDesign(Element design, DesignContext designContext) {
  628. // process default attributes
  629. super.readDesign(design, designContext);
  630. // handle children
  631. for (Element childComponent : design.children()) {
  632. Attributes attr = childComponent.attributes();
  633. Component newChild = designContext.readDesign(childComponent);
  634. StringBuilder css = new StringBuilder();
  635. if (attr.hasKey(ATTR_TOP)) {
  636. css.append("top:").append(attr.get(ATTR_TOP)).append(";");
  637. }
  638. if (attr.hasKey(ATTR_RIGHT)) {
  639. css.append("right:").append(attr.get(ATTR_RIGHT)).append(";");
  640. }
  641. if (attr.hasKey(ATTR_BOTTOM)) {
  642. css.append("bottom:").append(attr.get(ATTR_BOTTOM)).append(";");
  643. }
  644. if (attr.hasKey(ATTR_LEFT)) {
  645. css.append("left:").append(attr.get(ATTR_LEFT)).append(";");
  646. }
  647. if (attr.hasKey(ATTR_Z_INDEX)) {
  648. css.append("z-index:").append(attr.get(ATTR_Z_INDEX))
  649. .append(";");
  650. }
  651. addComponent(newChild, css.toString());
  652. }
  653. }
  654. /*
  655. * (non-Javadoc)
  656. *
  657. * @see com.vaadin.ui.AbstractComponent#writeDesign(org.jsoup.nodes.Node,
  658. * com.vaadin.ui.declarative.DesignContext)
  659. */
  660. @Override
  661. public void writeDesign(Element design, DesignContext designContext) {
  662. super.writeDesign(design, designContext);
  663. AbsoluteLayout def = designContext.getDefaultInstance(this);
  664. if (!designContext.shouldWriteChildren(this, def)) {
  665. return;
  666. }
  667. // handle children
  668. for (Component child : this) {
  669. Element childElement = designContext.createElement(child);
  670. design.appendChild(childElement);
  671. // handle position
  672. ComponentPosition position = getPosition(child);
  673. writePositionAttribute(childElement, ATTR_TOP,
  674. position.getTopUnits().getSymbol(), position.getTopValue());
  675. writePositionAttribute(childElement, ATTR_RIGHT,
  676. position.getRightUnits().getSymbol(),
  677. position.getRightValue());
  678. writePositionAttribute(childElement, ATTR_BOTTOM,
  679. position.getBottomUnits().getSymbol(),
  680. position.getBottomValue());
  681. writePositionAttribute(childElement, ATTR_LEFT,
  682. position.getLeftUnits().getSymbol(),
  683. position.getLeftValue());
  684. // handle z-index
  685. if (position.getZIndex() >= 0) {
  686. childElement.attr(ATTR_Z_INDEX,
  687. String.valueOf(position.zIndex));
  688. }
  689. }
  690. }
  691. /**
  692. * Private method for writing position attributes
  693. *
  694. * @since 7.4
  695. * @param node
  696. * target node
  697. * @param key
  698. * attribute key
  699. * @param symbol
  700. * value symbol
  701. * @param value
  702. * the value
  703. */
  704. private void writePositionAttribute(Node node, String key, String symbol,
  705. Float value) {
  706. if (value != null) {
  707. String valueString = DesignAttributeHandler.getFormatter()
  708. .format(value);
  709. node.attr(key, valueString + symbol);
  710. }
  711. }
  712. }