/*
 * 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.awt.Component;
import java.io.File;
import javax.swing.filechooser.FileFilter;
import javax.swing.JFileChooser;

public class ProjectFileChooser
{
  /**
   * The default file filter.
   */
  private FileFilter fileFilter;

  /**
   * The file chooser.
   */
  private JFileChooser fileChooser;

  /**
   * Creates an XML file chooser.
   */
  public ProjectFileChooser() {
    fileFilter = new FileFilter()
    {
      Override
      public boolean accept(File file) {
        return file.isDirectory() ||
            file.getName().endsWith("igpr");
      }

      Override
      public String getDescription() {
        return "Gantt Designer files";
      }

      Override
      public boolean equals(Object obj) {
        if (obj instanceof FileFilter &&
            getDescription().equals(((FileFilter) obj).getDescription()))
          return true;
        else
          return false;
      }
    };

    String slash = System.getProperty("file.separator");
    String dataFolder = System.getProperty("user.dir") + slash + "data";

    fileChooser = new JFileChooser(dataFolder);
    fileChooser.setFileFilter(fileFilter);
  }

  /**
   * Selects a file to open.
   * @return The selected file.
   */
  public File showOpenDialog(Component parent) {
    int ret = fileChooser.showOpenDialog(parent);
    if (ret != JFileChooser.APPROVE_OPTION)
      return null;
    return fileChooser.getSelectedFile();
  }

  /**
   * Selects a file to save.
   * @return The selected file.
   */
  public File showSaveDialog(Component parent) {
    int ret = fileChooser.showSaveDialog(parent);
    if (ret != JFileChooser.APPROVE_OPTION)
      return null;
    return fileChooser.getSelectedFile();
  }

  /**
   * Returns the current directory of the file chooser.
   */
  public File getCurrentDirectory() {
    return fileChooser.getCurrentDirectory();
  }

  /**
   * Sets the current directory of the file chooser. Passing in
   * <code>null</code> sets the file chooser to point to the user's default
   * directory.
   */
  public void setCurrentDirectory(File dir) {
    fileChooser.setCurrentDirectory(dir);
  }
}