LenientAWTEventProcessor.java

  1. package swingtree.threading;

  2. import org.slf4j.Logger;

  3. import java.util.function.Supplier;

  4. /**
  5.  *  This {@link EventProcessor} implementation is called lenient because it does not
  6.  *  throw any exceptions when a task is not registered by the UI thread.
  7.  *  When this processor is installed
  8.  *  all the event handling will typically be done by the UI thead (the AWT event dispatch thread, short EDT)
  9.  *  simply because all the Swing component events will be executed on the EDT,
  10.  *  however this will not be enforced. <br>
  11.  *  Note that this {@link EventProcessor} will effectively couple the UI
  12.  *  to the business logic of you application with respect to the execution model. <br>
  13.  *  This is not a big problem for simple demo applications,
  14.  *  however, when building larger products it is important to not use the UI thread for
  15.  *  business logic so that the UI stays responsive.
  16.  *  This is why you should prefer the usage of the {@link DecoupledEventProcessor}
  17.  *  alongside the registration of a worker thread through
  18.  *  {@link DecoupledEventProcessor#join()} at {@link EventProcessor#DECOUPLED}.
  19.  *  <br><br>
  20.  *  See {@link swingtree.UI#use(EventProcessor, Supplier)} for more information about the
  21.  *  usage of the {@link EventProcessor} interface.
  22.  */
  23. final class LenientAWTEventProcessor extends BasicSingleThreadedEventProcessor
  24. {
  25.     private static final Logger log = org.slf4j.LoggerFactory.getLogger(LenientAWTEventProcessor.class);

  26.     @Override protected void _tryRunning( Runnable runnable, boolean expectedToBeInvokedFromUIThread ) {
  27.         try {
  28.             runnable.run();
  29.         } catch (Exception e) {
  30.             // If a user wants better logging, they can do it through SLF4J or implement their own EventProcessor.
  31.             log.error("An exception occurred while running a task in the UI thread!", e);
  32.         }
  33.     }
  34. }