1
|
package eu.etaxonomy.vaadin.ui.mainmenu;
|
2
|
|
3
|
import java.util.Comparator;
|
4
|
import java.util.List;
|
5
|
import java.util.Map;
|
6
|
import java.util.stream.Collectors;
|
7
|
|
8
|
import org.springframework.beans.factory.annotation.Autowired;
|
9
|
import org.springframework.context.ApplicationContext;
|
10
|
import org.vaadin.spring.events.EventBus.UIEventBus;
|
11
|
import org.vaadin.spring.events.annotation.EventBusListenerMethod;
|
12
|
|
13
|
import com.vaadin.navigator.View;
|
14
|
import com.vaadin.spring.annotation.SpringView;
|
15
|
|
16
|
import eu.etaxonomy.vaadin.ui.MainMenu;
|
17
|
import eu.etaxonomy.vaadin.ui.MenuItem;
|
18
|
import eu.etaxonomy.vaadin.ui.UIInitializedEvent;
|
19
|
|
20
|
// MenuBeanDiscovery is not yet working with spring!!!!!
|
21
|
//
|
22
|
// @NormalUIScoped // FIXME this provided a MenuBeanDiscoveryBean as a proxy, which is the equivalent in spring?
|
23
|
// otherwise the following error will be thrown:
|
24
|
// Error creating bean with name 'testView': Scope 'vaadin-view' is not active for the current thread;
|
25
|
// consider defining a scoped proxy for this bean if you intend to refer to it from a singleton
|
26
|
//###DISABLED:
|
27
|
//@SpringComponent
|
28
|
//@UIScope
|
29
|
public class MenuBeanDiscoveryBean {
|
30
|
|
31
|
@Autowired
|
32
|
private ApplicationContext beanManager;
|
33
|
|
34
|
private UIEventBus uiEventBus;
|
35
|
|
36
|
@Autowired
|
37
|
private void setUiEventBus(UIEventBus uiEventBus){
|
38
|
this.uiEventBus = uiEventBus;
|
39
|
uiEventBus.subscribe(this);
|
40
|
}
|
41
|
|
42
|
private MainMenu mainMenuLookup = null;
|
43
|
|
44
|
MenuBeanDiscoveryBean menuBeanDiscoveryBean;
|
45
|
|
46
|
public MenuBeanDiscoveryBean() {
|
47
|
|
48
|
System.out.println("###########");
|
49
|
}
|
50
|
|
51
|
@Autowired
|
52
|
public void setMainMenu(MainMenu mainMenu){
|
53
|
this.mainMenuLookup = mainMenu;
|
54
|
}
|
55
|
|
56
|
@EventBusListenerMethod
|
57
|
protected void doMenuItemLookup(UIInitializedEvent event) {
|
58
|
|
59
|
if (mainMenuLookup == null) {
|
60
|
// IGNORE
|
61
|
return;
|
62
|
}
|
63
|
|
64
|
MainMenu mainMenu = mainMenuLookup;
|
65
|
|
66
|
Map<String, View> beans = beanManager.getBeansOfType(View.class);
|
67
|
|
68
|
List<View> menuItemBeans = beans.values().stream()
|
69
|
.filter(bean -> bean.getClass().isAnnotationPresent(MenuItem.class)).sorted(new BeanNameComparer())
|
70
|
.collect(Collectors.toList());
|
71
|
|
72
|
menuItemBeans.forEach(menuItemBean -> {
|
73
|
MenuItem menuItemAnnotation = menuItemBean.getClass().getAnnotation(MenuItem.class);
|
74
|
SpringView viewAnnotation = menuItemBean.getClass().getAnnotation(SpringView.class);
|
75
|
|
76
|
mainMenu.addMenuItem(menuItemAnnotation.name(), menuItemAnnotation.icon(), viewAnnotation.name());
|
77
|
});
|
78
|
}
|
79
|
|
80
|
private static class BeanNameComparer implements Comparator<View> {
|
81
|
|
82
|
@Override
|
83
|
public int compare(View a, View b) {
|
84
|
MenuItem aAnnotation = a.getClass().getAnnotation(MenuItem.class);
|
85
|
MenuItem bAnnotation = b.getClass().getAnnotation(MenuItem.class);
|
86
|
|
87
|
return aAnnotation.order() - bAnnotation.order();
|
88
|
}
|
89
|
}
|
90
|
}
|