1
|
package eu.etaxonomy.vaadin.ui.mainmenu;
|
2
|
|
3
|
import java.util.List;
|
4
|
import java.util.Optional;
|
5
|
import java.util.stream.Collectors;
|
6
|
import java.util.stream.StreamSupport;
|
7
|
|
8
|
import org.springframework.beans.factory.annotation.Autowired;
|
9
|
import org.springframework.context.ApplicationEventPublisher;
|
10
|
import org.springframework.context.event.EventListener;
|
11
|
|
12
|
import com.vaadin.server.FontAwesome;
|
13
|
import com.vaadin.server.Resource;
|
14
|
import com.vaadin.spring.annotation.SpringComponent;
|
15
|
import com.vaadin.spring.annotation.UIScope;
|
16
|
import com.vaadin.ui.Button;
|
17
|
import com.vaadin.ui.Component;
|
18
|
import com.vaadin.ui.CssLayout;
|
19
|
import com.vaadin.ui.themes.ValoTheme;
|
20
|
|
21
|
import eu.etaxonomy.vaadin.ui.MainMenu;
|
22
|
import eu.etaxonomy.vaadin.ui.navigation.AfterViewChangeEvent;
|
23
|
import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
|
24
|
|
25
|
/**
|
26
|
* ValoMainMenuBean is the implementation of responsive Vaadin Valo theme's menu
|
27
|
*
|
28
|
* @author Peter / Vaadin
|
29
|
*/
|
30
|
|
31
|
@SpringComponent
|
32
|
@UIScope
|
33
|
class ValoMainMenuBean extends CssLayout implements MainMenu {
|
34
|
|
35
|
private static final long serialVersionUID = -993548217313748689L;
|
36
|
|
37
|
private final static String MENU_VISIBLE = "valo-menu-visible";
|
38
|
|
39
|
private CssLayout menuArea;
|
40
|
|
41
|
private Button menuToggle;
|
42
|
|
43
|
@Autowired
|
44
|
ApplicationEventPublisher eventBus;
|
45
|
|
46
|
public ValoMainMenuBean() {
|
47
|
setPrimaryStyleName(ValoTheme.MENU_ROOT);
|
48
|
|
49
|
CssLayout menuPart = new CssLayout();
|
50
|
menuPart.addStyleName(ValoTheme.MENU_PART);
|
51
|
|
52
|
menuToggle = new Button("Menu", FontAwesome.LIST);
|
53
|
menuToggle.addClickListener(e -> onToggleMenu());
|
54
|
menuToggle.addStyleName("valo-menu-toggle");
|
55
|
menuToggle.addStyleName(ValoTheme.BUTTON_SMALL);
|
56
|
|
57
|
menuPart.addComponent(menuToggle);
|
58
|
|
59
|
menuArea = new CssLayout();
|
60
|
menuArea.setPrimaryStyleName("valo-menuitems");
|
61
|
menuArea.setWidth(100, Unit.PERCENTAGE);
|
62
|
|
63
|
menuPart.addComponent(menuArea);
|
64
|
|
65
|
addComponent(menuPart);
|
66
|
}
|
67
|
|
68
|
private void onToggleMenu() {
|
69
|
if (getStyleName().contains(MENU_VISIBLE)) {
|
70
|
removeStyleName(MENU_VISIBLE);
|
71
|
} else {
|
72
|
addStyleName(MENU_VISIBLE);
|
73
|
}
|
74
|
}
|
75
|
|
76
|
@Override
|
77
|
public MainMenuItem addMenuItem(String caption, Resource icon, String navigationResource) {
|
78
|
MainMenuItemBean mainMenuItem = new MainMenuItemBean(caption, icon, navigationResource,
|
79
|
e -> onMenuItemClicked(navigationResource));
|
80
|
menuArea.addComponent(mainMenuItem);
|
81
|
return mainMenuItem;
|
82
|
}
|
83
|
|
84
|
private void onMenuItemClicked(String navigationResource) {
|
85
|
eventBus.publishEvent(new NavigationEvent(navigationResource));
|
86
|
}
|
87
|
|
88
|
@EventListener
|
89
|
protected void onNavigationEvent(NavigationEvent e) {
|
90
|
List<MainMenuItemBean> menuItems = StreamSupport.stream(menuArea.spliterator(), false)
|
91
|
.filter(component -> MainMenuItemBean.class.isAssignableFrom(component.getClass()))
|
92
|
.map(component -> MainMenuItemBean.class.cast(component)).collect(Collectors.toList());
|
93
|
|
94
|
menuItems.forEach(menuItem -> menuItem.setSelected(false));
|
95
|
Optional<MainMenuItemBean> selectedMenuItem = menuItems.stream()
|
96
|
.filter(menuItem -> menuItem.getViewName().equals(e.getViewName())).findFirst();
|
97
|
if (selectedMenuItem.isPresent()) {
|
98
|
selectedMenuItem.get().setSelected(true);
|
99
|
}
|
100
|
}
|
101
|
|
102
|
@EventListener
|
103
|
protected void afterViewChange(AfterViewChangeEvent event) {
|
104
|
removeStyleName(MENU_VISIBLE);
|
105
|
}
|
106
|
|
107
|
/**
|
108
|
* {@inheritDoc}
|
109
|
*/
|
110
|
@Override
|
111
|
public void addMenuComponent(Component component) {
|
112
|
menuArea.addComponent(component);
|
113
|
}
|
114
|
}
|