/
BoxedTablePanel.java
284 lines (228 loc) · 8.54 KB
/
BoxedTablePanel.java
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
/*
* Copyright (c) 2010-2017 Evolveum
*
* 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.evolveum.midpoint.web.component.data;
import java.util.List;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.ISortableDataProvider;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Fragment;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.AbstractReadOnlyModel;
import com.evolveum.midpoint.gui.api.component.BasePanel;
import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.prism.query.ObjectPaging;
import com.evolveum.midpoint.web.session.UserProfileStorage;
import org.apache.wicket.model.IModel;
/**
* @author Viliam Repan (lazyman)
*/
public class BoxedTablePanel<T> extends BasePanel<T> implements Table {
private static final long serialVersionUID = 1L;
private static final String ID_BOX = "box";
private static final String ID_HEADER = "header";
private static final String ID_FOOTER = "footer";
private static final String ID_TABLE = "table";
private static final String ID_TABLE_CONTAINER = "tableContainer";
private static final String ID_PAGING_FOOTER = "pagingFooter";
private static final String ID_PAGING = "paging";
private static final String ID_COUNT = "count";
private static final String ID_MENU = "menu";
private static final String ID_FOOTER_CONTAINER = "footerContainer";
private static final String ID_BUTTON_TOOLBAR = "buttonToolbar";
private UserProfileStorage.TableId tableId;
private boolean showPaging;
private String additionalBoxCssClasses = null;
public BoxedTablePanel(String id, ISortableDataProvider provider, List<IColumn<T, String>> columns) {
this(id, provider, columns, null, Integer.MAX_VALUE);
}
public BoxedTablePanel(String id, ISortableDataProvider provider, List<IColumn<T, String>> columns,
UserProfileStorage.TableId tableId) {
this(id, provider, columns, tableId, UserProfileStorage.DEFAULT_PAGING_SIZE);
}
public BoxedTablePanel(String id, ISortableDataProvider provider, List<IColumn<T, String>> columns,
UserProfileStorage.TableId tableId, int pageSize) {
super(id);
this.tableId = tableId;
initLayout(columns, provider, pageSize);
}
private void initLayout(List<IColumn<T, String>> columns, ISortableDataProvider provider, int pageSize) {
setOutputMarkupId(true);
WebMarkupContainer box = new WebMarkupContainer(ID_BOX);
box.add(new AttributeAppender("class", new AbstractReadOnlyModel<String>() {
private static final long serialVersionUID = 1L;
@Override
public String getObject() {
String boxCssClasses = getAdditionalBoxCssClasses();
if (boxCssClasses == null) {
return "";
} else {
return " " + boxCssClasses;
}
}
}));
add(box);
WebMarkupContainer tableContainer = new WebMarkupContainer(ID_TABLE_CONTAINER);
tableContainer.setOutputMarkupId(true);
DataTable<T, String> table = new SelectableDataTable<T>(ID_TABLE, columns, provider, pageSize) {
@Override
protected Item<T> newRowItem(String id, int index, IModel<T> rowModel) {
Item<T> item = super.newRowItem(id, index, rowModel);
return customizeNewRowItem(item, rowModel);
}
};
table.setOutputMarkupId(true);
tableContainer.add(table);
box.add(tableContainer);
TableHeadersToolbar headersTop = new TableHeadersToolbar(table, provider);
headersTop.setOutputMarkupId(true);
table.addTopToolbar(headersTop);
box.add(createHeader(ID_HEADER));
box.add(createFooter(ID_FOOTER));
}
public String getAdditionalBoxCssClasses() {
return additionalBoxCssClasses;
}
public void setAdditionalBoxCssClasses(String boxCssClasses) {
this.additionalBoxCssClasses = boxCssClasses;
}
// TODO better name?
protected Item<T> customizeNewRowItem(Item<T> item, IModel<T> model) {
return item;
}
@Override
public DataTable getDataTable() {
return (DataTable) get(ID_BOX).get(ID_TABLE_CONTAINER).get(ID_TABLE);
}
public WebMarkupContainer getDataTableContainer() {
return (WebMarkupContainer) get(ID_BOX).get(ID_TABLE_CONTAINER);
}
@Override
public UserProfileStorage.TableId getTableId() {
return tableId;
}
@Override
public void setItemsPerPage(int size) {
getDataTable().setItemsPerPage(size);
}
@Override
public int getItemsPerPage() {
return (int) getDataTable().getItemsPerPage();
}
@Override
public void setShowPaging(boolean show) {
// todo make use of this [lazyman]
this.showPaging = show;
if (!show) {
setItemsPerPage(Integer.MAX_VALUE);
} else {
setItemsPerPage(10);
}
}
public WebMarkupContainer getHeader() {
return (WebMarkupContainer) get(ID_BOX).get(ID_HEADER);
}
public WebMarkupContainer getFooter() {
return (WebMarkupContainer) get(ID_BOX).get(ID_FOOTER);
}
protected WebMarkupContainer createHeader(String headerId) {
WebMarkupContainer header = new WebMarkupContainer(headerId);
header.setVisible(false);
return header;
}
protected WebMarkupContainer createFooter(String footerId) {
return new PagingFooter(footerId, ID_PAGING_FOOTER, this, this);
}
public Component getFooterMenu() {
return ((PagingFooter) getFooter()).getFooterMenu();
}
public Component getFooterCountLabel() {
return ((PagingFooter) getFooter()).getFooterCountLabel();
}
public Component getFooterPaging() {
return ((PagingFooter) getFooter()).getFooterPaging();
}
@Override
public void setCurrentPage(ObjectPaging paging) {
WebComponentUtil.setCurrentPage(this, paging);
}
@Override
public void setCurrentPage(long page) {
getDataTable().setCurrentPage(page);
}
protected WebMarkupContainer createButtonToolbar(String id) {
return new WebMarkupContainer(id);
}
private static class PagingFooter extends Fragment {
public PagingFooter(String id, String markupId, BoxedTablePanel markupProvider, Table table) {
super(id, markupId, markupProvider);
setOutputMarkupId(true);
initLayout(markupProvider, table);
}
private void initLayout(final BoxedTablePanel boxedTablePanel, final Table table) {
WebMarkupContainer buttonToolbar = boxedTablePanel.createButtonToolbar(ID_BUTTON_TOOLBAR);
add(buttonToolbar);
final DataTable dataTable = table.getDataTable();
WebMarkupContainer footerContainer = new WebMarkupContainer(ID_FOOTER_CONTAINER);
footerContainer.setOutputMarkupId(true);
final Label count = new Label(ID_COUNT, new AbstractReadOnlyModel<String>() {
@Override
public String getObject() {
return CountToolbar.createCountString(PagingFooter.this, dataTable);
}
});
count.setOutputMarkupId(true);
footerContainer.add(count);
BoxedPagingPanel nb2 = new BoxedPagingPanel(ID_PAGING, dataTable, true) {
@Override
protected void onPageChanged(AjaxRequestTarget target, long page) {
target.add(count);
}
};
footerContainer.add(nb2);
TableConfigurationPanel menu = new TableConfigurationPanel(ID_MENU) {
@Override
protected void pageSizeChanged(AjaxRequestTarget target) {
Table table = findParent(Table.class);
UserProfileStorage.TableId tableId = table.getTableId();
if (tableId != null) {
PageBase page = (PageBase) getPage();
Integer pageSize = page.getSessionStorage().getUserProfile().getPagingSize(tableId);
table.setItemsPerPage(pageSize);
}
target.add(findParent(PagingFooter.class));
target.add((Component) table);
}
};
footerContainer.add(menu);
add(footerContainer);
}
public Component getFooterMenu() {
return get(ID_FOOTER_CONTAINER).get(ID_MENU);
}
public Component getFooterCountLabel() {
return get(ID_FOOTER_CONTAINER).get(ID_COUNT);
}
public Component getFooterPaging() {
return get(ID_FOOTER_CONTAINER).get(ID_PAGING);
}
}
}