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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright 2011 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.Collections;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. import com.google.gwt.core.client.GWT;
  23. import com.google.gwt.dom.client.Element;
  24. import com.google.gwt.user.client.ui.Widget;
  25. public class ConnectorMap {
  26. private Map<String, ServerConnector> idToConnector = new HashMap<String, ServerConnector>();
  27. public static ConnectorMap get(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 id
  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. return idToConnector.get(connectorId);
  43. }
  44. /**
  45. * Returns a {@link ComponentConnector} element by its root element.
  46. *
  47. * @param element
  48. * Root element of the {@link ComponentConnector}
  49. * @return A connector or null if a connector with the given id has not been
  50. * registered
  51. */
  52. public ComponentConnector getConnector(Element element) {
  53. ServerConnector connector = getConnector(getConnectorId(element));
  54. if (!(connector instanceof ComponentConnector)) {
  55. // This can happen at least if element is not part of this
  56. // application but is part of another application and the connector
  57. // id happens to map to e.g. an extension in this application
  58. return null;
  59. }
  60. // Ensure this connector is really connected to the element. We cannot
  61. // be sure of this otherwise as the id comes from the DOM and could be
  62. // part of another application.
  63. ComponentConnector cc = (ComponentConnector) connector;
  64. if (cc.getWidget() == null || cc.getWidget().getElement() != element) {
  65. return null;
  66. }
  67. return cc;
  68. }
  69. /**
  70. * FIXME: What does this even do and why?
  71. *
  72. * @param pid
  73. * @return
  74. */
  75. public boolean isDragAndDropPaintable(String pid) {
  76. return (pid.startsWith("DD"));
  77. }
  78. /**
  79. * Checks if a connector with the given id has been registered.
  80. *
  81. * @param connectorId
  82. * The id to check for
  83. * @return true if a connector has been registered with the given id, false
  84. * otherwise
  85. */
  86. public boolean hasConnector(String connectorId) {
  87. return idToConnector.containsKey(connectorId);
  88. }
  89. /**
  90. * Removes all registered connectors
  91. */
  92. public void clear() {
  93. idToConnector.clear();
  94. idToComponentDetail.clear();
  95. }
  96. /**
  97. * Retrieves the connector whose widget matches the parameter.
  98. *
  99. * @param widget
  100. * The widget
  101. * @return A connector with {@literal widget} as its root widget or null if
  102. * no connector was found
  103. */
  104. public ComponentConnector getConnector(Widget widget) {
  105. return getConnector(widget.getElement());
  106. }
  107. public void registerConnector(String id, ServerConnector connector) {
  108. ComponentDetail componentDetail = GWT.create(ComponentDetail.class);
  109. idToComponentDetail.put(id, componentDetail);
  110. idToConnector.put(id, connector);
  111. if (connector instanceof ComponentConnector) {
  112. ComponentConnector pw = (ComponentConnector) connector;
  113. setConnectorId(pw.getWidget().getElement(), id);
  114. }
  115. }
  116. private native void setConnectorId(Element el, String id)
  117. /*-{
  118. el.tkPid = id;
  119. }-*/;
  120. /**
  121. * Gets the connector id using a DOM element - the element should be the
  122. * root element for a connector, otherwise no id will be found. Use
  123. * {@link #getConnectorId(ServerConnector)} instead whenever possible.
  124. *
  125. * @see #getConnectorId(ServerConnector)
  126. * @param el
  127. * element of the connector whose id is desired
  128. * @return the id of the element's connector, if it's a connector
  129. */
  130. native String getConnectorId(Element el)
  131. /*-{
  132. return el.tkPid;
  133. }-*/;
  134. /**
  135. * Gets the main element for the connector with the given id. The reverse of
  136. * {@link #getConnectorId(Element)}.
  137. *
  138. * @param connectorId
  139. * the id of the widget whose element is desired
  140. * @return the element for the connector corresponding to the id
  141. */
  142. public Element getElement(String connectorId) {
  143. ServerConnector p = getConnector(connectorId);
  144. if (p instanceof ComponentConnector) {
  145. return ((ComponentConnector) p).getWidget().getElement();
  146. }
  147. return null;
  148. }
  149. /**
  150. * Unregisters the given connector; always use after removing a connector.
  151. * This method does not remove the connector from the DOM, but marks the
  152. * connector so that ApplicationConnection may clean up its references to
  153. * it. Removing the widget from DOM is component containers responsibility.
  154. *
  155. * @param connector
  156. * the connector to remove
  157. */
  158. public void unregisterConnector(ServerConnector connector) {
  159. if (connector == null) {
  160. VConsole.error("Trying to unregister null connector");
  161. return;
  162. }
  163. String connectorId = connector.getConnectorId();
  164. idToComponentDetail.remove(connectorId);
  165. idToConnector.remove(connectorId);
  166. connector.onUnregister();
  167. for (ServerConnector child : connector.getChildren()) {
  168. if (child.getParent() == connector) {
  169. /*
  170. * Only unregister children that are actually connected to this
  171. * parent. For instance when moving connectors from one layout
  172. * to another and removing the first layout it will still
  173. * contain references to its old children, which are now
  174. * attached to another connector.
  175. */
  176. unregisterConnector(child);
  177. }
  178. }
  179. }
  180. /**
  181. * Gets all registered {@link ComponentConnector} instances
  182. *
  183. * @return An array of all registered {@link ComponentConnector} instances
  184. */
  185. public ComponentConnector[] getComponentConnectors() {
  186. ArrayList<ComponentConnector> result = new ArrayList<ComponentConnector>();
  187. for (ServerConnector connector : getConnectors()) {
  188. if (connector instanceof ComponentConnector) {
  189. result.add((ComponentConnector) connector);
  190. }
  191. }
  192. return result.toArray(new ComponentConnector[result.size()]);
  193. }
  194. @Deprecated
  195. private ComponentDetail getComponentDetail(
  196. ComponentConnector componentConnector) {
  197. return idToComponentDetail.get(componentConnector.getConnectorId());
  198. }
  199. public int size() {
  200. return idToConnector.size();
  201. }
  202. public Collection<? extends ServerConnector> getConnectors() {
  203. return Collections.unmodifiableCollection(idToConnector.values());
  204. }
  205. /**
  206. * Tests if the widget is the root widget of a {@link ComponentConnector}.
  207. *
  208. * @param widget
  209. * The widget to test
  210. * @return true if the widget is the root widget of a
  211. * {@link ComponentConnector}, false otherwise
  212. */
  213. public boolean isConnector(Widget w) {
  214. return getConnectorId(w.getElement()) != null;
  215. }
  216. }