summaryrefslogtreecommitdiffstats
path: root/src/com/vaadin/terminal/gwt/client/ConnectorMap.java
blob: e57776cf1c628a3c57028303ba3e1a1272802e75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
@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.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.Widget;

public class ConnectorMap {

    private Map<String, ServerConnector> idToConnector = new HashMap<String, ServerConnector>();

    public static ConnectorMap get(ApplicationConnection applicationConnection) {
        return applicationConnection.getConnectorMap();
    }

    @Deprecated
    private final ComponentDetailMap idToComponentDetail = ComponentDetailMap
            .create();

    /**
     * Returns a {@link ServerConnector} 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 ServerConnector 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();
    }

    /**
     * Retrieves the connector whose widget matches the parameter.
     * 
     * @param widget
     *            The widget
     * @return A connector with {@literal widget} as its root widget or null if
     *         no connector was found
     */
    public ComponentConnector getConnector(Widget widget) {
        return getConnector(widget.getElement());
    }

    public void registerConnector(String id, ServerConnector 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 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(ServerConnector)} instead whenever possible.
     * 
     * @see #getConnectorId(ServerConnector)
     * @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) {
        ServerConnector 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(ServerConnector connector) {
        if (connector == null) {
            VConsole.error("Trying to unregister null connector");
            return;
        }

        String connectorId = connector.getConnectorId();

        idToComponentDetail.remove(connectorId);
        idToConnector.remove(connectorId);
        connector.onUnregister();

        if (connector instanceof ComponentContainerConnector) {
            for (ComponentConnector child : ((ComponentContainerConnector) connector)
                    .getChildren()) {
                if (child.getParent() == connector) {
                    // Only unregister children that are actually connected to
                    // this parent. For instance when moving connectors from one
                    // layout to another and removing the first layout it will
                    // still contain references to its old children, which are
                    // now attached to another connector.
                    unregisterConnector(child);
                }
            }
        }
    }

    /**
     * Gets all registered {@link ComponentConnector} instances
     * 
     * @return An array of all registered {@link ComponentConnector} instances
     */
    public ComponentConnector[] getComponentConnectors() {
        ArrayList<ComponentConnector> result = new ArrayList<ComponentConnector>();

        for (ServerConnector connector : getConnectors()) {
            if (connector instanceof ComponentConnector) {
                result.add((ComponentConnector) connector);
            }
        }

        return result.toArray(new ComponentConnector[result.size()]);
    }

    @Deprecated
    private ComponentDetail getComponentDetail(
            ComponentConnector componentConnector) {
        return idToComponentDetail.get(componentConnector.getConnectorId());
    }

    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 ServerConnector> 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);

    }

    /**
     * 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.getElement()) != null;
    }

}