/*
 * 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.Color;
import java.awt.Font;

import javax.swing.ImageIcon;

import ilog.views.IlvPoint;
import ilog.views.IlvRect;
import ilog.views.graphic.IlvGraphicSet;
import ilog.views.graphic.IlvIcon;
import ilog.views.graphic.IlvText;

/**
 * This custom graphic object is made of several objects. Some of methods of
 * this objects are bound with properties of the model nodes, as defined in the
 * CSS file.
 */
public class Graphic extends IlvGraphicSet {

  /**
   * Those images are used in the icon to represent the "color" state of the
   * object
   */
  private static final ImageIcon redBall = new ImageIcon(Graphic.class.getResource("ballRed.gif"));
  private static final ImageIcon greenBall = new ImageIcon(Graphic.class.getResource("ballGreen.gif"));
  private static final ImageIcon blueBall = new ImageIcon(Graphic.class.getResource("ballBlue.gif"));
  private static final ImageIcon blackBall = new ImageIcon(Graphic.class.getResource("ballBlack.gif"));

  /**
   * The "color" property of this object
   */
  private String color = "green";

  // The graphic objects
  /**
   * A main icon displaying an image according to the color state of the object
   */
  private IlvIcon icon;

  /**
   * A text used to display the name of the underlaying node of the model
   */
  private IlvText label;

  /**
   * A text displaying the position of the object according to the properties of
   * the node in the model
   */
  private IlvText position;

  /**
   * Creates the object and its several parts
   */
  public Graphic() {
    super();
    customize();
  }

  /**
   * Creates all the parts of the object
   */
  private void customize() {

    // creates a label on the top of the object
    label = new IlvText(new IlvPoint(0, 0), "");
    label.setForeground(Color.YELLOW);
    addObject(label, false);

    // creates a central icon
    icon = new IlvIcon(greenBall.getImage(), new IlvRect(0, 0, 18, 18));
    addObject(icon, false);

    // creates an other label under the icon
    position = new IlvText(new IlvPoint(0, 30), "");
    position.setFont(new Font("Arial", Font.PLAIN, 10));
    position.setForeground(Color.WHITE);
    addObject(position, false);
  }

  /**
   * Returns the value of the "color" property
   */
  public String getColor() {
    return color;
  }

  /**
   * Sets the new value of the "color" property and change the image of the icon
   * according to this new value
   * 
   * @param c
   *          The new color
   */
  public void setColor(String c) {
    if (c.equals(color) == false) {
      this.color = c;

      if (color.equals("green"))
        icon.setImage(greenBall.getImage());
      if (color.equals("red"))
        icon.setImage(redBall.getImage());
      if (color.equals("blue"))
        icon.setImage(blueBall.getImage());
      if (color.equals("black"))
        icon.setImage(blackBall.getImage());
    }
  }

  /**
   * Returns the text displayed in the "label" part
   */
  public String getLabel() {
    return label.getLabel();
  }

  /**
   * Sets the text that must be displayed in the "label" part
   * 
   * @param str
   */
  public void setLabel(String str) {
    label.setLabel(str);
  }

  /**
   * Returns the text displayed in the "position" part
   * 
   */
  public String getPosition() {
    return position.getLabel();
  }

  /**
   * Sets the new text to display in the "position" part
   * 
   * @param str
   *          The new position
   */
  public void setPosition(String str) {
    position.setLabel(str);
  }

}