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.

MeasuredSize.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright 2011 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.terminal.gwt.client;
  17. import com.google.gwt.core.client.JsArrayString;
  18. import com.google.gwt.dom.client.Element;
  19. public class MeasuredSize {
  20. public static class MeasureResult {
  21. private final boolean widthChanged;
  22. private final boolean heightChanged;
  23. private MeasureResult(boolean widthChanged, boolean heightChanged) {
  24. this.widthChanged = widthChanged;
  25. this.heightChanged = heightChanged;
  26. }
  27. public boolean isHeightChanged() {
  28. return heightChanged;
  29. }
  30. public boolean isWidthChanged() {
  31. return widthChanged;
  32. }
  33. public boolean isChanged() {
  34. return heightChanged || widthChanged;
  35. }
  36. }
  37. private int width = -1;
  38. private int height = -1;
  39. private int[] paddings = new int[4];
  40. private int[] borders = new int[4];
  41. private int[] margins = new int[4];
  42. private FastStringSet dependents = FastStringSet.create();
  43. public int getOuterHeight() {
  44. return height;
  45. }
  46. public int getOuterWidth() {
  47. return width;
  48. }
  49. public void addDependent(String pid) {
  50. dependents.add(pid);
  51. }
  52. public void removeDependent(String pid) {
  53. dependents.remove(pid);
  54. }
  55. public boolean hasDependents() {
  56. return !dependents.isEmpty();
  57. }
  58. public JsArrayString getDependents() {
  59. return dependents.dump();
  60. }
  61. private static int sumWidths(int[] sizes) {
  62. return sizes[1] + sizes[3];
  63. }
  64. private static int sumHeights(int[] sizes) {
  65. return sizes[0] + sizes[2];
  66. }
  67. public int getInnerHeight() {
  68. return height - sumHeights(margins) - sumHeights(borders)
  69. - sumHeights(paddings);
  70. }
  71. public int getInnerWidth() {
  72. return width - sumWidths(margins) - sumWidths(borders)
  73. - sumWidths(paddings);
  74. }
  75. public boolean setOuterHeight(int height) {
  76. if (this.height != height) {
  77. this.height = height;
  78. return true;
  79. } else {
  80. return false;
  81. }
  82. }
  83. public boolean setOuterWidth(int width) {
  84. if (this.width != width) {
  85. this.width = width;
  86. return true;
  87. } else {
  88. return false;
  89. }
  90. }
  91. public int getBorderHeight() {
  92. return sumHeights(borders);
  93. }
  94. public int getBorderWidth() {
  95. return sumWidths(borders);
  96. }
  97. public int getPaddingHeight() {
  98. return sumHeights(paddings);
  99. }
  100. public int getPaddingWidth() {
  101. return sumWidths(paddings);
  102. }
  103. public int getMarginHeight() {
  104. return sumHeights(margins);
  105. }
  106. public int getMarginWidth() {
  107. return sumWidths(margins);
  108. }
  109. public int getMarginTop() {
  110. return margins[0];
  111. }
  112. public int getMarginRight() {
  113. return margins[1];
  114. }
  115. public int getMarginBottom() {
  116. return margins[2];
  117. }
  118. public int getMarginLeft() {
  119. return margins[3];
  120. }
  121. public int getBorderTop() {
  122. return margins[0];
  123. }
  124. public int getBorderRight() {
  125. return margins[1];
  126. }
  127. public int getBorderBottom() {
  128. return margins[2];
  129. }
  130. public int getBorderLeft() {
  131. return margins[3];
  132. }
  133. public int getPaddingTop() {
  134. return paddings[0];
  135. }
  136. public int getPaddingRight() {
  137. return paddings[1];
  138. }
  139. public int getPaddingBottom() {
  140. return paddings[2];
  141. }
  142. public int getPaddingLeft() {
  143. return paddings[3];
  144. }
  145. public MeasureResult measure(Element element) {
  146. boolean heightChanged = false;
  147. boolean widthChanged = false;
  148. ComputedStyle computedStyle = new ComputedStyle(element);
  149. int[] paddings = computedStyle.getPadding();
  150. if (!heightChanged && hasHeightChanged(this.paddings, paddings)) {
  151. heightChanged = true;
  152. }
  153. if (!widthChanged && hasWidthChanged(this.paddings, paddings)) {
  154. widthChanged = true;
  155. }
  156. this.paddings = paddings;
  157. int[] margins = computedStyle.getMargin();
  158. if (!heightChanged && hasHeightChanged(this.margins, margins)) {
  159. heightChanged = true;
  160. }
  161. if (!widthChanged && hasWidthChanged(this.margins, margins)) {
  162. widthChanged = true;
  163. }
  164. this.margins = margins;
  165. int[] borders = computedStyle.getBorder();
  166. if (!heightChanged && hasHeightChanged(this.borders, borders)) {
  167. heightChanged = true;
  168. }
  169. if (!widthChanged && hasWidthChanged(this.borders, borders)) {
  170. widthChanged = true;
  171. }
  172. this.borders = borders;
  173. int requiredHeight = Util.getRequiredHeight(element);
  174. int marginHeight = sumHeights(margins);
  175. if (setOuterHeight(requiredHeight + marginHeight)) {
  176. heightChanged = true;
  177. }
  178. int requiredWidth = Util.getRequiredWidth(element);
  179. int marginWidth = sumWidths(margins);
  180. if (setOuterWidth(requiredWidth + marginWidth)) {
  181. widthChanged = true;
  182. }
  183. return new MeasureResult(widthChanged, heightChanged);
  184. }
  185. private static boolean hasWidthChanged(int[] sizes1, int[] sizes2) {
  186. return sizes1[1] != sizes2[1] || sizes1[3] != sizes2[3];
  187. }
  188. private static boolean hasHeightChanged(int[] sizes1, int[] sizes2) {
  189. return sizes1[0] != sizes2[0] || sizes1[2] != sizes2[2];
  190. }
  191. }