package org.mwc.debrief.lite.util;

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

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;

public class DoSave extends DoSaveAs
{


  /**
   * 
   */
  private static final long serialVersionUID = 1L;

  public DoSave(Session session,JRibbonFrame theFrame)
  {
    super(session,theFrame);
  }

  @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.
    String fileName = DebriefLiteApp.currentFileName;
    File targetFile = null;
    String outputFileName = fileName;
    if(fileName == null) {
      outputFileName = showSaveDialog(null,DEFAULT_FILENAME);
      if(outputFileName!=null) {
        targetFile = new File(outputFileName);
      }
    }
    else {
        if(fileName.endsWith(".dpf")) {
          targetFile = new File(fileName);
        }
        else {
          // if the file is already loaded and 
          // has a different extension than dpf,
          // then show the save dialog 
          File f = new File(fileName);
          fileName = getFileName(fileName);
          fileName = showSaveDialog(f.getParentFile(),fileName);
          if(fileName!=null) {
            targetFile = new File(fileName);
          }
      }
    }
    if(targetFile!=null) {
      DebriefRibbonFile.saveChanges(targetFile.getAbsolutePath(), _session, _theFrame);
    }
  }
}

@@ -731,24 +731,30 @@
 leName;%0A    
+final 
 File targetF
@@ -760,15 +760,8 @@
 File
- = null
 ;%0A  
@@ -798,24 +798,25 @@
 Name;%0A    if
+ 
 (fileName ==
@@ -817,24 +817,28 @@
 ame == null)
+%0A   
  %7B%0A      out
@@ -871,16 +871,17 @@
 og(null,
+ 
 DEFAULT_
@@ -899,16 +899,17 @@
       if
+ 
 (outputF
@@ -911,31 +911,39 @@
 tputFileName
+ 
 !=
+ 
 null)
+%0A     
  %7B%0A        t
@@ -980,32 +980,86 @@
 eName);%0A      %7D%0A
+      else%0A      %7B%0A        targetFile = null;%0A      %7D%0A
     %7D%0A    else %7B
@@ -1048,32 +1048,36 @@
 %7D%0A    %7D%0A    else
+%0A   
  %7B%0A        if(fi
@@ -1065,28 +1065,27 @@
     %7B%0A      
-  
 if
+ 
 (fileName.en
@@ -1099,21 +1099,25 @@
 %22.dpf%22))
- %7B
 %0A  
+    %7B%0A
         
@@ -1151,30 +1151,26 @@
 ame);%0A      
-  %7D%0A  
+%7D%0A
       else %7B
@@ -1167,29 +1167,33 @@
     else
- %7B
 %0A   
+   %7B%0A
        
+ 
 // if th
@@ -1220,20 +1220,17 @@
 aded and
- %0A  
+%0A
         
@@ -1268,18 +1268,16 @@
 an dpf,%0A
-  
         
@@ -1304,20 +1304,17 @@
 e dialog
- %0A  
+%0A
         
@@ -1342,34 +1342,32 @@
 eName);%0A        
-  
 fileName = getFi
@@ -1376,34 +1376,32 @@
 Name(fileName);%0A
-  
         fileName
@@ -1436,16 +1436,17 @@
 tFile(),
+ 
 fileName
@@ -1456,20 +1456,19 @@
         
-  
 if
+ 
 (fileNam
@@ -1468,28 +1468,36 @@
 fileName
+ 
 !=
+ 
 null)
- %7B
 %0A  
+      %7B%0A
         
@@ -1533,24 +1533,84 @@
 ame);%0A      
+  
+%7D%0A        else%0A        %7B%0A          targetFile = null;%0A    
     %7D%0A      
@@ -1623,16 +1623,17 @@
 %7D%0A    if
+ 
 (targetF
@@ -1639,15 +1639,21 @@
 File
+ 
 !=
+ 
 null)
+%0A   
  %7B%0A 
@@ -1726,16 +1726,26 @@
 session,
+%0A         
  _theFra

use final keyword to ensure target only generated once