-
Notifications
You must be signed in to change notification settings - Fork 188
/
ChooseTypePanel.java
219 lines (179 loc) · 7.88 KB
/
ChooseTypePanel.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
/*
* Copyright (c) 2010-2017 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.page.admin.configuration.component;
import com.evolveum.midpoint.gui.api.component.BasePanel;
import com.evolveum.midpoint.gui.api.component.ObjectBrowserPanel;
import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.component.util.EnableBehaviour;
import com.evolveum.midpoint.web.page.admin.dto.ObjectViewDto;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import org.apache.wicket.ajax.AjaxChannel;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
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.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import javax.xml.namespace.QName;
import java.util.ArrayList;
import java.util.List;
/**
* @author shood
*
* TODO use a better name (ChooseObjectPanel ? ObjectChoosePanel ?)
* Distinguish between chooser panels that reside on "main page" and
* the one that resides in the popup window (ObjectSelectionPanel).
*/
public class ChooseTypePanel<T extends ObjectType> extends BasePanel<ObjectViewDto<T>> {
private static final long serialVersionUID = 1L;
private static final Trace LOGGER = TraceManager.getTrace(ChooseTypePanel.class);
private static final String ID_OBJECT_NAME = "name";
private static final String ID_INPUT_CONTAINER = "inputContainer";
private static final String ID_LINK_CHOOSE = "choose";
private static final String ID_LINK_REMOVE = "remove";
public ChooseTypePanel(String id, IModel<ObjectViewDto<T>> model){
super(id, model);
initLayout();
}
public ChooseTypePanel(String id, ObjectReferenceType ref){
super(id, Model.of(new ObjectViewDto<>(ref != null ? ref.getOid() : null, ref != null ? WebComponentUtil.getOrigStringFromPoly(ref.getTargetName()) : null)));
initLayout();
}
protected void initLayout() {
WebMarkupContainer inputContainer = new WebMarkupContainer(ID_INPUT_CONTAINER);
inputContainer.setOutputMarkupId(true);
inputContainer.add(getInputStyleClass());
add(inputContainer);
final TextField<String> name = new TextField<>(ID_OBJECT_NAME, new PropertyModel<>(getModel(), ObjectViewDto.F_NAME));
// new Model<String>(){
// private static final long serialVersionUID = 1L;
//
// @Override
// public String getObject() {
// ObjectViewDto<T> dto = getModel().getObject();
// if (dto != null) {
// if (dto.getName() != null)
// return getModel().getObject().getName();
// else if (ObjectViewDto.BAD_OID.equals(dto.getOid())) {
// return createStringResource("chooseTypePanel.ObjectNameValue.badOid").getString();
// } else {
// return createStringResource("chooseTypePanel.ObjectNameValue.null").getString();
// }
// }
// return "";
// }
// });
name.add(AttributeAppender.append("title", getPanelTitle()));
name.setOutputMarkupId(true);
AjaxLink<String> choose = new AjaxLink<String>(ID_LINK_CHOOSE) {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
changeOptionPerformed(target);
}
@Override
protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
super.updateAjaxAttributes(attributes);
attributes.setChannel(new AjaxChannel("blocking", AjaxChannel.Type.ACTIVE));
}
};
choose.add(new EnableBehaviour(() -> isEnabledToEdit()));
choose.add(AttributeAppender.append("title", getPanelTitle()));
choose.setOutputMarkupId(true);
AjaxLink<String> remove = new AjaxLink<String>(ID_LINK_REMOVE) {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
setToDefault(target);
target.add(name);
}
};
remove.setOutputMarkupId(true);
inputContainer.add(name);
inputContainer.add(choose);
add(remove);
}
protected boolean isSearchEnabled(){
return false;
}
protected QName getSearchProperty(){
return null;
}
protected ObjectQuery getChooseQuery(){
return null;
}
private void choosePerformed(AjaxRequestTarget target, T object){
getPageBase().hideMainPopup(target);
ObjectViewDto<T> o = getModel().getObject();
o.setName(WebComponentUtil.getName(object));
o.setOid(object.getOid());
o.setObject((PrismObject) object.asPrismObject().clone());
if(LOGGER.isTraceEnabled()){
LOGGER.trace("Choose operation performed: {} ({})", o.getName(), o.getOid());
}
target.add(get(ID_INPUT_CONTAINER).get(ID_OBJECT_NAME));
executeCustomAction(target, object);
}
protected void executeCustomAction(AjaxRequestTarget target, T object) {
}
protected boolean isEnabledToEdit(){
return true;
}
protected IModel<String> getPanelTitle(){
return null;
}
private void changeOptionPerformed(AjaxRequestTarget target){
Class<T> type = getObjectTypeClass();
List<QName> supportedTypes = new ArrayList<>();
supportedTypes.add(WebComponentUtil.classToQName(getPageBase().getPrismContext(), type));
ObjectBrowserPanel<T> objectBrowserPanel = new ObjectBrowserPanel<T>(getPageBase().getMainPopupBodyId(),
type, supportedTypes, false, getPageBase(), getChooseQuery() != null ? getChooseQuery().getFilter() : null){
private static final long serialVersionUID = 1L;
@Override
protected void onSelectPerformed(AjaxRequestTarget target, T focus) {
choosePerformed(target, focus);
}
};
objectBrowserPanel.setOutputMarkupId(true);
getPageBase().showMainPopup(objectBrowserPanel, target);
}
private void setToDefault(AjaxRequestTarget target){
ObjectViewDto<T> dto = new ObjectViewDto<>();
dto.setType(getObjectTypeClass());
getModel().setObject(dto);
executeCustomRemoveAction(target);
}
protected void executeCustomRemoveAction(AjaxRequestTarget target) {
}
protected AttributeAppender getInputStyleClass(){
return AttributeAppender.append("class", "col-md-4");
}
public Class<T> getObjectTypeClass(){
return ChooseTypePanel.this.getModelObject().getType();
}
public void setPanelEnabled(boolean isEnabled){
get(ID_INPUT_CONTAINER).get(ID_LINK_CHOOSE).setEnabled(isEnabled);
get(ID_LINK_REMOVE).setEnabled(isEnabled);
}
}