/*
 * 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>CompletionProperty</code> is an adapter that allows the
 * completion property of an
 * {@link ilog.views.gantt.model.IlvPropertyHolderActivity} to be accessed
 * through the generic {@link ilog.views.gantt.property.IlvStringProperty}
 * interface.
 */
public class FormattedCompletionProperty extends IlvFormattedNumberProperty {

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

  /**
   * Identifies the activity's completion property. The value is "completed".
   */
  public static final String COMPLETION_PROPERTY = "completed";

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

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

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

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

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

  /**
   * Returns the completion proportion of the specified activity.
   * 
   * @param activity
   *          The activity.
   */
  Override
  protected Object getValueImpl(Object activity) {
    return CompletionProperty.getCompletion((IlvActivity) activity);
  }

  /**
   * Sets the completion proportion of the specified activity.
   * 
   * @param activity
   *          The activity.
   * @param completion
   *          The new completion proportion for the activity.
   */
  Override
  protected void setValueImpl(Object activity, Object completion) {
    CompletionProperty.setCompletion((IlvActivity) activity, (Number) completion);
  }

  /**
   * Returns the class of events that are triggered by an
   * <code>IlvPropertyHolderActivity</code> when its completion 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 COMPLETION_PROPERTY.equals(((ActivityUserPropertyEvent) event).getPropertyName());
  }

}