-
Notifications
You must be signed in to change notification settings - Fork 160
/
NorthView.java
391 lines (330 loc) · 17 KB
/
NorthView.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*******************************************************************************
* Copyright (c) 2016, 2020 Eurotech and/or its affiliates and others
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.app.console.core.client;
import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.extjs.gxt.ui.client.widget.menu.SeparatorMenuItem;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import org.eclipse.kapua.app.console.core.client.util.TokenCleaner;
import org.eclipse.kapua.app.console.core.shared.service.GwtAuthorizationService;
import org.eclipse.kapua.app.console.core.shared.service.GwtAuthorizationServiceAsync;
import org.eclipse.kapua.app.console.core.shared.service.GwtSettingsService;
import org.eclipse.kapua.app.console.core.shared.service.GwtSettingsServiceAsync;
import org.eclipse.kapua.app.console.module.account.shared.model.GwtAccount;
import org.eclipse.kapua.app.console.module.account.shared.model.permission.AccountSessionPermission;
import org.eclipse.kapua.app.console.module.account.shared.service.GwtAccountService;
import org.eclipse.kapua.app.console.module.account.shared.service.GwtAccountServiceAsync;
import org.eclipse.kapua.app.console.module.api.client.messages.ConsoleMessages;
import org.eclipse.kapua.app.console.module.api.client.resources.icons.IconSet;
import org.eclipse.kapua.app.console.module.api.client.ui.view.AbstractView;
import org.eclipse.kapua.app.console.module.api.client.ui.view.descriptor.MainViewDescriptor;
import org.eclipse.kapua.app.console.module.api.client.ui.widget.KapuaMenuItem;
import org.eclipse.kapua.app.console.module.api.client.util.FailureHandler;
import org.eclipse.kapua.app.console.module.api.client.util.UserAgentUtils;
import org.eclipse.kapua.app.console.module.api.shared.model.session.GwtSession;
import org.eclipse.kapua.app.console.module.api.shared.service.GwtConsoleService;
import org.eclipse.kapua.app.console.module.api.shared.service.GwtConsoleServiceAsync;
import java.util.List;
public class NorthView extends LayoutContainer {
private static final ConsoleMessages MSGS = GWT.create(ConsoleMessages.class);
private final GwtAuthorizationServiceAsync gwtAuthorizationService = GWT.create(GwtAuthorizationService.class);
private final GwtAccountServiceAsync gwtAccountService = GWT.create(GwtAccountService.class);
private final GwtConsoleServiceAsync gwtConsoleService = GWT.create(GwtConsoleService.class);
private final GwtSettingsServiceAsync gwtSettingService = GWT.create(GwtSettingsService.class);
// UI stuff
private KapuaCloudConsole parent;
private Menu subAccountMenu;
private Button userActionButton;
// Data
private GwtSession currentSession;
private String rootAccountId;
private String userId;
private String username;
private String userDisplayName;
private String rootAccountName;
private String selectedAccountName;
// Listener
private final SelectionListener<MenuEvent> switchToAccountListener = new SelectionListener<MenuEvent>() {
@Override
public void componentSelected(MenuEvent ce) {
switchToAccount(ce);
}
};
public NorthView(GwtSession currentSession, KapuaCloudConsole parent) {
this.parent = parent;
this.currentSession = currentSession;
this.rootAccountId = currentSession.getRootAccountId();
this.userId = currentSession.getUserId();
this.username = currentSession.getUserName();
this.userDisplayName = currentSession.getUserDisplayName();
this.rootAccountName = currentSession.getRootAccountName();
this.selectedAccountName = currentSession.getSelectedAccountName();
}
@Override
protected void onRender(Element parent, int index) {
super.onRender(parent, index);
TableLayout layout = new TableLayout(2);
layout.setWidth("100%");
layout.setTableStyle("background-color: transparent");
ContentPanel panel = new ContentPanel();
panel.setHeaderVisible(false);
panel.setBorders(false);
panel.setBodyBorder(false);
panel.setId("header-panel");
panel.setBodyStyle("background-color: transparent");
panel.setLayout(layout);
// Logo
panel.add(getKapuaHeader(),
new TableData(Style.HorizontalAlignment.LEFT,
Style.VerticalAlignment.MIDDLE));
// User Action Menu
panel.add(getUserActionMenu(),
new TableData(Style.HorizontalAlignment.RIGHT,
Style.VerticalAlignment.MIDDLE));
add(panel);
}
private Widget getKapuaHeader() {
SimplePanel logo = new SimplePanel();
if (!UserAgentUtils.isIE() ||
UserAgentUtils.getIEDocumentMode() > 8) {
logo.setStyleName("headerLogo");
} else {
logo.setStyleName("headerLogo-ie8");
}
return logo;
}
private Widget getUserActionMenu() {
userActionButton = new Button();
userActionButton.addListener(Events.OnMouseOver, new Listener<BaseEvent>() {
@Override
public void handleEvent(BaseEvent be) {
Menu userActionMenu = new Menu();
// Child Accounts menu item
if (currentSession.hasPermission(AccountSessionPermission.read())) {
userActionMenu.add(createAccountNavigationMenuItem());
userActionMenu.add(new SeparatorMenuItem());
}
// Change Password menu item
// (only if the current user is an INTERNAL one; note that an INTERNAL user has no ssoIdToken)
if (currentSession.getSsoIdToken() == null) {
KapuaMenuItem changePassword = new KapuaMenuItem();
changePassword.setText(MSGS.changePassword());
changePassword.setIcon(IconSet.KEY);
changePassword.addSelectionListener(new SelectionListener<MenuEvent>() {
@Override
public void componentSelected(MenuEvent ce) {
ChangePasswordDialog changePasswordDialog = new ChangePasswordDialog(currentSession);
changePasswordDialog.show();
}
});
userActionMenu.add(changePassword);
userActionMenu.add(new SeparatorMenuItem());
}
//
// Logout menu item
KapuaMenuItem userLogoutMenuItem = new KapuaMenuItem();
userLogoutMenuItem.setText(MSGS.consoleHeaderUserActionLogout());
userLogoutMenuItem.setIcon(IconSet.SIGN_OUT);
userLogoutMenuItem.addSelectionListener(new SelectionListener<MenuEvent>() {
@Override
public void componentSelected(MenuEvent ce) {
gwtAuthorizationService.logout(new AsyncCallback<Void>() {
@Override
public void onFailure(Throwable caught) {
FailureHandler.handle(caught);
}
@Override
public void onSuccess(Void arg0) {
if (currentSession.isSsoEnabled() && currentSession.getSsoIdToken() != null) {
gwtSettingService.getSsoLogoutUri(currentSession.getSsoIdToken(),
new AsyncCallback<String>() {
@Override
public void onFailure(Throwable caught) {
FailureHandler.handle(caught);
}
@Override
public void onSuccess(String result) {
if (!result.isEmpty()) {
Window.Location.assign(result);
} else {
// result is empty, thus the OpenID logout is disabled
TokenCleaner.cleanToken();
}
}
});
} else {
TokenCleaner.cleanToken();
}
}
});
}
});
userActionMenu.add(userLogoutMenuItem);
userActionButton.setMenu(userActionMenu);
}
});
userActionButton.setId("header-button");
userActionButton.addStyleName("x-btn-arrow-custom");
userActionButton.setAutoWidth(true);
updateUserActionButtonLabel();
return userActionButton;
}
/**
* Creates an MenuItem with the list of all child accounts of the account of the current logged user,
* and also add as first sub-MenuItem the name of the root account.
*
* @return the MenuItem
*/
public MenuItem createAccountNavigationMenuItem() {
KapuaMenuItem rootAccountMenuItem = new KapuaMenuItem();
rootAccountMenuItem.setText(MSGS.accountSelectorItemYourAccount(rootAccountName));
rootAccountMenuItem.setToolTip(MSGS.accountSelectorTooltipYourAccount());
rootAccountMenuItem.setIcon(IconSet.USER_MD);
rootAccountMenuItem.setId(rootAccountId);
rootAccountMenuItem.addSelectionListener(switchToAccountListener);
subAccountMenu = new Menu();
subAccountMenu.setAutoWidth(true);
subAccountMenu.add(rootAccountMenuItem);
subAccountMenu.add(new SeparatorMenuItem());
populateNavigatorMenu(subAccountMenu, rootAccountId);
KapuaMenuItem switchToAccountMenuItem = new KapuaMenuItem();
switchToAccountMenuItem.setText(MSGS.consoleHeaderUserActionSwitchToAccount());
switchToAccountMenuItem.setIcon(IconSet.USERS);
switchToAccountMenuItem.setSubMenu(subAccountMenu);
return switchToAccountMenuItem;
}
/**
* Populates a Menu objects with a entry for each child account for the given account.
* It does this recursively for each child.
*
* @param menu the menu to fill
* @param accountId the account of the current menu item.
*/
private void populateNavigatorMenu(final Menu menu, String accountId) {
gwtAccountService.find(accountId, new AsyncCallback<GwtAccount>() {
@Override
public void onFailure(Throwable caught) {
FailureHandler.handle(caught);
}
@Override
public void onSuccess(GwtAccount result) {
// If no children are found, add "No Child" label
if (result.getChildAccounts() != null && result.getChildAccounts().isEmpty()) {
MenuItem noChildMenuItem = new MenuItem(MSGS.accountSelectorItemNoChild());
noChildMenuItem.disable();
menu.add(noChildMenuItem);
} else {
// For each child found create a item menu and search for its children
for (GwtAccount childAccount : result.getChildAccounts()) {
// Add item menu entry
KapuaMenuItem childAccountMenuItem = new KapuaMenuItem();
childAccountMenuItem.setIcon(IconSet.USER);
childAccountMenuItem.setText(childAccount.getName());
childAccountMenuItem.setTitle(childAccount.getName());
childAccountMenuItem.setId(String.valueOf(childAccount.getId()));
menu.add(childAccountMenuItem);
// Add selection listener to make the switch happen when selected
childAccountMenuItem.addSelectionListener(switchToAccountListener);
// Search for its children
if (!childAccount.getChildAccounts().isEmpty()) {
Menu childMenu = new Menu();
childMenu.setAutoWidth(true);
childAccountMenuItem.setSubMenu(childMenu);
populateNavigatorMenu(childMenu, childAccount.getId());
}
}
}
}
});
}
/**
* Make the switch between accounts when an account is selected from
* the account navigator.
*
* @param ce The MenuEvent that has been fired with the selection of a menu entry.
*/
private void switchToAccount(MenuEvent ce) {
String accountName = ce.getItem().getTitle();
String accountIdString = ce.getItem().getId();
// Mask the whole page
parent.getViewport().mask(MSGS.accountSelectorMenuMaskLoading(accountName));
gwtAccountService.find(accountIdString, new AsyncCallback<GwtAccount>() {
@Override
public void onFailure(Throwable caught) {
parent.getViewport().unmask();
FailureHandler.handle(caught);
}
@Override
public void onSuccess(final GwtAccount result) {
gwtConsoleService.getCustomEntityViews(new AsyncCallback<List<MainViewDescriptor>>() {
@Override
public void onFailure(Throwable caught) {
parent.getViewport().unmask();
FailureHandler.handle(caught);
}
@Override
public void onSuccess(List<MainViewDescriptor> viewDescriptors) {
if (result != null) {
currentSession.setSelectedAccountId(result.getId());
currentSession.setSelectedAccountName(result.getName());
currentSession.setSelectedAccountPath(result.getParentAccountPath());
}
// Update userActionButtonLabel with the current data
updateUserActionButtonLabel();
if (((ContentPanel) parent.getCenterView().getItem(0)).getItem(0) instanceof AbstractView) {
AbstractView aev = (AbstractView) ((ContentPanel) parent.getCenterView().getItem(0)).getItem(0);
aev.onUserChange();
}
// Force the west view (which contains the navigation menu) to reload available components
parent.getWestView().addMenuItems(viewDescriptors);
parent.getWestView().setDashboardSelected(true);
parent.getWestView().layout(true);
// Unmask the whole page
parent.getViewport().unmask();
}
});
}
});
}
/**
* Updates the UserActionButton label with the current selected informations for username and target account.<br/>
* The label format is:<br/>
* <p>
* If the display name of the user is defined:<br/>
* {displayName} @ {selectedAccountName}<br/>
* else:<br/>
* {username} @ {selectedAccountName}<br/>
*/
private void updateUserActionButtonLabel() {
String accountName = currentSession.getSelectedAccountName();
if (userDisplayName == null || userDisplayName.isEmpty()) {
userDisplayName = username;
}
userActionButton.setText(MSGS.consoleHeaderUserActionButtonLabel(userDisplayName, accountName));
}
}