summaryrefslogtreecommitdiffstats
path: root/server/src/com/vaadin/terminal/AbstractExtension.java
blob: 8ac0ce01e36657244308e4b6d112cb6bd0db71c3 (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
/*
 * Copyright 2011 Vaadin Ltd.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.vaadin.terminal;

import com.vaadin.terminal.gwt.server.ClientConnector;

/**
 * An extension is an entity that is attached to a Component or another
 * Extension and independently communicates between client and server.
 * <p>
 * Extensions can use shared state and RPC in the same way as components.
 * <p>
 * AbstractExtension adds a mechanism for adding the extension to any Connector
 * (extend). To let the Extension determine what kind target it can be added to,
 * the extend method is declared as protected.
 * 
 * @author Vaadin Ltd
 * @since 7.0.0
 */
public abstract class AbstractExtension extends AbstractClientConnector
        implements Extension {
    private boolean previouslyAttached = false;

    /**
     * Gets a type that the parent must be an instance of. Override this if the
     * extension only support certain targets, e.g. if only TextFields can be
     * extended.
     * 
     * @return a type that the parent must be an instance of
     */
    protected Class<? extends ClientConnector> getSupportedParentType() {
        return ClientConnector.class;
    }

    /**
     * Add this extension to the target connector. This method is protected to
     * allow subclasses to require a more specific type of target.
     * 
     * @param target
     *            the connector to attach this extension to
     */
    protected void extend(AbstractClientConnector target) {
        target.addExtension(this);
    }

    /**
     * Remove this extension from its target. After an extension has been
     * removed, it can not be attached again.
     */
    public void removeFromTarget() {
        getParent().removeExtension(this);
    }

    @Override
    public void setParent(ClientConnector parent) {
        if (previouslyAttached && parent != null) {
            throw new IllegalStateException(
                    "An extension can not be set to extend a new target after getting detached from the previous.");
        }

        Class<? extends ClientConnector> supportedParentType = getSupportedParentType();
        if (parent == null || supportedParentType.isInstance(parent)) {
            super.setParent(parent);
            previouslyAttached = true;
        } else {
            throw new IllegalArgumentException(getClass().getName()
                    + " can only be attached to targets of type "
                    + supportedParentType.getName() + " but attach to "
                    + parent.getClass().getName() + " was attempted.");
        }
    }

}