Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

LegacyLocatorStrategy.java 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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.componentlocator;
  17. import java.util.ArrayList;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import com.google.gwt.core.client.JavaScriptObject;
  21. import com.google.gwt.dom.client.Element;
  22. import com.google.gwt.regexp.shared.RegExp;
  23. import com.google.gwt.user.client.DOM;
  24. import com.google.gwt.user.client.ui.HasWidgets;
  25. import com.google.gwt.user.client.ui.RootPanel;
  26. import com.google.gwt.user.client.ui.Widget;
  27. import com.vaadin.client.ApplicationConnection;
  28. import com.vaadin.client.ComponentConnector;
  29. import com.vaadin.client.ConnectorMap;
  30. import com.vaadin.client.ServerConnector;
  31. import com.vaadin.client.Util;
  32. import com.vaadin.client.VCaption;
  33. import com.vaadin.client.WidgetUtil;
  34. import com.vaadin.client.ui.SubPartAware;
  35. import com.vaadin.client.ui.VCssLayout;
  36. import com.vaadin.client.ui.VGridLayout;
  37. import com.vaadin.client.ui.VOverlay;
  38. import com.vaadin.client.ui.VTabsheetPanel;
  39. import com.vaadin.client.ui.VUI;
  40. import com.vaadin.client.ui.VWindow;
  41. import com.vaadin.client.ui.orderedlayout.Slot;
  42. import com.vaadin.client.ui.orderedlayout.VAbstractOrderedLayout;
  43. import com.vaadin.client.ui.window.WindowConnector;
  44. import com.vaadin.shared.AbstractComponentState;
  45. import com.vaadin.shared.Connector;
  46. import com.vaadin.shared.communication.SharedState;
  47. /**
  48. * The LegacyLocatorStrategy class handles the legacy locator syntax that was
  49. * introduced in version 5.4 of the framework. The legacy locator strategy is
  50. * always used if no other strategy claims responsibility for a locator string.
  51. *
  52. * @since 7.2
  53. * @author Vaadin Ltd
  54. */
  55. public class LegacyLocatorStrategy implements LocatorStrategy {
  56. /**
  57. * Separator used in the String locator between a parent and a child widget.
  58. */
  59. static final String PARENTCHILD_SEPARATOR = "/";
  60. /**
  61. * Separator used in the String locator between the part identifying the
  62. * containing widget and the part identifying the target element within the
  63. * widget.
  64. */
  65. static final String SUBPART_SEPARATOR = "#";
  66. /**
  67. * String that identifies the root panel when appearing first in the String
  68. * locator.
  69. */
  70. static final String ROOT_ID = "Root";
  71. private final ApplicationConnection client;
  72. private static final RegExp validSyntax = RegExp.compile(
  73. "^((\\w+::)?((PID_S)?\\w[-$_a-zA-Z0-9.' ]*)?)?(/[-$_a-zA-Z0-9]+\\[\\d+\\])*/?(#.*)?$");
  74. public LegacyLocatorStrategy(ApplicationConnection clientConnection) {
  75. client = clientConnection;
  76. }
  77. @Override
  78. public boolean validatePath(String path) {
  79. return validSyntax.test(path);
  80. }
  81. @Override
  82. public String getPathForElement(Element targetElement) {
  83. ComponentConnector connector = Util.findPaintable(client,
  84. targetElement);
  85. Widget w = null;
  86. if (connector != null) {
  87. // If we found a Paintable then we use that as reference. We should
  88. // find the Paintable for all but very special cases (like
  89. // overlays).
  90. w = connector.getWidget();
  91. /*
  92. * Still if the Paintable contains a widget that implements
  93. * SubPartAware, we want to use that as a reference
  94. */
  95. Widget targetParent = findParentWidget(targetElement, w);
  96. while (targetParent != w && targetParent != null) {
  97. if (targetParent instanceof SubPartAware) {
  98. /*
  99. * The targetParent widget is a child of the Paintable and
  100. * the first parent (of the targetElement) that implements
  101. * SubPartAware
  102. */
  103. w = targetParent;
  104. break;
  105. }
  106. targetParent = targetParent.getParent();
  107. }
  108. }
  109. if (w == null) {
  110. // Check if the element is part of a widget that is attached
  111. // directly to the root panel
  112. RootPanel rootPanel = RootPanel.get();
  113. int rootWidgetCount = rootPanel.getWidgetCount();
  114. for (int i = 0; i < rootWidgetCount; i++) {
  115. Widget rootWidget = rootPanel.getWidget(i);
  116. if (rootWidget.getElement().isOrHasChild(targetElement)) {
  117. // The target element is contained by this root widget
  118. w = findParentWidget(targetElement, rootWidget);
  119. break;
  120. }
  121. }
  122. if (w != null) {
  123. // We found a widget but we should still see if we find a
  124. // SubPartAware implementor (we cannot find the Paintable as
  125. // there is no link from VOverlay to its paintable/owner).
  126. Widget subPartAwareWidget = findSubPartAwareParentWidget(w);
  127. if (subPartAwareWidget != null) {
  128. w = subPartAwareWidget;
  129. }
  130. }
  131. }
  132. if (w == null) {
  133. // Containing widget not found
  134. return null;
  135. }
  136. // Determine the path for the target widget
  137. String path = getPathForWidget(w);
  138. if (path == null) {
  139. /*
  140. * No path could be determined for the target widget. Cannot create
  141. * a locator string.
  142. */
  143. return null;
  144. }
  145. // The parent check is a work around for Firefox 15 which fails to
  146. // compare elements properly (#9534)
  147. if (w.getElement() == targetElement) {
  148. /*
  149. * We are done if the target element is the root of the target
  150. * widget.
  151. */
  152. return path;
  153. } else if (w instanceof SubPartAware) {
  154. /*
  155. * If the widget can provide an identifier for the targetElement we
  156. * let it do that
  157. */
  158. String elementLocator = ((SubPartAware) w)
  159. .getSubPartName(DOM.asOld(targetElement));
  160. if (elementLocator != null) {
  161. return path + LegacyLocatorStrategy.SUBPART_SEPARATOR
  162. + elementLocator;
  163. }
  164. }
  165. /*
  166. * If everything else fails we use the DOM path to identify the target
  167. * element
  168. */
  169. String domPath = getDOMPathForElement(targetElement, w.getElement());
  170. if (domPath == null) {
  171. return path;
  172. } else {
  173. return path + domPath;
  174. }
  175. }
  176. /**
  177. * {@inheritDoc}
  178. */
  179. @Override
  180. public Element getElementByPath(String path) {
  181. return getElementByPathStartingAt(path, null);
  182. }
  183. /**
  184. * {@inheritDoc}
  185. */
  186. @Override
  187. public Element getElementByPathStartingAt(String path,
  188. Element baseElement) {
  189. /*
  190. * Path is of type "targetWidgetPath#componentPart" or
  191. * "targetWidgetPath".
  192. */
  193. String parts[] = path.split(LegacyLocatorStrategy.SUBPART_SEPARATOR, 2);
  194. String widgetPath = parts[0];
  195. // Note that this only works if baseElement can be mapped to a
  196. // widget to which the path is relative. Otherwise, the current
  197. // implementation simply interprets the path as if baseElement was
  198. // null.
  199. Widget baseWidget = WidgetUtil.findWidget(baseElement, null);
  200. Widget w = getWidgetFromPath(widgetPath, baseWidget);
  201. if (w == null || !WidgetUtil.isAttachedAndDisplayed(w)) {
  202. return null;
  203. }
  204. if (parts.length == 1) {
  205. int pos = widgetPath.indexOf("domChild");
  206. if (pos == -1) {
  207. return w.getElement();
  208. }
  209. // Contains dom reference to a sub element of the widget
  210. String subPath = widgetPath.substring(pos);
  211. return getElementByDOMPath(w.getElement(), subPath);
  212. } else if (parts.length == 2) {
  213. if (w instanceof SubPartAware) {
  214. return ((SubPartAware) w).getSubPartElement(parts[1]);
  215. }
  216. }
  217. return null;
  218. }
  219. /**
  220. * {@inheritDoc}
  221. */
  222. @Override
  223. public List<Element> getElementsByPath(String path) {
  224. // This type of search is not supported in LegacyLocator
  225. List<Element> array = new ArrayList<Element>();
  226. Element e = getElementByPath(path);
  227. if (e != null) {
  228. array.add(e);
  229. }
  230. return array;
  231. }
  232. /**
  233. * {@inheritDoc}
  234. */
  235. @Override
  236. public List<Element> getElementsByPathStartingAt(String path,
  237. Element root) {
  238. // This type of search is not supported in LegacyLocator
  239. List<Element> array = new ArrayList<Element>();
  240. Element e = getElementByPathStartingAt(path, root);
  241. if (e != null) {
  242. array.add(e);
  243. }
  244. return array;
  245. }
  246. /**
  247. * Finds the first widget in the hierarchy (moving upwards) that implements
  248. * SubPartAware. Returns the SubPartAware implementor or null if none is
  249. * found.
  250. *
  251. * @param w
  252. * The widget to start from. This is returned if it implements
  253. * SubPartAware.
  254. * @return The first widget (upwards in hierarchy) that implements
  255. * SubPartAware or null
  256. */
  257. Widget findSubPartAwareParentWidget(Widget w) {
  258. while (w != null) {
  259. if (w instanceof SubPartAware) {
  260. return w;
  261. }
  262. w = w.getParent();
  263. }
  264. return null;
  265. }
  266. /**
  267. * Returns the first widget found when going from {@code targetElement}
  268. * upwards in the DOM hierarchy, assuming that {@code ancestorWidget} is a
  269. * parent of {@code targetElement}.
  270. *
  271. * @param targetElement
  272. * @param ancestorWidget
  273. * @return The widget whose root element is a parent of
  274. * {@code targetElement}.
  275. */
  276. private Widget findParentWidget(Element targetElement,
  277. Widget ancestorWidget) {
  278. /*
  279. * As we cannot resolve Widgets from the element we start from the
  280. * widget and move downwards to the correct child widget, as long as we
  281. * find one.
  282. */
  283. if (ancestorWidget instanceof HasWidgets) {
  284. for (Widget w : ((HasWidgets) ancestorWidget)) {
  285. if (w.getElement().isOrHasChild(targetElement)) {
  286. return findParentWidget(targetElement, w);
  287. }
  288. }
  289. }
  290. // No children found, this is it
  291. return ancestorWidget;
  292. }
  293. /**
  294. * Locates an element based on a DOM path and a base element.
  295. *
  296. * @param baseElement
  297. * The base element which the path is relative to
  298. * @param path
  299. * String locator (consisting of domChild[x] parts) that
  300. * identifies the element
  301. * @return The element identified by path, relative to baseElement or null
  302. * if the element could not be found.
  303. */
  304. private Element getElementByDOMPath(Element baseElement, String path) {
  305. String parts[] = path.split(PARENTCHILD_SEPARATOR);
  306. Element element = baseElement;
  307. for (int i = 0, l = parts.length; i < l; ++i) {
  308. String part = parts[i];
  309. if (part.startsWith("domChild[")) {
  310. String childIndexString = part.substring("domChild[".length(),
  311. part.length() - 1);
  312. if (WidgetUtil.findWidget(baseElement,
  313. null) instanceof VAbstractOrderedLayout) {
  314. if (element.hasChildNodes()) {
  315. Element e = element.getFirstChildElement().cast();
  316. String cn = e.getClassName();
  317. if (cn != null && (cn.equals("v-expand")
  318. || cn.contains("v-has-caption"))) {
  319. element = e;
  320. }
  321. }
  322. }
  323. try {
  324. int childIndex = Integer.parseInt(childIndexString);
  325. element = DOM.getChild(element, childIndex);
  326. } catch (Exception e) {
  327. return null;
  328. }
  329. if (element == null) {
  330. return null;
  331. }
  332. } else {
  333. path = parts[i];
  334. for (int j = i + 1; j < l; ++j) {
  335. path += PARENTCHILD_SEPARATOR + parts[j];
  336. }
  337. return getElementByPathStartingAt(path, element);
  338. }
  339. }
  340. return element;
  341. }
  342. /**
  343. * Generates a String locator using domChild[x] parts for the element
  344. * relative to the baseElement.
  345. *
  346. * @param element
  347. * The target element
  348. * @param baseElement
  349. * The starting point for the locator. The generated path is
  350. * relative to this element.
  351. * @return A String locator that can be used to locate the target element
  352. * using {@link #getElementByDOMPath(Element, String)} or null if
  353. * the locator String cannot be created.
  354. */
  355. private String getDOMPathForElement(Element element, Element baseElement) {
  356. Element e = element;
  357. String path = "";
  358. while (true) {
  359. int childIndex = -1;
  360. Element siblingIterator = e;
  361. while (siblingIterator != null) {
  362. childIndex++;
  363. siblingIterator = siblingIterator.getPreviousSiblingElement()
  364. .cast();
  365. }
  366. path = PARENTCHILD_SEPARATOR + "domChild[" + childIndex + "]"
  367. + path;
  368. JavaScriptObject parent = e.getParentElement();
  369. if (parent == null) {
  370. return null;
  371. }
  372. // The parent check is a work around for Firefox 15 which fails to
  373. // compare elements properly (#9534)
  374. if (parent == baseElement) {
  375. break;
  376. }
  377. e = parent.cast();
  378. }
  379. return path;
  380. }
  381. /**
  382. * Creates a locator String for the given widget. The path can be used to
  383. * locate the widget using {@link #getWidgetFromPath(String, Widget)}.
  384. * <p/>
  385. * Returns null if no path can be determined for the widget or if the widget
  386. * is null.
  387. *
  388. * @param w
  389. * The target widget
  390. * @return A String locator for the widget
  391. */
  392. private String getPathForWidget(Widget w) {
  393. if (w == null) {
  394. return null;
  395. }
  396. String elementId = w.getElement().getId();
  397. if (elementId != null && !elementId.isEmpty()
  398. && !elementId.startsWith("gwt-uid-")) {
  399. // Use PID_S+id if the user has set an id but do not use it for auto
  400. // generated id:s as these might not be consistent
  401. return "PID_S" + elementId;
  402. } else if (w instanceof VUI) {
  403. return "";
  404. } else if (w instanceof VWindow) {
  405. Connector windowConnector = ConnectorMap.get(client)
  406. .getConnector(w);
  407. List<WindowConnector> subWindowList = client.getUIConnector()
  408. .getSubWindows();
  409. int indexOfSubWindow = subWindowList.indexOf(windowConnector);
  410. return PARENTCHILD_SEPARATOR + "VWindow[" + indexOfSubWindow + "]";
  411. } else if (w instanceof RootPanel) {
  412. return ROOT_ID;
  413. }
  414. Widget parent = w.getParent();
  415. String basePath = getPathForWidget(parent);
  416. if (basePath == null) {
  417. return null;
  418. }
  419. String simpleName = Util.getSimpleName(w);
  420. /*
  421. * Check if the parent implements Iterable. At least VPopupView does not
  422. * implement HasWdgets so we cannot check for that.
  423. */
  424. if (!(parent instanceof Iterable<?>)) {
  425. // Parent does not implement Iterable so we cannot find out which
  426. // child this is
  427. return null;
  428. }
  429. Iterator<?> i = ((Iterable<?>) parent).iterator();
  430. int pos = 0;
  431. while (i.hasNext()) {
  432. Object child = i.next();
  433. if (child == w) {
  434. return basePath + PARENTCHILD_SEPARATOR + simpleName + "[" + pos
  435. + "]";
  436. }
  437. String simpleName2 = Util.getSimpleName(child);
  438. if (simpleName.equals(simpleName2)) {
  439. pos++;
  440. }
  441. }
  442. return null;
  443. }
  444. /**
  445. * Locates the widget based on a String locator.
  446. *
  447. * @param path
  448. * The String locator that identifies the widget.
  449. * @param baseWidget
  450. * the widget to which the path is relative, null if relative to
  451. * root
  452. * @return The Widget identified by the String locator or null if the widget
  453. * could not be identified.
  454. */
  455. @SuppressWarnings("unchecked")
  456. private Widget getWidgetFromPath(String path, Widget baseWidget) {
  457. Widget w = baseWidget;
  458. String parts[] = path.split(PARENTCHILD_SEPARATOR);
  459. for (int i = 0; i < parts.length; i++) {
  460. String part = parts[i];
  461. if (part.equals(ROOT_ID)) {
  462. w = RootPanel.get();
  463. } else if (part.equals("")) {
  464. if (w == null) {
  465. w = client.getUIConnector().getWidget();
  466. }
  467. } else if (w == null) {
  468. String id = part;
  469. // Must be old static pid (PID_S*)
  470. ServerConnector connector = ConnectorMap.get(client)
  471. .getConnector(id);
  472. if (connector == null) {
  473. // Lookup by component id
  474. // TODO Optimize this
  475. connector = findConnectorById(client.getUIConnector(),
  476. id.substring(5));
  477. }
  478. if (connector instanceof ComponentConnector) {
  479. w = ((ComponentConnector) connector).getWidget();
  480. } else {
  481. // Not found
  482. return null;
  483. }
  484. } else if (part.startsWith("domChild[")) {
  485. // The target widget has been found and the rest identifies the
  486. // element
  487. break;
  488. } else if (w instanceof Iterable) {
  489. // W identifies a widget that contains other widgets, as it
  490. // should. Try to locate the child
  491. Iterable<?> parent = (Iterable<?>) w;
  492. // Part is of type "VVerticalLayout[0]", split this into
  493. // VVerticalLayout and 0
  494. String[] split = part.split("\\[", 2);
  495. String widgetClassName = split[0];
  496. String indexString = split[1].substring(0,
  497. split[1].length() - 1);
  498. int widgetPosition;
  499. try {
  500. widgetPosition = Integer.parseInt(indexString);
  501. } catch (NumberFormatException e) {
  502. // We've probably been fed a new-style Vaadin locator with a
  503. // string-form predicate, that doesn't match anything in the
  504. // search space.
  505. return null;
  506. }
  507. // AbsolutePanel in GridLayout has been removed -> skip it
  508. if (w instanceof VGridLayout
  509. && "AbsolutePanel".equals(widgetClassName)) {
  510. continue;
  511. }
  512. // FlowPane in CSSLayout has been removed -> skip it
  513. if (w instanceof VCssLayout
  514. && "VCssLayout$FlowPane".equals(widgetClassName)) {
  515. continue;
  516. }
  517. // ChildComponentContainer and VOrderedLayout$Slot have been
  518. // replaced with Slot
  519. if (w instanceof VAbstractOrderedLayout
  520. && ("ChildComponentContainer".equals(widgetClassName)
  521. || "VOrderedLayout$Slot"
  522. .equals(widgetClassName))) {
  523. widgetClassName = "Slot";
  524. }
  525. if (w instanceof VTabsheetPanel && widgetPosition != 0) {
  526. // TabSheetPanel now only contains 1 connector => the index
  527. // is always 0 which indicates the widget in the active tab
  528. widgetPosition = 0;
  529. }
  530. if (w instanceof VOverlay
  531. && "VCalendarPanel".equals(widgetClassName)) {
  532. // Vaadin 7.1 adds a wrapper for datefield popups
  533. parent = (Iterable<?>) ((Iterable<?>) parent).iterator()
  534. .next();
  535. }
  536. /*
  537. * The new grid and ordered layouts do not contain
  538. * ChildComponentContainer widgets. This is instead simulated by
  539. * constructing a path step that would find the desired widget
  540. * from the layout and injecting it as the next search step
  541. * (which would originally have found the widget inside the
  542. * ChildComponentContainer)
  543. */
  544. if ((w instanceof VGridLayout)
  545. && "ChildComponentContainer".equals(widgetClassName)
  546. && i + 1 < parts.length) {
  547. HasWidgets layout = (HasWidgets) w;
  548. String nextPart = parts[i + 1];
  549. String[] nextSplit = nextPart.split("\\[", 2);
  550. String nextWidgetClassName = nextSplit[0];
  551. // Find the n:th child and count the number of children with
  552. // the same type before it
  553. int nextIndex = 0;
  554. for (Widget child : layout) {
  555. boolean matchingType = nextWidgetClassName
  556. .equals(Util.getSimpleName(child));
  557. if (matchingType && widgetPosition == 0) {
  558. // This is the n:th child that we looked for
  559. break;
  560. } else if (widgetPosition < 0) {
  561. // Error if we're past the desired position without
  562. // a match
  563. return null;
  564. } else if (matchingType) {
  565. // If this was another child of the expected type,
  566. // increase the count for the next step
  567. nextIndex++;
  568. }
  569. // Don't count captions
  570. if (!(child instanceof VCaption)) {
  571. widgetPosition--;
  572. }
  573. }
  574. // Advance to the next step, this time checking for the
  575. // actual child widget
  576. parts[i + 1] = nextWidgetClassName + '[' + nextIndex + ']';
  577. continue;
  578. }
  579. // Locate the child
  580. Iterator<? extends Widget> iterator;
  581. /*
  582. * VWindow and VContextMenu workarounds for backwards
  583. * compatibility
  584. */
  585. if (widgetClassName.equals("VWindow")) {
  586. List<WindowConnector> windows = client.getUIConnector()
  587. .getSubWindows();
  588. List<VWindow> windowWidgets = new ArrayList<VWindow>(
  589. windows.size());
  590. for (WindowConnector wc : windows) {
  591. windowWidgets.add(wc.getWidget());
  592. }
  593. iterator = windowWidgets.iterator();
  594. } else if (widgetClassName.equals("VContextMenu")) {
  595. return client.getContextMenu();
  596. } else {
  597. iterator = (Iterator<? extends Widget>) parent.iterator();
  598. }
  599. boolean ok = false;
  600. // Find the widgetPosition:th child of type "widgetClassName"
  601. while (iterator.hasNext()) {
  602. Widget child = iterator.next();
  603. String simpleName2 = Util.getSimpleName(child);
  604. if (!widgetClassName.equals(simpleName2)
  605. && child instanceof Slot) {
  606. /*
  607. * Support legacy tests without any selector for the
  608. * Slot widget (i.e. /VVerticalLayout[0]/VButton[0]) by
  609. * directly checking the stuff inside the slot
  610. */
  611. child = ((Slot) child).getWidget();
  612. simpleName2 = Util.getSimpleName(child);
  613. }
  614. if (widgetClassName.equals(simpleName2)) {
  615. if (widgetPosition == 0) {
  616. w = child;
  617. ok = true;
  618. break;
  619. }
  620. widgetPosition--;
  621. }
  622. }
  623. if (!ok) {
  624. // Did not find the child
  625. return null;
  626. }
  627. } else {
  628. // W identifies something that is not a "HasWidgets". This
  629. // should not happen as all widget containers should implement
  630. // HasWidgets.
  631. return null;
  632. }
  633. }
  634. return w;
  635. }
  636. private ServerConnector findConnectorById(ServerConnector root, String id) {
  637. SharedState state = root.getState();
  638. if (state instanceof AbstractComponentState
  639. && id.equals(((AbstractComponentState) state).id)) {
  640. return root;
  641. }
  642. for (ServerConnector child : root.getChildren()) {
  643. ServerConnector found = findConnectorById(child, id);
  644. if (found != null) {
  645. return found;
  646. }
  647. }
  648. return null;
  649. }
  650. }