/*
 * Licensed Materials - Property of Perforce Software, Inc. 
 * © Copyright Perforce Software, Inc. 2014, 2021 
 * © Copyright IBM Corp. 2009, 2014
 * © Copyright ILOG 1996, 2009
 * All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:
 * The Software and Documentation were developed at private expense and
 * are "Commercial Items" as that term is defined at 48 CFR 2.101,
 * consisting of "Commercial Computer Software" and
 * "Commercial Computer Software Documentation", as such terms are
 * used in 48 CFR 12.212 or 48 CFR 227.7202-1 through 227.7202-4,
 * as applicable.
 */

package interactor;

import java.awt.event.KeyEvent;
import java.awt.event.ActionEvent;
import java.util.Locale;

import ilog.views.chart.IlvChartInteractor;
import ilog.views.chart.action.IlvChartAction;
import ilog.views.chart.action.IlvChartFitAction;
import ilog.views.chart.action.IlvChartZoomAction;
import ilog.views.util.IlvResourceUtil;

/**
 * An interactor to execute simple keyboard interactions implemented
 * by {@link IlvChartAction} objects. By default, this class
 * associates:
 * <ul>
 * <li>VK_Z keycode with a zoom in action (see {@link IlvChartZoomAction}),</li>
 * <li>VK_U keycode with a zoom out action (see {@link IlvChartZoomAction}),</li>
 * <li>VK_F keycode with a fit action (see {@link IlvChartFitAction}).</li>
 * </ul>
 * <p>
 * You can modify the default behavior or use your own action classes by
 * overriding the <code>getAction()</code> method.
 * <p>The registered name for this interactor is "ChartAction".
 */
public class IlvChartActionInteractor extends IlvChartInteractor
{
  // ============================ Metainformation ============================

  static {
    IlvChartInteractor.register("ChartAction", IlvChartActionInteractor.class);
  }

  /**
   * Returns a localized name for this interactor class.
   */
  public static String getLocalizedName(Locale locale)
  {
    return IlvResourceUtil.getBundle("messages",
                                     IlvChartActionInteractor.class,
                                     locale)
           .getString("IlvChartActionInteractor");
  }


  // ===================== Customization, Bean Properties =====================

  /**
   * The <code>actionCommand</code> string for <code>ActionEvent</code> events
   * coming from the interactor.
   */
  public static final String ACTION_COMMAND = "ActionInteractor";


  // ========================== Key event handling ==========================

  /**
   * Returns an <code>IlvChartAction</code> object corresponding to the
   * given event. May returns <code>null</code> if no action matches the
   * event.
   * <p>By default, VK_Z is associated with an <code>IlvChartZoomAction</code>
   * with a zoom factor of 2, VK_U is associated with an
   * <code>IlvChartZoomAction</code> with a zoom factor of .5, and VK_F is
   * associated with an <code>IlvChartFitAction</code>.
   * @see ilog.views.chart.action.IlvChartAction
   * @see ilog.views.chart.action.IlvChartZoomAction
   * @see ilog.views.chart.action.IlvChartFitAction
   */
  protected IlvChartAction getAction(KeyEvent event)
  {
    if (event.getKeyCode() == KeyEvent.VK_Z)
      return new IlvChartZoomAction("ZoomIn", 2.);
    else if (event.getKeyCode() == KeyEvent.VK_U)
      return new IlvChartZoomAction("ZoomOut", .5);
    else if (event.getKeyCode() == KeyEvent.VK_F)
      return new IlvChartFitAction("Fit");
    return null;
  }

  /**
   * Processes the key events. This method recovers the corresponding action
   * (invoking <code>getAction()</code>) and calls its
   * <code>actionPerformed</code> method.
   */
  Override
  public void processKeyEvent(KeyEvent event)
  {
    if (event.getID() == KeyEvent.KEY_PRESSED) {
      IlvChartAction action = getAction(event);
      if (action != null) {
        action.setChart(getChart());
        action.actionPerformed(new ActionEvent(this,
                                               ActionEvent.ACTION_PERFORMED,
                                               ACTION_COMMAND));
        if (isConsumeEvents())
          event.consume();
      }
    }
  }


  // ============================= Constructors =============================

  /**
   * Creates a new <code>IlvChartActionInteractor</code> object.
   */
  public IlvChartActionInteractor()
  {
    enableEvents(KeyEvent.KEY_EVENT_MASK);
  }

}