123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- /*
- @VaadinApache2LicenseForJavaFiles@
- */
- package com.vaadin.terminal.gwt.client;
-
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
-
- import com.google.gwt.core.client.GWT;
- import com.google.gwt.dom.client.Element;
- import com.google.gwt.user.client.ui.HasWidgets;
- import com.google.gwt.user.client.ui.Widget;
- import com.vaadin.terminal.Paintable;
- import com.vaadin.terminal.gwt.client.RenderInformation.Size;
-
- public class ConnectorMap {
-
- private Map<String, Connector> idToConnector = new HashMap<String, Connector>();
-
- public static ConnectorMap get(ApplicationConnection applicationConnection) {
- return applicationConnection.getConnectorMap();
- }
-
- @Deprecated
- private final ComponentDetailMap idToComponentDetail = ComponentDetailMap
- .create();
-
- private Set<String> unregistryBag = new HashSet<String>();
-
- /**
- * Returns a {@link Connector} by its id
- *
- * @param id
- * The connector id
- * @return A connector or null if a connector with the given id has not been
- * registered
- */
- public Connector getConnector(String connectorId) {
- return idToConnector.get(connectorId);
- }
-
- /**
- * Returns a {@link ComponentConnector} element by its root element
- *
- * @param element
- * Root element of the {@link ComponentConnector}
- * @return A connector or null if a connector with the given id has not been
- * registered
- */
- public ComponentConnector getConnector(Element element) {
- return (ComponentConnector) getConnector(getConnectorId(element));
- }
-
- /**
- * FIXME: What does this even do and why?
- *
- * @param pid
- * @return
- */
- public boolean isDragAndDropPaintable(String pid) {
- return (pid.startsWith("DD"));
- }
-
- /**
- * Checks if a connector with the given id has been registered.
- *
- * @param connectorId
- * The id to check for
- * @return true if a connector has been registered with the given id, false
- * otherwise
- */
- public boolean hasConnector(String connectorId) {
- return idToConnector.containsKey(connectorId);
- }
-
- /**
- * Removes all registered connectors
- */
- public void clear() {
- idToConnector.clear();
- idToComponentDetail.clear();
- }
-
- public ComponentConnector getConnector(Widget widget) {
- return getConnector(widget.getElement());
- }
-
- public void registerConnector(String id, Connector connector) {
- ComponentDetail componentDetail = GWT.create(ComponentDetail.class);
- idToComponentDetail.put(id, componentDetail);
- idToConnector.put(id, connector);
- if (connector instanceof ComponentConnector) {
- ComponentConnector pw = (ComponentConnector) connector;
- setConnectorId(pw.getWidget().getElement(), id);
- }
- }
-
- private native void setConnectorId(Element el, String id)
- /*-{
- el.tkPid = id;
- }-*/;
-
- /**
- * Gets the id for a specific connector.
- * <p>
- * The id is used in the UIDL to identify a specific widget instance,
- * effectively linking the widget with it's server side Component.
- * </p>
- *
- * @param connector
- * the connector whose id is needed
- * @return the id for the given connector or null if the connector could not
- * be found
- * @deprecated use {@link Connector#getId()} instead
- */
- @Deprecated
- public String getConnectorId(Connector connector) {
- if (connector == null) {
- return null;
- }
- return connector.getId();
- }
-
- @Deprecated
- public String getConnectorId(Widget widget) {
- return getConnectorId(widget.getElement());
- }
-
- /**
- * Gets the connector id using a DOM element - the element should be the
- * root element for a connector, otherwise no id will be found. Use
- * {@link #getConnectorId(Connector)} instead whenever possible.
- *
- * @see #getConnectorId(Paintable)
- * @param el
- * element of the connector whose id is desired
- * @return the id of the element's connector, if it's a connector
- */
- native String getConnectorId(Element el)
- /*-{
- return el.tkPid;
- }-*/;
-
- /**
- * Gets the main element for the connector with the given id. The reverse of
- * {@link #getConnectorId(Element)}.
- *
- * @param connectorId
- * the id of the widget whose element is desired
- * @return the element for the connector corresponding to the id
- */
- public Element getElement(String connectorId) {
- Connector p = getConnector(connectorId);
- if (p instanceof ComponentConnector) {
- return ((ComponentConnector) p).getWidget().getElement();
- }
-
- return null;
- }
-
- /**
- * Unregisters the given connector; always use after removing a connector.
- * This method does not remove the connector from the DOM, but marks the
- * connector so that ApplicationConnection may clean up its references to
- * it. Removing the widget from DOM is component containers responsibility.
- *
- * @param connector
- * the connector to remove
- */
- public void unregisterConnector(Connector connector) {
-
- // add to unregistry que
-
- if (connector == null) {
- VConsole.error("WARN: Trying to unregister null connector");
- return;
- }
- String id = connector.getId();
- Widget widget = null;
- if (connector instanceof ComponentConnector) {
- widget = ((ComponentConnector) connector).getWidget();
- }
-
- if (id == null) {
- /*
- * Uncomment the following to debug unregistring components. No
- * paintables with null id should end here. At least one exception
- * is our VScrollTableRow, that is hacked to fake it self as a
- * Paintable to build support for sizing easier.
- */
- // if (!(p instanceof VScrollTableRow)) {
- // VConsole.log("Trying to unregister Paintable not created by Application Connection.");
- // }
- } else {
- unregistryBag.add(id);
- }
- if (widget != null && widget instanceof HasWidgets) {
- unregisterChildConnectors((HasWidgets) widget);
- }
-
- }
-
- public ComponentConnector[] getRegisteredComponentConnectors() {
- ArrayList<ComponentConnector> result = new ArrayList<ComponentConnector>();
-
- for (Connector connector : getConnectors()) {
- if (connector instanceof ComponentConnector) {
- ComponentConnector componentConnector = (ComponentConnector) connector;
- if (!unregistryBag.contains(getConnectorId(connector))) {
- result.add(componentConnector);
- }
- }
- }
-
- return result.toArray(new ComponentConnector[result.size()]);
- }
-
- void purgeUnregistryBag(boolean unregisterConnectors) {
- if (unregisterConnectors) {
- for (String connectorId : unregistryBag) {
- // TODO purge shared state for pid
- Connector connector = getConnector(connectorId);
- if (connector == null) {
- /*
- * this should never happen, but it does :-( See e.g.
- * com.vaadin.tests.components.accordion.RemoveTabs (with
- * test script)
- */
- VConsole.error("Tried to unregister component (id="
- + connectorId
- + ") that is never registered (or already unregistered)");
- continue;
- }
- Widget widget = null;
- if (connector instanceof ComponentConnector) {
- widget = ((ComponentConnector) connector).getWidget();
- }
-
- // check if can be cleaned
- if (widget == null || !widget.isAttached()) {
- // clean reference to paintable
- idToComponentDetail.remove(connectorId);
- idToConnector.remove(connectorId);
- }
- /*
- * else NOP : same component has been reattached to another
- * parent or replaced by another component implementation.
- */
- }
- }
-
- unregistryBag.clear();
- }
-
- /**
- * Unregisters the child connectors for the given container recursively.
- *
- * Use when after removing a connector that contains other connectors. Does
- * not unregister the given container itself. Does not actually remove the
- * widgets from the DOM.
- *
- * @see #unregisterConnector(Connector)
- * @param container
- * The container that contains the connectors that should be
- * unregistered
- */
- public void unregisterChildConnectors(HasWidgets container) {
- // FIXME: This should be based on the paintable hierarchy
- final Iterator<Widget> it = container.iterator();
- while (it.hasNext()) {
- final Widget w = it.next();
- ComponentConnector p = getConnector(w);
- if (p != null) {
- // This will unregister the paintable and all its children
- unregisterConnector(p);
- } else if (w instanceof HasWidgets) {
- // For normal widget containers, unregister the children
- unregisterChildConnectors((HasWidgets) w);
- }
- }
- }
-
- /**
- * FIXME: Should not be here
- *
- * @param pid
- * @param uidl
- */
- @Deprecated
- public void registerEventListenersFromUIDL(String pid, UIDL uidl) {
- ComponentDetail cd = idToComponentDetail.get(pid);
- if (cd == null) {
- throw new IllegalArgumentException("Pid must not be null");
- }
-
- cd.registerEventListenersFromUIDL(uidl);
-
- }
-
- /**
- * FIXME: Should not be here
- *
- * @param paintable
- * @return
- */
- @Deprecated
- public Size getOffsetSize(ComponentConnector paintable) {
- return getComponentDetail(paintable).getOffsetSize();
- }
-
- /**
- * FIXME: Should not be here
- *
- * @param componentConnector
- * @return
- */
- @Deprecated
- public void setOffsetSize(ComponentConnector componentConnector,
- Size newSize) {
- getComponentDetail(componentConnector).setOffsetSize(newSize);
- }
-
- private ComponentDetail getComponentDetail(
- ComponentConnector componentConnector) {
- return idToComponentDetail.get(getConnectorId(componentConnector));
- }
-
- public int size() {
- return idToConnector.size();
- }
-
- /**
- * FIXME: Should be moved to VAbstractPaintableWidget
- *
- * @param paintable
- * @return
- */
- @Deprecated
- public TooltipInfo getTooltipInfo(ComponentConnector paintable, Object key) {
- return getComponentDetail(paintable).getTooltipInfo(key);
- }
-
- @Deprecated
- public TooltipInfo getWidgetTooltipInfo(Widget widget, Object key) {
- return getTooltipInfo(getConnector(widget), key);
- }
-
- public Collection<? extends Connector> getConnectors() {
- return Collections.unmodifiableCollection(idToConnector.values());
- }
-
- /**
- * FIXME: Should not be here
- *
- * @param componentConnector
- * @return
- */
- @Deprecated
- public void registerTooltip(ComponentConnector componentConnector,
- Object key, TooltipInfo tooltip) {
- getComponentDetail(componentConnector).putAdditionalTooltip(key,
- tooltip);
-
- }
-
- /**
- * FIXME: Should not be here
- *
- * @param componentConnector
- * @return
- */
- @Deprecated
- public boolean hasEventListeners(ComponentConnector componentConnector,
- String eventIdentifier) {
- return getComponentDetail(componentConnector).hasEventListeners(
- eventIdentifier);
- }
-
- /**
- * Tests if the widget is the root widget of a {@link ComponentConnector}.
- *
- * @param widget
- * The widget to test
- * @return true if the widget is the root widget of a
- * {@link ComponentConnector}, false otherwise
- */
- public boolean isConnector(Widget w) {
- return getConnectorId(w) != null;
- }
-
- }
|