-
Notifications
You must be signed in to change notification settings - Fork 188
/
CountToolbar.java
113 lines (93 loc) · 3.8 KB
/
CountToolbar.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
package com.evolveum.midpoint.web.component.data;
import com.evolveum.midpoint.web.component.util.LoadableModel;
import com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractToolbar;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.navigation.paging.IPageable;
import org.apache.wicket.markup.repeater.data.DataViewBase;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.StringResourceModel;
/**
* @author lazyman
*/
public class CountToolbar extends AbstractToolbar {
private static final String ID_TD = "td";
private static final String ID_COUNT = "count";
private static final String ID_PAGE_SIZE = "pageSize";
public CountToolbar(DataTable<?, ?> table) {
super(table);
}
@Override
protected void onInitialize() {
super.onInitialize();
WebMarkupContainer td = new WebMarkupContainer(ID_TD);
td.add(AttributeModifier.replace("colspan", new AbstractReadOnlyModel<String>() {
@Override
public String getObject() {
return String.valueOf(getTable().getColumns().size());
}
}));
add(td);
Label count = new Label(ID_COUNT, createModel());
count.setRenderBodyOnly(true);
td.add(count);
PageSizePopover popover = new PageSizePopover(ID_PAGE_SIZE) {
@Override
protected void pageSizeChanged(AjaxRequestTarget target) {
CountToolbar.this.pageSizeChanged(target);
}
};
popover.add(new VisibleEnableBehaviour() {
@Override
public boolean isVisible() {
return CountToolbar.this.isPageSizePopupVisible();
}
});
td.add(popover);
}
private IModel<String> createModel() {
return new LoadableModel<String>() {
@Override
protected String load() {
long from = 0;
long to = 0;
long count = 0;
IPageable pageable = getTable();
if (pageable instanceof DataViewBase) {
DataViewBase view = (DataViewBase) pageable;
from = view.getFirstItemOffset() + 1;
to = from + view.getItemsPerPage() - 1;
long itemCount = view.getItemCount();
if (to > itemCount) {
to = itemCount;
}
count = itemCount;
} else if (pageable instanceof DataTable) {
DataTable table = (DataTable) pageable;
from = table.getCurrentPage() * table.getItemsPerPage() + 1;
to = from + table.getItemsPerPage() - 1;
long itemCount = table.getItemCount();
if (to > itemCount) {
to = itemCount;
}
count = itemCount;
}
if (count > 0) {
return new StringResourceModel("CountToolbar.label", CountToolbar.this, null,
new Object[]{from, to, count}).getString();
}
return new StringResourceModel("CountToolbar.noFound", CountToolbar.this, null).getString();
}
};
}
protected void pageSizeChanged(AjaxRequestTarget target) {
}
protected boolean isPageSizePopupVisible() {
return true;
}
}