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 package it.tidalwave.ui.javafx;
27
28 import javax.annotation.Nonnull;
29 import java.util.ArrayList;
30 import java.util.List;
31 import org.springframework.context.ConfigurableApplicationContext;
32 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
33 import org.springframework.context.annotation.Bean;
34 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import it.tidalwave.util.PreferencesHandler;
38 import it.tidalwave.role.ui.MenuBarModel;
39 import it.tidalwave.role.ui.ToolBarModel;
40 import it.tidalwave.role.ui.annotation.EnableMessageBus;
41 import it.tidalwave.role.ui.javafx.StackPaneSelector;
42 import it.tidalwave.messagebus.MessageBus;
43 import it.tidalwave.messagebus.spi.SimpleMessageBus;
44
45
46
47
48
49
50
51
52
53 public class JavaFXSpringAnnotationApplication extends AbstractJavaFXSpringApplication
54 {
55 static class Beans
56 {
57 @Bean
58 public ThreadPoolTaskExecutor javafxBinderExecutor()
59 {
60 return JavaFXSafeProxyCreator.getExecutor();
61 }
62
63 @Bean
64 public StackPaneSelector stackPaneSelector()
65 {
66 return new StackPaneSelector();
67 }
68
69 @Bean
70 public PreferencesHandler preferencesHandler()
71 {
72 return PreferencesHandler.getInstance();
73 }
74
75 @Bean
76 public ToolBarModel toolBarModel()
77 {
78 return JavaFXSafeProxyCreator.getToolBarModel();
79 }
80
81 @Bean
82 public MenuBarModel menuBarModel()
83 {
84 return JavaFXSafeProxyCreator.getMenuBarModel();
85 }
86 }
87
88 static class MessageBusBeans
89 {
90 @Bean(name = APPLICATION_MESSAGE_BUS_BEAN_NAME)
91 public MessageBus applicationMessageBus()
92 {
93 final var executor = new ThreadPoolTaskExecutor();
94 executor.setWaitForTasksToCompleteOnShutdown(false);
95 executor.setThreadNamePrefix("messageBus-");
96 executor.setCorePoolSize(10);
97 executor.setMaxPoolSize(10);
98 executor.setQueueCapacity(10);
99 executor.afterPropertiesSet();
100 return new SimpleMessageBus(executor);
101 }
102 }
103
104 protected static final String APPLICATION_MESSAGE_BUS_BEAN_NAME = "applicationMessageBus";
105
106
107 private final Logger log = LoggerFactory.getLogger(JavaFXSpringApplication.class);
108
109 @Override @Nonnull
110 protected ConfigurableApplicationContext createApplicationContext()
111 {
112 final var mainClass = getClass();
113 final var componentClasses = new ArrayList<>(List.of(mainClass, Beans.class));
114
115 if (getClass().isAnnotationPresent(EnableMessageBus.class))
116 {
117 log.info("Detected @{}, enabling message bus", EnableMessageBus.class.getSimpleName());
118 componentClasses.add(MessageBusBeans.class);
119 }
120
121 log.info("Scanning beans from {}", mainClass);
122 return new AnnotationConfigApplicationContext(componentClasses.toArray(new Class<?>[0]));
123 }
124 }