/
BasePanel.java
157 lines (131 loc) · 5.11 KB
/
BasePanel.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
/*
* Copyright (C) 2010-2020 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.api.component;
import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.StringResourceModel;
import org.jetbrains.annotations.Contract;
import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.schema.SchemaService;
import com.evolveum.midpoint.web.security.MidPointApplication;
import com.evolveum.midpoint.web.security.MidPointAuthWebSession;
import com.evolveum.midpoint.web.security.WebApplicationConfiguration;
import com.evolveum.prism.xml.ns._public.types_3.PolyStringType;
/**
* Base class for most midPoint GUI panels. It has a constructor and
* utility methods for convenient handling of the model. It also has
* other utility methods often used in reusable components.
* <p>
* Almost all reusable components should extend this class.
*
* @author lazyman
* @author semancik
*/
public class BasePanel<T> extends Panel {
private static final long serialVersionUID = 1L;
private IModel<T> model;
public BasePanel(String id) {
super(id);
}
public BasePanel(String id, IModel<T> model) {
super(id);
this.model = model == null ? createModel() : model;
}
public IModel<T> createModel() {
return null;
}
public IModel<T> getModel() {
return model;
}
public T getModelObject() {
return model != null ? model.getObject() : null;
}
public String getString(String resourceKey, Object... objects) {
return createStringResource(resourceKey, objects).getString();
}
public String getString(Enum<?> e) {
return createStringResource(e).getString();
}
public StringResourceModel createStringResource(String resourceKey, IModel<?> model, Object... objects) {
return new StringResourceModel(resourceKey, this).setModel(model)
.setDefaultValue(resourceKey)
.setParameters(objects);
}
public StringResourceModel createStringResource(String resourceKey, Object... objects) {
return new StringResourceModel(resourceKey, this).setModel(null)
.setDefaultValue(resourceKey)
.setParameters(objects);
}
public StringResourceModel createStringResource(PolyString polystringKey, Object... objects) {
String resourceKey = null;
if (polystringKey != null) {
// TODO later: use polystringKey.getKey()
resourceKey = polystringKey.getOrig();
}
return new StringResourceModel(resourceKey, this).setModel(null)
.setDefaultValue(resourceKey)
.setParameters(objects);
}
public StringResourceModel createStringResource(PolyStringType polystringKey, Object... objects) {
String resourceKey = null;
if (polystringKey != null) {
// TODO later: use polystringKey.getKey()
resourceKey = polystringKey.getOrig();
}
return new StringResourceModel(resourceKey, this).setModel(null)
.setDefaultValue(resourceKey)
.setParameters(objects);
}
public StringResourceModel createStringResource(Enum<?> e) {
return createStringResource(e, null);
}
public StringResourceModel createStringResource(Enum<?> e, String prefix) {
return createStringResource(e, prefix, null);
}
public StringResourceModel createStringResource(Enum<?> e, String prefix, String nullKey) {
StringBuilder sb = new StringBuilder();
if (StringUtils.isNotEmpty(prefix)) {
sb.append(prefix).append('.');
}
if (e == null) {
if (StringUtils.isNotEmpty(nullKey)) {
sb.append(nullKey);
} else {
sb = new StringBuilder();
}
} else {
sb.append(e.getDeclaringClass().getSimpleName()).append('.');
sb.append(e.name());
}
return createStringResource(sb.toString());
}
@Contract(pure = true)
public PageBase getPageBase() {
return WebComponentUtil.getPageBase(this);
}
public PrismContext getPrismContext() {
return getPageBase().getPrismContext();
}
public SchemaService getSchemaService() {
return getPageBase().getSchemaService();
}
protected String createComponentPath(String... components) {
return StringUtils.join(components, ":");
}
public WebApplicationConfiguration getWebApplicationConfiguration() {
MidPointApplication application = MidPointApplication.get();
return application.getWebApplicationConfiguration();
}
@Override
public MidPointAuthWebSession getSession() {
return (MidPointAuthWebSession) super.getSession();
}
}