/*
 * 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 faces.dhtml;

import java.net.MalformedURLException;
import java.net.URL;

import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;

import ilog.views.faces.dhtml.event.FacesMenuActionEvent;
import ilog.views.gantt.IlvActivity;
import ilog.views.gantt.IlvGanttChart;
import ilog.views.gantt.IlvGanttModel;
import ilog.views.gantt.IlvHierarchyChart;
import ilog.views.gantt.model.IlvDefaultGanttModel;
import ilog.views.gantt.project.IlvGanttXMLDataSource;
import ilog.views.util.servlet.IlvMenuFactory;

public class GanttBean {

  private IlvGanttChart ganttChart;
  private IlvGanttModel ganttModel;
  private IlvMenuFactory menuFactory = new MenuFactory();

  private IlvGanttModel createDefaultModel() {
    IlvGanttXMLDataSource xmlDs = new IlvGanttXMLDataSource();
    FacesContext context = FacesContext.getCurrentInstance();
    URL url;
    try {
      url = context.getExternalContext().getResource("/data/sdxl.sdxl");
      xmlDs.setDataURL(url);
      IlvGanttModel model = new IlvDefaultGanttModel();
      xmlDs.read(model);
      return model;
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (Exception e1) {
      e1.printStackTrace();
    }
    return null;
  }

  public void setGanttModel(IlvGanttModel ganttModel) {
    this.ganttModel = ganttModel;
  }

  public IlvGanttModel getGanttModel() {
    if (ganttModel == null)
      ganttModel = createDefaultModel();
    return ganttModel;
  }

  public IlvGanttChart getGantt() {
    if (ganttChart == null) {
      ganttChart = new IlvGanttChart();
      ganttChart.setGanttModel(getGanttModel());
    }
    return ganttChart;
  }

  public void setGantt(IlvGanttChart gantt) {
    this.ganttChart = gantt;
  }

  public void expandAllRows(ValueChangeEvent event) {
    IlvActivity activity = (IlvActivity) event.getNewValue();
    expandAllRows(activity);
  }

  public void expandAllRows(FacesMenuActionEvent event) {
    IlvActivity activity = (IlvActivity) event.getObject();
    expandAllRows(activity);
  }

  public void expandAllRows(IlvActivity activity) {
    if (activity != null) {
      IlvHierarchyChart chart = getGantt();
      if (chart.isRowExpanded(activity)) {
        chart.collapseRow(activity);
      } else {
        chart.expandAllRows(activity);
      }
    }
  }

  public IlvMenuFactory getMenuFactory() {
    return menuFactory;
  }
}