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