/*
 * 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.
 */
package database;

import java.awt.event.ActionEvent;
import java.util.Date;

import ilog.views.gantt.IlvDuration;
import ilog.views.gantt.IlvGanttModel;
import ilog.views.gantt.IlvHierarchyChart;
import ilog.views.gantt.IlvTimeInterval;
import ilog.views.gantt.IlvTimeUtil;
import ilog.views.gantt.action.IlvZoomToFitAction;
import xml.XMLGanttActions;

/**
 * This class groups a number of <code>IlvAction</code>(s) and utility methods
 * that are used in the database demos.
 */
public final class DBGanttActions {

  /**
   * Prevent instance creation.
   */
  private DBGanttActions() {
  }

  /**
   * Creates a new data model connected to a simulated database, where the
   * database is constructed from the specified data model.
   */
  public static DBROGanttModel createDBGanttModel(IlvGanttModel model) {
    GanttDBRO db = new GanttModelDBROWrapper(model);
    return new DBROGanttModel(db);
  }

  /**
   * An action that opens an XML project file and creates a simulated database.
   */
  public static class OpenXMLAsDatabaseAction extends XMLGanttActions.OpenXMLAction {

    public OpenXMLAsDatabaseAction(IlvHierarchyChart chart) {
      super(chart);
      getZoomToFitAction().setIntervalPolicy(new IlvZoomToFitAction.RootActivityIntervalPolicy());
    }

    /**
     * This function is called when the user wants to open an XML file.
     */
    Override
    public void actionPerformed(ActionEvent event) {
      IlvGanttModel model = readXMLFile();
      // Change the Gantt model of the Gantt chart.
      if (model != null) {
        IlvHierarchyChart chart = getChart();

        // Set the chart to view the first 5% of the schedule before we bind
        // the data model to the chart.
        chart.setGanttModel(null);
        IlvTimeInterval interval = getZoomToFitAction().getIntervalPolicy().getInterval(model);
        IlvDuration duration = interval.getDuration().divide(20);
        Date start = IlvTimeUtil.subtract(interval.getStart(), duration.divide(10));
        chart.setVisibleInterval(start, duration);

        chart.setGanttModel(createDBGanttModel(model));
      }
    }

  }

}