UserActionProvider.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;

  27. import jakarta.annotation.Nonnull;
  28. import java.util.Collection;
  29. import java.util.List;
  30. import java.util.Optional;
  31. import it.tidalwave.util.NotFoundException;

  32. /***************************************************************************************************************************************************************
  33.  *
  34.  * A role that provides {@link UserAction}s.
  35.  *
  36.  * @since   2.0-ALPHA-1
  37.  * @stereotype role
  38.  *
  39.  * @author  Fabrizio Giudici
  40.  *
  41.  **************************************************************************************************************************************************************/
  42. public interface UserActionProvider
  43.   {
  44.     public static final Class<UserActionProvider> _UserActionProvider_ = UserActionProvider.class;

  45.     /***********************************************************************************************************************************************************
  46.      * Returns a collection of {@link UserAction}s.
  47.      *
  48.      * @return  a collection of actions
  49.      **********************************************************************************************************************************************************/
  50.     @Nonnull
  51.     public Collection<? extends UserAction> getActions();

  52.     /***********************************************************************************************************************************************************
  53.      * Returns the default action, if available.
  54.      *
  55.      *
  56.      * @return                      the default action
  57.      * @throws  NotFoundException   if there's no default action
  58.      * @deprecated                  Use {@link #getOptionalDefaultAction()}
  59.      **********************************************************************************************************************************************************/
  60.     @Nonnull @Deprecated
  61.     public UserAction getDefaultAction()
  62.       throws NotFoundException;

  63.     /***********************************************************************************************************************************************************
  64.      * Returns the default action, if available.
  65.      *
  66.      * @since   3.1-ALPHA-2
  67.      * @return                      the default action
  68.      **********************************************************************************************************************************************************/
  69.     @Nonnull
  70.     public default Optional<UserAction> getOptionalDefaultAction()
  71.       {
  72.         try
  73.           {
  74.             return Optional.of(getDefaultAction());
  75.           }
  76.         catch (NotFoundException e)
  77.           {
  78.             return Optional.empty();
  79.           }
  80.       }

  81.     /***********************************************************************************************************************************************************
  82.      * Factory method which creates an instance out of an array of {@link UserAction}s. The first one is considered the
  83.      * default action.
  84.      *
  85.      * @since   3.1-ALPHA-2
  86.      * @param   actions     the actions
  87.      * @return              the {@code UserActionProvider}
  88.      **********************************************************************************************************************************************************/
  89.     @Nonnull
  90.     public static UserActionProvider of (@Nonnull final UserAction ... actions)
  91.       {
  92.         return new UserActionProvider()
  93.           {
  94.             @Override @Nonnull
  95.             public Collection<? extends UserAction> getActions()
  96.               {
  97.                 return List.of(actions);
  98.               }

  99.             @Override @Nonnull
  100.             public UserAction getDefaultAction()
  101.               throws NotFoundException
  102.               {
  103.                 if (actions.length == 0)
  104.                   {
  105.                     throw new NotFoundException();
  106.                   }

  107.                 return actions[0];
  108.               }
  109.           };
  110.       }
  111.   }