/*
 * 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.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import com.ibm.icu.util.ULocale;

import ilog.views.gantt.model.table.IlvTableGanttModel;
import ilog.views.util.IlvProductUtil;
import ilog.views.util.convert.IlvConvert;
import ilog.views.util.convert.IlvConvertException;
import ilog.views.util.convert.IlvConverter;
import ilog.views.util.data.IlvBasicTableModelPropertyDescriptor;
import ilog.views.util.data.IlvTableModelMapper;
import ilog.views.util.data.IlvTableModelMappingException;
import ilog.views.util.text.IlvDateFormatFactory;


public class TableModelSample {

  private static TableModel activityModel = new DefaultTableModel(new Object[] {"Category", "ID", "StartTime", "EndTime", "ParentID"}, 0);;
  private static TableModel constraintModel = new DefaultTableModel(new Object[] {"Type", "FromActivityID", "ToActivityID"}, 0);
  
  public static void main(String[] args) {
    // This sample uses JViews Gantt features. When deploying an
    // application that includes this code, you need to be in possession
    // of a Perforce JViews Gantt Deployment license.
    IlvProductUtil.DeploymentLicenseRequired(
        IlvProductUtil.JViews_Gantt_Deployment);

    IlvTableGanttModel tableModel = new IlvTableGanttModel();
    IlvTableModelMapper activityMapper =
      IlvTableGanttModel.createActivityMapper(activityModel, 1, 1, 2, 3, 4);
    IlvConvert.addConverter(new IlvConverter() {
      private final DateFormat formatter =
        IlvDateFormatFactory.getDateInstance(DateFormat.SHORT, ULocale.US);
      Override
      public Object convert(Object value, Class toType) throws IlvConvertException {
        try {
          return formatter.parse((String)value);
        } catch (ParseException e) {
          throw new IlvConvertException("error");
        }
      }
      Override
      public Class[] fromTypes() {
        return new Class[] {String.class};
      }
      Override
      public Class[] toTypes() {
        return new Class[] {Date.class};
      }
    });     
    activityMapper.addPropertyDescriptor("OptionalCategory",
        new IlvBasicTableModelPropertyDescriptor("CATEGORY"), null);
    tableModel.setActivityMapper(activityMapper);
    IlvTableModelMapper constraintMapper =
      IlvTableGanttModel.createConstraintMapper(constraintModel, "FromActivityID",
                                                "ToActivityID", "Type",
                                                new Object[] {new Integer(1),
                                                              new Integer(2),
                                                              new Integer(4),
                                                              new Integer(3)});
    tableModel.setConstraintMapper(constraintMapper);
    try {
      tableModel.initializeMapping();
    } catch (IlvTableModelMappingException e) {
      // in case something went wrong
    }
  }
}