content: /*
   * The MIT License (MIT)
   *
   * Copyright (c) 2011-2021 Broad Institute, Aiden Lab, Rice University, Baylor College of Medicine
   *
   * Permission is hereby granted, free of charge, to any person obtaining a copy
   * of this software and associated documentation files (the "Software"), to deal
   * in the Software without restriction, including without limitation the rights
   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   * copies of the Software, and to permit persons to whom the Software is
   * furnished to do so, subject to the following conditions:
   *
   * The above copyright notice and this permission notice shall be included in
   * all copies or substantial portions of the Software.
   *
   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   *  FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
   *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   *  THE SOFTWARE.
   */
  
  package juicebox.state;
  
  import juicebox.HiCGlobals;
  import juicebox.MainWindow;
  
  import javax.swing.*;
  import javax.swing.filechooser.FileNameExtensionFilter;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.nio.channels.FileChannel;
  import java.text.SimpleDateFormat;
  import java.util.Date;
  
  /**
   * Created by Zulkifl on 7/23/2015.
   */
  public class SaveFileDialog extends JFileChooser {
  
      private static final long serialVersionUID = 9000017;
      public SaveFileDialog(File fileToSave) {
          super();
          setCurrentDirectory(new File(System.getProperty("user.dir")));
          String timeStamp = new SimpleDateFormat("yyyy.MM.dd-HH.mm").format(new Date());
          setSelectedFile(new File(timeStamp + ".xml"));
          FileNameExtensionFilter filter = new FileNameExtensionFilter("XML Files", "xml", "XML");
          setFileFilter(filter);
          if (HiCGlobals.guiIsCurrentlyActive) {
              int actionDialog = showSaveDialog(MainWindow.getInstance());
              if (actionDialog == JFileChooser.APPROVE_OPTION) {
                  File file = getSelectedFile();
                  if (!file.getPath().endsWith(".xml") && !file.getPath().endsWith(".XML")) {
                      file = new File(file + ".xml");
                  }
                  
                  try {
                      copyFile(fileToSave, file);
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }
      }
  
  
      private static void copyFile(File sourceFile, File destFile) throws IOException {
          if (!destFile.exists()) {
              destFile.createNewFile();
          }
  
          FileChannel source = null;
          FileChannel destination = null;
  
          try {
              source = new FileInputStream(sourceFile).getChannel();
              destination = new FileOutputStream(destFile).getChannel();
              destination.transferFrom(source, 0, source.size());
          } finally {
              if (source != null) {
                  source.close();
              }
              if (destination != null) {
                  destination.close();
              }
          }
      }
  }
  