summaryrefslogtreecommitdiffstats
path: root/src/com/vaadin/terminal/AbstractExtension.java
blob: 6753d6f929a288e84aa2c6b5f5ea022953157dec (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
/*
@VaadinApache2LicenseForJavaFiles@
 */

package com.vaadin.terminal;

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

public abstract class AbstractExtension extends AbstractClientConnector
        implements Extension {
    private boolean previouslyAttached = false;

    protected Class<? extends ClientConnector> getAcceptedParentType() {
        return ClientConnector.class;
    }

    protected void extend(AbstractClientConnector target) {
        target.addExtension(this);
    }

    protected 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> acceptedParentType = getAcceptedParentType();
        if (parent == null || acceptedParentType.isInstance(parent)) {
            super.setParent(parent);
            previouslyAttached = true;
        } else {
            throw new IllegalArgumentException(getClass().getName()
                    + " can only be attached to targets of type "
                    + acceptedParentType.getName() + " but attach to "
                    + parent.getClass().getName() + " was attempted.");
        }
    }

}