Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

LayoutManager.java 68KB

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