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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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, ServerConnector> idToConnector = new HashMap<String, ServerConnector>();
  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 ServerConnector} 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 ServerConnector 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, ServerConnector 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 ServerConnector#getId()} instead
  105. */
  106. @Deprecated
  107. public String getConnectorId(ServerConnector 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(ServerConnector)} 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. ServerConnector 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(ServerConnector connector) {
  156. // add to unregistry queue
  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. VConsole.log("Tried to unregister a "
  168. + connector.getClass().getName() + " (" + id
  169. + ") which was not registered");
  170. } else {
  171. unregistryBag.add(id);
  172. }
  173. if (widget != null && widget instanceof HasWidgets) {
  174. unregisterChildConnectors((HasWidgets) widget);
  175. }
  176. }
  177. public ComponentConnector[] getRegisteredComponentConnectors() {
  178. ArrayList<ComponentConnector> result = new ArrayList<ComponentConnector>();
  179. for (ServerConnector connector : getConnectors()) {
  180. if (connector instanceof ComponentConnector) {
  181. ComponentConnector componentConnector = (ComponentConnector) connector;
  182. if (!unregistryBag.contains(connector.getId())) {
  183. result.add(componentConnector);
  184. }
  185. }
  186. }
  187. return result.toArray(new ComponentConnector[result.size()]);
  188. }
  189. void purgeUnregistryBag(boolean unregisterConnectors) {
  190. if (unregisterConnectors) {
  191. for (String connectorId : unregistryBag) {
  192. // TODO purge shared state for pid
  193. ServerConnector connector = getConnector(connectorId);
  194. if (connector == null) {
  195. /*
  196. * this should never happen, but it does :-( See e.g.
  197. * com.vaadin.tests.components.accordion.RemoveTabs (with
  198. * test script)
  199. */
  200. VConsole.error("Tried to unregister component (id="
  201. + connectorId
  202. + ") that is never registered (or already unregistered)");
  203. continue;
  204. }
  205. VConsole.log("Unregistering connector "
  206. + connector.getClass().getName() + " " + connectorId);
  207. Widget widget = null;
  208. if (connector instanceof ComponentConnector) {
  209. widget = ((ComponentConnector) connector).getWidget();
  210. }
  211. // check if can be cleaned
  212. if (widget == null || !widget.isAttached()) {
  213. // clean reference to paintable
  214. idToComponentDetail.remove(connectorId);
  215. idToConnector.remove(connectorId);
  216. }
  217. /*
  218. * else NOP : same component has been reattached to another
  219. * parent or replaced by another component implementation.
  220. */
  221. }
  222. }
  223. unregistryBag.clear();
  224. }
  225. /**
  226. * Unregisters the child connectors for the given container recursively.
  227. *
  228. * Use when after removing a connector that contains other connectors. Does
  229. * not unregister the given container itself. Does not actually remove the
  230. * widgets from the DOM.
  231. *
  232. * @see #unregisterConnector(ServerConnector)
  233. * @param container
  234. * The container that contains the connectors that should be
  235. * unregistered
  236. */
  237. public void unregisterChildConnectors(HasWidgets container) {
  238. // FIXME: This should be based on the paintable hierarchy
  239. final Iterator<Widget> it = container.iterator();
  240. while (it.hasNext()) {
  241. final Widget w = it.next();
  242. ComponentConnector p = getConnector(w);
  243. if (p != null) {
  244. // This will unregister the paintable and all its children
  245. unregisterConnector(p);
  246. } else if (w instanceof HasWidgets) {
  247. // For normal widget containers, unregister the children
  248. unregisterChildConnectors((HasWidgets) w);
  249. }
  250. }
  251. }
  252. /**
  253. * FIXME: Should not be here
  254. *
  255. * @param pid
  256. * @param uidl
  257. */
  258. @Deprecated
  259. public void registerEventListenersFromUIDL(String pid, UIDL uidl) {
  260. ComponentDetail cd = idToComponentDetail.get(pid);
  261. if (cd == null) {
  262. throw new IllegalArgumentException("Pid must not be null");
  263. }
  264. cd.registerEventListenersFromUIDL(uidl);
  265. }
  266. /**
  267. * FIXME: Should not be here
  268. *
  269. * @param paintable
  270. * @return
  271. */
  272. @Deprecated
  273. public Size getOffsetSize(ComponentConnector paintable) {
  274. return getComponentDetail(paintable).getOffsetSize();
  275. }
  276. /**
  277. * FIXME: Should not be here
  278. *
  279. * @param componentConnector
  280. * @return
  281. */
  282. @Deprecated
  283. public void setOffsetSize(ComponentConnector componentConnector,
  284. Size newSize) {
  285. getComponentDetail(componentConnector).setOffsetSize(newSize);
  286. }
  287. private ComponentDetail getComponentDetail(
  288. ComponentConnector componentConnector) {
  289. return idToComponentDetail.get(getConnectorId(componentConnector));
  290. }
  291. public int size() {
  292. return idToConnector.size();
  293. }
  294. /**
  295. * FIXME: Should be moved to VAbstractPaintableWidget
  296. *
  297. * @param paintable
  298. * @return
  299. */
  300. @Deprecated
  301. public TooltipInfo getTooltipInfo(ComponentConnector paintable, Object key) {
  302. return getComponentDetail(paintable).getTooltipInfo(key);
  303. }
  304. @Deprecated
  305. public TooltipInfo getWidgetTooltipInfo(Widget widget, Object key) {
  306. return getTooltipInfo(getConnector(widget), key);
  307. }
  308. public Collection<? extends ServerConnector> getConnectors() {
  309. return Collections.unmodifiableCollection(idToConnector.values());
  310. }
  311. /**
  312. * FIXME: Should not be here
  313. *
  314. * @param componentConnector
  315. * @return
  316. */
  317. @Deprecated
  318. public void registerTooltip(ComponentConnector componentConnector,
  319. Object key, TooltipInfo tooltip) {
  320. getComponentDetail(componentConnector).putAdditionalTooltip(key,
  321. tooltip);
  322. }
  323. /**
  324. * FIXME: Should not be here
  325. *
  326. * @param componentConnector
  327. * @return
  328. */
  329. @Deprecated
  330. public boolean hasEventListeners(ComponentConnector componentConnector,
  331. String eventIdentifier) {
  332. return getComponentDetail(componentConnector).hasEventListeners(
  333. eventIdentifier);
  334. }
  335. /**
  336. * Tests if the widget is the root widget of a {@link ComponentConnector}.
  337. *
  338. * @param widget
  339. * The widget to test
  340. * @return true if the widget is the root widget of a
  341. * {@link ComponentConnector}, false otherwise
  342. */
  343. public boolean isConnector(Widget w) {
  344. return getConnectorId(w) != null;
  345. }
  346. }