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

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