Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

PaintableMap.java 11KB

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