You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConnectorMap.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client;
  5. import java.util.ArrayList;
  6. import java.util.Collection;
  7. import java.util.Collections;
  8. import java.util.HashMap;
  9. import java.util.HashSet;
  10. import java.util.Iterator;
  11. import java.util.Map;
  12. import java.util.Set;
  13. import com.google.gwt.core.client.GWT;
  14. import com.google.gwt.dom.client.Element;
  15. import com.google.gwt.user.client.ui.HasWidgets;
  16. import com.google.gwt.user.client.ui.Widget;
  17. import com.vaadin.terminal.Paintable;
  18. import com.vaadin.terminal.gwt.client.RenderInformation.Size;
  19. public class ConnectorMap {
  20. private Map<String, Connector> idToConnector = new HashMap<String, Connector>();
  21. public static ConnectorMap get(ApplicationConnection applicationConnection) {
  22. return applicationConnection.getConnectorMap();
  23. }
  24. @Deprecated
  25. private final ComponentDetailMap idToComponentDetail = ComponentDetailMap
  26. .create();
  27. private Set<String> unregistryBag = new HashSet<String>();
  28. /**
  29. * Returns a {@link Connector} by its id
  30. *
  31. * @param id
  32. * The connector id
  33. * @return A connector or null if a connector with the given id has not been
  34. * registered
  35. */
  36. public Connector getConnector(String connectorId) {
  37. return idToConnector.get(connectorId);
  38. }
  39. /**
  40. * Returns a {@link ComponentConnector} element by its root element
  41. *
  42. * @param element
  43. * Root element of the {@link ComponentConnector}
  44. * @return A connector or null if a connector with the given id has not been
  45. * registered
  46. */
  47. public ComponentConnector getConnector(Element element) {
  48. return (ComponentConnector) getConnector(getConnectorId(element));
  49. }
  50. /**
  51. * FIXME: What does this even do and why?
  52. *
  53. * @param pid
  54. * @return
  55. */
  56. public boolean isDragAndDropPaintable(String pid) {
  57. return (pid.startsWith("DD"));
  58. }
  59. /**
  60. * Checks if a connector with the given id has been registered.
  61. *
  62. * @param connectorId
  63. * The id to check for
  64. * @return true if a connector has been registered with the given id, false
  65. * otherwise
  66. */
  67. public boolean hasConnector(String connectorId) {
  68. return idToConnector.containsKey(connectorId);
  69. }
  70. /**
  71. * Removes all registered connectors
  72. */
  73. public void clear() {
  74. idToConnector.clear();
  75. idToComponentDetail.clear();
  76. }
  77. public ComponentConnector getConnector(Widget widget) {
  78. return getConnector(widget.getElement());
  79. }
  80. public void registerConnector(String id, Connector connector) {
  81. ComponentDetail componentDetail = GWT.create(ComponentDetail.class);
  82. idToComponentDetail.put(id, componentDetail);
  83. idToConnector.put(id, connector);
  84. if (connector instanceof ComponentConnector) {
  85. ComponentConnector pw = (ComponentConnector) connector;
  86. setConnectorId(pw.getWidget().getElement(), id);
  87. }
  88. }
  89. private native void setConnectorId(Element el, String id)
  90. /*-{
  91. el.tkPid = id;
  92. }-*/;
  93. /**
  94. * Gets the id for a specific connector.
  95. * <p>
  96. * The id is used in the UIDL to identify a specific widget instance,
  97. * effectively linking the widget with it's server side Component.
  98. * </p>
  99. *
  100. * @param connector
  101. * the connector whose id is needed
  102. * @return the id for the given connector or null if the connector could not
  103. * be found
  104. * @deprecated use {@link Connector#getId()} instead
  105. */
  106. @Deprecated
  107. public String getConnectorId(Connector connector) {
  108. if (connector == null) {
  109. return null;
  110. }
  111. return connector.getId();
  112. }
  113. @Deprecated
  114. public String getConnectorId(Widget widget) {
  115. return getConnectorId(widget.getElement());
  116. }
  117. /**
  118. * Gets the connector id using a DOM element - the element should be the
  119. * root element for a connector, otherwise no id will be found. Use
  120. * {@link #getConnectorId(Connector)} instead whenever possible.
  121. *
  122. * @see #getConnectorId(Paintable)
  123. * @param el
  124. * element of the connector whose id is desired
  125. * @return the id of the element's connector, if it's a connector
  126. */
  127. native String getConnectorId(Element el)
  128. /*-{
  129. return el.tkPid;
  130. }-*/;
  131. /**
  132. * Gets the main element for the connector with the given id. The reverse of
  133. * {@link #getConnectorId(Element)}.
  134. *
  135. * @param connectorId
  136. * the id of the widget whose element is desired
  137. * @return the element for the connector corresponding to the id
  138. */
  139. public Element getElement(String connectorId) {
  140. Connector p = getConnector(connectorId);
  141. if (p instanceof ComponentConnector) {
  142. return ((ComponentConnector) p).getWidget().getElement();
  143. }
  144. return null;
  145. }
  146. /**
  147. * Unregisters the given connector; always use after removing a connector.
  148. * This method does not remove the connector from the DOM, but marks the
  149. * connector so that ApplicationConnection may clean up its references to
  150. * it. Removing the widget from DOM is component containers responsibility.
  151. *
  152. * @param connector
  153. * the connector to remove
  154. */
  155. public void unregisterConnector(Connector connector) {
  156. // add to unregistry que
  157. if (connector == null) {
  158. VConsole.error("WARN: Trying to unregister null connector");
  159. return;
  160. }
  161. String id = connector.getId();
  162. Widget widget = null;
  163. if (connector instanceof ComponentConnector) {
  164. widget = ((ComponentConnector) connector).getWidget();
  165. }
  166. if (id == null) {
  167. /*
  168. * Uncomment the following to debug unregistring components. No
  169. * paintables with null id should end here. At least one exception
  170. * is our VScrollTableRow, that is hacked to fake it self as a
  171. * Paintable to build support for sizing easier.
  172. */
  173. // if (!(p instanceof VScrollTableRow)) {
  174. // VConsole.log("Trying to unregister Paintable not created by Application Connection.");
  175. // }
  176. } else {
  177. unregistryBag.add(id);
  178. }
  179. if (widget != null && widget instanceof HasWidgets) {
  180. unregisterChildConnectors((HasWidgets) widget);
  181. }
  182. }
  183. public ComponentConnector[] getRegisteredComponentConnectors() {
  184. ArrayList<ComponentConnector> result = new ArrayList<ComponentConnector>();
  185. for (Connector connector : getConnectors()) {
  186. if (connector instanceof ComponentConnector) {
  187. ComponentConnector componentConnector = (ComponentConnector) connector;
  188. if (!unregistryBag.contains(getConnectorId(connector))) {
  189. result.add(componentConnector);
  190. }
  191. }
  192. }
  193. return result.toArray(new ComponentConnector[result.size()]);
  194. }
  195. void purgeUnregistryBag(boolean unregisterConnectors) {
  196. if (unregisterConnectors) {
  197. for (String connectorId : unregistryBag) {
  198. // TODO purge shared state for pid
  199. Connector connector = getConnector(connectorId);
  200. if (connector == null) {
  201. /*
  202. * this should never happen, but it does :-( See e.g.
  203. * com.vaadin.tests.components.accordion.RemoveTabs (with
  204. * test script)
  205. */
  206. VConsole.error("Tried to unregister component (id="
  207. + connectorId
  208. + ") that is never registered (or already unregistered)");
  209. continue;
  210. }
  211. Widget widget = null;
  212. if (connector instanceof ComponentConnector) {
  213. widget = ((ComponentConnector) connector).getWidget();
  214. }
  215. // check if can be cleaned
  216. if (widget == null || !widget.isAttached()) {
  217. // clean reference to paintable
  218. idToComponentDetail.remove(connectorId);
  219. idToConnector.remove(connectorId);
  220. }
  221. /*
  222. * else NOP : same component has been reattached to another
  223. * parent or replaced by another component implementation.
  224. */
  225. }
  226. }
  227. unregistryBag.clear();
  228. }
  229. /**
  230. * Unregisters the child connectors for the given container recursively.
  231. *
  232. * Use when after removing a connector that contains other connectors. Does
  233. * not unregister the given container itself. Does not actually remove the
  234. * widgets from the DOM.
  235. *
  236. * @see #unregisterConnector(Connector)
  237. * @param container
  238. * The container that contains the connectors that should be
  239. * unregistered
  240. */
  241. public void unregisterChildConnectors(HasWidgets container) {
  242. // FIXME: This should be based on the paintable hierarchy
  243. final Iterator<Widget> it = container.iterator();
  244. while (it.hasNext()) {
  245. final Widget w = it.next();
  246. ComponentConnector p = getConnector(w);
  247. if (p != null) {
  248. // This will unregister the paintable and all its children
  249. unregisterConnector(p);
  250. } else if (w instanceof HasWidgets) {
  251. // For normal widget containers, unregister the children
  252. unregisterChildConnectors((HasWidgets) w);
  253. }
  254. }
  255. }
  256. /**
  257. * FIXME: Should not be here
  258. *
  259. * @param pid
  260. * @param uidl
  261. */
  262. @Deprecated
  263. public void registerEventListenersFromUIDL(String pid, UIDL uidl) {
  264. ComponentDetail cd = idToComponentDetail.get(pid);
  265. if (cd == null) {
  266. throw new IllegalArgumentException("Pid must not be null");
  267. }
  268. cd.registerEventListenersFromUIDL(uidl);
  269. }
  270. /**
  271. * FIXME: Should not be here
  272. *
  273. * @param paintable
  274. * @return
  275. */
  276. @Deprecated
  277. public Size getOffsetSize(ComponentConnector paintable) {
  278. return getComponentDetail(paintable).getOffsetSize();
  279. }
  280. /**
  281. * FIXME: Should not be here
  282. *
  283. * @param componentConnector
  284. * @return
  285. */
  286. @Deprecated
  287. public void setOffsetSize(ComponentConnector componentConnector,
  288. Size newSize) {
  289. getComponentDetail(componentConnector).setOffsetSize(newSize);
  290. }
  291. private ComponentDetail getComponentDetail(
  292. ComponentConnector componentConnector) {
  293. return idToComponentDetail.get(getConnectorId(componentConnector));
  294. }
  295. public int size() {
  296. return idToConnector.size();
  297. }
  298. /**
  299. * FIXME: Should be moved to VAbstractPaintableWidget
  300. *
  301. * @param paintable
  302. * @return
  303. */
  304. @Deprecated
  305. public TooltipInfo getTooltipInfo(ComponentConnector paintable, Object key) {
  306. return getComponentDetail(paintable).getTooltipInfo(key);
  307. }
  308. @Deprecated
  309. public TooltipInfo getWidgetTooltipInfo(Widget widget, Object key) {
  310. return getTooltipInfo(getConnector(widget), key);
  311. }
  312. public Collection<? extends Connector> getConnectors() {
  313. return Collections.unmodifiableCollection(idToConnector.values());
  314. }
  315. /**
  316. * FIXME: Should not be here
  317. *
  318. * @param componentConnector
  319. * @return
  320. */
  321. @Deprecated
  322. public void registerTooltip(ComponentConnector componentConnector,
  323. Object key, TooltipInfo tooltip) {
  324. getComponentDetail(componentConnector).putAdditionalTooltip(key,
  325. tooltip);
  326. }
  327. /**
  328. * FIXME: Should not be here
  329. *
  330. * @param componentConnector
  331. * @return
  332. */
  333. @Deprecated
  334. public boolean hasEventListeners(ComponentConnector componentConnector,
  335. String eventIdentifier) {
  336. return getComponentDetail(componentConnector).hasEventListeners(
  337. eventIdentifier);
  338. }
  339. /**
  340. * Tests if the widget is the root widget of a {@link ComponentConnector}.
  341. *
  342. * @param widget
  343. * The widget to test
  344. * @return true if the widget is the root widget of a
  345. * {@link ComponentConnector}, false otherwise
  346. */
  347. public boolean isConnector(Widget w) {
  348. return getConnectorId(w) != null;
  349. }
  350. }