aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/com/vaadin/ui/components/grid/ColumnGroupRow.java
blob: 5e1ba1100ff733284d50e61978fd1ebb3ad0470d (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
/*
 * 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 java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.vaadin.server.KeyMapper;
import com.vaadin.shared.ui.grid.ColumnGroupRowState;
import com.vaadin.shared.ui.grid.ColumnGroupState;

/**
 * A column group row represents an auxiliary header or footer row added to the
 * grid. A column group row includes column groups that group columns together.
 * 
 * @since 7.4
 * @author Vaadin Ltd
 */
public class ColumnGroupRow implements Serializable {

    /**
     * The common state shared between the client and server
     */
    private final ColumnGroupRowState state;

    /**
     * The column groups in this row
     */
    private List<ColumnGroup> groups = new ArrayList<ColumnGroup>();

    /**
     * Grid that the group row belongs to
     */
    private final Grid grid;

    /**
     * The column keys used to identify the column on the client side
     */
    private final KeyMapper<Object> columnKeys;

    /**
     * Constructs a new column group
     * 
     * @param grid
     *            The grid that the column group is associated to
     * @param state
     *            The shared state which contains the data shared between server
     *            and client
     * @param columnKeys
     *            The column key mapper for converting property ids to client
     *            side column identifiers
     */
    ColumnGroupRow(Grid grid, ColumnGroupRowState state,
            KeyMapper<Object> columnKeys) {
        this.grid = grid;
        this.columnKeys = columnKeys;
        this.state = state;
    }

    /**
     * Gets the shared state for the column group row. Used internally to send
     * the group row to the client.
     * 
     * @return The current state of the row
     */
    ColumnGroupRowState getState() {
        return state;
    }

    /**
     * Add a new group to the row by using property ids for the columns.
     * 
     * @param propertyIds
     *            The property ids of the columns that should be included in the
     *            group. A column can only belong in group on a row at a time.
     * @return a column group representing the collection of columns added to
     *         the group
     */
    public ColumnGroup addGroup(Object... propertyIds)
            throws IllegalArgumentException {
        assert propertyIds != null : "propertyIds cannot be null.";

        for (Object propertyId : propertyIds) {
            if (hasColumnBeenGrouped(propertyId)) {
                throw new IllegalArgumentException("Column "
                        + String.valueOf(propertyId)
                        + " already belongs to another group.");
            }
        }

        validateNewGroupProperties(Arrays.asList(propertyIds));

        ColumnGroupState state = new ColumnGroupState();
        for (Object propertyId : propertyIds) {
            assert propertyId != null : "null items in columns array not supported.";
            state.columns.add(columnKeys.key(propertyId));
        }
        this.state.groups.add(state);

        ColumnGroup group = new ColumnGroup(grid, this, state,
                Arrays.asList(propertyIds));
        groups.add(group);

        grid.markAsDirty();
        return group;
    }

    private void validateNewGroupProperties(List<Object> propertyIds)
            throws IllegalArgumentException {

        /*
         * Validate parent grouping
         */
        int rowIndex = grid.getColumnGroupRows().indexOf(this);
        int parentRowIndex = rowIndex - 1;

        // Get the parent row of this row.
        ColumnGroupRow parentRow = null;
        if (parentRowIndex > -1) {
            parentRow = grid.getColumnGroupRows().get(parentRowIndex);
        }

        if (parentRow == null) {
            // A parentless row is always valid and is usually the first row
            // added to the grid
            return;
        }

        for (Object id : propertyIds) {
            if (parentRow.hasColumnBeenGrouped(id)) {
                /*
                 * If a property has been grouped in the parent row then all of
                 * the properties in the parent group also needs to be included
                 * in the child group for the groups to be valid
                 */
                ColumnGroup parentGroup = parentRow.getGroupForProperty(id);
                if (!propertyIds.containsAll(parentGroup.getColumns())) {
                    throw new IllegalArgumentException(
                            "Grouped properties overlaps previous grouping bounderies");
                }
            }
        }
    }

    /**
     * Add a new group to the row by using column instances.
     * 
     * @param columns
     *            the columns that should belong to the group
     * @return a column group representing the collection of columns added to
     *         the group
     */
    public ColumnGroup addGroup(GridColumn... columns)
            throws IllegalArgumentException {
        assert columns != null : "columns cannot be null";

        List<Object> propertyIds = new ArrayList<Object>();
        for (GridColumn column : columns) {
            assert column != null : "null items in columns array not supported.";

            String columnId = column.getState().id;
            Object propertyId = grid.getPropertyIdByColumnId(columnId);
            propertyIds.add(propertyId);
        }
        return addGroup(propertyIds.toArray());
    }

    /**
     * Add a new group to the row by using other already greated groups
     * 
     * @param groups
     *            the subgroups of the group
     * @return a column group representing the collection of columns added to
     *         the group
     * 
     */
    public ColumnGroup addGroup(ColumnGroup... groups)
            throws IllegalArgumentException {
        assert groups != null : "groups cannot be null";

        // Gather all groups columns into one list
        List<Object> propertyIds = new ArrayList<Object>();
        for (ColumnGroup group : groups) {
            propertyIds.addAll(group.getColumns());
        }

        validateNewGroupProperties(propertyIds);

        ColumnGroupState state = new ColumnGroupState();
        ColumnGroup group = new ColumnGroup(grid, this, state, propertyIds);
        this.groups.add(group);

        // Update state
        for (Object propertyId : group.getColumns()) {
            state.columns.add(columnKeys.key(propertyId));
        }
        this.state.groups.add(state);

        grid.markAsDirty();
        return group;
    }

    /**
     * Removes a group from the row. Does not remove the group from subgroups,
     * to remove it from the subgroup invoke removeGroup on the subgroup.
     * 
     * @param group
     *            the group to remove
     */
    public void removeGroup(ColumnGroup group) {
        int index = groups.indexOf(group);
        groups.remove(index);
        state.groups.remove(index);
        grid.markAsDirty();
    }

    /**
     * Get the groups in the row.
     * 
     * @return unmodifiable list of groups in this row
     */
    public List<ColumnGroup> getGroups() {
        return Collections.unmodifiableList(groups);
    }

    /**
     * Checks if a property id has been added to a group in this row.
     * 
     * @param propertyId
     *            the property id to check for
     * @return <code>true</code> if the column is included in a group
     */
    private boolean hasColumnBeenGrouped(Object propertyId) {
        return getGroupForProperty(propertyId) != null;
    }

    private ColumnGroup getGroupForProperty(Object propertyId) {
        for (ColumnGroup group : groups) {
            if (group.isColumnInGroup(propertyId)) {
                return group;
            }
        }
        return null;
    }

    /**
     * Is the header visible for the row.
     * 
     * @return <code>true</code> if header is visible
     */
    public boolean isHeaderVisible() {
        return state.headerVisible;
    }

    /**
     * Sets the header visible for the row.
     * 
     * @param visible
     *            should the header be shown
     */
    public void setHeaderVisible(boolean visible) {
        state.headerVisible = visible;
        grid.markAsDirty();
    }

    /**
     * Is the footer visible for the row.
     * 
     * @return <code>true</code> if footer is visible
     */
    public boolean isFooterVisible() {
        return state.footerVisible;
    }

    /**
     * Sets the footer visible for the row.
     * 
     * @param visible
     *            should the footer be shown
     */
    public void setFooterVisible(boolean visible) {
        state.footerVisible = visible;
        grid.markAsDirty();
    }

}