Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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