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.List;
  29. import java.util.Optional;
  30. import it.tidalwave.util.NotFoundException;

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

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

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

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

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

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

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