UserNotification.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.util.ui;

  28. import javax.annotation.Nonnull;
  29. import javax.annotation.concurrent.Immutable;
  30. import lombok.Getter;
  31. import lombok.RequiredArgsConstructor;
  32. import lombok.ToString;
  33. import static it.tidalwave.util.BundleUtilities.getMessage;
  34. import static lombok.AccessLevel.PROTECTED;

  35. /***********************************************************************************************************************
  36.  *
  37.  * @author  Fabrizio Giudici
  38.  *
  39.  **********************************************************************************************************************/
  40. @Immutable
  41. @RequiredArgsConstructor(access = PROTECTED) @ToString
  42. public class UserNotification
  43.   {
  44.     @Getter
  45.     protected final String text;

  46.     @Getter
  47.     protected final String caption;

  48.     /*******************************************************************************************************************
  49.      *
  50.      * Creates a notification with empty caption and text.
  51.      *
  52.      * @return               the notification
  53.      *
  54.      ******************************************************************************************************************/
  55.     @Nonnull
  56.     public static UserNotification notification()
  57.       {
  58.         return new UserNotification("", "");
  59.       }

  60.     /*******************************************************************************************************************
  61.      *
  62.      * Associates a caption to the notification.
  63.      *
  64.      * @param  caption       the caption
  65.      * @return               the notification
  66.      *
  67.      ******************************************************************************************************************/
  68.     @Nonnull
  69.     public UserNotification withCaption (@Nonnull final String caption)
  70.       {
  71.         return new UserNotification(text, caption);
  72.       }

  73.     /*******************************************************************************************************************
  74.      *
  75.      * Associates a caption to the notification, retrieved from a resource bundle.
  76.      *
  77.      * @param  bundleClass   the class where to search the resource bundle from
  78.      * @param  resourceName  the resource name of the caption in the bundle
  79.      * @param  params        some (optional) parameters to the resource
  80.      * @return               the notification
  81.      *
  82.      ******************************************************************************************************************/
  83.     @Nonnull
  84.     public UserNotification withCaption (@Nonnull final Class<?> bundleClass,
  85.                                          @Nonnull final String resourceName,
  86.                                          @Nonnull final Object ... params)
  87.       {
  88.         return new UserNotification(text, getMessage(bundleClass, resourceName, params));
  89.       }

  90.     /*******************************************************************************************************************
  91.      *
  92.      * Associates a text to the notification.
  93.      *
  94.      * @param  text          the text
  95.      * @return               the notification
  96.      *
  97.      ******************************************************************************************************************/
  98.     @Nonnull
  99.     public UserNotification withText (@Nonnull final String text)
  100.       {
  101.         return new UserNotification(text, caption);
  102.       }

  103.     /*******************************************************************************************************************
  104.      *
  105.      * Associates a text to the notification, retrieved from a resource bundle.
  106.      *
  107.      * @param  bundleClass   the class where to search the resource bundle from
  108.      * @param  resourceName  the resource name of the text in the bundle
  109.      * @param  params        some (optional) parameters to the resource
  110.      * @return               the notification
  111.      *
  112.      ******************************************************************************************************************/
  113.     @Nonnull
  114.     public UserNotification withText (@Nonnull final Class<?> bundleClass,
  115.                                       @Nonnull final String resourceName,
  116.                                       @Nonnull final Object ... params)
  117.       {
  118.         return new UserNotification(getMessage(bundleClass, resourceName, params), caption);
  119.       }
  120.   }