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.

ConnectorMap.java 7.4KB

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