/*
 * 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 nonlinearTimeScale;

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

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JTabbedPane;

import ganttChart.GanttExample;
import ilog.views.gantt.IlvActivity;
import ilog.views.gantt.IlvDuration;
import ilog.views.gantt.IlvHierarchyChart;
import ilog.views.gantt.IlvTimeUtil;
import ilog.views.gantt.graphic.IlvLinearTimeConverter;
import shared.swing.CustomizerPanel;
import shared.swing.ExampleFrame;

/**
 * This is an extension to the Gantt Example that illustrates how a non-linear
 * time converter can be used to display weekend days at a shorter width than
 * days during the work week.
 */
public class NonLinearTimeScaleExample extends GanttExample {

  /**
   * The item to select the default linear time converter.
   */
  private static final String LINEAR_TC = "Default";

  /**
   * The item to select the non-linear time converter.
   */
  private static final String WORKING_DAYS_TC = "Working Days";

  /**
   * Creates and returns a customizer for the time converter.
   *
   * @return The time converter customizer.
   */
  protected JComponent createTimeConverterCustomizer() {
    CustomizerPanel panel = new CustomizerPanel();

    final JComboBox<String> tcCombo = new JComboBox<String>();
    tcCombo.addItem(LINEAR_TC);
    tcCombo.addItem(WORKING_DAYS_TC);
    tcCombo.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent evt) {
        String selected = (String) tcCombo.getSelectedItem();
        if (selected.equals(LINEAR_TC)) {
          chart.setTimeConverter(new IlvLinearTimeConverter());
        } else if (selected.equals(WORKING_DAYS_TC)) {
          chart.setTimeConverter(new WeekTimeConverter());
        }
      }
    });
    panel.add(tcCombo);

    panel.addSeparator(5);

    final JCheckBox boundedScrollButton = new JCheckBox("Bounded scroll mode");
    boundedScrollButton.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent e) {
        IlvHierarchyChart chart = getChart();
        if (boundedScrollButton.isSelected()) {
          IlvActivity rootActivity = chart.getGanttModel().getRootActivity();
          Date minTime = IlvTimeUtil.subtract(rootActivity.getStartTime(), IlvDuration.ONE_WEEK);
          Date maxTime = IlvTimeUtil.add(rootActivity.getEndTime(), IlvDuration.ONE_WEEK);
          chart.setMinVisibleTime(minTime);
          chart.setMaxVisibleTime(maxTime);
        } else {
          chart.setMinVisibleTime(null);
          chart.setMaxVisibleTime(null);
        }
      }
    });
    panel.add(boundedScrollButton);

    return panel;
  }

  /**
   * Adds the tabbed pages to the customizer panel.
   *
   * @param customizerPanel
   *          The customizer panel.
   */
  Override
  protected void addCustomizerTabs(JTabbedPane customizerPanel) {
    JComponent customizer = createTimeConverterCustomizer();
    if (customizer != null) {
      customizerPanel.addTab("Time Converter", customizer);
    }
    super.addCustomizerTabs(customizerPanel);
  }

  // =========================================
  // Example Application
  // =========================================

  /**
   * Returns the title of the example.
   *
   * @return The title of the example.
   */
  Override
  public String getTitle() {
    return "Non-Linear Time Scale Example";
  }

  /**
   * Application mainline.
   *
   * @param args
   *          The command line arguments.
   */
  public static void main(String[] args) {
    ExampleFrame.createAndShowGUI(NonLinearTimeScaleExample.class);
  }

}