aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/com/vaadin/ui/components/grid/GridColumn.java
blob: 76b17bda41808b762ce9eaa445c1670d0dd252c6 (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
/*
 * Copyright 2000-2014 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.components.grid;

import java.io.Serializable;

import com.vaadin.shared.ui.grid.GridColumnState;

/**
 * A column in the grid. Can be obtained by calling
 * {@link Grid#getColumn(Object propertyId)}.
 * 
 * @since 7.4
 * @author Vaadin Ltd
 */
public class GridColumn implements Serializable {

    /**
     * The state of the column shared to the client
     */
    private final GridColumnState state;

    /**
     * The grid this column is associated with
     */
    private final Grid grid;

    /**
     * Internally used constructor.
     * 
     * @param grid
     *            The grid this column belongs to. Should not be null.
     * @param state
     *            the shared state of this column
     */
    GridColumn(Grid grid, GridColumnState state) {
        this.grid = grid;
        this.state = state;
    }

    /**
     * Returns the serializable state of this column that is sent to the client
     * side connector.
     * 
     * @return the internal state of the column
     */
    GridColumnState getState() {
        return state;
    }

    /**
     * Returns the caption of the header. By default the header caption is the
     * property id of the column.
     * 
     * @return the text in the header
     * 
     * @throws IllegalStateException
     *             if the column no longer is attached to the grid
     */
    public String getHeaderCaption() throws IllegalStateException {
        checkColumnIsAttached();
        return state.header;
    }

    /**
     * Sets the caption of the header.
     * 
     * @param caption
     *            the text to show in the caption
     * 
     * @throws IllegalStateException
     *             if the column is no longer attached to any grid
     */
    public void setHeaderCaption(String caption) throws IllegalStateException {
        checkColumnIsAttached();
        state.header = caption;
        grid.markAsDirty();
    }

    /**
     * Returns the caption of the footer. By default the captions are
     * <code>null</code>.
     * 
     * @return the text in the footer
     * @throws IllegalStateException
     *             if the column is no longer attached to any grid
     */
    public String getFooterCaption() throws IllegalStateException {
        checkColumnIsAttached();
        return state.footer;
    }

    /**
     * Sets the caption of the footer.
     * 
     * @param caption
     *            the text to show in the caption
     * 
     * @throws IllegalStateException
     *             if the column is no longer attached to any grid
     */
    public void setFooterCaption(String caption) throws IllegalStateException {
        checkColumnIsAttached();
        state.footer = caption;
        grid.markAsDirty();
    }

    /**
     * Returns the width (in pixels). By default a column is 100px wide.
     * 
     * @return the width in pixels of the column
     * @throws IllegalStateException
     *             if the column is no longer attached to any grid
     */
    public int getWidth() throws IllegalStateException {
        checkColumnIsAttached();
        return state.width;
    }

    /**
     * Sets the width (in pixels).
     * 
     * @param pixelWidth
     *            the new pixel width of the column
     * @throws IllegalStateException
     *             if the column is no longer attached to any grid
     * @throws IllegalArgumentException
     *             thrown if pixel width is less than zero
     */
    public void setWidth(int pixelWidth) throws IllegalStateException,
            IllegalArgumentException {
        checkColumnIsAttached();
        if (pixelWidth < 0) {
            throw new IllegalArgumentException(
                    "Pixel width should be greated than 0");
        }
        state.width = pixelWidth;
        grid.markAsDirty();
    }

    /**
     * Marks the column width as undefined meaning that the grid is free to
     * resize the column based on the cell contents and available space in the
     * grid.
     */
    public void setWidthUndefined() {
        checkColumnIsAttached();
        state.width = -1;
        grid.markAsDirty();
    }

    /**
     * Is this column visible in the grid. By default all columns are visible.
     * 
     * @return <code>true</code> if the column is visible
     * @throws IllegalStateException
     *             if the column is no longer attached to any grid
     */
    public boolean isVisible() throws IllegalStateException {
        checkColumnIsAttached();
        return state.visible;
    }

    /**
     * Set the visibility of this column
     * 
     * @param visible
     *            is the column visible
     * @throws IllegalStateException
     *             if the column is no longer attached to any grid
     */
    public void setVisible(boolean visible) throws IllegalStateException {
        checkColumnIsAttached();
        state.visible = visible;
        grid.markAsDirty();
    }

    /**
     * Checks if column is attached and throws an {@link IllegalStateException}
     * if it is not
     * 
     * @throws IllegalStateException
     *             if the column is no longer attached to any grid
     */
    protected void checkColumnIsAttached() throws IllegalStateException {
        if (grid.getColumnByColumnId(state.id) == null) {
            throw new IllegalStateException("Column no longer exists.");
        }
    }

    /**
     * Sets this column as the last frozen column in its grid.
     * 
     * @throws IllegalArgumentException
     *             if the column is no longer attached to any grid
     * @see Grid#setLastFrozenColumn(GridColumn)
     */
    public void setLastFrozenColumn() {
        checkColumnIsAttached();
        grid.setLastFrozenColumn(this);
    }
}