Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

VPaintableMap.java 12KB

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