-
Notifications
You must be signed in to change notification settings - Fork 188
/
TablePanel.java
198 lines (156 loc) · 6.49 KB
/
TablePanel.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
/*
* Copyright (c) 2010-2013 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 com.evolveum.midpoint.prism.query.ObjectPaging;
import com.evolveum.midpoint.web.component.data.paging.NavigatorPanel;
import com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour;
import com.evolveum.midpoint.web.page.PageBase;
import com.evolveum.midpoint.web.session.UserProfileStorage;
import org.apache.commons.lang.Validate;
import org.apache.wicket.AttributeModifier;
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.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import java.util.List;
/**
* @author lazyman
*/
public class TablePanel<T> extends Panel {
private static final String ID_TABLE = "table";
private static final String ID_PAGING = "paging";
private IModel<Boolean> showPaging = new Model<Boolean>(true);
private IModel<Boolean> showCount = new Model<Boolean>(true);
private IModel<Boolean> showPagingSize = new Model<Boolean>(false);
private UserProfileStorage.TableId tableId;
public TablePanel(String id, ISortableDataProvider provider, List<IColumn<T, String>> columns) {
this(id, provider, columns, null);
}
public TablePanel(String id, ISortableDataProvider provider, List<IColumn<T, String>> columns,
UserProfileStorage.TableId tableId) {
super(id);
Validate.notNull(provider, "Object type must not be null.");
Validate.notNull(columns, "Columns must not be null.");
add(AttributeModifier.prepend("style", "display: table; width: 100%;"));
this.tableId = tableId;
initLayout(columns, provider);
}
@Override
protected void onInitialize() {
super.onInitialize();
DataTable table = (DataTable) get(ID_TABLE);
PageBase page = (PageBase) getPage();
UserProfileStorage userProfile = page.getSessionStorage().getUserProfile();
table.setItemsPerPage(userProfile.getPagingSize(tableId));
}
private void initLayout(List<IColumn<T, String>> columns, ISortableDataProvider provider) {
DataTable<T, String> table = new SelectableDataTable<T>(ID_TABLE, columns, provider,
UserProfileStorage.DEFAULT_PAGING_SIZE);
table.setOutputMarkupId(true);
TableHeadersToolbar headers = new TableHeadersToolbar(table, provider);
headers.setOutputMarkupId(true);
table.addTopToolbar(headers);
CountToolbar count = new CountToolbar(table) {
@Override
protected void pageSizeChanged(AjaxRequestTarget target) {
PageBase page = (PageBase) getPage();
Integer pageSize = page.getSessionStorage().getUserProfile().getPagingSize(tableId);
setItemsPerPage(pageSize);
target.add(getNavigatorPanel());
target.add(getDataTable());
}
@Override
protected boolean isPageSizePopupVisible() {
return showPagingSize.getObject();
}
};
addVisibleBehaviour(count, showCount);
table.addBottomToolbar(count);
add(table);
NavigatorPanel nb2 = new NavigatorPanel(ID_PAGING, table, showPagedPaging(provider));
addVisibleBehaviour(nb2, showPaging);
add(nb2);
}
public UserProfileStorage.TableId getTableId() {
return tableId;
}
private void addVisibleBehaviour(Component comp, final IModel<Boolean> model) {
comp.add(new VisibleEnableBehaviour() {
@Override
public boolean isVisible() {
return model.getObject();
}
});
}
private boolean showPagedPaging(ISortableDataProvider provider) {
if (!(provider instanceof BaseSortableDataProvider)) {
return true;
}
BaseSortableDataProvider baseProvider = (BaseSortableDataProvider) provider;
return baseProvider.isSizeAvailable();
}
public DataTable getDataTable() {
return (DataTable) get(ID_TABLE);
}
public NavigatorPanel getNavigatorPanel() {
return (NavigatorPanel) get(ID_PAGING);
}
public void setItemsPerPage(int size) {
getDataTable().setItemsPerPage(size);
}
public void setCurrentPage(ObjectPaging paging) {
if (paging == null) {
getDataTable().setCurrentPage(0);
return;
}
long itemsPerPage = getDataTable().getItemsPerPage();
long page = ((paging.getOffset() + itemsPerPage) / itemsPerPage) - 1;
if (page < 0) {
page = 0;
}
getDataTable().setCurrentPage(page);
}
public void setShowPagingSize(boolean show) {
this.showPagingSize.setObject(show);
}
public void setShowPaging(boolean showPaging) {
this.showPaging.setObject(showPaging);
this.showCount.setObject(showPaging);
if (!showPaging) {
setItemsPerPage(Integer.MAX_VALUE);
} else {
setItemsPerPage(10);
}
}
public void setShowCount(boolean showCount) {
this.showCount.setObject(showCount);
}
public void setTableCssClass(String cssClass) {
Validate.notEmpty(cssClass, "Css class must not be null or empty.");
DataTable table = getDataTable();
table.add(new AttributeAppender("class", new Model(cssClass), " "));
}
public void setStyle(String value) {
Validate.notEmpty(value, "Value must not be null or empty.");
DataTable table = getDataTable();
table.add(new AttributeModifier("style", new Model(value)));
}
}