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

import java.text.NumberFormat;
import java.util.EventObject;

import ilog.views.gantt.IlvActivity;
import ilog.views.gantt.model.general.ActivityUserPropertyEvent;
import ilog.views.gantt.property.IlvFormattedNumberProperty;

/**
 * The class <code>PriorityProperty</code> is an adapter that allows the
 * priority property of an
 * {@link ilog.views.gantt.model.IlvPropertyHolderActivity} to be accessed
 * through the generic {@link ilog.views.gantt.property.IlvStringProperty}
 * interface.
 */
public class FormattedPriorityProperty extends IlvFormattedNumberProperty {

  // =========================================
  // Class Constants
  // =========================================

  /**
   * Identifies the activity's priority property. The value is "priority".
   */
  public static final String PRIORITY_PROPERTY = "priority";

  /**
   * The highest priority supported by this class. Numerically, this is the
   * smallest number.
   */
  public static final int HIGHEST_PRIORITY = 1;

  /**
   * The lowest priority supported by this class. Numerically, this is the
   * largest number.
   */
  public static final int LOWEST_PRIORITY = 10;

  /**
   * The default priority.
   */
  public static final int DEFAULT_PRIORITY = 5;

  // =========================================
  // Instance Construction and Initialization
  // =========================================

  /**
   * Constructs a new <code>PriorityProperty</code> with a default
   * <code>NumberFormat</code>.
   * 
   * @see NumberFormat#getInstance
   */
  public FormattedPriorityProperty() {
  }

  /**
   * Constructs a new <code>PriorityProperty</code> with the specified number
   * formatter.
   * 
   * @param formatter
   *          The number formatter.
   */
  public FormattedPriorityProperty(NumberFormat formatter) {
    super(formatter);
  }

  /**
   * Constructs a new <code>PriorityProperty</code> with a
   * <code>DecimalFormat</code> that uses the specified formatting string in the
   * default locale.
   */
  public FormattedPriorityProperty(String pattern) {
    super(pattern);
  }

  // =========================================
  // Accessing
  // =========================================

  /**
   * Returns the priority of the specified activity.
   * 
   * @param activity
   *          The activity.
   */
  Override
  protected Object getValueImpl(Object activity) {
    return PriorityProperty.getPriority((IlvActivity) activity);
  }

  /**
   * Sets the priority of the specified activity.
   * 
   * @param activity
   *          The activity.
   * @param priority
   *          The new priority for the activity.
   */
  Override
  protected void setValueImpl(Object activity, Object priority) {
    PriorityProperty.setPriority((IlvActivity) activity, (Number) priority);
  }

  /**
   * Returns the class of events that are triggered by an
   * <code>IlvPropertyHolderActivity</code> when its priority changes value.
   */
  Override
  public Class<?> getEventClass() {
    return ActivityUserPropertyEvent.class;
  }

  /**
   * Returns whether the specified event is a <em>changed</em> event for this
   * property. The default implementation returns <code>true</code> if and only
   * if <code>event</code> is the same or a subclass of {@link #getEventClass},
   * is also an {@link ilog.views.gantt.util.event.IlvPropertyEvent}, and
   * <code>((IlvPropertyEvent)event).isChangedEvent()</code> returns
   * <code>true</code>. This implementation is suitable for a property that
   * fires a specific <code>IlvPropertyEvent</code> subclass when its value
   * changes. Subclasses should override this method as necessary.
   */
  Override
  public boolean isPropertyChangedEvent(EventObject event) {
    if (!super.isPropertyChangedEvent(event))
      return false;
    return PRIORITY_PROPERTY.equals(((ActivityUserPropertyEvent) event).getPropertyName());
  }

}