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

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