DefaultUserAction.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.core.role.impl;

  27. import jakarta.annotation.Nonnull;
  28. import java.util.Collection;
  29. import it.tidalwave.util.As;
  30. import it.tidalwave.util.Callback;
  31. import it.tidalwave.ui.core.BoundProperty;
  32. import it.tidalwave.ui.core.role.UserAction;
  33. import lombok.Getter;
  34. import lombok.RequiredArgsConstructor;
  35. import lombok.experimental.Accessors;
  36. import lombok.experimental.Delegate;
  37. import lombok.extern.slf4j.Slf4j;
  38. import static it.tidalwave.ui.core.role.Displayable._Displayable_;

  39. /***************************************************************************************************************************************************************
  40.  *
  41.  * @author  Fabrizio Giudici
  42.  *
  43.  **************************************************************************************************************************************************************/
  44. @RequiredArgsConstructor(staticName = "withCallback") @Slf4j @SuppressWarnings("this-escape")
  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 As as;

  51.     @Nonnull
  52.     private final Callback callback;

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

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

  71.     @Override
  72.     public void actionPerformed() // FIXME: change with composition
  73.       {
  74.         try
  75.           {
  76.             callback.call();
  77.           }
  78.         catch (Throwable e)
  79.           {
  80.             log.error("", e);
  81.           }
  82.       }

  83.     /***********************************************************************************************************************************************************
  84.      * {@inheritDoc}
  85.      **********************************************************************************************************************************************************/
  86.     @Override @Nonnull
  87.     public String toString ()
  88.       {
  89.         final var id = "@" + Integer.toHexString(System.identityHashCode(this));
  90.         var details = id;

  91.         try
  92.           {
  93.             details = this.maybeAs(_Displayable_)
  94.                           .map(d -> String.format("[\"%s\"]", d.getDisplayName()))
  95.                           .orElse(id);
  96.           }
  97.         catch (RuntimeException e)
  98.           {
  99.             details += ",broken";
  100.             log.error("In DefaultDisplayable.toString()", e);
  101.           }

  102.         return String.format("DefaultUserAction%s", details);
  103.       }
  104.   }