ViewStateMachineImpl.java

/*
** Module   : ViewStateMachineImpl.java
** Abstract : ViewStateMachineImpl implements methods to change view's states. It is migrated
**            from the old administration java client with new features.
**
** Copyright (c) 2017, Golden Code Development Corporation.
**
** -#- -I- --Date-- ----------------Description----------------------
** 001 SBI 20170601 Created initial version.
*/
/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
** 
** Additional terms under GNU Affero GPL version 3 section 7:
** 
**   Under Section 7 of the GNU Affero GPL version 3, the following additional
**   terms apply to the works covered under the License.  These additional terms
**   are non-permissive additional terms allowed under Section 7 of the GNU
**   Affero GPL version 3 and may not be removed by you.
** 
**   0. Attribution Requirement.
** 
**     You must preserve all legal notices or author attributions in the covered
**     work or Appropriate Legal Notices displayed by works containing the covered
**     work.  You may not remove from the covered work any author or developer
**     credit already included within the covered work.
** 
**   1. No License To Use Trademarks.
** 
**     This license does not grant any license or rights to use the trademarks
**     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
**     of Golden Code Development Corporation. You are not authorized to use the
**     name Golden Code, FWD, or the names of any author or contributor, for
**     publicity purposes without written authorization.
** 
**   2. No Misrepresentation of Affiliation.
** 
**     You may not represent yourself as Golden Code Development Corporation or FWD.
** 
**     You may not represent yourself for publicity purposes as associated with
**     Golden Code Development Corporation, FWD, or any author or contributor to
**     the covered work, without written authorization.
** 
**   3. No Misrepresentation of Source or Origin.
** 
**     You may not represent the covered work as solely your work.  All modified
**     versions of the covered work must be marked in a reasonable way to make it
**     clear that the modified work is not originating from Golden Code Development
**     Corporation or FWD.  All modified versions must contain the notices of
**     attribution required in this license.
*/


package com.goldencode.p2j.admin.client.application.home;

import com.google.gwt.user.client.ui.HasEnabled;
import com.google.inject.Provider;


/**
 * ViewStateMachineImpl implements methods to change view's states.
 */
public class ViewStateMachineImpl implements ViewStateMachine
{
   /** The widgets which enabled states are controlled by this state machine */
   private final HasEnabled[] widgets;
   
   /**
    * Creates the instance of the view state machine.
    * 
    * @param    view
    *           The widgets provider
    */
   public ViewStateMachineImpl(Provider<HasEnabled[]> view)
   {
      this.widgets = view.get();
   }

   /**
    * Creates the instance of the view state machine.
    * 
    * @param    widgets
    *           The widgets under control
    */
   public ViewStateMachineImpl(HasEnabled[] widgets)
   {
      this.widgets = widgets;
   }

   /**
    * Sets the state of the components according to the encoded array.
    * 
    * @param    newState
    *           array of boolean states, one per component, in a predefined
    *           order. <code>true</code> means enabled.
    */
   @Override
   public void stateMachine(boolean[] newState) 
   {
      if (newState.length != widgets.length)
      {
         throw new RuntimeException("Invalid state machine state definition");
      }
      
      for (int i = 0; i < widgets.length; i++)
      {
         widgets[i].setEnabled(newState[i]);
      }
   }
   
   /**
    * Selectively sets the state of the components according to the encoded
    * array.
    * 
    * @param    which
    *           array of booleans acting as a mask,one boolean per component.
    *           <code>true</code> means component changes state as specified
    *           in newState array.
    * 
    * @param    newState
    *           array of boolean states, one per component, in a predefined
    *           order. <code>true</code> means enabled.
    */
   @Override
   public void stateMachine(boolean[] which, boolean[] newState) 
   {
      if (which.length    != widgets.length ||
          newState.length != widgets.length)
      {
         throw new RuntimeException("Invalid state machine state definition");
      }
      
      for (int i = 0; i < widgets.length; i++)
      {
         if (which[i])
         {
            widgets[i].setEnabled(newState[i]);
         }
      }
   }
   
   /**
    * Selectively sets the state of the components according to the encoded
    * array and returns the array of the saved current state. The saved state
    * can be used as input to the stateMachine(boolean[]) method call.
    * 
    * @param    which
    *           array of booleans acting as a mask,one boolean per component.
    *           <code>true</code> means component changes state as specified
    *           in newState array.
    * 
    * @param    newState
    *           array of boolean states, one per component, in a predefined
    *           order. <code>true</code> means enabled.
    * 
    * @return   saved state of all components
    */
   @Override
   public boolean[] stateMachineModal(boolean[] which, boolean[] newState) 
   {
      if (which.length    != widgets.length ||
          newState.length != widgets.length)
      {
         throw new RuntimeException("Invalid state machine state definition");
      }
      
      boolean[] saved = new boolean[widgets.length];
      
      for (int i = 0; i < widgets.length; i++)
      {
         saved[i] = widgets[i].isEnabled();
         
         if (which[i])
         {
            widgets[i].setEnabled(newState[i]);
         }
      }
      
      return saved;
   }

   /**
    * It initializes the current state.
    * 
    * @param    current
    *           The current initial state
    */
   @Override
   public void init(State current)
   {
      stateMachine(current.getState());
   }

   /**
    * It changes the current state according to the provided transition state and returns
    * the previous state.
    * 
    * @param    transition
    *           The provided transition state
    * 
    * @return   The previous state
    */
   @Override
   public State apply(TransitionState transition)
   {
      boolean[] which    = transition.getMask();
      boolean[] newState = transition.getState();
      
      boolean[] saved = stateMachineModal(which, newState);
      
      State previous = new State ()
      {
         @Override
         public boolean[] getState()
         {
            return saved;
         }
      };
      
      return previous;
   }

}