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.

LayoutManager.java 74KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852
  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.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 DEBUG_LOGGING = 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 (DEBUG_LOGGING) {
  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 (DEBUG_LOGGING) {
  459. updatedSet.add(layout.getConnectorId());
  460. }
  461. }
  462. }
  463. Profiler.leave("LayoutManager handle ManagedLayout");
  464. if (DEBUG_LOGGING) {
  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. Map<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 (DEBUG_LOGGING) {
  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(
  1510. component.getState().height == null ? ""
  1511. : component.getState().height);
  1512. int effectiveHeight = Math.round(assignedHeight * (percentSize / 100));
  1513. reportOuterHeight(component, effectiveHeight);
  1514. }
  1515. /**
  1516. * Registers the width reserved for a relatively sized component. This can
  1517. * be used as an optimization by ManagedLayouts; by informing the
  1518. * LayoutManager about what size a component will have, the layout
  1519. * propagation can continue directly without first measuring the potentially
  1520. * resized elements.
  1521. *
  1522. * @param component
  1523. * the relatively sized component for which the size is reported
  1524. * @param assignedWidth
  1525. * the inner width of the relatively sized component's parent
  1526. * element in pixels
  1527. */
  1528. public void reportWidthAssignedToRelative(ComponentConnector component,
  1529. int assignedWidth) {
  1530. assert component.isRelativeWidth();
  1531. float percentSize = parsePercent(component.getState().width == null ? ""
  1532. : component.getState().width);
  1533. int effectiveWidth = Math.round(assignedWidth * (percentSize / 100));
  1534. reportOuterWidth(component, effectiveWidth);
  1535. }
  1536. private static float parsePercent(String size) {
  1537. return Float.parseFloat(size.substring(0, size.length() - 1));
  1538. }
  1539. /**
  1540. * Registers the outer width (including margins, borders and paddings) of a
  1541. * component. This can be used as an optimization by ManagedLayouts; by
  1542. * informing the LayoutManager about what size a component will have, the
  1543. * layout propagation can continue directly without first measuring the
  1544. * potentially resized elements.
  1545. *
  1546. * @param component
  1547. * the component for which the size is reported
  1548. * @param outerWidth
  1549. * the new outer width (including margins, borders and paddings)
  1550. * of the component in pixels
  1551. */
  1552. public void reportOuterWidth(ComponentConnector component, int outerWidth) {
  1553. Element element = component.getWidget().getElement();
  1554. MeasuredSize measuredSize = getMeasuredSize(element);
  1555. if (isLayoutRunning()) {
  1556. boolean widthChanged = measuredSize.setOuterWidth(outerWidth);
  1557. if (widthChanged) {
  1558. onConnectorChange(component, true, false);
  1559. notifyListenersAndDepdendents(element, true, false);
  1560. }
  1561. currentDependencyTree.setNeedsHorizontalMeasure(component, false);
  1562. } else if (measuredSize.getOuterWidth() != outerWidth) {
  1563. setNeedsMeasure(component);
  1564. }
  1565. }
  1566. /**
  1567. * Adds a listener that will be notified whenever the size of a specific
  1568. * element changes. Adding a listener to an element also ensures that all
  1569. * sizes for that element will be available starting from the next layout
  1570. * phase.
  1571. *
  1572. * @param element
  1573. * the element that should be checked for size changes
  1574. * @param listener
  1575. * an ElementResizeListener that will be informed whenever the
  1576. * size of the target element has changed
  1577. */
  1578. public void addElementResizeListener(Element element,
  1579. ElementResizeListener listener) {
  1580. Collection<ElementResizeListener> listeners = elementResizeListeners
  1581. .get(element);
  1582. if (listeners == null) {
  1583. listeners = new HashSet<>();
  1584. elementResizeListeners.put(element, listeners);
  1585. ensureMeasured(element);
  1586. }
  1587. listeners.add(listener);
  1588. }
  1589. /**
  1590. * Removes an element resize listener from the provided element. This might
  1591. * cause this LayoutManager to stop tracking the size of the element if no
  1592. * other sources are interested in the size.
  1593. *
  1594. * @param element
  1595. * the element to which the element resize listener was
  1596. * previously added
  1597. * @param listener
  1598. * the ElementResizeListener that should no longer get informed
  1599. * about size changes to the target element.
  1600. */
  1601. public void removeElementResizeListener(Element element,
  1602. ElementResizeListener listener) {
  1603. Collection<ElementResizeListener> listeners = elementResizeListeners
  1604. .get(element);
  1605. if (listeners != null) {
  1606. listeners.remove(listener);
  1607. if (listeners.isEmpty()) {
  1608. elementResizeListeners.remove(element);
  1609. stopMeasuringIfUnecessary(element);
  1610. }
  1611. }
  1612. }
  1613. private void stopMeasuringIfUnecessary(Element element) {
  1614. if (!needsMeasure(element)) {
  1615. measuredNonConnectorElements.remove(element);
  1616. setMeasuredSize(element, null);
  1617. }
  1618. }
  1619. /**
  1620. * Informs this LayoutManager that the size of a component might have
  1621. * changed. This method should be used whenever the size of an individual
  1622. * component might have changed from outside of Vaadin's normal update
  1623. * phase, e.g. when an icon has been loaded or when the user resizes some
  1624. * part of the UI using the mouse.
  1625. * <p>
  1626. * To set an entire component hierarchy to be measured, use
  1627. * {@link #setNeedsMeasureRecursively(ComponentConnector)} instead.
  1628. * <p>
  1629. * If there is no upcoming layout phase, a new layout phase is scheduled.
  1630. *
  1631. * @param component
  1632. * the component whose size might have changed.
  1633. */
  1634. public void setNeedsMeasure(ComponentConnector component) {
  1635. if (isLayoutRunning()) {
  1636. currentDependencyTree.setNeedsMeasure(component, true);
  1637. } else {
  1638. needsMeasure.add(component.getConnectorId());
  1639. layoutLater();
  1640. }
  1641. }
  1642. /**
  1643. * Informs this LayoutManager that some sizes in a component hierarchy might
  1644. * have changed. This method should be used whenever the size of any child
  1645. * component might have changed from outside of Vaadin's normal update
  1646. * phase, e.g. when a CSS class name related to sizing has been changed.
  1647. * <p>
  1648. * To set a single component to be measured, use
  1649. * {@link #setNeedsMeasure(ComponentConnector)} instead.
  1650. * <p>
  1651. * If there is no upcoming layout phase, a new layout phase is scheduled.
  1652. *
  1653. * @since 7.2
  1654. * @param component
  1655. * the component at the root of the component hierarchy to
  1656. * measure
  1657. */
  1658. public void setNeedsMeasureRecursively(ComponentConnector component) {
  1659. setNeedsMeasure(component);
  1660. if (component instanceof HasComponentsConnector) {
  1661. HasComponentsConnector hasComponents = (HasComponentsConnector) component;
  1662. for (ComponentConnector child : hasComponents
  1663. .getChildComponents()) {
  1664. setNeedsMeasureRecursively(child);
  1665. }
  1666. }
  1667. }
  1668. public void setEverythingNeedsMeasure() {
  1669. everythingNeedsMeasure = true;
  1670. }
  1671. private static Logger getLogger() {
  1672. return Logger.getLogger(LayoutManager.class.getName());
  1673. }
  1674. /**
  1675. * Checks if there is something waiting for a layout to take place.
  1676. *
  1677. * @since 7.5.6
  1678. * @return true if there are connectors waiting for measurement or layout,
  1679. * false otherwise
  1680. */
  1681. public boolean isLayoutNeeded() {
  1682. if (!needsHorizontalLayout.isEmpty()
  1683. || !needsVerticalLayout.isEmpty()) {
  1684. return true;
  1685. }
  1686. if (!needsMeasure.isEmpty()) {
  1687. return true;
  1688. }
  1689. if (everythingNeedsMeasure) {
  1690. return true;
  1691. }
  1692. return false;
  1693. }
  1694. }