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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.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. @Override
  36. protected AbstractLayoutState getState(boolean markAsDirty) {
  37. return (AbstractLayoutState) super.getState(markAsDirty);
  38. }
  39. /**
  40. * Reads margin attributes from a design into a MarginInfo object. This
  41. * helper method should be called from the
  42. * {@link #readDesign(Element, DesignContext) readDesign} method of layouts
  43. * that implement {@link MarginHandler}.
  44. *
  45. * @since 7.5
  46. *
  47. * @param design
  48. * the design from which to read
  49. * @param defMargin
  50. * the default margin state for edges that are not set in the
  51. * design
  52. * @param context
  53. * the DesignContext instance used for parsing the design
  54. * @return the margin info
  55. */
  56. protected MarginInfo readMargin(Element design, MarginInfo defMargin,
  57. DesignContext context) {
  58. if (design.hasAttr("margin")) {
  59. boolean margin = DesignAttributeHandler.readAttribute("margin",
  60. design.attributes(), boolean.class);
  61. return new MarginInfo(margin);
  62. } else {
  63. boolean left = DesignAttributeHandler.readAttribute("margin-left",
  64. design.attributes(), defMargin.hasLeft(), boolean.class);
  65. boolean right = DesignAttributeHandler.readAttribute("margin-right",
  66. design.attributes(), defMargin.hasRight(), boolean.class);
  67. boolean top = DesignAttributeHandler.readAttribute("margin-top",
  68. design.attributes(), defMargin.hasTop(), boolean.class);
  69. boolean bottom = DesignAttributeHandler.readAttribute(
  70. "margin-bottom", design.attributes(), defMargin.hasBottom(),
  71. boolean.class);
  72. return new MarginInfo(top, right, bottom, left);
  73. }
  74. }
  75. /**
  76. * Writes margin attributes from a MarginInfo object to a design. This
  77. * helper method should be called from the
  78. * {@link #readDesign(Element, DesignContext) writeDesign} method of layouts
  79. * that implement {@link MarginHandler}.
  80. *
  81. *
  82. * @since 7.5
  83. *
  84. * @param design
  85. * the design to write to
  86. * @param margin
  87. * the margin state to write
  88. * @param defMargin
  89. * the default margin state to compare against
  90. * @param context
  91. * the DesignContext instance used for parsing the design
  92. */
  93. protected void writeMargin(Element design, MarginInfo margin,
  94. MarginInfo defMargin, DesignContext context) {
  95. if (defMargin.getBitMask() == margin.getBitMask()) {
  96. // Default, no need to write
  97. } else if (margin.hasNone()) {
  98. // Write "margin='false'"
  99. DesignAttributeHandler.writeAttribute("margin", design.attributes(),
  100. false, true, boolean.class, context);
  101. } else if (margin.hasAll()) {
  102. // Write "margin"
  103. DesignAttributeHandler.writeAttribute("margin", design.attributes(),
  104. true, false, boolean.class, context);
  105. } else {
  106. DesignAttributeHandler.writeAttribute("margin-left",
  107. design.attributes(), margin.hasLeft(), defMargin.hasLeft(),
  108. boolean.class, context);
  109. DesignAttributeHandler.writeAttribute("margin-right",
  110. design.attributes(), margin.hasRight(),
  111. defMargin.hasRight(), boolean.class, context);
  112. DesignAttributeHandler.writeAttribute("margin-top",
  113. design.attributes(), margin.hasTop(), defMargin.hasTop(),
  114. boolean.class, context);
  115. DesignAttributeHandler.writeAttribute("margin-bottom",
  116. design.attributes(), margin.hasBottom(),
  117. defMargin.hasBottom(), boolean.class, context);
  118. }
  119. }
  120. }