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.

MarginInfo.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.shared.ui;
  17. import java.io.Serializable;
  18. /**
  19. * Describes the margin settings for each edge of a Component.
  20. *
  21. * @author Vaadin Ltd
  22. */
  23. public class MarginInfo implements Serializable {
  24. private static final int TOP = 1;
  25. private static final int RIGHT = 2;
  26. private static final int BOTTOM = 4;
  27. private static final int LEFT = 8;
  28. private static final int ALL = TOP | RIGHT | BOTTOM | LEFT;
  29. private int bitMask;
  30. /**
  31. * Creates a MarginInfo object with all edges set to either enabled or
  32. * disabled.
  33. *
  34. * @param enabled
  35. * the value to set for all edges
  36. */
  37. public MarginInfo(boolean enabled) {
  38. setMargins(enabled);
  39. }
  40. /**
  41. * Creates a MarginInfo object from a bit mask.
  42. *
  43. * @param bitMask
  44. * bits to set
  45. * @deprecated use other constructors instead of this one
  46. */
  47. @Deprecated
  48. public MarginInfo(int bitMask) {
  49. this.bitMask = bitMask;
  50. }
  51. /**
  52. * Creates a MarginInfo object by having each edge specified in clockwise
  53. * order (analogous to CSS).
  54. *
  55. * @param top
  56. * enable or disable top margin
  57. * @param right
  58. * enable or disable right margin
  59. * @param bottom
  60. * enable or disable bottom margin
  61. * @param left
  62. * enable or disable left margin
  63. */
  64. public MarginInfo(boolean top, boolean right, boolean bottom,
  65. boolean left) {
  66. doSetMargins(top, right, bottom, left);
  67. }
  68. /**
  69. * Creates a MarginInfo object by having horizontal and vertical margins
  70. * specified (analogous to CSS).
  71. *
  72. * @since 7.6.5
  73. *
  74. * @param vertical
  75. * enable or disable top and bottom margins
  76. * @param horizontal
  77. * enable or disable left and right margins
  78. */
  79. public MarginInfo(boolean vertical, boolean horizontal) {
  80. this(vertical, horizontal, vertical, horizontal);
  81. }
  82. /**
  83. * Creates a MarginInfo with the same values as another MarginInfo object.
  84. *
  85. * @param other
  86. * another MarginInfo object
  87. */
  88. public MarginInfo(MarginInfo other) {
  89. setMargins(other);
  90. }
  91. /**
  92. * Enables or disables margins on all edges simultaneously.
  93. *
  94. * @param enabled
  95. * if true, enables margins on all edges. If false, disables
  96. * margins on all edges.
  97. */
  98. public void setMargins(boolean enabled) {
  99. bitMask = enabled ? ALL : 0;
  100. }
  101. /**
  102. * Sets margins on all edges individually.
  103. *
  104. * @param top
  105. * enable or disable top margin
  106. * @param right
  107. * enable or disable right margin
  108. * @param bottom
  109. * enable or disable bottom margin
  110. * @param left
  111. * enable or disable left margin
  112. */
  113. public void setMargins(boolean top, boolean right, boolean bottom,
  114. boolean left) {
  115. doSetMargins(top, right, bottom, left);
  116. }
  117. /**
  118. * Copies margin values from another MarginInfo object.
  119. *
  120. * @param marginInfo
  121. * another marginInfo object
  122. */
  123. public void setMargins(MarginInfo marginInfo) {
  124. bitMask = marginInfo.bitMask;
  125. }
  126. /**
  127. * Checks if this MarginInfo object has margins on all edges enabled.
  128. *
  129. * @since 7.5.0
  130. *
  131. * @return true if all edges have margins enabled
  132. */
  133. public boolean hasAll() {
  134. return (bitMask & ALL) == ALL;
  135. }
  136. /**
  137. * Checks if this MarginInfo object has no margins enabled.
  138. *
  139. * @since 8.0
  140. *
  141. * @return true if all edges have margins disabled
  142. */
  143. public boolean hasNone() {
  144. return (bitMask & ALL) == 0;
  145. }
  146. /**
  147. * Checks if this MarginInfo object has the left edge margin enabled.
  148. *
  149. * @return true if left edge margin is enabled
  150. */
  151. public boolean hasLeft() {
  152. return (bitMask & LEFT) == LEFT;
  153. }
  154. /**
  155. * Checks if this MarginInfo object has the right edge margin enabled.
  156. *
  157. * @return true if right edge margin is enabled
  158. */
  159. public boolean hasRight() {
  160. return (bitMask & RIGHT) == RIGHT;
  161. }
  162. /**
  163. * Checks if this MarginInfo object has the top edge margin enabled.
  164. *
  165. * @return true if top edge margin is enabled
  166. */
  167. public boolean hasTop() {
  168. return (bitMask & TOP) == TOP;
  169. }
  170. /**
  171. * Checks if this MarginInfo object has the bottom edge margin enabled.
  172. *
  173. * @return true if bottom edge margin is enabled
  174. */
  175. public boolean hasBottom() {
  176. return (bitMask & BOTTOM) == BOTTOM;
  177. }
  178. /**
  179. * Returns the current bit mask that make up the margin settings.
  180. * <p>
  181. * This method is for internal use by the framework.
  182. *
  183. * @return an integer bit mask
  184. */
  185. @Deprecated
  186. public int getBitMask() {
  187. return bitMask;
  188. }
  189. @Override
  190. public boolean equals(Object obj) {
  191. if (!(obj instanceof MarginInfo)) {
  192. return false;
  193. }
  194. return ((MarginInfo) obj).bitMask == bitMask;
  195. }
  196. @Override
  197. public int hashCode() {
  198. return bitMask;
  199. }
  200. @Override
  201. public String toString() {
  202. return "MarginInfo(" + hasTop() + ", " + hasRight() + ", " + hasBottom()
  203. + ", " + hasLeft() + ")";
  204. }
  205. private void doSetMargins(boolean top, boolean right, boolean bottom,
  206. boolean left) {
  207. bitMask = top ? TOP : 0;
  208. bitMask += right ? RIGHT : 0;
  209. bitMask += bottom ? BOTTOM : 0;
  210. bitMask += left ? LEFT : 0;
  211. }
  212. }