/*
 * 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.
 */

import java.awt.event.MouseEvent;

import ilog.views.IlvGrapher;
import ilog.views.IlvGraphic;
import ilog.views.IlvObjectInteractor;
import ilog.views.IlvObjectInteractorContext;
import ilog.views.sdm.IlvSDMEngine;

/**
 * This is a simple interactor, bound with the nodes of the model. When the
 * mouse button is released on a node, this interactor processes the event and
 * changes some properties of the node.
 */
public class Interactor extends IlvObjectInteractor {

  /**
   * Callback when user clicks a graphic object
   */
  Override
  public boolean processEvent(IlvGraphic obj, java.awt.AWTEvent event, IlvObjectInteractorContext context) {

    if (event.getID() == MouseEvent.MOUSE_RELEASED && obj.getGraphicBag() instanceof IlvGrapher) {

      // find sdm engine
      IlvSDMEngine engine = IlvSDMEngine.getSDMEngine((IlvGrapher) obj.getGraphicBag());

      // find object model
      if (engine != null) {
        Object modelObject = engine.getObject(obj);
        // cast and do whatever is needed here
        if (modelObject instanceof Node) {
          Node node = (Node) modelObject;

          // get current node motion action
          String action = (String) node.getProperty("action");

          // node is turning then park it
          if (action.equals("turning")) {
            node.setProperty("action", "parking");
          }
          // node is parked, then unparked
          else if (action.equals("parked")) {
            node.setProperty("action", "unparking");
          }

        }

        // event has been processed
        return true;
      }
    }
    // event has not been processed
    return false;
  }

}