summaryrefslogtreecommitdiffstats
path: root/src/com/vaadin/terminal/gwt/client/ui/AbstractComponentConnector.java
blob: fdb04f0ddfe73ecdfa51b13cb818f00e6240d702 (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
@VaadinApache2LicenseForJavaFiles@
 */
package com.vaadin.terminal.gwt.client.ui;

import java.util.Set;

import com.google.gwt.user.client.ui.Focusable;
import com.google.gwt.user.client.ui.HasEnabled;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.terminal.gwt.client.ApplicationConnection;
import com.vaadin.terminal.gwt.client.ComponentConnector;
import com.vaadin.terminal.gwt.client.ComponentContainerConnector;
import com.vaadin.terminal.gwt.client.ComponentState;
import com.vaadin.terminal.gwt.client.ConnectorMap;
import com.vaadin.terminal.gwt.client.LayoutManager;
import com.vaadin.terminal.gwt.client.TooltipInfo;
import com.vaadin.terminal.gwt.client.UIDL;
import com.vaadin.terminal.gwt.client.Util;
import com.vaadin.terminal.gwt.client.VConsole;
import com.vaadin.terminal.gwt.client.communication.SharedState;
import com.vaadin.terminal.gwt.client.communication.StateChangeEvent;
import com.vaadin.terminal.gwt.client.ui.root.RootConnector;

public abstract class AbstractComponentConnector extends AbstractConnector
        implements ComponentConnector {

    private ComponentContainerConnector parent;

    private Widget widget;

    // shared state from the server to the client
    private ComponentState state;

    private String lastKnownWidth = "";
    private String lastKnownHeight = "";

    /**
     * Default constructor
     */
    public AbstractComponentConnector() {
    }

    /**
     * Creates and returns the widget for this VPaintableWidget. This method
     * should only be called once when initializing the paintable.
     * 
     * @return
     */
    protected abstract Widget createWidget();

    /**
     * Returns the widget associated with this paintable. The widget returned by
     * this method must not changed during the life time of the paintable.
     * 
     * @return The widget associated with this paintable
     */
    public Widget getWidget() {
        if (widget == null) {
            widget = createWidget();
        }

        return widget;
    }

    /**
     * Returns the shared state object for this connector.
     * 
     * If overriding this method to return a more specific type, also
     * {@link #createState()} must be overridden.
     * 
     * @return current shared state (not null)
     */
    public ComponentState getState() {
        return state;
    }

    @Deprecated
    public static boolean isRealUpdate(UIDL uidl) {
        return !uidl.hasAttribute("cached");
    }

    @Override
    public void onStateChanged(StateChangeEvent stateChangeEvent) {
        super.onStateChanged(stateChangeEvent);

        ConnectorMap paintableMap = ConnectorMap.get(getConnection());

        if (getState().getDebugId() != null) {
            getWidget().getElement().setId(getState().getDebugId());
        } else {
            getWidget().getElement().removeAttribute("id");

        }

        /*
         * Disabled state may affect (override) tabindex so the order must be
         * first setting tabindex, then enabled state.
         */
        if (state instanceof TabIndexState && getWidget() instanceof Focusable) {
            ((Focusable) getWidget()).setTabIndex(((TabIndexState) state)
                    .getTabIndex());
        }

        setWidgetEnabled(isEnabled());

        // Style names
        String styleName = getStyleNames(getWidget().getStylePrimaryName());
        getWidget().setStyleName(styleName);

        // Update tooltip
        TooltipInfo tooltipInfo = paintableMap.getTooltipInfo(this, null);
        if (getState().hasDescription()) {
            tooltipInfo.setTitle(getState().getDescription());
        } else {
            tooltipInfo.setTitle(null);
        }
        // add error info to tooltip if present
        tooltipInfo.setErrorMessage(getState().getErrorMessage());

        // Set captions
        if (delegateCaptionHandling()) {
            ComponentContainerConnector parent = getParent();
            if (parent != null) {
                parent.updateCaption(this);
            } else if (!(this instanceof RootConnector)) {
                VConsole.error("Parent of connector "
                        + Util.getConnectorString(this)
                        + " is null. This is typically an indication of a broken component hierarchy");
            }
        }

        /*
         * updateComponentSize need to be after caption update so caption can be
         * taken into account
         */

        updateComponentSize();
    }

    public void setWidgetEnabled(boolean widgetEnabled) {
        if (getWidget() instanceof HasEnabled) {
            ((HasEnabled) getWidget()).setEnabled(widgetEnabled);
        }
    }

    private void updateComponentSize() {
        String newWidth = getState().getWidth();
        String newHeight = getState().getHeight();

        // Parent should be updated if either dimension changed between relative
        // and non-relative
        if (newWidth.endsWith("%") != lastKnownWidth.endsWith("%")) {
            ComponentContainerConnector parent = getParent();
            if (parent instanceof ManagedLayout) {
                getLayoutManager().setNeedsHorizontalLayout(
                        (ManagedLayout) parent);
            }
        }

        if (newHeight.endsWith("%") != lastKnownHeight.endsWith("%")) {
            ComponentContainerConnector parent = getParent();
            if (parent instanceof ManagedLayout) {
                getLayoutManager().setNeedsVerticalLayout(
                        (ManagedLayout) parent);
            }
        }

        lastKnownWidth = newWidth;
        lastKnownHeight = newHeight;

        // Set defined sizes
        Widget widget = getWidget();

        widget.setStyleName("v-has-width", !isUndefinedWidth());
        widget.setStyleName("v-has-height", !isUndefinedHeight());

        widget.setHeight(newHeight);
        widget.setWidth(newWidth);
    }

    public boolean isRelativeHeight() {
        return getState().getHeight().endsWith("%");
    }

    public boolean isRelativeWidth() {
        return getState().getWidth().endsWith("%");
    }

    public boolean isUndefinedHeight() {
        return getState().getHeight().length() == 0;
    }

    public boolean isUndefinedWidth() {
        return getState().getWidth().length() == 0;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.vaadin.terminal.gwt.client.Connector#isEnabled()
     */
    public boolean isEnabled() {
        if (!getState().isEnabled()) {
            return false;
        }

        if (getParent() == null) {
            return true;
        } else {
            return getParent().isEnabled();
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.vaadin.terminal.gwt.client.ComponentConnector#delegateCaptionHandling
     * ()
     */
    public boolean delegateCaptionHandling() {
        return true;
    }

    /**
     * Generates the style name for the widget based on the given primary style
     * name and the shared state.
     * <p>
     * This method can be overridden to provide additional style names for the
     * component
     * </p>
     * 
     * @param primaryStyleName
     *            The primary style name to use when generating the final style
     *            names
     * @return The style names, settable using
     *         {@link Widget#setStyleName(String)}
     */
    protected String getStyleNames(String primaryStyleName) {
        ComponentState state = getState();

        StringBuilder styleBuf = new StringBuilder();
        styleBuf.append(primaryStyleName);
        styleBuf.append(" v-connector");

        // Uses connector methods to enable connectors to take hierarchy or
        // multiple state variables into account
        if (!isEnabled()) {
            styleBuf.append(" ");
            styleBuf.append(ApplicationConnection.DISABLED_CLASSNAME);
        }
        if (isReadOnly()) {
            styleBuf.append(" ");
            styleBuf.append("v-readonly");
        }

        // add additional styles as css classes, prefixed with component default
        // stylename
        if (state.hasStyles()) {
            for (String style : state.getStyles()) {
                styleBuf.append(" ");
                styleBuf.append(primaryStyleName);
                styleBuf.append("-");
                styleBuf.append(style);
                styleBuf.append(" ");
                styleBuf.append(style);
            }
        }

        // add error classname to components w/ error
        if (null != state.getErrorMessage()) {
            styleBuf.append(" ");
            styleBuf.append(primaryStyleName);
            styleBuf.append(ApplicationConnection.ERROR_CLASSNAME_EXT);
        }

        return styleBuf.toString();
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.vaadin.terminal.gwt.client.ComponentConnector#isReadOnly()
     */
    @Deprecated
    public boolean isReadOnly() {
        return getState().isReadOnly();
    }

    /**
     * Sets the shared state for the paintable widget.
     * 
     * @param new shared state (must be compatible with the return value of
     *        {@link #getState()} - {@link ComponentState} if
     *        {@link #getState()} is not overridden
     */
    public final void setState(SharedState state) {
        this.state = (ComponentState) state;
    }

    public LayoutManager getLayoutManager() {
        return LayoutManager.get(getConnection());
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.vaadin.terminal.gwt.client.Connector#getParent()
     */
    public ComponentContainerConnector getParent() {
        return parent;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.vaadin.terminal.gwt.client.Connector#setParent(com.vaadin.terminal
     * .gwt.client.ComponentContainerConnector)
     */
    public void setParent(ComponentContainerConnector parent) {
        this.parent = parent;
    }

    /**
     * Checks if there is a registered server side listener for the given event
     * identifier.
     * 
     * @param eventIdentifier
     *            The identifier to check for
     * @return true if an event listener has been registered with the given
     *         event identifier on the server side, false otherwise
     */
    public boolean hasEventListener(String eventIdentifier) {
        Set<String> reg = getState().getRegisteredEventListeners();
        return (reg != null && reg.contains(eventIdentifier));
    }

    @Override
    public void onUnregister() {
        super.onUnregister();

        // Show an error if widget is still attached to DOM. It should never be
        // at this point.
        if (getWidget() != null && getWidget().isAttached()) {
            getWidget().removeFromParent();
            VConsole.error("Widget is still attached to the DOM after the connector ("
                    + Util.getConnectorString(this)
                    + ") has been unregistered. Widget was removed.");
        }
    }
}