DefaultUserAction.java

  1. /*
  2.  * *********************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2021 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
  12.  * the License. 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
  17.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  18.  * specific language governing permissions and limitations under the License.
  19.  *
  20.  * *********************************************************************************************************************
  21.  *
  22.  * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
  23.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  24.  *
  25.  * *********************************************************************************************************************
  26.  */
  27. package it.tidalwave.role.ui.impl;

  28. import javax.annotation.Nonnull;
  29. import java.util.Collection;
  30. import it.tidalwave.util.Callback;
  31. import it.tidalwave.util.spi.AsSupport;
  32. import it.tidalwave.role.ui.BoundProperty;
  33. import it.tidalwave.role.ui.UserAction;
  34. import lombok.Getter;
  35. import lombok.RequiredArgsConstructor;
  36. import lombok.experimental.Accessors;
  37. import lombok.experimental.Delegate;
  38. import lombok.extern.slf4j.Slf4j;

  39. /***********************************************************************************************************************
  40.  *
  41.  * @author  Fabrizio Giudici
  42.  *
  43.  **********************************************************************************************************************/
  44. @RequiredArgsConstructor(staticName = "withCallback") @Slf4j
  45. public class DefaultUserAction implements UserAction
  46.   {
  47.     @Getter @Accessors(fluent = true)
  48.     private final BoundProperty<Boolean> enabled = new BoundProperty<>(true);

  49.     @Delegate @Nonnull
  50.     private final AsSupport asSupport;

  51.     @Nonnull
  52.     private final Callback callback;

  53.     /*******************************************************************************************************************
  54.      *
  55.      * @since 3.2-ALPHA-1 (was previously in {@code UserAction8}
  56.      * @since  3.2-ALPHA-3 (refactored)
  57.      *
  58.      ******************************************************************************************************************/
  59.     public DefaultUserAction (@Nonnull final Callback callback, @Nonnull final Collection<Object> roles)
  60.       {
  61.         this.callback = callback;
  62.         this.asSupport = new AsSupport(this, roles);
  63.       }

  64.     /*******************************************************************************************************************
  65.      *
  66.      * @since 3.2-ALPHA-1 (was previously in {@code UserAction8}
  67.      * @since  3.2-ALPHA-3 (refactored)
  68.      *
  69.      ******************************************************************************************************************/
  70.     @Nonnull
  71.     public DefaultUserAction withRoles (@Nonnull final Collection<Object> roles)
  72.       {
  73.         return new DefaultUserAction(callback, roles);
  74.       }

  75.     @Override
  76.     public void actionPerformed() // FIXME: change with composition
  77.       {
  78.         try
  79.           {
  80.             callback.call();
  81.           }
  82.         catch (Throwable e)
  83.           {
  84.             log.error("", e);
  85.           }
  86.       }
  87.   }