您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LayoutManager.java 74KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851
  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.client;
  17. import java.util.Collection;
  18. import java.util.HashMap;
  19. import java.util.HashSet;
  20. import java.util.Map;
  21. import java.util.Set;
  22. import java.util.logging.Level;
  23. import java.util.logging.Logger;
  24. import com.google.gwt.core.client.Duration;
  25. import com.google.gwt.core.client.JsArrayString;
  26. import com.google.gwt.dom.client.Element;
  27. import com.google.gwt.dom.client.Style;
  28. import com.google.gwt.dom.client.Style.Overflow;
  29. import com.google.gwt.user.client.Timer;
  30. import com.vaadin.client.MeasuredSize.MeasureResult;
  31. import com.vaadin.client.ui.ManagedLayout;
  32. import com.vaadin.client.ui.PostLayoutListener;
  33. import com.vaadin.client.ui.SimpleManagedLayout;
  34. import com.vaadin.client.ui.VNotification;
  35. import com.vaadin.client.ui.layout.ElementResizeEvent;
  36. import com.vaadin.client.ui.layout.ElementResizeListener;
  37. import com.vaadin.client.ui.layout.LayoutDependencyTree;
  38. public class LayoutManager {
  39. private static final String STATE_CHANGE_MESSAGE = "Cannot run layout while processing state change from the server.";
  40. private static final String LOOP_ABORT_MESSAGE = "Aborting layout after 100 passes. This would probably be an infinite loop.";
  41. private static final boolean debugLogging = false;
  42. private ApplicationConnection connection;
  43. private final Set<Element> measuredNonConnectorElements = new HashSet<>();
  44. private final MeasuredSize nullSize = new MeasuredSize();
  45. private LayoutDependencyTree currentDependencyTree;
  46. private FastStringSet needsHorizontalLayout = FastStringSet.create();
  47. private FastStringSet needsVerticalLayout = FastStringSet.create();
  48. private FastStringSet needsMeasure = FastStringSet.create();
  49. private FastStringSet pendingOverflowFixes = FastStringSet.create();
  50. private final Map<Element, Collection<ElementResizeListener>> elementResizeListeners = new HashMap<>();
  51. private final Set<Element> listenersToFire = new HashSet<>();
  52. private boolean layoutPending = false;
  53. private Timer layoutTimer = new Timer() {
  54. @Override
  55. public void run() {
  56. layoutNow();
  57. }
  58. };
  59. private boolean everythingNeedsMeasure = false;
  60. /**
  61. * Sets the application connection this instance is connected to. Called
  62. * internally by the framework.
  63. *
  64. * @param connection
  65. * the application connection this instance is connected to
  66. */
  67. public void setConnection(ApplicationConnection connection) {
  68. if (this.connection != null) {
  69. throw new RuntimeException(
  70. "LayoutManager connection can never be changed");
  71. }
  72. this.connection = connection;
  73. }
  74. /**
  75. * Returns the application connection for this layout manager.
  76. *
  77. * @return connection
  78. */
  79. protected ApplicationConnection getConnection() {
  80. return connection;
  81. }
  82. /**
  83. * Gets the layout manager associated with the given
  84. * {@link ApplicationConnection}.
  85. *
  86. * @param connection
  87. * the application connection to get a layout manager for
  88. * @return the layout manager associated with the provided application
  89. * connection
  90. */
  91. public static LayoutManager get(ApplicationConnection connection) {
  92. return connection.getLayoutManager();
  93. }
  94. /**
  95. * Registers that a ManagedLayout is depending on the size of an Element.
  96. * This causes this layout manager to measure the element in the beginning
  97. * of every layout phase and call the appropriate layout method of the
  98. * managed layout if the size of the element has changed.
  99. *
  100. * @param owner
  101. * the ManagedLayout that depends on an element
  102. * @param element
  103. * the Element that should be measured
  104. */
  105. public void registerDependency(ManagedLayout owner, Element element) {
  106. MeasuredSize measuredSize = ensureMeasured(element);
  107. setNeedsLayout(owner);
  108. measuredSize.addDependent(owner.getConnectorId());
  109. }
  110. private MeasuredSize ensureMeasured(Element element) {
  111. MeasuredSize measuredSize = getMeasuredSize(element, null);
  112. if (measuredSize == null) {
  113. measuredSize = new MeasuredSize();
  114. if (ConnectorMap.get(connection).getConnector(element) == null) {
  115. measuredNonConnectorElements.add(element);
  116. }
  117. setMeasuredSize(element, measuredSize);
  118. }
  119. return measuredSize;
  120. }
  121. private boolean needsMeasure(Element e) {
  122. ComponentConnector connector = connection.getConnectorMap()
  123. .getConnector(e);
  124. if (connector != null && needsMeasureForManagedLayout(connector)) {
  125. return true;
  126. } else if (elementResizeListeners.containsKey(e)) {
  127. return true;
  128. }
  129. return getMeasuredSize(e, nullSize).hasDependents();
  130. }
  131. private boolean needsMeasureForManagedLayout(ComponentConnector connector) {
  132. if (connector instanceof ManagedLayout) {
  133. return true;
  134. }
  135. return connector.getParent() instanceof ManagedLayout;
  136. }
  137. /**
  138. * Assigns a measured size to an element. Method defined as protected for
  139. * legacy reasons.
  140. *
  141. * @param element
  142. * the dom element to attach the measured size to
  143. * @param measuredSize
  144. * the measured size to attach to the element. If
  145. * <code>null</code>, any previous measured size is removed.
  146. */
  147. protected native void setMeasuredSize(Element element,
  148. MeasuredSize measuredSize)
  149. /*-{
  150. if (measuredSize) {
  151. element.vMeasuredSize = measuredSize;
  152. } else {
  153. delete element.vMeasuredSize;
  154. }
  155. }-*/;
  156. /**
  157. * Gets the measured size for an element. Method defined as protected for
  158. * legacy reasons.
  159. *
  160. * @param element
  161. * The element to get measured size for
  162. * @param defaultSize
  163. * The size to return if no measured size could be found
  164. * @return The measured size for the element or {@literal defaultSize}
  165. */
  166. protected native MeasuredSize getMeasuredSize(Element element,
  167. MeasuredSize defaultSize)
  168. /*-{
  169. return element.vMeasuredSize || defaultSize;
  170. }-*/;
  171. private final MeasuredSize getMeasuredSize(Element element) {
  172. MeasuredSize measuredSize = getMeasuredSize(element, null);
  173. if (measuredSize == null) {
  174. measuredSize = new MeasuredSize();
  175. setMeasuredSize(element, measuredSize);
  176. }
  177. return measuredSize;
  178. }
  179. /**
  180. * Registers that a ManagedLayout is no longer depending on the size of an
  181. * Element.
  182. *
  183. * @see #registerDependency(ManagedLayout, Element)
  184. *
  185. * @param owner
  186. * the ManagedLayout no longer depends on an element
  187. * @param element
  188. * the Element that that no longer needs to be measured
  189. */
  190. public void unregisterDependency(ManagedLayout owner, Element element) {
  191. MeasuredSize measuredSize = getMeasuredSize(element, null);
  192. if (measuredSize == null) {
  193. return;
  194. }
  195. measuredSize.removeDependent(owner.getConnectorId());
  196. stopMeasuringIfUnecessary(element);
  197. }
  198. public boolean isLayoutRunning() {
  199. return currentDependencyTree != null;
  200. }
  201. private void countLayout(FastStringMap<Integer> layoutCounts,
  202. ManagedLayout layout) {
  203. Integer count = layoutCounts.get(layout.getConnectorId());
  204. if (count == null) {
  205. count = Integer.valueOf(0);
  206. } else {
  207. count = Integer.valueOf(count.intValue() + 1);
  208. }
  209. layoutCounts.put(layout.getConnectorId(), count);
  210. if (count.intValue() > 2) {
  211. getLogger().severe(Util.getConnectorString(layout)
  212. + " has been layouted " + count.intValue() + " times");
  213. }
  214. }
  215. public void layoutLater() {
  216. if (!layoutPending) {
  217. layoutPending = true;
  218. layoutTimer.schedule(100);
  219. }
  220. }
  221. public void layoutNow() {
  222. if (isLayoutRunning()) {
  223. throw new IllegalStateException(
  224. "Can't start a new layout phase before the previous layout phase ends.");
  225. }
  226. if (connection.getMessageHandler().isUpdatingState()) {
  227. // If assertions are enabled, throw an exception
  228. assert false : STATE_CHANGE_MESSAGE;
  229. // Else just log a warning and postpone the layout
  230. getLogger().warning(STATE_CHANGE_MESSAGE);
  231. // Framework will call layoutNow when the state update is completed
  232. return;
  233. }
  234. layoutPending = false;
  235. layoutTimer.cancel();
  236. try {
  237. currentDependencyTree = new LayoutDependencyTree(connection);
  238. doLayout();
  239. } finally {
  240. currentDependencyTree = null;
  241. }
  242. }
  243. /**
  244. * Called once per iteration in the layout loop before size calculations so
  245. * different browsers quirks can be handled. Mainly this exists for legacy
  246. * reasons.
  247. */
  248. protected void performBrowserLayoutHacks() {
  249. // Permutations implement this
  250. }
  251. private void doLayout() {
  252. getLogger().info("Starting layout phase");
  253. Profiler.enter("LayoutManager phase init");
  254. FastStringMap<Integer> layoutCounts = FastStringMap.create();
  255. int passes = 0;
  256. Duration totalDuration = new Duration();
  257. ConnectorMap connectorMap = ConnectorMap.get(connection);
  258. JsArrayString dump = needsHorizontalLayout.dump();
  259. int dumpLength = dump.length();
  260. for (int i = 0; i < dumpLength; i++) {
  261. String layoutId = dump.get(i);
  262. currentDependencyTree.setNeedsHorizontalLayout(layoutId, true);
  263. }
  264. dump = needsVerticalLayout.dump();
  265. dumpLength = dump.length();
  266. for (int i = 0; i < dumpLength; i++) {
  267. String layoutId = dump.get(i);
  268. currentDependencyTree.setNeedsVerticalLayout(layoutId, true);
  269. }
  270. needsHorizontalLayout = FastStringSet.create();
  271. needsVerticalLayout = FastStringSet.create();
  272. dump = needsMeasure.dump();
  273. dumpLength = dump.length();
  274. for (int i = 0; i < dumpLength; i++) {
  275. ServerConnector connector = connectorMap.getConnector(dump.get(i));
  276. if (connector != null) {
  277. currentDependencyTree
  278. .setNeedsMeasure((ComponentConnector) connector, true);
  279. }
  280. }
  281. needsMeasure = FastStringSet.create();
  282. measureNonConnectors();
  283. Profiler.leave("LayoutManager phase init");
  284. while (true) {
  285. Profiler.enter("Layout pass");
  286. passes++;
  287. performBrowserLayoutHacks();
  288. Profiler.enter("Layout measure connectors");
  289. int measuredConnectorCount = measureConnectors(
  290. currentDependencyTree, everythingNeedsMeasure);
  291. Profiler.leave("Layout measure connectors");
  292. everythingNeedsMeasure = false;
  293. if (measuredConnectorCount == 0) {
  294. getLogger().info("No more changes in pass " + passes);
  295. Profiler.leave("Layout pass");
  296. break;
  297. }
  298. int firedListeners = 0;
  299. if (!listenersToFire.isEmpty()) {
  300. HashSet<Element> listenersCopy = new HashSet<Element>(
  301. listenersToFire);
  302. listenersToFire.clear();
  303. firedListeners = listenersToFire.size();
  304. Profiler.enter("Layout fire resize events");
  305. for (Element element : listenersCopy) {
  306. Collection<ElementResizeListener> listeners = elementResizeListeners
  307. .get(element);
  308. if (listeners != null) {
  309. Profiler.enter(
  310. "Layout fire resize events - listeners not null");
  311. Profiler.enter(
  312. "ElementResizeListener.onElementResize copy list");
  313. ElementResizeListener[] array = listeners.toArray(
  314. new ElementResizeListener[listeners.size()]);
  315. Profiler.leave(
  316. "ElementResizeListener.onElementResize copy list");
  317. ElementResizeEvent event = new ElementResizeEvent(this,
  318. element);
  319. for (ElementResizeListener listener : array) {
  320. try {
  321. String key = null;
  322. if (Profiler.isEnabled()) {
  323. Profiler.enter(
  324. "ElementResizeListener.onElementResize construct profiler key");
  325. key = "ElementResizeListener.onElementResize for "
  326. + listener.getClass()
  327. .getSimpleName();
  328. Profiler.leave(
  329. "ElementResizeListener.onElementResize construct profiler key");
  330. Profiler.enter(key);
  331. }
  332. listener.onElementResize(event);
  333. if (Profiler.isEnabled()) {
  334. Profiler.leave(key);
  335. }
  336. } catch (RuntimeException e) {
  337. getLogger().log(Level.SEVERE,
  338. "Error in resize listener", e);
  339. }
  340. }
  341. Profiler.leave(
  342. "Layout fire resize events - listeners not null");
  343. }
  344. }
  345. Profiler.leave("Layout fire resize events");
  346. }
  347. Profiler.enter("LayoutManager handle ManagedLayout");
  348. FastStringSet updatedSet = FastStringSet.create();
  349. int layoutCount = 0;
  350. while (currentDependencyTree.hasHorizontalConnectorToLayout()
  351. || currentDependencyTree.hasVerticaConnectorToLayout()) {
  352. JsArrayString layoutTargets = currentDependencyTree
  353. .getHorizontalLayoutTargetsJsArray();
  354. int length = layoutTargets.length();
  355. for (int i = 0; i < length; i++) {
  356. ManagedLayout layout = (ManagedLayout) connectorMap
  357. .getConnector(layoutTargets.get(i));
  358. if (layout instanceof DirectionalManagedLayout) {
  359. currentDependencyTree
  360. .markAsHorizontallyLayouted(layout);
  361. DirectionalManagedLayout cl = (DirectionalManagedLayout) layout;
  362. try {
  363. String key = null;
  364. if (Profiler.isEnabled()) {
  365. key = "layoutHorizontally() for "
  366. + cl.getClass().getSimpleName();
  367. Profiler.enter(key);
  368. }
  369. cl.layoutHorizontally();
  370. layoutCount++;
  371. if (Profiler.isEnabled()) {
  372. Profiler.leave(key);
  373. }
  374. } catch (RuntimeException e) {
  375. getLogger().log(Level.SEVERE,
  376. "Error in ManagedLayout handling", e);
  377. }
  378. countLayout(layoutCounts, cl);
  379. } else {
  380. currentDependencyTree
  381. .markAsHorizontallyLayouted(layout);
  382. currentDependencyTree.markAsVerticallyLayouted(layout);
  383. SimpleManagedLayout rr = (SimpleManagedLayout) layout;
  384. try {
  385. String key = null;
  386. if (Profiler.isEnabled()) {
  387. key = "layout() for "
  388. + rr.getClass().getSimpleName();
  389. Profiler.enter(key);
  390. }
  391. rr.layout();
  392. layoutCount++;
  393. if (Profiler.isEnabled()) {
  394. Profiler.leave(key);
  395. }
  396. } catch (RuntimeException e) {
  397. getLogger().log(Level.SEVERE,
  398. "Error in SimpleManagedLayout (horizontal) handling",
  399. e);
  400. }
  401. countLayout(layoutCounts, rr);
  402. }
  403. if (debugLogging) {
  404. updatedSet.add(layout.getConnectorId());
  405. }
  406. }
  407. layoutTargets = currentDependencyTree
  408. .getVerticalLayoutTargetsJsArray();
  409. length = layoutTargets.length();
  410. for (int i = 0; i < length; i++) {
  411. ManagedLayout layout = (ManagedLayout) connectorMap
  412. .getConnector(layoutTargets.get(i));
  413. if (layout instanceof DirectionalManagedLayout) {
  414. currentDependencyTree.markAsVerticallyLayouted(layout);
  415. DirectionalManagedLayout cl = (DirectionalManagedLayout) layout;
  416. try {
  417. String key = null;
  418. if (Profiler.isEnabled()) {
  419. key = "layoutVertically() for "
  420. + cl.getClass().getSimpleName();
  421. Profiler.enter(key);
  422. }
  423. cl.layoutVertically();
  424. layoutCount++;
  425. if (Profiler.isEnabled()) {
  426. Profiler.leave(key);
  427. }
  428. } catch (RuntimeException e) {
  429. getLogger().log(Level.SEVERE,
  430. "Error in DirectionalManagedLayout handling",
  431. e);
  432. }
  433. countLayout(layoutCounts, cl);
  434. } else {
  435. currentDependencyTree
  436. .markAsHorizontallyLayouted(layout);
  437. currentDependencyTree.markAsVerticallyLayouted(layout);
  438. SimpleManagedLayout rr = (SimpleManagedLayout) layout;
  439. try {
  440. String key = null;
  441. if (Profiler.isEnabled()) {
  442. key = "layout() for "
  443. + rr.getClass().getSimpleName();
  444. Profiler.enter(key);
  445. }
  446. rr.layout();
  447. layoutCount++;
  448. if (Profiler.isEnabled()) {
  449. Profiler.leave(key);
  450. }
  451. } catch (RuntimeException e) {
  452. getLogger().log(Level.SEVERE,
  453. "Error in SimpleManagedLayout (vertical) handling",
  454. e);
  455. }
  456. countLayout(layoutCounts, rr);
  457. }
  458. if (debugLogging) {
  459. updatedSet.add(layout.getConnectorId());
  460. }
  461. }
  462. }
  463. Profiler.leave("LayoutManager handle ManagedLayout");
  464. if (debugLogging) {
  465. JsArrayString changedCids = updatedSet.dump();
  466. StringBuilder b = new StringBuilder(" ");
  467. b.append(changedCids.length());
  468. b.append(" requestLayout invocations ");
  469. if (changedCids.length() < 30) {
  470. for (int i = 0; i < changedCids.length(); i++) {
  471. if (i != 0) {
  472. b.append(", ");
  473. } else {
  474. b.append(": ");
  475. }
  476. String connectorString = changedCids.get(i);
  477. if (changedCids.length() < 10) {
  478. ServerConnector connector = ConnectorMap
  479. .get(connection)
  480. .getConnector(connectorString);
  481. connectorString = Util
  482. .getConnectorString(connector);
  483. }
  484. b.append(connectorString);
  485. }
  486. }
  487. getLogger().info(b.toString());
  488. }
  489. Profiler.leave("Layout pass");
  490. getLogger().info("Pass " + passes + " measured "
  491. + measuredConnectorCount + " elements, fired "
  492. + firedListeners + " listeners and did " + layoutCount
  493. + " layouts.");
  494. if (passes > 100) {
  495. getLogger().severe(LOOP_ABORT_MESSAGE);
  496. if (ApplicationConfiguration.isDebugMode()) {
  497. VNotification
  498. .createNotification(VNotification.DELAY_FOREVER,
  499. connection.getUIConnector().getWidget())
  500. .show(LOOP_ABORT_MESSAGE, VNotification.CENTERED,
  501. "error");
  502. }
  503. break;
  504. }
  505. }
  506. Profiler.enter("layout PostLayoutListener");
  507. JsArrayObject<ComponentConnector> componentConnectors = connectorMap
  508. .getComponentConnectorsAsJsArray();
  509. int size = componentConnectors.size();
  510. for (int i = 0; i < size; i++) {
  511. ComponentConnector connector = componentConnectors.get(i);
  512. if (connector instanceof PostLayoutListener) {
  513. String key = null;
  514. if (Profiler.isEnabled()) {
  515. key = "layout PostLayoutListener for "
  516. + connector.getClass().getSimpleName();
  517. Profiler.enter(key);
  518. }
  519. ((PostLayoutListener) connector).postLayout();
  520. if (Profiler.isEnabled()) {
  521. Profiler.leave(key);
  522. }
  523. }
  524. }
  525. Profiler.leave("layout PostLayoutListener");
  526. // Ensure temporary variables are cleaned
  527. if (!pendingOverflowFixes.isEmpty()) {
  528. getLogger().warning(
  529. "pendingOverflowFixes is not empty at the end of doLayout: "
  530. + pendingOverflowFixes.dump());
  531. pendingOverflowFixes = FastStringSet.create();
  532. }
  533. getLogger().info("Total layout phase time: "
  534. + totalDuration.elapsedMillis() + "ms");
  535. }
  536. private void logConnectorStatus(int connectorId) {
  537. currentDependencyTree.logDependencyStatus(
  538. (ComponentConnector) ConnectorMap.get(connection)
  539. .getConnector(Integer.toString(connectorId)));
  540. }
  541. private int measureConnectors(LayoutDependencyTree layoutDependencyTree,
  542. boolean measureAll) {
  543. Profiler.enter("Layout overflow fix handling");
  544. JsArrayString pendingOverflowConnectorsIds = pendingOverflowFixes
  545. .dump();
  546. int pendingOverflowCount = pendingOverflowConnectorsIds.length();
  547. ConnectorMap connectorMap = ConnectorMap.get(connection);
  548. if (pendingOverflowCount > 0) {
  549. HashMap<Element, String> originalOverflows = new HashMap<>();
  550. FastStringSet delayedOverflowFixes = FastStringSet.create();
  551. // First set overflow to hidden (and save previous value so it can
  552. // be restored later)
  553. for (int i = 0; i < pendingOverflowCount; i++) {
  554. String connectorId = pendingOverflowConnectorsIds.get(i);
  555. ComponentConnector componentConnector = (ComponentConnector) connectorMap
  556. .getConnector(connectorId);
  557. if (delayOverflowFix(componentConnector)) {
  558. delayedOverflowFixes.add(connectorId);
  559. continue;
  560. }
  561. if (debugLogging) {
  562. getLogger().info("Doing overflow fix for "
  563. + Util.getConnectorString(componentConnector)
  564. + " in " + Util.getConnectorString(
  565. componentConnector.getParent()));
  566. }
  567. Profiler.enter("Overflow fix apply");
  568. Element parentElement = componentConnector.getWidget()
  569. .getElement().getParentElement();
  570. Style style = parentElement.getStyle();
  571. String originalOverflow = style.getOverflow();
  572. if (originalOverflow != null
  573. && !originalOverflows.containsKey(parentElement)) {
  574. // Store original value for restore, but only the first time
  575. // the value is changed
  576. originalOverflows.put(parentElement, originalOverflow);
  577. }
  578. style.setOverflow(Overflow.HIDDEN);
  579. Profiler.leave("Overflow fix apply");
  580. }
  581. pendingOverflowFixes.removeAll(delayedOverflowFixes);
  582. JsArrayString remainingOverflowFixIds = pendingOverflowFixes.dump();
  583. int remainingCount = remainingOverflowFixIds.length();
  584. Profiler.enter("Overflow fix reflow");
  585. // Then ensure all scrolling elements are reflowed by measuring
  586. for (int i = 0; i < remainingCount; i++) {
  587. ComponentConnector componentConnector = (ComponentConnector) connectorMap
  588. .getConnector(remainingOverflowFixIds.get(i));
  589. componentConnector.getWidget().getElement().getParentElement()
  590. .getOffsetHeight();
  591. }
  592. Profiler.leave("Overflow fix reflow");
  593. Profiler.enter("Overflow fix restore");
  594. // Finally restore old overflow value and update bookkeeping
  595. for (int i = 0; i < remainingCount; i++) {
  596. String connectorId = remainingOverflowFixIds.get(i);
  597. ComponentConnector componentConnector = (ComponentConnector) connectorMap
  598. .getConnector(connectorId);
  599. Element parentElement = componentConnector.getWidget()
  600. .getElement().getParentElement();
  601. parentElement.getStyle().setProperty("overflow",
  602. originalOverflows.get(parentElement));
  603. layoutDependencyTree.setNeedsMeasure(componentConnector, true);
  604. }
  605. Profiler.leave("Overflow fix restore");
  606. if (!pendingOverflowFixes.isEmpty()) {
  607. getLogger().info(
  608. "Did overflow fix for " + remainingCount + " elements");
  609. }
  610. pendingOverflowFixes = delayedOverflowFixes;
  611. }
  612. Profiler.leave("Layout overflow fix handling");
  613. int measureCount = 0;
  614. if (measureAll) {
  615. Profiler.enter("Layout measureAll");
  616. JsArrayObject<ComponentConnector> allConnectors = connectorMap
  617. .getComponentConnectorsAsJsArray();
  618. int size = allConnectors.size();
  619. // Find connectors that should actually be measured
  620. JsArrayObject<ComponentConnector> connectors = JsArrayObject
  621. .createArray().cast();
  622. for (int i = 0; i < size; i++) {
  623. ComponentConnector candidate = allConnectors.get(i);
  624. if (!Util.shouldSkipMeasurementOfConnector(candidate)
  625. && needsMeasure(candidate.getWidget().getElement())) {
  626. connectors.add(candidate);
  627. }
  628. }
  629. int connectorCount = connectors.size();
  630. for (int i = 0; i < connectorCount; i++) {
  631. measureConnector(connectors.get(i));
  632. }
  633. for (int i = 0; i < connectorCount; i++) {
  634. layoutDependencyTree.setNeedsMeasure(connectors.get(i), false);
  635. }
  636. measureCount += connectorCount;
  637. Profiler.leave("Layout measureAll");
  638. }
  639. Profiler.enter("Layout measure from tree");
  640. while (layoutDependencyTree.hasConnectorsToMeasure()) {
  641. JsArrayString measureTargets = layoutDependencyTree
  642. .getMeasureTargetsJsArray();
  643. int length = measureTargets.length();
  644. for (int i = 0; i < length; i++) {
  645. ComponentConnector connector = (ComponentConnector) connectorMap
  646. .getConnector(measureTargets.get(i));
  647. measureConnector(connector);
  648. measureCount++;
  649. }
  650. for (int i = 0; i < length; i++) {
  651. ComponentConnector connector = (ComponentConnector) connectorMap
  652. .getConnector(measureTargets.get(i));
  653. layoutDependencyTree.setNeedsMeasure(connector, false);
  654. }
  655. }
  656. Profiler.leave("Layout measure from tree");
  657. return measureCount;
  658. }
  659. /*
  660. * Delay the overflow fix if the involved connectors might still change
  661. */
  662. private boolean delayOverflowFix(ComponentConnector componentConnector) {
  663. if (!currentDependencyTree.noMoreChangesExpected(componentConnector)) {
  664. return true;
  665. }
  666. ServerConnector parent = componentConnector.getParent();
  667. if (parent instanceof ComponentConnector && !currentDependencyTree
  668. .noMoreChangesExpected((ComponentConnector) parent)) {
  669. return true;
  670. }
  671. return false;
  672. }
  673. private void measureConnector(ComponentConnector connector) {
  674. Profiler.enter("LayoutManager.measureConnector");
  675. Element element = connector.getWidget().getElement();
  676. MeasuredSize measuredSize = getMeasuredSize(element);
  677. MeasureResult measureResult = measuredAndUpdate(element, measuredSize);
  678. if (measureResult.isChanged()) {
  679. onConnectorChange(connector, measureResult.isWidthChanged(),
  680. measureResult.isHeightChanged());
  681. }
  682. Profiler.leave("LayoutManager.measureConnector");
  683. }
  684. private void onConnectorChange(ComponentConnector connector,
  685. boolean widthChanged, boolean heightChanged) {
  686. Profiler.enter("LayoutManager.onConnectorChange");
  687. Profiler.enter("LayoutManager.onConnectorChange setNeedsOverflowFix");
  688. setNeedsOverflowFix(connector);
  689. Profiler.leave("LayoutManager.onConnectorChange setNeedsOverflowFix");
  690. Profiler.enter("LayoutManager.onConnectorChange heightChanged");
  691. if (heightChanged) {
  692. currentDependencyTree.markHeightAsChanged(connector);
  693. }
  694. Profiler.leave("LayoutManager.onConnectorChange heightChanged");
  695. Profiler.enter("LayoutManager.onConnectorChange widthChanged");
  696. if (widthChanged) {
  697. currentDependencyTree.markWidthAsChanged(connector);
  698. }
  699. Profiler.leave("LayoutManager.onConnectorChange widthChanged");
  700. Profiler.leave("LayoutManager.onConnectorChange");
  701. }
  702. private void setNeedsOverflowFix(ComponentConnector connector) {
  703. // IE9 doesn't need the original fix, but for some reason it needs this
  704. if (BrowserInfo.get().requiresOverflowAutoFix()) {
  705. ComponentConnector scrollingBoundary = currentDependencyTree
  706. .getScrollingBoundary(connector);
  707. if (scrollingBoundary != null) {
  708. pendingOverflowFixes.add(scrollingBoundary.getConnectorId());
  709. }
  710. }
  711. }
  712. private void measureNonConnectors() {
  713. Profiler.enter("LayoutManager.measureNonConenctors");
  714. for (Element element : measuredNonConnectorElements) {
  715. measuredAndUpdate(element, getMeasuredSize(element, null));
  716. }
  717. Profiler.leave("LayoutManager.measureNonConenctors");
  718. getLogger().info("Measured " + measuredNonConnectorElements.size()
  719. + " non connector elements");
  720. }
  721. private MeasureResult measuredAndUpdate(Element element,
  722. MeasuredSize measuredSize) {
  723. MeasureResult measureResult = measuredSize.measure(element);
  724. if (measureResult.isChanged()) {
  725. notifyListenersAndDepdendents(element,
  726. measureResult.isWidthChanged(),
  727. measureResult.isHeightChanged());
  728. }
  729. return measureResult;
  730. }
  731. private void notifyListenersAndDepdendents(Element element,
  732. boolean widthChanged, boolean heightChanged) {
  733. assert widthChanged || heightChanged;
  734. Profiler.enter("LayoutManager.notifyListenersAndDepdendents");
  735. MeasuredSize measuredSize = getMeasuredSize(element, nullSize);
  736. JsArrayString dependents = measuredSize.getDependents();
  737. for (int i = 0; i < dependents.length(); i++) {
  738. String pid = dependents.get(i);
  739. if (pid != null) {
  740. if (heightChanged) {
  741. currentDependencyTree.setNeedsVerticalLayout(pid, true);
  742. }
  743. if (widthChanged) {
  744. currentDependencyTree.setNeedsHorizontalLayout(pid, true);
  745. }
  746. }
  747. }
  748. if (elementResizeListeners.containsKey(element)) {
  749. listenersToFire.add(element);
  750. }
  751. Profiler.leave("LayoutManager.notifyListenersAndDepdendents");
  752. }
  753. private static boolean isManagedLayout(ComponentConnector connector) {
  754. return connector instanceof ManagedLayout;
  755. }
  756. public void forceLayout() {
  757. ConnectorMap connectorMap = connection.getConnectorMap();
  758. JsArrayObject<ComponentConnector> componentConnectors = connectorMap
  759. .getComponentConnectorsAsJsArray();
  760. int size = componentConnectors.size();
  761. for (int i = 0; i < size; i++) {
  762. ComponentConnector connector = componentConnectors.get(i);
  763. if (connector instanceof ManagedLayout) {
  764. setNeedsLayout((ManagedLayout) connector);
  765. }
  766. }
  767. setEverythingNeedsMeasure();
  768. layoutNow();
  769. }
  770. /**
  771. * Marks that a ManagedLayout should be layouted in the next layout phase
  772. * even if none of the elements managed by the layout have been resized.
  773. * <p>
  774. * This method should not be invoked during a layout phase since it only
  775. * controls what will happen in the beginning of the next phase. If you want
  776. * to explicitly cause some layout to be considered in an ongoing layout
  777. * phase, you should use {@link #setNeedsMeasure(ComponentConnector)}
  778. * instead.
  779. *
  780. * @param layout
  781. * the managed layout that should be layouted
  782. */
  783. public final void setNeedsLayout(ManagedLayout layout) {
  784. setNeedsHorizontalLayout(layout);
  785. setNeedsVerticalLayout(layout);
  786. }
  787. /**
  788. * Marks that a ManagedLayout should be layouted horizontally in the next
  789. * layout phase even if none of the elements managed by the layout have been
  790. * resized horizontally.
  791. * <p>
  792. * For SimpleManagedLayout which is always layouted in both directions, this
  793. * has the same effect as {@link #setNeedsLayout(ManagedLayout)}.
  794. * <p>
  795. * This method should not be invoked during a layout phase since it only
  796. * controls what will happen in the beginning of the next phase. If you want
  797. * to explicitly cause some layout to be considered in an ongoing layout
  798. * phase, you should use {@link #setNeedsMeasure(ComponentConnector)}
  799. * instead.
  800. *
  801. * @param layout
  802. * the managed layout that should be layouted
  803. */
  804. public final void setNeedsHorizontalLayout(ManagedLayout layout) {
  805. if (isLayoutRunning()) {
  806. getLogger().warning(
  807. "setNeedsHorizontalLayout should not be run while a layout phase is in progress.");
  808. }
  809. needsHorizontalLayout.add(layout.getConnectorId());
  810. }
  811. /**
  812. * Marks that a ManagedLayout should be layouted vertically in the next
  813. * layout phase even if none of the elements managed by the layout have been
  814. * resized vertically.
  815. * <p>
  816. * For SimpleManagedLayout which is always layouted in both directions, this
  817. * has the same effect as {@link #setNeedsLayout(ManagedLayout)}.
  818. * <p>
  819. * This method should not be invoked during a layout phase since it only
  820. * controls what will happen in the beginning of the next phase. If you want
  821. * to explicitly cause some layout to be considered in an ongoing layout
  822. * phase, you should use {@link #setNeedsMeasure(ComponentConnector)}
  823. * instead.
  824. *
  825. * @param layout
  826. * the managed layout that should be layouted
  827. */
  828. public final void setNeedsVerticalLayout(ManagedLayout layout) {
  829. if (isLayoutRunning()) {
  830. getLogger().warning(
  831. "setNeedsVerticalLayout should not be run while a layout phase is in progress.");
  832. }
  833. needsVerticalLayout.add(layout.getConnectorId());
  834. }
  835. /**
  836. * Gets the outer height (including margins, paddings and borders) of the
  837. * given element, provided that it has been measured. These elements are
  838. * guaranteed to be measured:
  839. * <ul>
  840. * <li>ManagedLayouts and their child Connectors
  841. * <li>Elements for which there is at least one ElementResizeListener
  842. * <li>Elements for which at least one ManagedLayout has registered a
  843. * dependency
  844. * </ul>
  845. *
  846. * -1 is returned if the element has not been measured. If 0 is returned, it
  847. * might indicate that the element is not attached to the DOM.
  848. * <p>
  849. * The value returned by this method is always rounded up. To get the exact
  850. * outer width, use {@link #getOuterHeightDouble(Element)}
  851. *
  852. * @param element
  853. * the element to get the measured size for
  854. * @return the measured outer height (including margins, paddings and
  855. * borders) of the element in pixels.
  856. */
  857. public final int getOuterHeight(Element element) {
  858. assert needsMeasure(
  859. element) : "Getting measurement for element that is not measured";
  860. return (int) Math
  861. .ceil(getMeasuredSize(element, nullSize).getOuterHeight());
  862. }
  863. /**
  864. * Gets the outer height (including margins, paddings and borders) of the
  865. * given element, provided that it has been measured. These elements are
  866. * guaranteed to be measured:
  867. * <ul>
  868. * <li>ManagedLayouts and their child Connectors
  869. * <li>Elements for which there is at least one ElementResizeListener
  870. * <li>Elements for which at least one ManagedLayout has registered a
  871. * dependency
  872. * </ul>
  873. *
  874. * -1 is returned if the element has not been measured. If 0 is returned, it
  875. * might indicate that the element is not attached to the DOM.
  876. *
  877. * @since 7.5.1
  878. * @param element
  879. * the element to get the measured size for
  880. * @return the measured outer height (including margins, paddings and
  881. * borders) of the element in pixels.
  882. */
  883. public final double getOuterHeightDouble(Element element) {
  884. assert needsMeasure(
  885. element) : "Getting measurement for element that is not measured";
  886. return getMeasuredSize(element, nullSize).getOuterHeight();
  887. }
  888. /**
  889. * Gets the outer width (including margins, paddings and borders) of the
  890. * given element, provided that it has been measured. These elements are
  891. * guaranteed to be measured:
  892. * <ul>
  893. * <li>ManagedLayouts and their child Connectors
  894. * <li>Elements for which there is at least one ElementResizeListener
  895. * <li>Elements for which at least one ManagedLayout has registered a
  896. * dependency
  897. * </ul>
  898. *
  899. * -1 is returned if the element has not been measured. If 0 is returned, it
  900. * might indicate that the element is not attached to the DOM.
  901. * <p>
  902. * The value returned by this method is always rounded up. To get the exact
  903. * outer width, use {@link #getOuterWidthDouble(Element)}
  904. *
  905. * @since 7.5.1
  906. * @param element
  907. * the element to get the measured size for
  908. * @return the measured outer width (including margins, paddings and
  909. * borders) of the element in pixels.
  910. */
  911. public final int getOuterWidth(Element element) {
  912. assert needsMeasure(
  913. element) : "Getting measurement for element that is not measured";
  914. return (int) Math
  915. .ceil(getMeasuredSize(element, nullSize).getOuterWidth());
  916. }
  917. /**
  918. * Gets the outer width (including margins, paddings and borders) of the
  919. * given element, provided that it has been measured. These elements are
  920. * guaranteed to be measured:
  921. * <ul>
  922. * <li>ManagedLayouts and their child Connectors
  923. * <li>Elements for which there is at least one ElementResizeListener
  924. * <li>Elements for which at least one ManagedLayout has registered a
  925. * dependency
  926. * </ul>
  927. *
  928. * -1 is returned if the element has not been measured. If 0 is returned, it
  929. * might indicate that the element is not attached to the DOM.
  930. *
  931. * @param element
  932. * the element to get the measured size for
  933. * @return the measured outer width (including margins, paddings and
  934. * borders) of the element in pixels.
  935. */
  936. public final double getOuterWidthDouble(Element element) {
  937. assert needsMeasure(
  938. element) : "Getting measurement for element that is not measured";
  939. return getMeasuredSize(element, nullSize).getOuterWidth();
  940. }
  941. /**
  942. * Gets the inner height (excluding margins, paddings and borders) of the
  943. * given element, provided that it has been measured. These elements are
  944. * guaranteed to be measured:
  945. * <ul>
  946. * <li>ManagedLayouts and their child Connectors
  947. * <li>Elements for which there is at least one ElementResizeListener
  948. * <li>Elements for which at least one ManagedLayout has registered a
  949. * dependency
  950. * </ul>
  951. *
  952. * -1 is returned if the element has not been measured. If 0 is returned, it
  953. * might indicate that the element is not attached to the DOM.
  954. * <p>
  955. * The value returned by this method is always rounded up. To get the exact
  956. * outer width, use {@link #getInnerHeightDouble(Element)}
  957. *
  958. * @param element
  959. * the element to get the measured size for
  960. * @return the measured inner height (excluding margins, paddings and
  961. * borders) of the element in pixels.
  962. */
  963. public final int getInnerHeight(Element element) {
  964. assert needsMeasure(
  965. element) : "Getting measurement for element that is not measured";
  966. return (int) Math
  967. .ceil(getMeasuredSize(element, nullSize).getInnerHeight());
  968. }
  969. /**
  970. * Gets the inner height (excluding margins, paddings and borders) of the
  971. * given element, provided that it has been measured. These elements are
  972. * guaranteed to be measured:
  973. * <ul>
  974. * <li>ManagedLayouts and their child Connectors
  975. * <li>Elements for which there is at least one ElementResizeListener
  976. * <li>Elements for which at least one ManagedLayout has registered a
  977. * dependency
  978. * </ul>
  979. *
  980. * -1 is returned if the element has not been measured. If 0 is returned, it
  981. * might indicate that the element is not attached to the DOM.
  982. *
  983. * @since 7.5.1
  984. * @param element
  985. * the element to get the measured size for
  986. * @return the measured inner height (excluding margins, paddings and
  987. * borders) of the element in pixels.
  988. */
  989. public final double getInnerHeightDouble(Element element) {
  990. assert needsMeasure(
  991. element) : "Getting measurement for element that is not measured";
  992. return getMeasuredSize(element, nullSize).getInnerHeight();
  993. }
  994. /**
  995. * Gets the inner width (excluding margins, paddings and borders) of the
  996. * given element, provided that it has been measured. These elements are
  997. * guaranteed to be measured:
  998. * <ul>
  999. * <li>ManagedLayouts and their child Connectors
  1000. * <li>Elements for which there is at least one ElementResizeListener
  1001. * <li>Elements for which at least one ManagedLayout has registered a
  1002. * dependency
  1003. * </ul>
  1004. *
  1005. * -1 is returned if the element has not been measured. If 0 is returned, it
  1006. * might indicate that the element is not attached to the DOM.
  1007. * <p>
  1008. * The value returned by this method is always rounded up. To get the exact
  1009. * outer width, use {@link #getOuterHeightDouble(Element)}
  1010. *
  1011. * @param element
  1012. * the element to get the measured size for
  1013. * @return the measured inner width (excluding margins, paddings and
  1014. * borders) of the element in pixels.
  1015. */
  1016. public final int getInnerWidth(Element element) {
  1017. assert needsMeasure(
  1018. element) : "Getting measurement for element that is not measured";
  1019. return (int) Math
  1020. .ceil(getMeasuredSize(element, nullSize).getInnerWidth());
  1021. }
  1022. /**
  1023. * Gets the inner width (excluding margins, paddings and borders) of the
  1024. * given element, provided that it has been measured. These elements are
  1025. * guaranteed to be measured:
  1026. * <ul>
  1027. * <li>ManagedLayouts and their child Connectors
  1028. * <li>Elements for which there is at least one ElementResizeListener
  1029. * <li>Elements for which at least one ManagedLayout has registered a
  1030. * dependency
  1031. * </ul>
  1032. *
  1033. * -1 is returned if the element has not been measured. If 0 is returned, it
  1034. * might indicate that the element is not attached to the DOM.
  1035. *
  1036. * @since 7.5.1
  1037. * @param element
  1038. * the element to get the measured size for
  1039. * @return the measured inner width (excluding margins, paddings and
  1040. * borders) of the element in pixels.
  1041. */
  1042. public final double getInnerWidthDouble(Element element) {
  1043. assert needsMeasure(
  1044. element) : "Getting measurement for element that is not measured";
  1045. return getMeasuredSize(element, nullSize).getInnerWidth();
  1046. }
  1047. /**
  1048. * Gets the border height (top border + bottom border) of the given element,
  1049. * provided that it has been measured. These elements are guaranteed to be
  1050. * measured:
  1051. * <ul>
  1052. * <li>ManagedLayouts and their child Connectors
  1053. * <li>Elements for which there is at least one ElementResizeListener
  1054. * <li>Elements for which at least one ManagedLayout has registered a
  1055. * dependency
  1056. * </ul>
  1057. *
  1058. * A negative number is returned if the element has not been measured. If 0
  1059. * is returned, it might indicate that the element is not attached to the
  1060. * DOM.
  1061. *
  1062. * @param element
  1063. * the element to get the measured size for
  1064. * @return the measured border height (top border + bottom border) of the
  1065. * element in pixels.
  1066. */
  1067. public final int getBorderHeight(Element element) {
  1068. assert needsMeasure(
  1069. element) : "Getting measurement for element that is not measured";
  1070. return getMeasuredSize(element, nullSize).getBorderHeight();
  1071. }
  1072. /**
  1073. * Gets the padding height (top padding + bottom padding) of the given
  1074. * element, provided that it has been measured. These elements are
  1075. * guaranteed to be measured:
  1076. * <ul>
  1077. * <li>ManagedLayouts and their child Connectors
  1078. * <li>Elements for which there is at least one ElementResizeListener
  1079. * <li>Elements for which at least one ManagedLayout has registered a
  1080. * dependency
  1081. * </ul>
  1082. *
  1083. * A negative number is returned if the element has not been measured. If 0
  1084. * is returned, it might indicate that the element is not attached to the
  1085. * DOM.
  1086. *
  1087. * @param element
  1088. * the element to get the measured size for
  1089. * @return the measured padding height (top padding + bottom padding) of the
  1090. * element in pixels.
  1091. */
  1092. public int getPaddingHeight(Element element) {
  1093. assert needsMeasure(
  1094. element) : "Getting measurement for element that is not measured";
  1095. return getMeasuredSize(element, nullSize).getPaddingHeight();
  1096. }
  1097. /**
  1098. * Gets the border width (left border + right border) of the given element,
  1099. * provided that it has been measured. These elements are guaranteed to be
  1100. * measured:
  1101. * <ul>
  1102. * <li>ManagedLayouts and their child Connectors
  1103. * <li>Elements for which there is at least one ElementResizeListener
  1104. * <li>Elements for which at least one ManagedLayout has registered a
  1105. * dependency
  1106. * </ul>
  1107. *
  1108. * A negative number is returned if the element has not been measured. If 0
  1109. * is returned, it might indicate that the element is not attached to the
  1110. * DOM.
  1111. *
  1112. * @param element
  1113. * the element to get the measured size for
  1114. * @return the measured border width (left border + right border) of the
  1115. * element in pixels.
  1116. */
  1117. public int getBorderWidth(Element element) {
  1118. assert needsMeasure(
  1119. element) : "Getting measurement for element that is not measured";
  1120. return getMeasuredSize(element, nullSize).getBorderWidth();
  1121. }
  1122. /**
  1123. * Gets the top border of the given element, provided that it has been
  1124. * measured. These elements are guaranteed to be measured:
  1125. * <ul>
  1126. * <li>ManagedLayouts and their child Connectors
  1127. * <li>Elements for which there is at least one ElementResizeListener
  1128. * <li>Elements for which at least one ManagedLayout has registered a
  1129. * dependency
  1130. * </ul>
  1131. *
  1132. * A negative number is returned if the element has not been measured. If 0
  1133. * is returned, it might indicate that the element is not attached to the
  1134. * DOM.
  1135. *
  1136. * @param element
  1137. * the element to get the measured size for
  1138. * @return the measured top border of the element in pixels.
  1139. */
  1140. public int getBorderTop(Element element) {
  1141. assert needsMeasure(
  1142. element) : "Getting measurement for element that is not measured";
  1143. return getMeasuredSize(element, nullSize).getBorderTop();
  1144. }
  1145. /**
  1146. * Gets the left border of the given element, provided that it has been
  1147. * measured. These elements are guaranteed to be measured:
  1148. * <ul>
  1149. * <li>ManagedLayouts and their child Connectors
  1150. * <li>Elements for which there is at least one ElementResizeListener
  1151. * <li>Elements for which at least one ManagedLayout has registered a
  1152. * dependency
  1153. * </ul>
  1154. *
  1155. * A negative number is returned if the element has not been measured. If 0
  1156. * is returned, it might indicate that the element is not attached to the
  1157. * DOM.
  1158. *
  1159. * @param element
  1160. * the element to get the measured size for
  1161. * @return the measured left border of the element in pixels.
  1162. */
  1163. public int getBorderLeft(Element element) {
  1164. assert needsMeasure(
  1165. element) : "Getting measurement for element that is not measured";
  1166. return getMeasuredSize(element, nullSize).getBorderLeft();
  1167. }
  1168. /**
  1169. * Gets the bottom border of the given element, provided that it has been
  1170. * measured. These elements are guaranteed to be measured:
  1171. * <ul>
  1172. * <li>ManagedLayouts and their child Connectors
  1173. * <li>Elements for which there is at least one ElementResizeListener
  1174. * <li>Elements for which at least one ManagedLayout has registered a
  1175. * dependency
  1176. * </ul>
  1177. *
  1178. * A negative number is returned if the element has not been measured. If 0
  1179. * is returned, it might indicate that the element is not attached to the
  1180. * DOM.
  1181. *
  1182. * @param element
  1183. * the element to get the measured size for
  1184. * @return the measured bottom border of the element in pixels.
  1185. */
  1186. public int getBorderBottom(Element element) {
  1187. assert needsMeasure(
  1188. element) : "Getting measurement for element that is not measured";
  1189. return getMeasuredSize(element, nullSize).getBorderBottom();
  1190. }
  1191. /**
  1192. * Gets the right border of the given element, provided that it has been
  1193. * measured. These elements are guaranteed to be measured:
  1194. * <ul>
  1195. * <li>ManagedLayouts and their child Connectors
  1196. * <li>Elements for which there is at least one ElementResizeListener
  1197. * <li>Elements for which at least one ManagedLayout has registered a
  1198. * dependency
  1199. * </ul>
  1200. *
  1201. * A negative number is returned if the element has not been measured. If 0
  1202. * is returned, it might indicate that the element is not attached to the
  1203. * DOM.
  1204. *
  1205. * @param element
  1206. * the element to get the measured size for
  1207. * @return the measured right border of the element in pixels.
  1208. */
  1209. public int getBorderRight(Element element) {
  1210. assert needsMeasure(
  1211. element) : "Getting measurement for element that is not measured";
  1212. return getMeasuredSize(element, nullSize).getBorderRight();
  1213. }
  1214. /**
  1215. * Gets the padding width (left padding + right padding) of the given
  1216. * element, provided that it has been measured. These elements are
  1217. * guaranteed to be measured:
  1218. * <ul>
  1219. * <li>ManagedLayouts and their child Connectors
  1220. * <li>Elements for which there is at least one ElementResizeListener
  1221. * <li>Elements for which at least one ManagedLayout has registered a
  1222. * dependency
  1223. * </ul>
  1224. *
  1225. * A negative number is returned if the element has not been measured. If 0
  1226. * is returned, it might indicate that the element is not attached to the
  1227. * DOM.
  1228. *
  1229. * @param element
  1230. * the element to get the measured size for
  1231. * @return the measured padding width (left padding + right padding) of the
  1232. * element in pixels.
  1233. */
  1234. public int getPaddingWidth(Element element) {
  1235. assert needsMeasure(
  1236. element) : "Getting measurement for element that is not measured";
  1237. return getMeasuredSize(element, nullSize).getPaddingWidth();
  1238. }
  1239. /**
  1240. * Gets the top padding of the given element, provided that it has been
  1241. * measured. These elements are guaranteed to be measured:
  1242. * <ul>
  1243. * <li>ManagedLayouts and their child Connectors
  1244. * <li>Elements for which there is at least one ElementResizeListener
  1245. * <li>Elements for which at least one ManagedLayout has registered a
  1246. * dependency
  1247. * </ul>
  1248. *
  1249. * A negative number is returned if the element has not been measured. If 0
  1250. * is returned, it might indicate that the element is not attached to the
  1251. * DOM.
  1252. *
  1253. * @param element
  1254. * the element to get the measured size for
  1255. * @return the measured top padding of the element in pixels.
  1256. */
  1257. public int getPaddingTop(Element element) {
  1258. assert needsMeasure(
  1259. element) : "Getting measurement for element that is not measured";
  1260. return getMeasuredSize(element, nullSize).getPaddingTop();
  1261. }
  1262. /**
  1263. * Gets the left padding of the given element, provided that it has been
  1264. * measured. These elements are guaranteed to be measured:
  1265. * <ul>
  1266. * <li>ManagedLayouts and their child Connectors
  1267. * <li>Elements for which there is at least one ElementResizeListener
  1268. * <li>Elements for which at least one ManagedLayout has registered a
  1269. * dependency
  1270. * </ul>
  1271. *
  1272. * A negative number is returned if the element has not been measured. If 0
  1273. * is returned, it might indicate that the element is not attached to the
  1274. * DOM.
  1275. *
  1276. * @param element
  1277. * the element to get the measured size for
  1278. * @return the measured left padding of the element in pixels.
  1279. */
  1280. public int getPaddingLeft(Element element) {
  1281. assert needsMeasure(
  1282. element) : "Getting measurement for element that is not measured";
  1283. return getMeasuredSize(element, nullSize).getPaddingLeft();
  1284. }
  1285. /**
  1286. * Gets the bottom padding of the given element, provided that it has been
  1287. * measured. These elements are guaranteed to be measured:
  1288. * <ul>
  1289. * <li>ManagedLayouts and their child Connectors
  1290. * <li>Elements for which there is at least one ElementResizeListener
  1291. * <li>Elements for which at least one ManagedLayout has registered a
  1292. * dependency
  1293. * </ul>
  1294. *
  1295. * A negative number is returned if the element has not been measured. If 0
  1296. * is returned, it might indicate that the element is not attached to the
  1297. * DOM.
  1298. *
  1299. * @param element
  1300. * the element to get the measured size for
  1301. * @return the measured bottom padding of the element in pixels.
  1302. */
  1303. public int getPaddingBottom(Element element) {
  1304. assert needsMeasure(
  1305. element) : "Getting measurement for element that is not measured";
  1306. return getMeasuredSize(element, nullSize).getPaddingBottom();
  1307. }
  1308. /**
  1309. * Gets the right padding of the given element, provided that it has been
  1310. * measured. These elements are guaranteed to be measured:
  1311. * <ul>
  1312. * <li>ManagedLayouts and their child Connectors
  1313. * <li>Elements for which there is at least one ElementResizeListener
  1314. * <li>Elements for which at least one ManagedLayout has registered a
  1315. * dependency
  1316. * </ul>
  1317. *
  1318. * A negative number is returned if the element has not been measured. If 0
  1319. * is returned, it might indicate that the element is not attached to the
  1320. * DOM.
  1321. *
  1322. * @param element
  1323. * the element to get the measured size for
  1324. * @return the measured right padding of the element in pixels.
  1325. */
  1326. public int getPaddingRight(Element element) {
  1327. assert needsMeasure(
  1328. element) : "Getting measurement for element that is not measured";
  1329. return getMeasuredSize(element, nullSize).getPaddingRight();
  1330. }
  1331. /**
  1332. * Gets the top margin of the given element, provided that it has been
  1333. * measured. These elements are guaranteed to be measured:
  1334. * <ul>
  1335. * <li>ManagedLayouts and their child Connectors
  1336. * <li>Elements for which there is at least one ElementResizeListener
  1337. * <li>Elements for which at least one ManagedLayout has registered a
  1338. * dependency
  1339. * </ul>
  1340. *
  1341. * A negative number is returned if the element has not been measured. If 0
  1342. * is returned, it might indicate that the element is not attached to the
  1343. * DOM.
  1344. *
  1345. * @param element
  1346. * the element to get the measured size for
  1347. * @return the measured top margin of the element in pixels.
  1348. */
  1349. public int getMarginTop(Element element) {
  1350. assert needsMeasure(
  1351. element) : "Getting measurement for element that is not measured";
  1352. return getMeasuredSize(element, nullSize).getMarginTop();
  1353. }
  1354. /**
  1355. * Gets the right margin of the given element, provided that it has been
  1356. * measured. These elements are guaranteed to be measured:
  1357. * <ul>
  1358. * <li>ManagedLayouts and their child Connectors
  1359. * <li>Elements for which there is at least one ElementResizeListener
  1360. * <li>Elements for which at least one ManagedLayout has registered a
  1361. * dependency
  1362. * </ul>
  1363. *
  1364. * A negative number is returned if the element has not been measured. If 0
  1365. * is returned, it might indicate that the element is not attached to the
  1366. * DOM.
  1367. *
  1368. * @param element
  1369. * the element to get the measured size for
  1370. * @return the measured right margin of the element in pixels.
  1371. */
  1372. public int getMarginRight(Element element) {
  1373. assert needsMeasure(
  1374. element) : "Getting measurement for element that is not measured";
  1375. return getMeasuredSize(element, nullSize).getMarginRight();
  1376. }
  1377. /**
  1378. * Gets the bottom margin of the given element, provided that it has been
  1379. * measured. These elements are guaranteed to be measured:
  1380. * <ul>
  1381. * <li>ManagedLayouts and their child Connectors
  1382. * <li>Elements for which there is at least one ElementResizeListener
  1383. * <li>Elements for which at least one ManagedLayout has registered a
  1384. * dependency
  1385. * </ul>
  1386. *
  1387. * A negative number is returned if the element has not been measured. If 0
  1388. * is returned, it might indicate that the element is not attached to the
  1389. * DOM.
  1390. *
  1391. * @param element
  1392. * the element to get the measured size for
  1393. * @return the measured bottom margin of the element in pixels.
  1394. */
  1395. public int getMarginBottom(Element element) {
  1396. assert needsMeasure(
  1397. element) : "Getting measurement for element that is not measured";
  1398. return getMeasuredSize(element, nullSize).getMarginBottom();
  1399. }
  1400. /**
  1401. * Gets the left margin of the given element, provided that it has been
  1402. * measured. These elements are guaranteed to be measured:
  1403. * <ul>
  1404. * <li>ManagedLayouts and their child Connectors
  1405. * <li>Elements for which there is at least one ElementResizeListener
  1406. * <li>Elements for which at least one ManagedLayout has registered a
  1407. * dependency
  1408. * </ul>
  1409. *
  1410. * A negative number is returned if the element has not been measured. If 0
  1411. * is returned, it might indicate that the element is not attached to the
  1412. * DOM.
  1413. *
  1414. * @param element
  1415. * the element to get the measured size for
  1416. * @return the measured left margin of the element in pixels.
  1417. */
  1418. public int getMarginLeft(Element element) {
  1419. assert needsMeasure(
  1420. element) : "Getting measurement for element that is not measured";
  1421. return getMeasuredSize(element, nullSize).getMarginLeft();
  1422. }
  1423. /**
  1424. * Gets the combined top & bottom margin of the given element, provided that
  1425. * they have been measured. These elements are guaranteed to be measured:
  1426. * <ul>
  1427. * <li>ManagedLayouts and their child Connectors
  1428. * <li>Elements for which there is at least one ElementResizeListener
  1429. * <li>Elements for which at least one ManagedLayout has registered a
  1430. * dependency
  1431. * </ul>
  1432. *
  1433. * A negative number is returned if the element has not been measured. If 0
  1434. * is returned, it might indicate that the element is not attached to the
  1435. * DOM.
  1436. *
  1437. * @param element
  1438. * the element to get the measured margin for
  1439. * @return the measured top+bottom margin of the element in pixels.
  1440. */
  1441. public int getMarginHeight(Element element) {
  1442. return getMarginTop(element) + getMarginBottom(element);
  1443. }
  1444. /**
  1445. * Gets the combined left & right margin of the given element, provided that
  1446. * they have been measured. These elements are guaranteed to be measured:
  1447. * <ul>
  1448. * <li>ManagedLayouts and their child Connectors
  1449. * <li>Elements for which there is at least one ElementResizeListener
  1450. * <li>Elements for which at least one ManagedLayout has registered a
  1451. * dependency
  1452. * </ul>
  1453. *
  1454. * A negative number is returned if the element has not been measured. If 0
  1455. * is returned, it might indicate that the element is not attached to the
  1456. * DOM.
  1457. *
  1458. * @param element
  1459. * the element to get the measured margin for
  1460. * @return the measured left+right margin of the element in pixels.
  1461. */
  1462. public int getMarginWidth(Element element) {
  1463. return getMarginLeft(element) + getMarginRight(element);
  1464. }
  1465. /**
  1466. * Registers the outer height (including margins, borders and paddings) of a
  1467. * component. This can be used as an optimization by ManagedLayouts; by
  1468. * informing the LayoutManager about what size a component will have, the
  1469. * layout propagation can continue directly without first measuring the
  1470. * potentially resized elements.
  1471. *
  1472. * @param component
  1473. * the component for which the size is reported
  1474. * @param outerHeight
  1475. * the new outer height (including margins, borders and paddings)
  1476. * of the component in pixels
  1477. */
  1478. public void reportOuterHeight(ComponentConnector component,
  1479. int outerHeight) {
  1480. Element element = component.getWidget().getElement();
  1481. MeasuredSize measuredSize = getMeasuredSize(element);
  1482. if (isLayoutRunning()) {
  1483. boolean heightChanged = measuredSize.setOuterHeight(outerHeight);
  1484. if (heightChanged) {
  1485. onConnectorChange(component, false, true);
  1486. notifyListenersAndDepdendents(element, false, true);
  1487. }
  1488. currentDependencyTree.setNeedsVerticalMeasure(component, false);
  1489. } else if (measuredSize.getOuterHeight() != outerHeight) {
  1490. setNeedsMeasure(component);
  1491. }
  1492. }
  1493. /**
  1494. * Registers the height reserved for a relatively sized component. This can
  1495. * be used as an optimization by ManagedLayouts; by informing the
  1496. * LayoutManager about what size a component will have, the layout
  1497. * propagation can continue directly without first measuring the potentially
  1498. * resized elements.
  1499. *
  1500. * @param component
  1501. * the relatively sized component for which the size is reported
  1502. * @param assignedHeight
  1503. * the inner height of the relatively sized component's parent
  1504. * element in pixels
  1505. */
  1506. public void reportHeightAssignedToRelative(ComponentConnector component,
  1507. int assignedHeight) {
  1508. assert component.isRelativeHeight();
  1509. float percentSize = parsePercent(component.getState().height == null
  1510. ? "" : component.getState().height);
  1511. int effectiveHeight = Math.round(assignedHeight * (percentSize / 100));
  1512. reportOuterHeight(component, effectiveHeight);
  1513. }
  1514. /**
  1515. * Registers the width reserved for a relatively sized component. This can
  1516. * be used as an optimization by ManagedLayouts; by informing the
  1517. * LayoutManager about what size a component will have, the layout
  1518. * propagation can continue directly without first measuring the potentially
  1519. * resized elements.
  1520. *
  1521. * @param component
  1522. * the relatively sized component for which the size is reported
  1523. * @param assignedWidth
  1524. * the inner width of the relatively sized component's parent
  1525. * element in pixels
  1526. */
  1527. public void reportWidthAssignedToRelative(ComponentConnector component,
  1528. int assignedWidth) {
  1529. assert component.isRelativeWidth();
  1530. float percentSize = parsePercent(component.getState().width == null ? ""
  1531. : component.getState().width);
  1532. int effectiveWidth = Math.round(assignedWidth * (percentSize / 100));
  1533. reportOuterWidth(component, effectiveWidth);
  1534. }
  1535. private static float parsePercent(String size) {
  1536. return Float.parseFloat(size.substring(0, size.length() - 1));
  1537. }
  1538. /**
  1539. * Registers the outer width (including margins, borders and paddings) of a
  1540. * component. This can be used as an optimization by ManagedLayouts; by
  1541. * informing the LayoutManager about what size a component will have, the
  1542. * layout propagation can continue directly without first measuring the
  1543. * potentially resized elements.
  1544. *
  1545. * @param component
  1546. * the component for which the size is reported
  1547. * @param outerWidth
  1548. * the new outer width (including margins, borders and paddings)
  1549. * of the component in pixels
  1550. */
  1551. public void reportOuterWidth(ComponentConnector component, int outerWidth) {
  1552. Element element = component.getWidget().getElement();
  1553. MeasuredSize measuredSize = getMeasuredSize(element);
  1554. if (isLayoutRunning()) {
  1555. boolean widthChanged = measuredSize.setOuterWidth(outerWidth);
  1556. if (widthChanged) {
  1557. onConnectorChange(component, true, false);
  1558. notifyListenersAndDepdendents(element, true, false);
  1559. }
  1560. currentDependencyTree.setNeedsHorizontalMeasure(component, false);
  1561. } else if (measuredSize.getOuterWidth() != outerWidth) {
  1562. setNeedsMeasure(component);
  1563. }
  1564. }
  1565. /**
  1566. * Adds a listener that will be notified whenever the size of a specific
  1567. * element changes. Adding a listener to an element also ensures that all
  1568. * sizes for that element will be available starting from the next layout
  1569. * phase.
  1570. *
  1571. * @param element
  1572. * the element that should be checked for size changes
  1573. * @param listener
  1574. * an ElementResizeListener that will be informed whenever the
  1575. * size of the target element has changed
  1576. */
  1577. public void addElementResizeListener(Element element,
  1578. ElementResizeListener listener) {
  1579. Collection<ElementResizeListener> listeners = elementResizeListeners
  1580. .get(element);
  1581. if (listeners == null) {
  1582. listeners = new HashSet<>();
  1583. elementResizeListeners.put(element, listeners);
  1584. ensureMeasured(element);
  1585. }
  1586. listeners.add(listener);
  1587. }
  1588. /**
  1589. * Removes an element resize listener from the provided element. This might
  1590. * cause this LayoutManager to stop tracking the size of the element if no
  1591. * other sources are interested in the size.
  1592. *
  1593. * @param element
  1594. * the element to which the element resize listener was
  1595. * previously added
  1596. * @param listener
  1597. * the ElementResizeListener that should no longer get informed
  1598. * about size changes to the target element.
  1599. */
  1600. public void removeElementResizeListener(Element element,
  1601. ElementResizeListener listener) {
  1602. Collection<ElementResizeListener> listeners = elementResizeListeners
  1603. .get(element);
  1604. if (listeners != null) {
  1605. listeners.remove(listener);
  1606. if (listeners.isEmpty()) {
  1607. elementResizeListeners.remove(element);
  1608. stopMeasuringIfUnecessary(element);
  1609. }
  1610. }
  1611. }
  1612. private void stopMeasuringIfUnecessary(Element element) {
  1613. if (!needsMeasure(element)) {
  1614. measuredNonConnectorElements.remove(element);
  1615. setMeasuredSize(element, null);
  1616. }
  1617. }
  1618. /**
  1619. * Informs this LayoutManager that the size of a component might have
  1620. * changed. This method should be used whenever the size of an individual
  1621. * component might have changed from outside of Vaadin's normal update
  1622. * phase, e.g. when an icon has been loaded or when the user resizes some
  1623. * part of the UI using the mouse.
  1624. * <p>
  1625. * To set an entire component hierarchy to be measured, use
  1626. * {@link #setNeedsMeasureRecursively(ComponentConnector)} instead.
  1627. * <p>
  1628. * If there is no upcoming layout phase, a new layout phase is scheduled.
  1629. *
  1630. * @param component
  1631. * the component whose size might have changed.
  1632. */
  1633. public void setNeedsMeasure(ComponentConnector component) {
  1634. if (isLayoutRunning()) {
  1635. currentDependencyTree.setNeedsMeasure(component, true);
  1636. } else {
  1637. needsMeasure.add(component.getConnectorId());
  1638. layoutLater();
  1639. }
  1640. }
  1641. /**
  1642. * Informs this LayoutManager that some sizes in a component hierarchy might
  1643. * have changed. This method should be used whenever the size of any child
  1644. * component might have changed from outside of Vaadin's normal update
  1645. * phase, e.g. when a CSS class name related to sizing has been changed.
  1646. * <p>
  1647. * To set a single component to be measured, use
  1648. * {@link #setNeedsMeasure(ComponentConnector)} instead.
  1649. * <p>
  1650. * If there is no upcoming layout phase, a new layout phase is scheduled.
  1651. *
  1652. * @since 7.2
  1653. * @param component
  1654. * the component at the root of the component hierarchy to
  1655. * measure
  1656. */
  1657. public void setNeedsMeasureRecursively(ComponentConnector component) {
  1658. setNeedsMeasure(component);
  1659. if (component instanceof HasComponentsConnector) {
  1660. HasComponentsConnector hasComponents = (HasComponentsConnector) component;
  1661. for (ComponentConnector child : hasComponents
  1662. .getChildComponents()) {
  1663. setNeedsMeasureRecursively(child);
  1664. }
  1665. }
  1666. }
  1667. public void setEverythingNeedsMeasure() {
  1668. everythingNeedsMeasure = true;
  1669. }
  1670. private static Logger getLogger() {
  1671. return Logger.getLogger(LayoutManager.class.getName());
  1672. }
  1673. /**
  1674. * Checks if there is something waiting for a layout to take place.
  1675. *
  1676. * @since 7.5.6
  1677. * @return true if there are connectors waiting for measurement or layout,
  1678. * false otherwise
  1679. */
  1680. public boolean isLayoutNeeded() {
  1681. if (!needsHorizontalLayout.isEmpty()
  1682. || !needsVerticalLayout.isEmpty()) {
  1683. return true;
  1684. }
  1685. if (!needsMeasure.isEmpty()) {
  1686. return true;
  1687. }
  1688. if (everythingNeedsMeasure) {
  1689. return true;
  1690. }
  1691. return false;
  1692. }
  1693. }