您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ConnectorMap.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. Profiler.leave("ConnectorMap.registerConnector");
  116. }
  117. /**
  118. * Gets the connector id using a DOM element - the element should be the
  119. * root element for a connector, otherwise no id will be found. Use
  120. * {@link #getConnectorId(ServerConnector)} instead whenever possible.
  121. *
  122. * @see #getConnectorId(ServerConnector)
  123. * @param el
  124. * element of the connector whose id is desired
  125. * @return the id of the element's connector, if it's a connector
  126. */
  127. native static final String getConnectorId(Element el)
  128. /*-{
  129. return el.tkPid;
  130. }-*/;
  131. /**
  132. * Gets the main element for the connector with the given id. The reverse of
  133. * {@link #getConnectorId(Element)}.
  134. *
  135. * @param connectorId
  136. * the id of the widget whose element is desired
  137. * @return the element for the connector corresponding to the id
  138. */
  139. public Element getElement(String connectorId) {
  140. ServerConnector p = getConnector(connectorId);
  141. if (p instanceof ComponentConnector) {
  142. return ((ComponentConnector) p).getWidget().getElement();
  143. }
  144. return null;
  145. }
  146. /**
  147. * Unregisters the given connector; always use after removing a connector.
  148. * This method does not remove the connector from the DOM, but marks the
  149. * connector so that ApplicationConnection may clean up its references to
  150. * it. Removing the widget from DOM is component containers responsibility.
  151. *
  152. * @param connector
  153. * the connector to remove
  154. */
  155. public void unregisterConnector(ServerConnector connector) {
  156. if (connector == null) {
  157. getLogger().severe("Trying to unregister null connector");
  158. return;
  159. }
  160. String connectorId = connector.getConnectorId();
  161. idToComponentDetail.remove(connectorId);
  162. connector.onUnregister();
  163. for (ServerConnector child : connector.getChildren()) {
  164. if (child.getParent() == connector) {
  165. /*
  166. * Only unregister children that are actually connected to this
  167. * parent. For instance when moving connectors from one layout
  168. * to another and removing the first layout it will still
  169. * contain references to its old children, which are now
  170. * attached to another connector.
  171. */
  172. unregisterConnector(child);
  173. }
  174. }
  175. }
  176. /**
  177. * Gets all registered {@link ComponentConnector} instances
  178. *
  179. * @return An array of all registered {@link ComponentConnector} instances
  180. *
  181. * @deprecated As of 7.0.1, use {@link #getComponentConnectorsAsJsArray()}
  182. * for better performance.
  183. */
  184. @Deprecated
  185. public ComponentConnector[] getComponentConnectors() {
  186. ArrayList<ComponentConnector> result = new ArrayList<>();
  187. JsArrayObject<ServerConnector> connectors = getConnectorsAsJsArray();
  188. int size = connectors.size();
  189. for (int i = 0; i < size; i++) {
  190. ServerConnector connector = connectors.get(i);
  191. if (connector instanceof ComponentConnector) {
  192. result.add((ComponentConnector) connector);
  193. }
  194. }
  195. return result.toArray(new ComponentConnector[result.size()]);
  196. }
  197. public JsArrayObject<ComponentConnector> getComponentConnectorsAsJsArray() {
  198. JsArrayObject<ComponentConnector> result = JavaScriptObject
  199. .createArray().cast();
  200. JsArrayObject<ServerConnector> connectors = getConnectorsAsJsArray();
  201. int size = connectors.size();
  202. for (int i = 0; i < size; i++) {
  203. ServerConnector connector = connectors.get(i);
  204. if (connector instanceof ComponentConnector) {
  205. result.add((ComponentConnector) connector);
  206. }
  207. }
  208. return result;
  209. }
  210. @Deprecated
  211. private ComponentDetail getComponentDetail(
  212. ComponentConnector componentConnector) {
  213. return idToComponentDetail.get(componentConnector.getConnectorId());
  214. }
  215. public int size() {
  216. return idToComponentDetail.size();
  217. }
  218. /**
  219. * @return
  220. *
  221. * @deprecated As of 7.0.1, use {@link #getConnectorsAsJsArray()} for
  222. * improved performance.
  223. */
  224. @Deprecated
  225. public Collection<? extends ServerConnector> getConnectors() {
  226. Collection<ComponentDetail> values = idToComponentDetail.values();
  227. ArrayList<ServerConnector> arrayList = new ArrayList<>(values.size());
  228. for (ComponentDetail componentDetail : values) {
  229. arrayList.add(componentDetail.getConnector());
  230. }
  231. return arrayList;
  232. }
  233. public JsArrayObject<ServerConnector> getConnectorsAsJsArray() {
  234. JsArrayObject<ComponentDetail> componentDetails = idToComponentDetail
  235. .valuesAsJsArray();
  236. JsArrayObject<ServerConnector> connectors = JavaScriptObject
  237. .createArray().cast();
  238. int size = componentDetails.size();
  239. for (int i = 0; i < size; i++) {
  240. connectors.add(componentDetails.get(i).getConnector());
  241. }
  242. return connectors;
  243. }
  244. /**
  245. * Tests if the widget is the root widget of a {@link ComponentConnector}.
  246. *
  247. * @param widget
  248. * The widget to test
  249. * @return true if the widget is the root widget of a
  250. * {@link ComponentConnector}, false otherwise
  251. */
  252. public boolean isConnector(Widget w) {
  253. return getConnectorId(w.getElement()) != null;
  254. }
  255. private static Logger getLogger() {
  256. return Logger.getLogger(ConnectorMap.class.getName());
  257. }
  258. }