package eg.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

//--Eadgyth--/
import eg.Constants;

/**
 * Static methods to work with files
 */
public class FileUtils {
   
   public static String fileSuffix(String file) {
      int indDot = file.lastIndexOf(".") + 1;
      int indFileSep = file.lastIndexOf(File.separator);
      if (indDot > indFileSep) {
         return file.substring(indDot);
      }
      else {
         return "";
      }
   }
   
   /**
    * Deletes a folder and its content
    *
    * @param dir  the directory to be deleted
    * @return  true if the directory has been deleted
    */
   public static boolean deleteFolder(File dir) {
      boolean ret = true;
      if (dir.isDirectory()){
         for (File f : dir.listFiles()) {
            ret = ret && FileUtils.deleteFolder(f);
         }
      }
      return ret && dir.delete();
   }
   
   /**
    * If the specified directory is empty
    *
    * @param dir  the directory
    * @return  if dir is empty
    */
   public static boolean isFolderEmpty(File dir) {
      File[] content = dir.listFiles();
      return content.length == 0;
   }
   
   /**
    * Appends to the file 'log.txt' the stack trace of an exception
    * and shows a warning in a dialog window.
    * <p>The log file is saved in the program's directory.
    *
    * @param e  the <code>Exception</code>
    */
   public static void logStack(Exception e) {
      File f = new File("log.txt");
      try (FileWriter writer = new FileWriter(f, true)) {
         writer.write(e.getMessage() + Constants.LINE_SEP);
         for (StackTraceElement el : e.getStackTrace()) {
            writer.write("   " + el.toString() + Constants.LINE_SEP);
         }
         writer.write(Constants.LINE_SEP);
         Dialogs.errorMessage(
               "Error:\n" + e.getMessage() + ".\nSee log.txt file");
      }
      catch(IOException ioe) {
         throw new RuntimeException(
               "Could not write the stack trace to the log file", ioe);
      }
   }
   
   /**
    * Empties the 'log' file saved in the program's directory
    */
   public static void emptyLog() { 
      try (FileWriter writer = new FileWriter("log.txt", false)) {
         writer.write("");
      }
      catch(IOException e) {
         throw new RuntimeException("Could not empty the log file", e);
      }
   }
}

@@ -1207,24 +1207,36 @@
 og.txt' the 
+message and 
 stack trace 
@@ -1255,54 +1255,8 @@
 ion%0A
-    * and shows a warning in a dialog window.%0A
     
@@ -1740,32 +1740,97 @@
 ants.LINE_SEP);%0A
+         writer.write(%22_________________%22 + Constants.LINE_SEP);%0A
          Dialogs
@@ -1843,32 +1843,16 @@
 Message(
-%0A               
 %22Error:%5C
@@ -1874,32 +1874,8 @@
 ge()
- + %22.%5CnSee log.txt file%22
 );%0A 
@@ -1985,24 +1985,8 @@
 ite 
-the stack trace 
 to t
@@ -2022,338 +2022,6 @@
   %7D%0A
-   %0A   /**%0A    * Empties the 'log' file saved in the program's directory%0A    */%0A   public static void emptyLog() %7B %0A      try (FileWriter writer = new FileWriter(%22log.txt%22, false)) %7B%0A         writer.write(%22%22);%0A      %7D%0A      catch(IOException e) %7B%0A         throw new RuntimeException(%22Could not empty the log file%22, e);%0A      %7D%0A   %7D%0A
 %7D%0A

remove emptyLog method