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 11KB

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