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.vaadin.spring.events.EventBus;
|
10
|
import org.vaadin.spring.events.annotation.EventBusListenerMethod;
|
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
|
protected EventBus.UIEventBus uiEventBus;
|
45
|
|
46
|
protected void setUIEventBus(EventBus.UIEventBus uiEventBus){
|
47
|
this.uiEventBus = uiEventBus;
|
48
|
uiEventBus.subscribe(this);
|
49
|
}
|
50
|
|
51
|
public ValoMainMenuBean() {
|
52
|
setPrimaryStyleName(ValoTheme.MENU_ROOT);
|
53
|
|
54
|
CssLayout menuPart = new CssLayout();
|
55
|
menuPart.addStyleName(ValoTheme.MENU_PART);
|
56
|
|
57
|
menuToggle = new Button("Menu", FontAwesome.LIST);
|
58
|
menuToggle.addClickListener(e -> onToggleMenu());
|
59
|
menuToggle.addStyleName("valo-menu-toggle");
|
60
|
menuToggle.addStyleName(ValoTheme.BUTTON_SMALL);
|
61
|
|
62
|
menuPart.addComponent(menuToggle);
|
63
|
|
64
|
menuArea = new CssLayout();
|
65
|
menuArea.setPrimaryStyleName("valo-menuitems");
|
66
|
menuArea.setWidth(100, Unit.PERCENTAGE);
|
67
|
|
68
|
menuPart.addComponent(menuArea);
|
69
|
|
70
|
addComponent(menuPart);
|
71
|
}
|
72
|
|
73
|
private void onToggleMenu() {
|
74
|
if (getStyleName().contains(MENU_VISIBLE)) {
|
75
|
removeStyleName(MENU_VISIBLE);
|
76
|
} else {
|
77
|
addStyleName(MENU_VISIBLE);
|
78
|
}
|
79
|
}
|
80
|
|
81
|
@Override
|
82
|
public MainMenuItem addMenuItem(String caption, Resource icon, String navigationResource) {
|
83
|
MainMenuItemBean mainMenuItem = new MainMenuItemBean(caption, icon, navigationResource,
|
84
|
e -> onMenuItemClicked(navigationResource));
|
85
|
menuArea.addComponent(mainMenuItem);
|
86
|
return mainMenuItem;
|
87
|
}
|
88
|
|
89
|
private void onMenuItemClicked(String navigationResource) {
|
90
|
uiEventBus.publish(this, new NavigationEvent(navigationResource));
|
91
|
}
|
92
|
|
93
|
@EventBusListenerMethod
|
94
|
protected void onNavigationEvent(NavigationEvent e) {
|
95
|
List<MainMenuItemBean> menuItems = StreamSupport.stream(menuArea.spliterator(), false)
|
96
|
.filter(component -> MainMenuItemBean.class.isAssignableFrom(component.getClass()))
|
97
|
.map(component -> MainMenuItemBean.class.cast(component)).collect(Collectors.toList());
|
98
|
|
99
|
menuItems.forEach(menuItem -> menuItem.setSelected(false));
|
100
|
Optional<MainMenuItemBean> selectedMenuItem = menuItems.stream()
|
101
|
.filter(menuItem -> menuItem.getViewName().equals(e.getViewName())).findFirst();
|
102
|
if (selectedMenuItem.isPresent()) {
|
103
|
selectedMenuItem.get().setSelected(true);
|
104
|
}
|
105
|
}
|
106
|
|
107
|
@EventBusListenerMethod
|
108
|
protected void afterViewChange(org.vaadin.spring.events.Event<AfterViewChangeEvent> event) {
|
109
|
removeStyleName(MENU_VISIBLE);
|
110
|
}
|
111
|
|
112
|
/**
|
113
|
* {@inheritDoc}
|
114
|
*/
|
115
|
@Override
|
116
|
public void addMenuComponent(Component component) {
|
117
|
menuArea.addComponent(component);
|
118
|
}
|
119
|
}
|