package org.mwc.debrief.lite.util;

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.OutputStream;

import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.mwc.debrief.lite.DebriefLiteApp;
import org.mwc.debrief.lite.menu.DebriefRibbonFile;
import org.pushingpixels.flamingo.api.ribbon.JRibbonFrame;

import Debrief.GUI.Frames.Session;
import Debrief.ReaderWriter.XML.DebriefXMLReaderWriter;

public class DoSaveAs extends AbstractAction
{


  /**
   * 
   */
  private static final long serialVersionUID = 1L;
  protected final Session _session;
  protected JRibbonFrame _theFrame;

  public DoSaveAs(Session session,JRibbonFrame frame)
  {
    _session = session;
    _theFrame = frame;
  }

  protected void performSave(final OutputStream fos)
  {
    DebriefXMLReaderWriter.exportThis(_session, fos);
  }

  @Override
  public void actionPerformed(ActionEvent e)
  {
    // get the current file path,
    // check we have write permission to it before starting save
    // get a new file path to use.
    final String outputFile;
    if (DebriefLiteApp.currentFileName != null)
    {
      File curDir = new File(DebriefLiteApp.currentFileName);
      String fileName = getFileName(curDir.getName());
      outputFile = showSaveDialog(curDir.getParentFile(), fileName);
    }
    else
    {
      outputFile = showSaveDialog(null, null);
    }

    DebriefRibbonFile.saveChanges(outputFile, _session, _theFrame);
  }

  protected String getFileName(String fileName)
  {
    return fileName.substring(0,fileName.lastIndexOf("."));
  }

  public final static String showSaveDialog(final File parentDirectory,
      final String initialName)
  {
    String outputFileName = null;
    final JFileChooser fileChooser = new JFileChooser();
    final FileFilter filter = new FileNameExtensionFilter("dpf file", "dpf");
    fileChooser.setFileFilter(filter);
    if (initialName != null)
    {
      fileChooser.setSelectedFile(new File(initialName + ".dpf"));
    }
    if (parentDirectory != null)
    {
      fileChooser.setCurrentDirectory(parentDirectory);
    }
    int res = fileChooser.showSaveDialog(null);
    if (res == JOptionPane.OK_OPTION)
    {
      final File targetFile = fileChooser.getSelectedFile();
      if (targetFile != null)
      {
        if (targetFile.exists())
        {
          int yesNo = JOptionPane.showConfirmDialog(null, targetFile.getName()
              + " already exists. Do you want to overwrite?");
          if (JOptionPane.YES_OPTION == yesNo)
          {
            outputFileName = targetFile.getAbsolutePath();
          }
          // let the user try again otherwise
        }
        else
        {
          outputFileName = targetFile.getAbsolutePath();
        }
      }
    }
    else
    {
      outputFileName = null;
    }
    return outputFileName;
  }

}

@@ -88,37 +88,8 @@
 ile;
-%0Aimport java.io.OutputStream;
 %0A%0Aim
@@ -480,64 +480,8 @@
 ion;
-%0Aimport Debrief.ReaderWriter.XML.DebriefXMLReaderWriter;
 %0A%0Apu
@@ -644,16 +644,22 @@
 otected 
+final 
 JRibbonF
@@ -693,16 +693,22 @@
 oSaveAs(
+final 
 Session 
@@ -715,16 +715,22 @@
 session,
+final 
 JRibbonF
@@ -801,124 +801,8 @@
  %7D%0A%0A
-  protected void performSave(final OutputStream fos)%0A  %7B%0A    DebriefXMLReaderWriter.exportThis(_session, fos);%0A  %7D%0A%0A
   @O
@@ -839,16 +839,22 @@
 rformed(
+final 
 ActionEv
@@ -1446,16 +1446,22 @@
 ileName(
+final 
 String f
@@ -2063,24 +2063,30 @@
 ;%0A    %7D%0A    
+final 
 int res = fi

Remove unused method, minor improvements for static analysis