/
DashboardMenu.java
280 lines (245 loc) · 10.6 KB
/
DashboardMenu.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
package com.vaadin.demo.dashboard.view;
import java.util.Collection;
import com.google.common.eventbus.Subscribe;
import com.vaadin.demo.dashboard.DashboardUI;
import com.vaadin.demo.dashboard.component.ProfilePreferencesWindow;
import com.vaadin.demo.dashboard.domain.Transaction;
import com.vaadin.demo.dashboard.domain.User;
import com.vaadin.demo.dashboard.event.DashboardEvent.NotificationsCountUpdatedEvent;
import com.vaadin.demo.dashboard.event.DashboardEvent.PostViewChangeEvent;
import com.vaadin.demo.dashboard.event.DashboardEvent.ProfileUpdatedEvent;
import com.vaadin.demo.dashboard.event.DashboardEvent.ReportsCountUpdatedEvent;
import com.vaadin.demo.dashboard.event.DashboardEvent.TransactionReportEvent;
import com.vaadin.demo.dashboard.event.DashboardEvent.UserLoggedOutEvent;
import com.vaadin.demo.dashboard.event.DashboardEventBus;
import com.vaadin.event.dd.DragAndDropEvent;
import com.vaadin.event.dd.DropHandler;
import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
import com.vaadin.server.FontAwesome;
import com.vaadin.server.ThemeResource;
import com.vaadin.server.VaadinSession;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.AbstractSelect.AcceptItem;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.DragAndDropWrapper;
import com.vaadin.ui.DragAndDropWrapper.DragStartMode;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.MenuBar.Command;
import com.vaadin.ui.MenuBar.MenuItem;
import com.vaadin.ui.Table;
import com.vaadin.ui.UI;
import com.vaadin.ui.themes.ValoTheme;
/**
* A responsive menu component providing user information and the controls for
* primary navigation between the views.
*/
@SuppressWarnings({ "serial", "unchecked" })
public final class DashboardMenu extends CustomComponent {
public static final String ID = "dashboard-menu";
public static final String REPORTS_BADGE_ID = "dashboard-menu-reports-badge";
public static final String NOTIFICATIONS_BADGE_ID = "dashboard-menu-notifications-badge";
private static final String STYLE_VISIBLE = "valo-menu-visible";
private Label notificationsBadge;
private Label reportsBadge;
private MenuItem settingsItem;
public DashboardMenu() {
setPrimaryStyleName("valo-menu");
setId(ID);
setSizeUndefined();
// There's only one DashboardMenu per UI so this doesn't need to be
// unregistered from the UI-scoped DashboardEventBus.
DashboardEventBus.register(this);
setCompositionRoot(buildContent());
}
private Component buildContent() {
final CssLayout menuContent = new CssLayout();
menuContent.addStyleName("sidebar");
menuContent.addStyleName(ValoTheme.MENU_PART);
menuContent.addStyleName("no-vertical-drag-hints");
menuContent.addStyleName("no-horizontal-drag-hints");
menuContent.setWidth(null);
menuContent.setHeight("100%");
menuContent.addComponent(buildTitle());
menuContent.addComponent(buildUserMenu());
menuContent.addComponent(buildToggleButton());
menuContent.addComponent(buildMenuItems());
return menuContent;
}
private Component buildTitle() {
Label logo = new Label("QuickTickets <strong>Dashboard</strong>",
ContentMode.HTML);
logo.setSizeUndefined();
HorizontalLayout logoWrapper = new HorizontalLayout(logo);
logoWrapper.setComponentAlignment(logo, Alignment.MIDDLE_CENTER);
logoWrapper.addStyleName("valo-menu-title");
return logoWrapper;
}
private User getCurrentUser() {
return (User) VaadinSession.getCurrent().getAttribute(
User.class.getName());
}
private Component buildUserMenu() {
final MenuBar settings = new MenuBar();
settings.addStyleName("user-menu");
final User user = getCurrentUser();
settingsItem = settings.addItem("", new ThemeResource(
"img/profile-pic-300px.jpg"), null);
updateUserName(null);
settingsItem.addItem("Edit Profile", new Command() {
@Override
public void menuSelected(final MenuItem selectedItem) {
ProfilePreferencesWindow.open(user, false);
}
});
settingsItem.addItem("Preferences", new Command() {
@Override
public void menuSelected(final MenuItem selectedItem) {
ProfilePreferencesWindow.open(user, true);
}
});
settingsItem.addSeparator();
settingsItem.addItem("Sign Out", new Command() {
@Override
public void menuSelected(final MenuItem selectedItem) {
DashboardEventBus.post(new UserLoggedOutEvent());
}
});
return settings;
}
private Component buildToggleButton() {
Button valoMenuToggleButton = new Button("Menu", new ClickListener() {
@Override
public void buttonClick(final ClickEvent event) {
if (getCompositionRoot().getStyleName().contains(STYLE_VISIBLE)) {
getCompositionRoot().removeStyleName(STYLE_VISIBLE);
} else {
getCompositionRoot().addStyleName(STYLE_VISIBLE);
}
}
});
valoMenuToggleButton.setIcon(FontAwesome.LIST);
valoMenuToggleButton.addStyleName("valo-menu-toggle");
valoMenuToggleButton.addStyleName(ValoTheme.BUTTON_BORDERLESS);
valoMenuToggleButton.addStyleName(ValoTheme.BUTTON_SMALL);
return valoMenuToggleButton;
}
private Component buildMenuItems() {
CssLayout menuItemsLayout = new CssLayout();
menuItemsLayout.addStyleName("valo-menuitems");
for (final DashboardViewType view : DashboardViewType.values()) {
Component menuItemComponent = new ValoMenuItemButton(view);
if (view == DashboardViewType.REPORTS) {
// Add drop target to reports button
DragAndDropWrapper reports = new DragAndDropWrapper(
menuItemComponent);
reports.setSizeUndefined();
reports.setDragStartMode(DragStartMode.NONE);
reports.setDropHandler(new DropHandler() {
@Override
public void drop(final DragAndDropEvent event) {
UI.getCurrent()
.getNavigator()
.navigateTo(
DashboardViewType.REPORTS.getViewName());
Table table = (Table) event.getTransferable()
.getSourceComponent();
DashboardEventBus.post(new TransactionReportEvent(
(Collection<Transaction>) table.getValue()));
}
@Override
public AcceptCriterion getAcceptCriterion() {
return AcceptItem.ALL;
}
});
menuItemComponent = reports;
}
if (view == DashboardViewType.DASHBOARD) {
notificationsBadge = new Label();
notificationsBadge.setId(NOTIFICATIONS_BADGE_ID);
menuItemComponent = buildBadgeWrapper(menuItemComponent,
notificationsBadge);
}
if (view == DashboardViewType.REPORTS) {
reportsBadge = new Label();
reportsBadge.setId(REPORTS_BADGE_ID);
menuItemComponent = buildBadgeWrapper(menuItemComponent,
reportsBadge);
}
menuItemsLayout.addComponent(menuItemComponent);
}
return menuItemsLayout;
}
private Component buildBadgeWrapper(final Component menuItemButton,
final Component badgeLabel) {
CssLayout dashboardWrapper = new CssLayout(menuItemButton);
dashboardWrapper.addStyleName("badgewrapper");
dashboardWrapper.addStyleName(ValoTheme.MENU_ITEM);
badgeLabel.addStyleName(ValoTheme.MENU_BADGE);
badgeLabel.setWidthUndefined();
badgeLabel.setVisible(false);
dashboardWrapper.addComponent(badgeLabel);
return dashboardWrapper;
}
@Override
public void attach() {
super.attach();
updateNotificationsCount(null);
}
@Subscribe
public void postViewChange(final PostViewChangeEvent event) {
// After a successful view change the menu can be hidden in mobile view.
getCompositionRoot().removeStyleName(STYLE_VISIBLE);
}
@Subscribe
public void updateNotificationsCount(
final NotificationsCountUpdatedEvent event) {
int unreadNotificationsCount = DashboardUI.getDataProvider()
.getUnreadNotificationsCount();
notificationsBadge.setValue(String.valueOf(unreadNotificationsCount));
notificationsBadge.setVisible(unreadNotificationsCount > 0);
}
@Subscribe
public void updateReportsCount(final ReportsCountUpdatedEvent event) {
reportsBadge.setValue(String.valueOf(event.getCount()));
reportsBadge.setVisible(event.getCount() > 0);
}
@Subscribe
public void updateUserName(final ProfileUpdatedEvent event) {
User user = getCurrentUser();
settingsItem.setText(user.getFirstName() + " " + user.getLastName());
}
public final class ValoMenuItemButton extends Button {
private static final String STYLE_SELECTED = "selected";
private final DashboardViewType view;
public ValoMenuItemButton(final DashboardViewType view) {
this.view = view;
setPrimaryStyleName("valo-menu-item");
setIcon(view.getIcon());
setCaption(view.getViewName().substring(0, 1).toUpperCase()
+ view.getViewName().substring(1));
DashboardEventBus.register(this);
addClickListener(new ClickListener() {
@Override
public void buttonClick(final ClickEvent event) {
UI.getCurrent().getNavigator()
.navigateTo(view.getViewName());
}
});
}
@Subscribe
public void postViewChange(final PostViewChangeEvent event) {
removeStyleName(STYLE_SELECTED);
if (event.getView() == view) {
addStyleName(STYLE_SELECTED);
}
}
}
}