/
ItemPanel.java
317 lines (244 loc) · 10.8 KB
/
ItemPanel.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* Copyright (c) 2010-2018 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.gui.impl.prism;
import java.util.List;
import com.evolveum.midpoint.prism.PrismValue;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import com.evolveum.midpoint.gui.api.component.BasePanel;
import com.evolveum.midpoint.gui.api.factory.GuiComponentFactory;
import com.evolveum.midpoint.gui.api.prism.ItemWrapper;
import com.evolveum.midpoint.gui.api.util.WebPrismUtil;
import com.evolveum.midpoint.gui.impl.component.data.column.AbstractItemWrapperColumnPanel;
import com.evolveum.midpoint.prism.PrismPropertyValue;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.component.prism.ValueStatus;
import com.evolveum.midpoint.web.component.util.VisibleBehaviour;
/**
* @author katka
*
*/
public abstract class ItemPanel<VW extends PrismValueWrapper, IW extends ItemWrapper> extends BasePanel<IW>{
private static final long serialVersionUID = 1L;
private static final Trace LOGGER = TraceManager.getTrace(ItemPanel.class);
private static final String DOT_CLASS = ItemPanel.class.getName() + ".";
private static final String OPERATION_CREATE_NEW_VALUE = DOT_CLASS + "createNewValue";
private static final String ID_HEADER = "header";
private static final String ID_VALUES = "values";
private static final String ID_ADD_BUTTON = "addButton";
private static final String ID_REMOVE_BUTTON = "removeButton";
private static final String ID_BUTTON_CONTAINER = "buttonContainer";
protected ItemPanelSettings itemPanelSettings;
public ItemPanel(String id, IModel<IW> model, ItemPanelSettings itemPanelSettings) {
super(id, model);
this.itemPanelSettings = itemPanelSettings;
}
@Override
protected void onInitialize() {
super.onInitialize();
initLayout();
setOutputMarkupId(true);
}
private void initLayout() {
if (getModelObject() != null) {
getModelObject().revive(getPrismContext());
}
Panel headerPanel = createHeaderPanel();
headerPanel.add(new VisibleBehaviour(() -> getHeaderVisibility()));
add(headerPanel);
ListView<VW> valuesPanel = createValuesPanel();
add(valuesPanel);
}
protected boolean getHeaderVisibility() {
if (!isHeaderVisible()) {
return false;
}
return getParent().findParent(AbstractItemWrapperColumnPanel.class) == null;
}
protected abstract Panel createHeaderPanel();
protected ListView<VW> createValuesPanel() {
ListView<VW> values = new ListView<VW>(ID_VALUES, new PropertyModel<>(getModel(), "values")) {
private static final long serialVersionUID = 1L;
@Override
protected void populateItem(ListItem<VW> item) {
GuiComponentFactory componentFactory = getPageBase().getRegistry()
.findValuePanelFactory(ItemPanel.this.getModelObject());
Component panel = createValuePanel(item, componentFactory, getVisibilityHandler(), getEditabilityHandler());
// panel.add(getEnableBehaviourOfValuePanel(ItemPanel.this.getModelObject()));
createButtons(item);
}
};
// values.setReuseItems(true);
// customValuesPanel(values);
return values;
}
@Override
public boolean isEnabled() {
return !getModelObject().isReadOnly();
}
// VALUE REGION
protected abstract Component createValuePanel(ListItem<VW> item, GuiComponentFactory componentFactory,
ItemVisibilityHandler visibilityHandler, ItemEditabilityHandler editabilityHandler);
protected void createButtons(ListItem<VW> item) {
WebMarkupContainer buttonContainer = new WebMarkupContainer(ID_BUTTON_CONTAINER);
buttonContainer.add(new AttributeModifier("class", getButtonsCssClass()));
item.add(buttonContainer);
// buttons
AjaxLink<Void> addButton = new AjaxLink<Void>(ID_ADD_BUTTON) {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
addValue(target);
}
};
addButton.add(new VisibleBehaviour(() -> isAddButtonVisible()));
buttonContainer.add(addButton);
AjaxLink<Void> removeButton = new AjaxLink<Void>(ID_REMOVE_BUTTON) {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
try {
removeValue(item.getModelObject(), target);
} catch (SchemaException e) {
LOGGER.error("Cannot remove value: {}", item.getModelObject());
getSession().error("Cannot remove value "+ item.getModelObject());
target.add(getPageBase().getFeedbackPanel());
target.add(ItemPanel.this);
}
}
};
removeButton.add(new VisibleBehaviour(() -> isRemoveButtonVisible()));
buttonContainer.add(removeButton);
item.add(AttributeModifier.append("class", createStyleClassModel(item.getModel())));
item.add(new VisibleBehaviour(() -> isVisibleValue(item.getModel())));
}
protected String getButtonsCssClass() {
return"col-xs-2";
}
protected IModel<String> createStyleClassModel(final IModel<VW> value) {
return new IModel<String>() {
private static final long serialVersionUID = 1L;
@Override
public String getObject() {
if (getIndexOfValue(value.getObject()) > 0) {
return getItemCssClass();
}
return null;
}
};
}
private int getIndexOfValue(VW value) {
IW property = (IW) value.getParent();
List<VW> values = property.getValues();
for (int i = 0; i < values.size(); i++) {
if (values.get(i).equals(value)) {
return i;
}
}
return -1;
}
protected String getItemCssClass() {
return " col-md-offset-2 prism-value ";
}
protected void addValue(AjaxRequestTarget target) {
IW propertyWrapper = getModel().getObject();
PrismPropertyValue<?> newValue = getPrismContext().itemFactory().createPropertyValue();
WebPrismUtil.createNewValueWrapper(propertyWrapper, createNewValue(propertyWrapper), getPageBase(), target);
target.add(ItemPanel.this);
}
protected abstract <PV extends PrismValue> PV createNewValue(IW itemWrapper);
protected void removeValue(VW valueToRemove, AjaxRequestTarget target) throws SchemaException {
LOGGER.debug("Removing value of {}", valueToRemove);
List<VW> values = getModelObject().getValues();
switch (valueToRemove.getStatus()) {
case ADDED:
values.remove(valueToRemove);
getModelObject().getItem().remove(valueToRemove.getOldValue());
getModelObject().getItem().remove(valueToRemove.getNewValue());
break;
case DELETED:
valueToRemove.setStatus(ValueStatus.NOT_CHANGED);
break;
case NOT_CHANGED:
valueToRemove.setStatus(ValueStatus.DELETED);
break;
}
int count = countUsableValues(values);
if (count == 0 && !hasEmptyPlaceholder(values)) {
addValue(target);
}
target.add(ItemPanel.this);
}
private int countUsableValues(List<VW> values) {
int count = 0;
for (VW value : values) {
// if (ValueStatus.DELETED.equals(value.getStatus())) {
// continue;
// }
if (ValueStatus.ADDED.equals(value.getStatus())) {
continue;
}
count++;
}
return count;
}
private boolean hasEmptyPlaceholder(List<VW> values) {
for (VW value : values) {
if (ValueStatus.ADDED.equals(value.getStatus()) ) {//&& !value.hasValueChanged()) {
return true;
}
}
return false;
}
private boolean isAddButtonVisible() {
return getModelObject().isMultiValue();
}
protected boolean isRemoveButtonVisible() {
return !getModelObject().isReadOnly();
}
private boolean isVisibleValue(IModel<VW> model) {
VW value = model.getObject();
return !ValueStatus.DELETED.equals(value.getStatus());
}
public ItemVisibilityHandler getVisibilityHandler() {
if (itemPanelSettings == null) {
return null;
}
return itemPanelSettings.getVisibilityHandler();
}
public ItemEditabilityHandler getEditabilityHandler() {
if (itemPanelSettings == null) {
return null;
}
return itemPanelSettings.getEditabilityHandler();
}
protected boolean isShowOnTopLevel() {
if (itemPanelSettings == null) {
return false;
}
return itemPanelSettings.isShowOnTopLevel();
}
protected boolean isHeaderVisible() {
if (itemPanelSettings == null) {
return true;
}
return itemPanelSettings.isHeaderVisible();
}
public ItemPanelSettings getSettings() {
return itemPanelSettings;
}
}