-
Notifications
You must be signed in to change notification settings - Fork 188
/
DropDownChoicePanel.java
119 lines (95 loc) · 3.91 KB
/
DropDownChoicePanel.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
/*
* Copyright (c) 2010-2013 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.web.component.input;
import java.util.List;
import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.midpoint.web.component.prism.InputPanel;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import javax.xml.namespace.QName;
/**
* @author lazyman
*/
public class DropDownChoicePanel<T> extends InputPanel {
private static final long serialVersionUID = 1L;
private static final String ID_INPUT = "input";
private boolean sortChoices = true;
public DropDownChoicePanel(String id, IModel<T> model, IModel<? extends List<? extends T>> choices) {
this(id, model, choices, false);
}
public DropDownChoicePanel(String id, IModel<T> model, IModel<? extends List<? extends T>> choices, boolean allowNull) {
this(id, model, choices, new ChoiceRenderer(), allowNull);
}
public DropDownChoicePanel(String id, IModel<T> model, IModel<? extends List<? extends T>> choices, IChoiceRenderer<T> renderer) {
this(id, model, choices, renderer, false);
}
public DropDownChoicePanel(String id, IModel<T> model, IModel<? extends List<? extends T>> choices, IChoiceRenderer<T> renderer,
boolean allowNull) {
super(id);
DropDownChoice<T> input = new DropDownChoice<T>(ID_INPUT, model,
choices, renderer) {
private static final long serialVersionUID = 1L;
@Override
protected CharSequence getDefaultChoice(String selectedValue) {
if (allowNull){
return super.getDefaultChoice(selectedValue);
} else {
return getString("DropDownChoicePanel.notDefined");
}
}
@Override
protected String getNullValidDisplayValue() {
return DropDownChoicePanel.this.getNullValidDisplayValue();
}
@Override
public IModel<? extends List<? extends T>> getChoicesModel() {
IModel<? extends List<? extends T>> choices = super.getChoicesModel();
if (sortChoices) {
return Model.ofList(WebComponentUtil.sortDropDownChoices(choices, renderer));
} else {
return choices;
}
}
@Override
public String getModelValue() {
T object = this.getModelObject();
if (object != null) {
if (QName.class.isAssignableFrom(object.getClass())) {
for (int i = 0; i < getChoices().size(); i++) {
if (QNameUtil.match((QName) getChoices().get(i), (QName) object)) {
return this.getChoiceRenderer().getIdValue(object, i);
}
}
}
}
return super.getModelValue();
}
};
input.setNullValid(allowNull);
add(input);
}
@Override
public DropDownChoice<T> getBaseFormComponent() {
return (DropDownChoice) get("input");
}
public IModel<T> getModel() {
return getBaseFormComponent().getModel();
}
protected String getNullValidDisplayValue() {
return getString("DropDownChoicePanel.notDefined");
}
public boolean isSortChoices() {
return sortChoices;
}
public void setSortChoices(boolean sortChoices) {
this.sortChoices = sortChoices;
}
}