/*
 * Licensed Materials - Property of Rogue Wave Software, Inc. 
 * © Copyright Rogue Wave Software, Inc. 2014, 2015 
 * © 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 linkPorts;

import shared.ResourceUtils;
import ilog.cpl.css.IlpMutableStyleSheet;
import ilog.cpl.graphic.views.IlpViewsView;
import ilog.cpl.graphic.views.IlpViewsViewInteractor;
import ilog.cpl.network.action.toolbar.IlpNetworkInteractorAction;
import ilog.cpl.network.action.toolbar.IlpNetworkInteractorButton;
import ilog.cpl.service.IlpContext;

import ilog.tgo.interactor.IltEditLinkPortInteractor;

import ilog.views.IlvManagerView;

import java.awt.Image;
import java.awt.Insets;
import java.io.FileOutputStream;

import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.ImageIcon;

/**
 * This class models a bean that represents a button
 * to be added in the Network Component toolbar. 
 *
 * <p>The following steps are required to customize the Network
 * Component toolbar with custom buttons using cascading style
 * sheets:
 * <ul>
 * <li>Create your button class, as illustrated by this class, which
 *     contains a constructor that takes an <code>IlpViewsView</code>
 *     instance as argument.</li>
 * <li>Register this new class in the toolbar configuration, as follows:
 * <pre>
 * ToolBar {
 *   ...
 *   button[8]: @+EditPinButton;
 * }
 * #EditPinButton { 
 *   actionType: "EditPinButton";
 * }
 * </pre>
 *    The name of the custom button class is registered using property 
 *    <code>actionType</code>.
 *    The properties that exist in the button class can be customized as
 *    bean properties in the CSS file. For instance,
 * <pre> 
 * #EditPinButton { 
 *   actionType: "linkPorts.EditPinButton";
 *   freeMode: false;
 * }
 * </pre>
 * </li>
 * </ul>
 */
public class EditPinButton extends IlpNetworkInteractorButton {

  /**
   * Edit Link Port Interactor which stores the link ports that have
   * been changed in the link port configuration style sheet file.
   */
  static class EditLinkPortInteractor extends IltEditLinkPortInteractor {
    /**
     * Default constructor
     */
    public EditLinkPortInteractor() {
      super();
      this.setPermanent(true);
    }
    /**
     * This method is overriden to store the link port configuration in a file
     * that can be seen later on by the user.
     */
    protected void detachStyleSheet (IlvManagerView view) {
      // Store the existing configuration
      IlpMutableStyleSheet css = getStyleSheet(view);
      try {
        FileOutputStream stream = new FileOutputStream(Main.sampleLinkPortOutputConfigurationFile);
        stream.write(css.toString().getBytes());
        stream.flush();
        stream.close();
      } catch (Exception ex) {
        // Silent exception. Applets are not able to write the output file.
      }
      super.detachStyleSheet(view);
    }
  }

  /**
   * Action that encapsulates an edit link port interactor.
   */
  static class EditPinAction extends IlpNetworkInteractorAction {
    public EditPinAction (IlpViewsView view, AbstractButton button) {
      super(view,button, new EditLinkPortInteractor());
      // Disable object interactors
      IlpViewsViewInteractor ilpinter = getIlpInteractor();
      ilpinter.setUsingObjectInteractor(false);
      
      IlpContext context = view.getController().getContext();
      Image icon = context.getImageRepository().getImage(ResourceUtils.getString("icon.editPin"));
      if (icon != null)
        putValue(Action.SMALL_ICON, new ImageIcon(icon));
      putValue(Action.NAME, ResourceUtils.getString("label.editPin"));
    }
  }

  /**
   * Creates the button.
   * @param view The view on which to operate.
   * @documented
   */
  public EditPinButton (IlpViewsView view) {
    setAction(new EditPinAction(view,this));
    setText(null);
    setIcon((ImageIcon)getAction().getValue(Action.SMALL_ICON));
    setToolTipText((String)getAction().getValue(Action.NAME));
    setMargin(new Insets(0,0,0,0));
    setName("EditPin");
  }

  /**
   * Property that can be customized via CSS and which specifies if
   * the <code>IltEditLinkPortInteractor</code> will behave according
   * to the free mode or the existing link ports mode. 
   *
   * <p>In the first case, link ports can be added or removed from a 
   * node by clicking on the node base. When you click on a link port,
   * it is removed. When you click on the node base, a link port is
   * added at the given position.
   *
   * <p>In the second case, you can active or deactivate link ports
   * that have already been created in the <code>IltLinkPort</code>
   * enumeration.
   *
   * @param v <code>true</code> if free mode is to be used
   *          by the interactor.
   */  
  public void setFreeMode(boolean v) {
    if (v)
      ((IltEditLinkPortInteractor)getIlvInteractor()).setMode(IltEditLinkPortInteractor.FREE_LINK_PORT_MODE);
    else
      ((IltEditLinkPortInteractor)getIlvInteractor()).setMode(IltEditLinkPortInteractor.EXISTING_LINK_PORT_MODE);
  }

  /**
   * Property that can be customized via CSS and which specifies if
   * the <code>IltEditLinkPortInteractor</code> will behave according
   * to the free mode or the existing link ports mode. 
   *
   * <p>In the first case, link ports can be added or removed from a 
   * node by clicking on the node base. When you click on a link port,
   * it is removed. When you click on the node base, a link port is
   * added at the given position.
   *
   * <p>In the second case, you can active or deactivate link ports
   * that have already been created in the <code>IltLinkPort</code>
   * enumeration.
   *
   * @return <code>true</code> if the interactor is customized
   *         with <code>IltEditLinkPortInteractor.FREE_LINK_PORT_MODE</code>
   */  
  public boolean isFreeMode() {
    int m = ((IltEditLinkPortInteractor)getIlvInteractor()).getMode();
    return (m == IltEditLinkPortInteractor.FREE_LINK_PORT_MODE);    
  }
}