/
SettingsWindow.java
331 lines (276 loc) · 12.3 KB
/
SettingsWindow.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* Copyright (c) 2008-2016 Haulmont.
*
* 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.haulmont.cuba.web.app.ui.core.settings;
import com.haulmont.bali.util.ParamsMap;
import com.haulmont.cuba.client.ClientConfig;
import com.haulmont.cuba.core.global.AppBeans;
import com.haulmont.cuba.core.global.ClientType;
import com.haulmont.cuba.core.global.GlobalConfig;
import com.haulmont.cuba.core.global.TimeZones;
import com.haulmont.cuba.gui.WindowManager.OpenType;
import com.haulmont.cuba.gui.components.*;
import com.haulmont.cuba.gui.components.actions.BaseAction;
import com.haulmont.cuba.gui.components.mainwindow.AppWorkArea;
import com.haulmont.cuba.gui.config.MenuConfig;
import com.haulmont.cuba.gui.config.MenuItem;
import com.haulmont.cuba.gui.config.WindowConfig;
import com.haulmont.cuba.gui.config.WindowInfo;
import com.haulmont.cuba.gui.settings.SettingsClient;
import com.haulmont.cuba.gui.theme.ThemeConstantsRepository;
import com.haulmont.cuba.security.app.UserManagementService;
import com.haulmont.cuba.security.app.UserSettingService;
import com.haulmont.cuba.security.app.UserTimeZone;
import com.haulmont.cuba.security.entity.User;
import com.haulmont.cuba.security.global.UserSession;
import com.haulmont.cuba.web.App;
import com.haulmont.cuba.web.WebConfig;
import com.haulmont.cuba.web.app.UserSettingsTools;
import com.haulmont.cuba.web.gui.components.WebComponentsHelper;
import com.haulmont.cuba.web.security.ExternalUserCredentials;
import com.haulmont.cuba.web.settings.WebSettingsClient;
import com.vaadin.ui.ComboBox;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import java.util.*;
import java.util.stream.Collectors;
public class SettingsWindow extends AbstractWindow {
private static final Logger log = LoggerFactory.getLogger(SettingsWindow.class);
public static final String CHANGE_THEME_ENABLED_PARAM = "changeThemeEnabled";
protected boolean changeThemeEnabled = true;
protected String msgTabbed;
protected String msgSingle;
@Inject
protected App app;
@Inject
protected UserSettingsTools userSettingsTools;
@Inject
protected UserSession userSession;
@Inject
protected UserManagementService userManagementService;
@Inject
protected ClientConfig clientConfig;
@Inject
protected GlobalConfig globalConfig;
@Inject
protected TimeZones timeZones;
@Inject
protected Button okBtn;
@Inject
protected Button cancelBtn;
@Inject
protected Button changePasswordBtn;
@Inject
protected Button resetScreenSettingsBtn;
@Inject
protected OptionsGroup<String, String> modeOptions;
@Inject
protected LookupField<String> appThemeField;
@Inject
protected LookupField<String> timeZoneLookup;
@Inject
protected CheckBox timeZoneAutoField;
@Inject
protected Label<String> languageLabel;
@Inject
protected LookupField<String> appLangField;
@Inject
protected LookupField<String> defaultScreenField;
@Inject
protected MenuConfig menuConfig;
@Inject
protected WebConfig webConfig;
@Inject
protected WindowConfig windowConfig;
@Inject
protected UserSettingService userSettingService;
@Inject
protected SettingsClient settingsClient;
@Override
public void init(Map<String, Object> params) {
Boolean changeThemeEnabledParam = (Boolean) params.get(CHANGE_THEME_ENABLED_PARAM);
if (changeThemeEnabledParam != null) {
changeThemeEnabled = changeThemeEnabledParam;
}
AppWorkArea.Mode mode = userSettingsTools.loadAppWindowMode();
msgTabbed = getMessage("modeTabbed");
msgSingle = getMessage("modeSingle");
modeOptions.setOptionsList(Arrays.asList(msgTabbed, msgSingle));
if (mode == AppWorkArea.Mode.TABBED)
modeOptions.setValue(msgTabbed);
else
modeOptions.setValue(msgSingle);
ThemeConstantsRepository themeRepository = AppBeans.get(ThemeConstantsRepository.NAME);
Set<String> supportedThemes = themeRepository.getAvailableThemes();
appThemeField.setOptionsList(new ArrayList<>(supportedThemes));
String userAppTheme = userSettingsTools.loadAppWindowTheme();
appThemeField.setValue(userAppTheme);
ComboBox vAppThemeField = (ComboBox) WebComponentsHelper.unwrap(appThemeField);
vAppThemeField.setTextInputAllowed(false);
appThemeField.setEditable(changeThemeEnabled);
boolean langFieldVisible = !globalConfig.getLocaleSelectVisible();
languageLabel.setVisible(langFieldVisible);
appLangField.setVisible(langFieldVisible);
initTimeZoneFields();
User user = userSession.getUser();
changePasswordBtn.setAction(new BaseAction("changePassw")
.withCaption(getMessage("changePassw"))
.withHandler(event -> {
Window passwordDialog = openWindow("sec$User.changePassword", OpenType.DIALOG,
ParamsMap.of("currentPasswordRequired", true));
passwordDialog.addCloseListener(actionId -> {
// move focus back to window
changePasswordBtn.focus();
});
}));
if (!user.equals(userSession.getCurrentOrSubstitutedUser())
|| ExternalUserCredentials.isLoggedInWithExternalAuth(userSession)) {
changePasswordBtn.setEnabled(false);
}
Map<String, Locale> locales = globalConfig.getAvailableLocales();
TreeMap<String, String> options = new TreeMap<>();
for (Map.Entry<String, Locale> entry : locales.entrySet()) {
options.put(entry.getKey(), messages.getTools().localeToString(entry.getValue()));
}
appLangField.setOptionsMap(options);
appLangField.setValue(userManagementService.loadOwnLocale());
Action commitAction = new BaseAction("commit")
.withCaption(messages.getMainMessage("actions.Ok"))
.withShortcut(clientConfig.getCommitShortcut())
.withPrimary(true)
.withHandler(event ->
commit()
);
addAction(commitAction);
okBtn.setAction(commitAction);
cancelBtn.setAction(new BaseAction("cancel")
.withCaption(messages.getMainMessage("actions.Cancel"))
.withHandler(event ->
cancel()
));
resetScreenSettingsBtn.setAction(new BaseAction("resetScreenSettings")
.withCaption(getMessage("resetScreenSettings"))
.withHandler(buttonEvent ->
showOptionDialog(getMessage("resetScreenSettings"),
getMessage("resetScreenSettings.description"),
MessageType.CONFIRMATION,
new Action[]{
new DialogAction(DialogAction.Type.YES)
.withHandler(event -> resetScreenSettings()),
new DialogAction(DialogAction.Type.NO)
})
));
initDefaultScreenField();
}
protected void initDefaultScreenField() {
boolean screenSelectionEnabled = webConfig.getUserCanChooseDefaultScreen();
if (!screenSelectionEnabled) {
defaultScreenField.setEditable(false);
defaultScreenField.setDescription(getMessage("defaultScreenSelectionDisabled"));
}
defaultScreenField.setOptionsMap(collectScreens());
String defaultScreen = userSettingService.loadSetting(ClientType.WEB, "userDefaultScreen");
if (StringUtils.isEmpty(defaultScreen) || !screenSelectionEnabled) {
defaultScreen = webConfig.getDefaultScreenId();
}
defaultScreenField.setValue(defaultScreen);
}
protected Map<String, String> collectScreens() {
Map<String, String> map = new LinkedHashMap<>();
HashSet<String> screens = new HashSet<>();
for (MenuItem item : collectPermittedScreens(menuConfig.getRootItems())) {
String screen = item.getScreen();
if (!screens.contains(screen)) {
map.put(menuConfig.getItemCaption(item), screen);
screens.add(screen);
} else {
log.info("Menu contains items with the same screen: '{}'. " +
"The entry with id '{}' will be ignored", screen, item.getId());
}
}
return map;
}
protected List<MenuItem> collectPermittedScreens(List<MenuItem> menuItems) {
List<MenuItem> collectedItems = new ArrayList<>();
for (MenuItem item : menuItems) {
if (!item.isPermitted(userSession))
continue;
if (StringUtils.isNotEmpty(item.getScreen())) {
collectedItems.add(item);
}
if (CollectionUtils.isNotEmpty(item.getChildren())) {
collectedItems.addAll(collectPermittedScreens(item.getChildren()));
}
}
return collectedItems;
}
protected void commit() {
if (changeThemeEnabled) {
String selectedTheme = appThemeField.getValue();
userSettingsTools.saveAppWindowTheme(selectedTheme);
app.setUserAppTheme(selectedTheme);
}
AppWorkArea.Mode m = Objects.equals(modeOptions.getValue(), msgTabbed) ?
AppWorkArea.Mode.TABBED : AppWorkArea.Mode.SINGLE;
userSettingsTools.saveAppWindowMode(m);
saveTimeZoneSettings();
saveLocaleSettings();
if (webConfig.getUserCanChooseDefaultScreen()) {
userSettingService.saveSetting(ClientType.WEB, "userDefaultScreen", defaultScreenField.getValue());
}
showNotification(getMessage("modeChangeNotification"), NotificationType.HUMANIZED);
close(COMMIT_ACTION_ID);
}
protected void cancel() {
close(CLOSE_ACTION_ID);
}
protected void initTimeZoneFields() {
Map<String, String> options = new TreeMap<>();
for (String id : TimeZone.getAvailableIDs()) {
TimeZone timeZone = TimeZone.getTimeZone(id);
options.put(timeZones.getDisplayNameLong(timeZone), id);
}
timeZoneLookup.setOptionsMap(options);
timeZoneAutoField.setCaption(messages.getMainMessage("timeZone.auto"));
timeZoneAutoField.setDescription(messages.getMainMessage("timeZone.auto.descr"));
timeZoneAutoField.addValueChangeListener(e -> timeZoneLookup.setEnabled(!Boolean.TRUE.equals(e.getValue())));
UserTimeZone userTimeZone = userManagementService.loadOwnTimeZone();
timeZoneLookup.setValue(userTimeZone.name);
timeZoneAutoField.setValue(userTimeZone.auto);
}
protected void saveTimeZoneSettings() {
UserTimeZone userTimeZone = new UserTimeZone(timeZoneLookup.getValue(), timeZoneAutoField.getValue());
userManagementService.saveOwnTimeZone(userTimeZone);
}
protected void saveLocaleSettings() {
String userLocale = appLangField.getValue();
userManagementService.saveOwnLocale(userLocale);
}
protected void resetScreenSettings() {
userSettingService.deleteScreenSettings(ClientType.WEB, getAllWindowIds());
((WebSettingsClient) settingsClient).clearCache();
showNotification(getMessage("resetScreenSettings.notification"));
}
protected Set<String> getAllWindowIds() {
Collection<WindowInfo> windows = windowConfig.getWindows();
return windows.stream()
.map(WindowInfo::getId)
.collect(Collectors.toSet());
}
}