-
Notifications
You must be signed in to change notification settings - Fork 188
/
MultiButtonTable.java
249 lines (211 loc) · 9.82 KB
/
MultiButtonTable.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
/*
* Copyright (c) 2016 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.gui.api.component.BasePanel;
import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.web.component.AjaxButton;
import com.evolveum.midpoint.web.component.AjaxIconButton;
import com.evolveum.midpoint.web.component.assignment.*;
import com.evolveum.midpoint.web.page.self.PageAssignmentDetails;
import com.evolveum.midpoint.web.session.UsersStorage;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxEventBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.navigation.paging.IPageableItems;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import java.util.ArrayList;
import java.util.List;
/**
* Created honchar.
*/
public class MultiButtonTable extends BasePanel<List<AssignmentEditorDto>> {
private static final long serialVersionUID = 1L;
private static final String ID_ROW = "row";
private static final String ID_CELL = "cell";
private static final String ID_ITEM_BUTTON_CONTAINER = "itemButtonContainer";
private static final String ID_INNER = "inner";
private static final String ID_INNER_LABEL = "innerLabel";
private static final String ID_TYPE_ICON = "typeIcon";
private static final String ID_ADD_TO_CART_LINK = "addToCartLink";
private static final String ID_ADD_TO_CART_LINK_LABEL = "addToCartLinkLabel";
private static final String ID_ADD_TO_CART_LINK_ICON = "addToCartLinkIcon";
private static final String ID_DETAILS_LINK = "detailsLink";
private static final String ID_DETAILS_LINK_LABEL = "detailsLinkLabel";
private static final String ID_DETAILS_LINK_ICON = "detailsLinkIcon";
private String addToCartLinkIcon = "fa fa-times-circle fa-lg text-danger";
private String detailsLinkIcon = "fa fa-arrow-circle-right";
private long itemsCount = 0;
private long itemsPerRow = 0;
private PageBase pageBase;
private boolean plusIconClicked = false;
public MultiButtonTable (String id){
super(id);
}
public MultiButtonTable (String id, long itemsPerRow, IModel<List<AssignmentEditorDto>> model, PageBase pageBase){
super(id, model);
this.itemsPerRow = itemsPerRow;
this.pageBase = pageBase;
initLayout();
}
private void initLayout(){
itemsCount = getModel() != null ? (getModel().getObject() != null ? getModel().getObject().size() : 0) : 0;
RepeatingView rows = new RepeatingView(ID_ROW);
rows.setOutputMarkupId(true);
if (itemsCount > 0 && itemsPerRow > 0){
int index = 0;
List<AssignmentEditorDto> assignmentsList = getModelObject();
long rowCount = itemsCount % itemsPerRow == 0 ? (itemsCount / itemsPerRow) : (itemsCount / itemsPerRow + 1);
for (int rowNumber = 0; rowNumber < rowCount; rowNumber++){
WebMarkupContainer rowContainer = new WebMarkupContainer(rows.newChildId());
rows.add(rowContainer);
RepeatingView columns = new RepeatingView(ID_CELL);
columns.setOutputMarkupId(true);
rowContainer.add(columns);
for (int colNumber = 0; colNumber < itemsPerRow; colNumber++){
WebMarkupContainer colContainer = new WebMarkupContainer(columns.newChildId());
columns.add(colContainer);
WebMarkupContainer itemButtonContainer = new WebMarkupContainer(ID_ITEM_BUTTON_CONTAINER);
itemButtonContainer.setOutputMarkupId(true);
itemButtonContainer.add(new AttributeAppender("class", getBackgroundClass(assignmentsList.get(index).getType())));
colContainer.add(itemButtonContainer);
populateCell(itemButtonContainer, assignmentsList.get(index));
index++;
if (index >= assignmentsList.size()){
break;
}
}
}
}
add(rows);
}
protected void populateCell(WebMarkupContainer cellContainer, final AssignmentEditorDto assignment){
AjaxLink inner = new AjaxLink(ID_INNER) {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget ajaxRequestTarget) {
assignmentDetailsPerformed(assignment, ajaxRequestTarget);
}
};
cellContainer.add(inner);
Label nameLabel = new Label(ID_INNER_LABEL, assignment.getName());
inner.add(nameLabel);
AjaxLink detailsLink = new AjaxLink(ID_DETAILS_LINK) {
@Override
public void onClick(AjaxRequestTarget ajaxRequestTarget) {
assignmentDetailsPerformed(assignment, ajaxRequestTarget);
}
};
cellContainer.add(detailsLink);
Label detailsLinkLabel = new Label(ID_DETAILS_LINK_LABEL, pageBase.createStringResource("MultiButtonPanel.detailsLink"));
detailsLinkLabel.setRenderBodyOnly(true);
detailsLink.add(detailsLinkLabel);
AjaxLink detailsLinkIcon = new AjaxLink(ID_DETAILS_LINK_ICON) {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
}
};
detailsLink.add(detailsLinkIcon);
AjaxLink addToCartLink = new AjaxLink(ID_ADD_TO_CART_LINK) {
@Override
public void onClick(AjaxRequestTarget ajaxRequestTarget) {
addAssignmentPerformed(assignment, ajaxRequestTarget);
}
};
cellContainer.add(addToCartLink);
// Label addToCartLinkLabel = new Label(ID_ADD_TO_CART_LINK_LABEL, pageBase.createStringResource("MultiButtonPanel.addToCartLink"));
// addToCartLinkLabel.setRenderBodyOnly(true);
// addToCartLinkLabel.add(new AjaxEventBehavior("click") {
// @Override
// protected void onEvent(AjaxRequestTarget ajaxRequestTarget) {
// addAssignmentPerformed(assignment, ajaxRequestTarget);
// }
// });
// addToCartLink.add(addToCartLinkLabel);
AjaxLink addToCartLinkIcon = new AjaxLink(ID_ADD_TO_CART_LINK_ICON) {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
}
};
addToCartLink.add(addToCartLinkIcon);
WebMarkupContainer icon = new WebMarkupContainer(ID_TYPE_ICON);
icon.add(new AttributeAppender("class", getIconClass(assignment.getType())));
cellContainer.add(icon);
}
private void assignmentDetailsPerformed(final AssignmentEditorDto assignment, AjaxRequestTarget target){
if (!plusIconClicked) {
IModel<AssignmentEditorDto> assignmentModel = new IModel<AssignmentEditorDto>() {
@Override
public AssignmentEditorDto getObject() {
assignment.setMinimized(false);
assignment.setSimpleView(true);
return assignment;
}
@Override
public void setObject(AssignmentEditorDto assignmentEditorDto) {
}
@Override
public void detach() {
}
};
setResponsePage(new PageAssignmentDetails(assignmentModel));
} else {
plusIconClicked = false;
}
}
private String getIconClass(AssignmentEditorDtoType type){
// TODO: switch to icon constants
if (AssignmentEditorDtoType.ROLE.equals(type)){
return "fa fa-street-view";
}else if (AssignmentEditorDtoType.SERVICE.equals(type)){
return "fa fa-cloud";
}else if (AssignmentEditorDtoType.ORG_UNIT.equals(type)){
return "fa fa-building";
} else {
return "";
}
}
private String getBackgroundClass(AssignmentEditorDtoType type){
if (AssignmentEditorDtoType.ROLE.equals(type)){
return "object-role-bg";
}else if (AssignmentEditorDtoType.SERVICE.equals(type)){
return "object-service-bg";
}else if (AssignmentEditorDtoType.ORG_UNIT.equals(type)){
return "object-org-bg";
} else {
return "";
}
}
private void addAssignmentPerformed(AssignmentEditorDto assignment, AjaxRequestTarget target){
plusIconClicked = true;
UsersStorage storage = getPageBase().getSessionStorage().getUsers();
if (storage.getAssignmentShoppingCart() == null){
storage.setAssignmentShoppingCart(new ArrayList<AssignmentEditorDto>());
}
List<AssignmentEditorDto> assignmentsToAdd = storage.getAssignmentShoppingCart();
assignmentsToAdd.add(assignment);
storage.setAssignmentShoppingCart(assignmentsToAdd);
CatalogItemsPanel parent = MultiButtonTable.this.findParent(CatalogItemsPanel.class);
parent.reloadCartButton(target);
}
}