All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.helger.bootstrap3.table.BootstrapTableForm Maven / Gradle / Ivy

There is a newer version: 2.0.2
Show newest version
/**
 * Copyright (C) 2014 Philip Helger (www.helger.com)
 * philip[at]helger[dot]com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.helger.bootstrap3.table;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import com.helger.commons.annotations.OverrideOnDemand;
import com.helger.commons.microdom.IMicroElement;
import com.helger.html.hc.IHCControl;
import com.helger.html.hc.IHCHasFocus;
import com.helger.html.hc.IHCNode;
import com.helger.html.hc.IHCNodeBuilder;
import com.helger.html.hc.conversion.IHCConversionSettingsToNode;
import com.helger.html.hc.html.HCCol;
import com.helger.html.hc.html.HCRow;
import com.helger.validation.error.IErrorList;
import com.helger.webctrls.custom.IFormLabel;
import com.helger.webctrls.custom.table.IHCTableForm;
import com.helger.webscopes.mgr.WebScopeManager;

public class BootstrapTableForm extends AbstractBootstrapTable  implements IHCTableForm 
{
  private static final String REQUEST_ATTR_FIRST_FOCUSABLE = "Bootstrap3TableForm$FirstFocusable";

  private boolean m_bFocusHandlingEnabled = true;
  private boolean m_bSetAutoFocus = false;
  private IHCHasFocus  m_aFirstFocusable;

  public BootstrapTableForm (@Nullable final HCCol... aWidths)
  {
    super (aWidths);
    setCondensed (true);
    setStriped (true);
  }

  @Nonnull
  public BootstrapTableForm setFocusHandlingEnabled (final boolean bFocusHandlingEnabled)
  {
    m_bFocusHandlingEnabled = bFocusHandlingEnabled;
    return this;
  }

  public boolean isFocusHandlingEnabled ()
  {
    return m_bFocusHandlingEnabled;
  }

  @OverrideOnDemand
  protected void focusNode (@Nonnull final IHCHasFocus  aCtrl)
  {
    aCtrl.setFocused (true);
    if (aCtrl instanceof IHCControl )
    {
      // Ensure that an ID is present
      ((IHCControl ) aCtrl).ensureID ();
    }
  }

  private void _handleFocus (@Nonnull final Iterable  aCtrls, final boolean bHasErrors)
  {
    if (isFocusHandlingEnabled ())
    {
      // Set focus on first element with error
      if (bHasErrors && !m_bSetAutoFocus)
        for (final IHCNode aCtrl : aCtrls)
          if (aCtrl instanceof IHCHasFocus )
          {
            focusNode ((IHCHasFocus ) aCtrl);
            m_bSetAutoFocus = true;
            break;
          }

      // Find first focusable control
      if (m_aFirstFocusable == null)
        for (final IHCNode aCtrl : aCtrls)
          if (aCtrl instanceof IHCHasFocus )
          {
            m_aFirstFocusable = (IHCHasFocus ) aCtrl;
            break;
          }
    }
  }

  @Nonnull
  public BootstrapTableFormItemRow createItemRow ()
  {
    final BootstrapTableFormItemRow ret = new BootstrapTableFormItemRow (false, getColumnCount () > 2)
    {
      @Override
      protected void modifyControls (@Nonnull final Iterable  aCtrls, final boolean bHasErrors)
      {
        _handleFocus (aCtrls, bHasErrors);
        super.modifyControls (aCtrls, bHasErrors);
      }
    };
    addBodyRow (ret);
    return ret;
  }

  @Nonnull
  @Deprecated
  public HCRow addItemRow (@Nullable final IFormLabel aLabel, @Nullable final String sValue)
  {
    return createItemRow ().setLabel (aLabel).setCtrl (sValue);
  }

  @Nonnull
  @Deprecated
  public HCRow addItemRow (@Nullable final IFormLabel aLabel, @Nullable final IHCNodeBuilder aCtrlBuilder)
  {
    return createItemRow ().setLabel (aLabel).setCtrl (aCtrlBuilder);
  }

  @Nonnull
  @Deprecated
  public HCRow addItemRow (@Nullable final IFormLabel aLabel, @Nullable final IHCNode aCtrl)
  {
    return createItemRow ().setLabel (aLabel).setCtrl (aCtrl);
  }

  @Nonnull
  @Deprecated
  public HCRow addItemRow (@Nullable final IFormLabel aLabel,
                           @Nullable final IHCNodeBuilder aCtrlBuilder,
                           @Nullable final IErrorList aFormErrors)
  {
    return createItemRow ().setLabel (aLabel).setCtrl (aCtrlBuilder).setErrorList (aFormErrors);
  }

  @Nonnull
  @Deprecated
  public HCRow addItemRow (@Nullable final IFormLabel aLabel,
                           @Nullable final IHCNode aCtrl,
                           @Nullable final IErrorList aFormErrors)
  {
    return createItemRow ().setLabel (aLabel).setCtrl (aCtrl).setErrorList (aFormErrors);
  }

  @Nonnull
  @Deprecated
  public HCRow addItemRow (@Nullable final IFormLabel aLabel, @Nullable final Iterable  aCtrls)
  {
    return createItemRow ().setLabel (aLabel).setCtrl (aCtrls);
  }

  @Nonnull
  @Deprecated
  public HCRow addItemRow (@Nullable final IFormLabel aLabel,
                           @Nullable final Iterable  aCtrls,
                           @Nullable final IErrorList aFormErrors)
  {
    return createItemRow ().setLabel (aLabel).setCtrl (aCtrls).setErrorList (aFormErrors);
  }

  @Nonnull
  @Deprecated
  public HCRow addItemRowWithNote (@Nullable final IFormLabel aLabel,
                                   @Nullable final String sText,
                                   @Nullable final IHCNode aNote)
  {
    return createItemRow ().setLabel (aLabel).setCtrl (sText).setNote (aNote);
  }

  @Deprecated
  @Nonnull
  public HCRow addItemRowWithNote (@Nullable final IFormLabel aLabel,
                                   @Nullable final String sText,
                                   @Nullable final IHCNode aNote,
                                   @Nullable final IErrorList aFormErrors)
  {
    return createItemRow ().setLabel (aLabel).setCtrl (sText).setErrorList (aFormErrors).setNote (aNote);
  }

  @Deprecated
  @Nonnull
  public HCRow addItemRowWithNote (@Nullable final IFormLabel aLabel,
                                   @Nullable final IHCNodeBuilder aCtrlBuilder,
                                   @Nullable final IHCNode aNote)
  {
    return createItemRow ().setLabel (aLabel).setCtrl (aCtrlBuilder).setNote (aNote);
  }

  @Deprecated
  @Nonnull
  public HCRow addItemRowWithNote (@Nullable final IFormLabel aLabel,
                                   @Nullable final IHCNode aCtrl,
                                   @Nullable final IHCNode aNote)
  {
    return createItemRow ().setLabel (aLabel).setCtrl (aCtrl).setNote (aNote);
  }

  @Deprecated
  @Nonnull
  public HCRow addItemRowWithNote (@Nullable final IFormLabel aLabel,
                                   @Nullable final Iterable  aCtrls,
                                   @Nullable final IHCNode aNote)
  {
    return createItemRow ().setLabel (aLabel).setCtrl (aCtrls).setNote (aNote);
  }

  @Deprecated
  @Nonnull
  public HCRow addItemRowWithNote (@Nullable final IFormLabel aLabel,
                                   @Nullable final IHCNodeBuilder aCtrlBuilder,
                                   @Nullable final IHCNode aNote,
                                   @Nullable final IErrorList aFormErrors)
  {
    return createItemRow ().setLabel (aLabel).setCtrl (aCtrlBuilder).setErrorList (aFormErrors).setNote (aNote);
  }

  @Deprecated
  @Nonnull
  public HCRow addItemRowWithNote (@Nullable final IFormLabel aLabel,
                                   @Nullable final IHCNode aCtrl,
                                   @Nullable final IHCNode aNote,
                                   @Nullable final IErrorList aFormErrors)
  {
    return createItemRow ().setLabel (aLabel).setCtrl (aCtrl).setErrorList (aFormErrors).setNote (aNote);
  }

  @Deprecated
  @Nonnull
  public HCRow addItemRowWithNote (@Nullable final IFormLabel aLabel,
                                   @Nullable final Iterable  aCtrls,
                                   @Nullable final IHCNode aNote,
                                   @Nullable final IErrorList aFormErrors)
  {
    return createItemRow ().setLabel (aLabel).setCtrl (aCtrls).setErrorList (aFormErrors).setNote (aNote);
  }

  @Override
  protected void applyProperties (final IMicroElement aDivElement, final IHCConversionSettingsToNode aConversionSettings)
  {
    if (isFocusHandlingEnabled () && !m_bSetAutoFocus && m_aFirstFocusable != null)
    {
      // No focus has yet be set
      // Try to focus the first control (if available), but do it only once per
      // request because the cursor can only be on one control at a time :)
      if (!WebScopeManager.getRequestScope ().getAndSetAttributeFlag (REQUEST_ATTR_FIRST_FOCUSABLE))
        focusNode (m_aFirstFocusable);
    }
    super.applyProperties (aDivElement, aConversionSettings);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy