JavaFXSpringAnnotationApplication.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * SteelBlue: DCI User Interfaces
  5.  * http://tidalwave.it/projects/steelblue
  6.  *
  7.  * Copyright (C) 2015 - 2025 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. import jakarta.annotation.Nonnull;
  28. import java.util.ArrayList;
  29. import java.util.List;
  30. import org.springframework.beans.factory.BeanFactory;
  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 it.tidalwave.ui.javafx.impl.DefaultJavaFXPanelGroupControl;
  36. import it.tidalwave.ui.javafx.impl.util.JavaFXSafeComponentBuilder;
  37. import it.tidalwave.ui.javafx.spi.AbstractJavaFXSpringApplication;
  38. import org.slf4j.Logger;
  39. import org.slf4j.LoggerFactory;
  40. import it.tidalwave.util.PreferencesHandler;
  41. import it.tidalwave.ui.core.annotation.EnableMessageBus;
  42. import it.tidalwave.messagebus.MessageBus;
  43. import it.tidalwave.messagebus.spi.SimpleMessageBus;

  44. /***************************************************************************************************************************************************************
  45.  *
  46.  * A base class for JavaFX applications with use Spring annotation scanning.
  47.  *
  48.  * @author  Fabrizio Giudici
  49.  * @since   1.1-ALPHA-4
  50.  *
  51.  **************************************************************************************************************************************************************/
  52. public class JavaFXSpringAnnotationApplication extends AbstractJavaFXSpringApplication
  53.   {
  54.     static class Beans
  55.       {
  56.         @Bean
  57.         public ThreadPoolTaskExecutor javafxBinderExecutor()
  58.           {
  59.             return JavaFXSafeComponentBuilder.getExecutor();
  60.           }

  61.         @Bean
  62.         public PreferencesHandler preferencesHandler()
  63.           {
  64.             return PreferencesHandler.getInstance();
  65.           }

  66.         @Bean
  67.         public JavaFXToolBarControl toolBarControl()
  68.           {
  69.             return JavaFXSafeComponentBuilder.getToolBarControl();
  70.           }

  71.         @Bean
  72.         public JavaFXMenuBarControl menuBarControl()
  73.           {
  74.             return JavaFXSafeComponentBuilder.getMenuBarControl();
  75.           }

  76.         @Bean
  77.         public JavaFXPanelGroupControl panelGroupControl (@Nonnull final BeanFactory beanFactory)
  78.           {
  79.             return new DefaultJavaFXPanelGroupControl(beanFactory);
  80.           }
  81.       }

  82.     static class MessageBusBeans
  83.       {
  84.         @Bean(name = APPLICATION_MESSAGE_BUS_BEAN_NAME)
  85.         public MessageBus applicationMessageBus()
  86.           {
  87.             final var executor = new ThreadPoolTaskExecutor();
  88.             executor.setWaitForTasksToCompleteOnShutdown(false);
  89.             executor.setThreadNamePrefix("messageBus-");
  90.             executor.setCorePoolSize(10);
  91.             executor.setMaxPoolSize(10);
  92.             executor.setQueueCapacity(10);
  93.             executor.afterPropertiesSet();
  94.             return new SimpleMessageBus(executor);
  95.           }
  96.       }

  97.     public static final String APPLICATION_MESSAGE_BUS_BEAN_NAME = "applicationMessageBus";

  98.     // Don't use Slf4j and its static logger - give Main a chance to initialize things
  99.     private final Logger log = LoggerFactory.getLogger(JavaFXSpringAnnotationApplication.class);

  100.     @Override @Nonnull
  101.     protected ConfigurableApplicationContext createApplicationContext()
  102.       {
  103.         final var mainClass = getClass();
  104.         final var componentClasses = new ArrayList<>(List.of(mainClass, Beans.class));

  105.         if (getClass().isAnnotationPresent(EnableMessageBus.class))
  106.           {
  107.             log.info("Detected @{}, enabling message bus", EnableMessageBus.class.getSimpleName());
  108.             componentClasses.add(MessageBusBeans.class);
  109.           }

  110.         log.info("Scanning beans from {}", mainClass);
  111.         return new AnnotationConfigApplicationContext(componentClasses.toArray(new Class<?>[0]));
  112.       }
  113.   }