View Javadoc
1   /*
2    * *************************************************************************************************************************************************************
3    *
4    * SteelBlue: DCI User Interfaces
5    * http://tidalwave.it/projects/steelblue
6    *
7    * Copyright (C) 2015 - 2024 by Tidalwave s.a.s. (http://tidalwave.it)
8    *
9    * *************************************************************************************************************************************************************
10   *
11   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12   * You may obtain a copy of the License at
13   *
14   *     http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17   * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
18   *
19   * *************************************************************************************************************************************************************
20   *
21   * git clone https://bitbucket.org/tidalwave/steelblue-src
22   * git clone https://github.com/tidalwave-it/steelblue-src
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   * A base class for JavaFX applications with use Spring annotation scanning.
48   *
49   * @author  Fabrizio Giudici
50   * @since   1.1-ALPHA-4
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     // Don't use Slf4j and its static logger - give Main a chance to initialize things
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   }