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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client;
  17. import java.util.ArrayList;
  18. import java.util.Collection;
  19. import java.util.logging.Logger;
  20. import com.google.gwt.core.client.GWT;
  21. import com.google.gwt.core.client.JavaScriptObject;
  22. import com.google.gwt.dom.client.Element;
  23. import com.google.gwt.user.client.ui.Widget;
  24. public class ConnectorMap {
  25. public static ConnectorMap get(
  26. ApplicationConnection applicationConnection) {
  27. return applicationConnection.getConnectorMap();
  28. }
  29. @Deprecated
  30. private final ComponentDetailMap idToComponentDetail = ComponentDetailMap
  31. .create();
  32. /**
  33. * Returns a {@link ServerConnector} by its id
  34. *
  35. * @param id
  36. * The connector id
  37. * @return A connector or null if a connector with the given id has not been
  38. * registered
  39. */
  40. public ServerConnector getConnector(String connectorId) {
  41. ComponentDetail componentDetail = idToComponentDetail.get(connectorId);
  42. if (componentDetail == null) {
  43. return null;
  44. } else {
  45. return componentDetail.getConnector();
  46. }
  47. }
  48. /**
  49. * Returns a {@link ComponentConnector} element by its root element.
  50. *
  51. * @param element
  52. * Root element of the {@link ComponentConnector}
  53. * @return A connector or null if a connector with the given id has not been
  54. * registered
  55. */
  56. public ComponentConnector getConnector(Element element) {
  57. ServerConnector connector = getConnector(getConnectorId(element));
  58. if (!(connector instanceof ComponentConnector)) {
  59. // This can happen at least if element is not part of this
  60. // application but is part of another application and the connector
  61. // id happens to map to e.g. an extension in this application
  62. return null;
  63. }
  64. // Ensure this connector is really connected to the element. We cannot
  65. // be sure of this otherwise as the id comes from the DOM and could be
  66. // part of another application.
  67. ComponentConnector cc = (ComponentConnector) connector;
  68. if (cc.getWidget() == null || cc.getWidget().getElement() != element) {
  69. return null;
  70. }
  71. return cc;
  72. }
  73. /**
  74. * FIXME: What does this even do and why?
  75. *
  76. * @param pid
  77. * @return
  78. */
  79. public boolean isDragAndDropPaintable(String pid) {
  80. return (pid.startsWith("DD"));
  81. }
  82. /**
  83. * Checks if a connector with the given id has been registered.
  84. *
  85. * @param connectorId
  86. * The id to check for
  87. * @return true if a connector has been registered with the given id, false
  88. * otherwise
  89. */
  90. public boolean hasConnector(String connectorId) {
  91. return idToComponentDetail.containsKey(connectorId);
  92. }
  93. /**
  94. * Removes all registered connectors
  95. */
  96. public void clear() {
  97. idToComponentDetail.clear();
  98. }
  99. /**
  100. * Retrieves the connector whose widget matches the parameter.
  101. *
  102. * @param widget
  103. * The widget
  104. * @return A connector with {@literal widget} as its root widget or null if
  105. * no connector was found
  106. */
  107. public ComponentConnector getConnector(Widget widget) {
  108. return widget == null ? null : getConnector(widget.getElement());
  109. }
  110. public void registerConnector(String id, ServerConnector connector) {
  111. Profiler.enter("ConnectorMap.registerConnector");
  112. ComponentDetail componentDetail = GWT.create(ComponentDetail.class);
  113. idToComponentDetail.put(id, componentDetail);
  114. componentDetail.setConnector(connector);
  115. if (connector instanceof ComponentConnector) {
  116. ComponentConnector pw = (ComponentConnector) connector;
  117. Widget widget = pw.getWidget();
  118. Profiler.enter("ConnectorMap.setConnectorId");
  119. setConnectorId(widget.getElement(), id);
  120. Profiler.leave("ConnectorMap.setConnectorId");
  121. }
  122. Profiler.leave("ConnectorMap.registerConnector");
  123. }
  124. private static native void setConnectorId(Element el, String id)
  125. /*-{
  126. el.tkPid = id;
  127. }-*/;
  128. /**
  129. * Gets the connector id using a DOM element - the element should be the
  130. * root element for a connector, otherwise no id will be found. Use
  131. * {@link #getConnectorId(ServerConnector)} instead whenever possible.
  132. *
  133. * @see #getConnectorId(ServerConnector)
  134. * @param el
  135. * element of the connector whose id is desired
  136. * @return the id of the element's connector, if it's a connector
  137. */
  138. native static final String getConnectorId(Element el)
  139. /*-{
  140. return el.tkPid;
  141. }-*/;
  142. /**
  143. * Gets the main element for the connector with the given id. The reverse of
  144. * {@link #getConnectorId(Element)}.
  145. *
  146. * @param connectorId
  147. * the id of the widget whose element is desired
  148. * @return the element for the connector corresponding to the id
  149. */
  150. public Element getElement(String connectorId) {
  151. ServerConnector p = getConnector(connectorId);
  152. if (p instanceof ComponentConnector) {
  153. return ((ComponentConnector) p).getWidget().getElement();
  154. }
  155. return null;
  156. }
  157. /**
  158. * Unregisters the given connector; always use after removing a connector.
  159. * This method does not remove the connector from the DOM, but marks the
  160. * connector so that ApplicationConnection may clean up its references to
  161. * it. Removing the widget from DOM is component containers responsibility.
  162. *
  163. * @param connector
  164. * the connector to remove
  165. */
  166. public void unregisterConnector(ServerConnector connector) {
  167. if (connector == null) {
  168. getLogger().severe("Trying to unregister null connector");
  169. return;
  170. }
  171. String connectorId = connector.getConnectorId();
  172. idToComponentDetail.remove(connectorId);
  173. connector.onUnregister();
  174. for (ServerConnector child : connector.getChildren()) {
  175. if (child.getParent() == connector) {
  176. /*
  177. * Only unregister children that are actually connected to this
  178. * parent. For instance when moving connectors from one layout
  179. * to another and removing the first layout it will still
  180. * contain references to its old children, which are now
  181. * attached to another connector.
  182. */
  183. unregisterConnector(child);
  184. }
  185. }
  186. }
  187. /**
  188. * Gets all registered {@link ComponentConnector} instances
  189. *
  190. * @return An array of all registered {@link ComponentConnector} instances
  191. *
  192. * @deprecated As of 7.0.1, use {@link #getComponentConnectorsAsJsArray()}
  193. * for better performance.
  194. */
  195. @Deprecated
  196. public ComponentConnector[] getComponentConnectors() {
  197. ArrayList<ComponentConnector> result = new ArrayList<ComponentConnector>();
  198. JsArrayObject<ServerConnector> connectors = getConnectorsAsJsArray();
  199. int size = connectors.size();
  200. for (int i = 0; i < size; i++) {
  201. ServerConnector connector = connectors.get(i);
  202. if (connector instanceof ComponentConnector) {
  203. result.add((ComponentConnector) connector);
  204. }
  205. }
  206. return result.toArray(new ComponentConnector[result.size()]);
  207. }
  208. public JsArrayObject<ComponentConnector> getComponentConnectorsAsJsArray() {
  209. JsArrayObject<ComponentConnector> result = JavaScriptObject
  210. .createArray().cast();
  211. JsArrayObject<ServerConnector> connectors = getConnectorsAsJsArray();
  212. int size = connectors.size();
  213. for (int i = 0; i < size; i++) {
  214. ServerConnector connector = connectors.get(i);
  215. if (connector instanceof ComponentConnector) {
  216. result.add((ComponentConnector) connector);
  217. }
  218. }
  219. return result;
  220. }
  221. @Deprecated
  222. private ComponentDetail getComponentDetail(
  223. ComponentConnector componentConnector) {
  224. return idToComponentDetail.get(componentConnector.getConnectorId());
  225. }
  226. public int size() {
  227. return idToComponentDetail.size();
  228. }
  229. /**
  230. * @return
  231. *
  232. * @deprecated As of 7.0.1, use {@link #getConnectorsAsJsArray()} for
  233. * improved performance.
  234. */
  235. @Deprecated
  236. public Collection<? extends ServerConnector> getConnectors() {
  237. Collection<ComponentDetail> values = idToComponentDetail.values();
  238. ArrayList<ServerConnector> arrayList = new ArrayList<ServerConnector>(
  239. values.size());
  240. for (ComponentDetail componentDetail : values) {
  241. arrayList.add(componentDetail.getConnector());
  242. }
  243. return arrayList;
  244. }
  245. public JsArrayObject<ServerConnector> getConnectorsAsJsArray() {
  246. JsArrayObject<ComponentDetail> componentDetails = idToComponentDetail
  247. .valuesAsJsArray();
  248. JsArrayObject<ServerConnector> connectors = JavaScriptObject
  249. .createArray().cast();
  250. int size = componentDetails.size();
  251. for (int i = 0; i < size; i++) {
  252. connectors.add(componentDetails.get(i).getConnector());
  253. }
  254. return connectors;
  255. }
  256. /**
  257. * Tests if the widget is the root widget of a {@link ComponentConnector}.
  258. *
  259. * @param widget
  260. * The widget to test
  261. * @return true if the widget is the root widget of a
  262. * {@link ComponentConnector}, false otherwise
  263. */
  264. public boolean isConnector(Widget w) {
  265. return getConnectorId(w.getElement()) != null;
  266. }
  267. private static Logger getLogger() {
  268. return Logger.getLogger(ConnectorMap.class.getName());
  269. }
  270. }