/*
 * 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.InputEvent;
import java.awt.event.MouseEvent;

import ilog.views.IlvApplyObject;
import ilog.views.IlvDrawSelection;
import ilog.views.IlvGraphic;
import ilog.views.IlvGraphicBag;
import ilog.views.IlvObjectInteractorContext;
import ilog.views.IlvReshapeSelection;

/**
 * An object interactor for the marker selection.
 * When clicking on the selection handle, the marker changed its type.
 */
public class MyMarkerEdition
  extends IlvReshapeSelection
{ 
  /** 
   * Creates the new selection.
   */
  public MyMarkerEdition()
  {
    super();
  }

  /**
   * Processes the button down events.
   * @return <code>true</code> if the event has been processed,
   *         <code>false</code> otherwise.
   */
  Override
  protected boolean handleButtonDown(IlvDrawSelection sel, MouseEvent event,
                                     IlvObjectInteractorContext context)
  {
    if ((event.getModifiersEx() & InputEvent.BUTTON2_DOWN_MASK) != 0 ||
        (event.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK) != 0)
      return true;

    MyMarkerSelection msel = (MyMarkerSelection)sel;
    MyMarker marker = (MyMarker)msel.getObject();

    // even though the object interactor is on the selection, it
    // modifies the selected object, not the selection
    final int tp = (marker.getType() >= 512 ? 1 : marker.getType() * 2);
    IlvGraphicBag bag = marker.getGraphicBag();
    if (bag == null)
      marker.setType(tp);
    else
      bag.applyToObject(marker, new IlvApplyObject() {
          Override
          public void apply(IlvGraphic g, Object arg) {
            ((MyMarker)g).setType(tp);
          }
        }, null, true);
    return true;
  }
}