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

import java.awt.Color;
import java.util.regex.PatternSyntaxException;

import javax.swing.JComponent;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import ilog.views.gantt.IlvGanttChart;
import ilog.views.gantt.IlvGanttModel;
import ilog.views.gantt.IlvHierarchyChart;
import ilog.views.gantt.model.filter.IlvBasicFilterGanttModel;
import ilog.views.util.filter.IlvFilter;
import shared.swing.CustomizerPanel;
import shared.swing.ExampleFrame;
import xml.XMLGanttExample;

/**
 * This example shows how to create an implementation of the {@link IlvFilter}
 * interface that filters activities by applying a regular expression to their
 * names.
 */
public class GanttFilterExample extends XMLGanttExample {

  /**
   * The filter model that wrappers the data model loaded by the user.
   */
  protected IlvBasicFilterGanttModel filterModel;

  /**
   * The regex filter on activity names.
   */
  protected ActivityNameFilter activityFilter;

  /**
   * The activity filter regex entry field.
   */
  protected JTextField activityField;

  /**
   * Creates a new example.
   */
  public GanttFilterExample() {
    activityFilter = new ActivityNameFilter();
    filterModel = new IlvBasicFilterGanttModel(null, activityFilter, null);
  }

  /**
   * Creates the Gantt chart.
   *
   * @return The Gantt chart.
   */
  Override
  protected IlvHierarchyChart createChart() {
    return new IlvGanttChart() {
      Override
      public void setGanttModel(IlvGanttModel model) {
        filterModel.setFilteredModel(model);
        if (getGanttModel() != filterModel) {
          super.setGanttModel(filterModel);
        }
      }
    };
  }

  /**
   * This method is invoked whenever the user modifies the regex entry field.
   * The activity filter is updated and the entry field is colored to indicate
   * if the regex is valid or not.
   *
   * @param regex
   *          The regular expression.
   */
  protected void setRegex(String regex) {
    try {
      activityFilter.setRegex(regex);
      activityField.setForeground(Color.black);
    } catch (PatternSyntaxException e) {
      activityField.setForeground(Color.red);
    }
  }

  // =========================================
  // Menu
  // =========================================

  /**
   * Initializes the actions for the Edit menu.
   */
  Override
  protected void initEditActions() {
    super.initEditActions();
    // Disable the following actions that create non-intuitive
    // results when filtering is enabled.
    rowIndentAction = null;
    rowOutdentAction = null;
    rowUpAction = null;
    rowDownAction = null;
  }

  // =========================================
  // Control Panel
  // =========================================

  /**
   * Adds the tabbed pages to the customizer panel.
   *
   * @param customizerPanel
   *          The customizer panel.
   */
  Override
  protected void addCustomizerTabs(JTabbedPane customizerPanel) {
    customizerPanel.addTab("Filter", createFilterCustomizer());
    super.addCustomizerTabs(customizerPanel);
  }

  /**
   * Creates and returns a customizer for applying filters.
   *
   * @return The filter customizer.
   */
  protected JComponent createFilterCustomizer() {
    CustomizerPanel panel = new CustomizerPanel();
    panel.addHeading("Activity Name Filter");

    activityField = new JTextField(15);
    activityField.setText(activityFilter.getRegex());
    activityField.getDocument().addDocumentListener(new DocumentListener() {
      private void updateRegex() {
        setRegex(activityField.getText());
      }

      Override
      public void insertUpdate(DocumentEvent e) {
        updateRegex();
      }

      Override
      public void removeUpdate(DocumentEvent e) {
        updateRegex();
      }

      Override
      public void changedUpdate(DocumentEvent e) {
        updateRegex();
      }
    });

    panel.add(activityField);

    return panel;
  }

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

  /**
   * Returns the title of the example.
   *
   * @return The title of the example.
   */
  Override
  public String getTitle() {
    return "Gantt Chart Activity Filter Example";
  }

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

}