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.

AbstractLayout.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 org.jsoup.nodes.Element;
  18. import com.vaadin.shared.ui.AbstractLayoutState;
  19. import com.vaadin.shared.ui.MarginInfo;
  20. import com.vaadin.ui.declarative.DesignAttributeHandler;
  21. import com.vaadin.ui.declarative.DesignContext;
  22. /**
  23. * An abstract class that defines default implementation for the {@link Layout}
  24. * interface.
  25. *
  26. * @author Vaadin Ltd.
  27. * @since 5.0
  28. */
  29. public abstract class AbstractLayout extends AbstractComponentContainer
  30. implements Layout {
  31. @Override
  32. protected AbstractLayoutState getState() {
  33. return (AbstractLayoutState) super.getState();
  34. }
  35. /**
  36. * Reads margin attributes from a design into a MarginInfo object. This
  37. * helper method should be called from the
  38. * {@link #readDesign(Element, DesignContext) readDesign} method of layouts
  39. * that implement {@link MarginHandler}.
  40. *
  41. * @since 7.5
  42. *
  43. * @param design
  44. * the design from which to read
  45. * @param defMargin
  46. * the default margin state for edges that are not set in the
  47. * design
  48. * @param context
  49. * the DesignContext instance used for parsing the design
  50. * @return the margin info
  51. */
  52. protected MarginInfo readMargin(Element design, MarginInfo defMargin,
  53. DesignContext context) {
  54. if (design.hasAttr("margin")) {
  55. boolean margin = DesignAttributeHandler.readAttribute("margin",
  56. design.attributes(), boolean.class);
  57. return new MarginInfo(margin);
  58. } else {
  59. boolean left = DesignAttributeHandler.readAttribute("margin-left",
  60. design.attributes(), defMargin.hasLeft(), boolean.class);
  61. boolean right = DesignAttributeHandler.readAttribute("margin-right",
  62. design.attributes(), defMargin.hasRight(), boolean.class);
  63. boolean top = DesignAttributeHandler.readAttribute("margin-top",
  64. design.attributes(), defMargin.hasTop(), boolean.class);
  65. boolean bottom = DesignAttributeHandler.readAttribute(
  66. "margin-bottom", design.attributes(), defMargin.hasBottom(),
  67. boolean.class);
  68. return new MarginInfo(top, right, bottom, left);
  69. }
  70. }
  71. /**
  72. * Writes margin attributes from a MarginInfo object to a design. This
  73. * helper method should be called from the
  74. * {@link #readDesign(Element, DesignContext) writeDesign} method of layouts
  75. * that implement {@link MarginHandler}.
  76. *
  77. *
  78. * @since 7.5
  79. *
  80. * @param design
  81. * the design to write to
  82. * @param margin
  83. * the margin state to write
  84. * @param defMargin
  85. * the default margin state to compare against
  86. * @param context
  87. * the DesignContext instance used for parsing the design
  88. */
  89. protected void writeMargin(Element design, MarginInfo margin,
  90. MarginInfo defMargin, DesignContext context) {
  91. if (margin.hasAll()) {
  92. DesignAttributeHandler.writeAttribute("margin", design.attributes(),
  93. margin.hasAll(), defMargin.hasAll(), boolean.class);
  94. } else {
  95. DesignAttributeHandler.writeAttribute("margin-left",
  96. design.attributes(), margin.hasLeft(), defMargin.hasLeft(),
  97. boolean.class);
  98. DesignAttributeHandler.writeAttribute("margin-right",
  99. design.attributes(), margin.hasRight(),
  100. defMargin.hasRight(), boolean.class);
  101. DesignAttributeHandler.writeAttribute("margin-top",
  102. design.attributes(), margin.hasTop(), defMargin.hasTop(),
  103. boolean.class);
  104. DesignAttributeHandler.writeAttribute("margin-bottom",
  105. design.attributes(), margin.hasBottom(),
  106. defMargin.hasBottom(), boolean.class);
  107. }
  108. }
  109. }