# EnumRadioGroup.java

    /*
    ** Module   : EnumRadioGroup.java
    ** Abstract : EnumRadioGroup is a GWT radio group control initialized by enumeration values.
    **
    ** 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.widget;

    import java.text.ParseException;
    import java.util.Map;

    import org.gwtbootstrap3.client.ui.InlineRadio;
    import org.gwtbootstrap3.client.ui.Radio;
    import org.gwtbootstrap3.client.ui.base.RadioGroupBase;

    import com.google.gwt.text.shared.Parser;
    import com.google.gwt.uibinder.client.UiConstructor;
    import com.google.gwt.user.client.ui.Widget;

    /**
     * GWT radio group control that can be initialized by the target enumeration. It displays
     * radio button labels from a translation map if it is provided or takes its labels from
     * enumeration values.
     *
     * @param    <E>
     *           Enumeration that implements EnumCreator interface.
     */
    public class EnumRadioGroup<E extends EnumCreator> extends RadioGroupBase<E>
    {
       /**
        * Radio group styles
        */
       public static enum RadioStyles
       {
          /** Vertically stacked radio buttons */
          RADIO,

          /** In-line horizontal radio buttons */
          RADIO_INLINE
       }

       /**
        * The auto initialization flag that means how radio buttons are created for this widget. If
        * its value is true, then radio buttons are created from the enumeration values, otherwise
        * radio buttons are provided by the UI xml descriptor.
        */
       private boolean init = true;

       /** The enumeration creator */
       private final E creator;

       /** The layout style used to place radio buttons */
       private final RadioStyles rstyle;

       /** The translation map that holds entries of the enumeration name and its presentation value */
       private final Map<String, String> translationMap;

       /**
        * Radio Group UI constructor.
        *
        * @param    name
        *           A radio group common name.
        * @param    creator
        *           A labels/buttons creator
        * @param    rstyle
        *           Radio group style tha can be an in-line style or a vertical style
        * @param    init
        *           If its value is true, then radio buttons are created with the help of
        *           the provided creator. The false value means that the values are provided by
        *           the UI xml descriptor.
        */
       @UiConstructor
       public EnumRadioGroup(String name, E creator, RadioStyles rstyle, boolean init)
       {
          this(name, creator, rstyle, init, null);
       }

       /**
        * Radio Group UI constructor.
        *
        * @param    name
        *           A radio group common name.
        * @param    creator
        *           A labels/buttons creator
        * @param    rstyle
        *           Radio group style tha can be an in-line style or a vertical style
        * @param    init
        *           If its value is true, then radio buttons are created with the help of
        *           the provided creator. The false value means that the values are provided by
        *           the UI xml descriptor.
        * @param    translationMap
        *           The translation map that maps the target enumeration type to its labels.
        */
       public EnumRadioGroup(String name,
                             E creator,
                             RadioStyles rstyle,
                             boolean init,
                             Map<String, String> translationMap)
       {
          super(name, getParser(creator));
          this.creator = creator;
          this.rstyle = rstyle;
          this.init = init;
          this.translationMap = translationMap;

          init();
       }

       /**
        * Enable a radio button by its enumeration value.
        *
        * @param    value
        *           The target value that represents the button that should be enabled or disabled.
        * @param    enabled
        *           The enable state for the target radio button.
        */
       public void setEnabled(Enum value, boolean enabled)
       {
          Widget radio = getWidget(value.ordinal());
          if (radio instanceof Radio)
          {
             ((Radio) radio).setEnabled(enabled);
          }
       }

       /**
        * Creates radio buttons from the given enumeration values.
        */
       private void init()
       {
          if (init)
          {
             for(Enum value : creator.getValues())
             {
                String label = null;
                if (translationMap != null)
                {
                   label = translationMap.get(value.name());
                }
                Radio radioButton;
                switch(rstyle)
                {
                   case RADIO:
                      radioButton = new Radio(getName(), (label != null) ? label : value.toString());
                      break;
                   case RADIO_INLINE:
                   default:
                      radioButton = new InlineRadio(getName(), (label != null) ? label : value.toString());
                }

                radioButton.setFormValue(value.name());

                add(radioButton);
             }
             init = false;
          }
       }

       /**
        * Creates the enumeration parser.
        * @param    <E>
        *           The parameter type of EnumCreator
        * @param    creator
        *           The enumeration creator
        *
        * @return   The object that can parse text and return its enumeration value.
        */
       private static <E extends EnumCreator> Parser<E> getParser(E creator)
       {
          /**
           * An object that can parse text and return its enumeration value.
           */
          return new Parser<E>()
          {
             @Override
             public E parse(CharSequence text) throws ParseException
             {
                if (text == null)
                {
                   throw new ParseException("The null input value was encountered.", 0);
                }

                return (E) creator.createFrom(text.toString());
             }
          };
       }
    }