aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/main/java/com/vaadin/ui/renderers/AbstractRenderer.java
blob: 43ab4108a9fa503ae3679b93e2920e4adf0bc006 (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
/*
 * Copyright 2000-2016 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.ui.renderers;

import java.util.Objects;

import com.vaadin.server.AbstractClientConnector;
import com.vaadin.server.AbstractExtension;
import com.vaadin.server.JsonCodec;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Grid.Column;
import com.vaadin.ui.renderers.ClickableRenderer.RendererClickEvent;

import elemental.json.JsonValue;

/**
 * An abstract base class for server-side
 * {@link com.vaadin.ui.renderers.Renderer Grid renderers}.
 *
 * <p>
 * This class currently extends the AbstractExtension superclass, but this fact
 * should be regarded as an implementation detail and subject to change in a
 * future major or minor Vaadin version.
 *
 * @param <T>
 *            the grid type this renderer can be attached to
 * @param <V>
 *            the type this renderer knows how to present
 */
public abstract class AbstractRenderer<T, V> extends AbstractExtension
        implements Renderer<V> {
    private final Class<V> presentationType;

    private final String nullRepresentation;

    /**
     * Creates a new renderer with the given presentation type and null
     * representation.
     *
     * @param presentationType
     *            the data type that this renderer displays, not
     *            <code>null</code>
     * @param nullRepresentation
     *            a string that will be sent to the client instead of a regular
     *            value in case the actual cell value is <code>null</code>. May
     *            be <code>null</code>.
     */
    protected AbstractRenderer(Class<V> presentationType,
            String nullRepresentation) {
        Objects.requireNonNull(presentationType,
                "Presentation type cannot be null");
        this.presentationType = presentationType;
        this.nullRepresentation = nullRepresentation;
    }

    /**
     * Creates a new renderer with the given presentation type. No null
     * representation will be used.
     *
     * @param presentationType
     *            the data type that this renderer displays, not
     *            <code>null</code>
     */
    protected AbstractRenderer(Class<V> presentationType) {
        this(presentationType, null);
    }

    /**
     * This method is inherited from AbstractExtension but should never be
     * called directly with an AbstractRenderer.
     */
    @Deprecated
    @Override
    @SuppressWarnings("rawtypes")
    protected Class<Column> getSupportedParentType() {
        return Column.class;
    }

    /**
     * This method is inherited from AbstractExtension but should never be
     * called directly with an AbstractRenderer.
     */
    @Deprecated
    @Override
    protected void extend(AbstractClientConnector target) {
        super.extend(target);
    }

    @Override
    public Class<V> getPresentationType() {
        return presentationType;
    }

    @Override
    public JsonValue encode(V value) {
        if (value == null) {
            return encode(getNullRepresentation(), String.class);
        } else {
            return encode(value, getPresentationType());
        }
    }

    /**
     * Null representation for the renderer
     *
     * @return a textual representation of {@code null}
     */
    protected String getNullRepresentation() {
        return nullRepresentation;
    }

    /**
     * Encodes the given value to JSON.
     * <p>
     * This is a helper method that can be invoked by an {@link #encode(Object)
     * encode(T)} override if serializing a value of type other than
     * {@link #getPresentationType() the presentation type} is desired. For
     * instance, a {@code Renderer<Date>} could first turn a date value into a
     * formatted string and return {@code encode(dateString, String.class)}.
     *
     * @param value
     *            the value to be encoded
     * @param type
     *            the type of the value
     * @return a JSON representation of the given value
     */
    protected <U> JsonValue encode(U value, Class<U> type) {
        return JsonCodec
                .encode(value, null, type, getUI().getConnectorTracker())
                .getEncodedValue();
    }

    /**
     * Gets the {@link Grid} this renderer is attached to. Used internally for
     * indicating the source grid of possible events emitted by this renderer,
     * such as {@link RendererClickEvent}s.
     *
     * @return the grid this renderer is attached to or null if unattached
     */
    @SuppressWarnings("unchecked")
    protected Grid<T> getParentGrid() {
        if (super.getParent() == null) {
            return null;
        }
        return (Grid<T>) super.getParent().getParent();
    }
}