JAL-4285 Better checkFiles processing. Better dialog.
[jalview.git] / src / jalview / bin / Commands.java
index cd72e60..ba72831 100644 (file)
@@ -14,6 +14,7 @@ import java.util.Map;
 
 import jalview.analysis.AlignmentUtils;
 import jalview.api.structures.JalviewStructureDisplayI;
+import jalview.bin.Jalview.ExitCode;
 import jalview.bin.argparser.Arg;
 import jalview.bin.argparser.ArgParser;
 import jalview.bin.argparser.ArgParser.Position;
@@ -142,26 +143,16 @@ public class Commands
     }
 
     // report errors
-    StringBuilder sb = new StringBuilder();
-    for (String error : errors)
-    {
-      sb.append("- " + error);
-      sb.append("\n");
-    }
-    if (Platform.isHeadless())
-    {
-      Console.debug("All errors from command line argument commands:\n"
-              + sb.toString());
-    }
-    else
-    {
-      // scrollable dialog box
-
-    }
+    Console.warn(
+            "The following errors and warnings occurred whilst processing files:\n"
+                    + errorsToString());
+    // gui errors reported in Jalview
 
     if (argParser.getBoolean(Arg.QUIT))
     {
-      Jalview.exit("Exiting due to " + Arg.QUIT.argString(), 0);
+      Jalview.getInstance().exit(
+              "Exiting due to " + Arg.QUIT.argString() + " argument.",
+              ExitCode.OK);
       return true;
     }
     // carry on with jalview.bin.Jalview
@@ -188,7 +179,7 @@ public class Commands
       return true;
     }
 
-    boolean isError = false;
+    Boolean isError = Boolean.valueOf(false);
 
     // set wrap scope here so it can be applied after structures are opened
     boolean wrap = false;
@@ -441,7 +432,8 @@ public class Commands
       {
         if (headless)
         {
-          Jalview.exit("Could not open any files in headless mode", 1);
+          Jalview.exit("Could not open any files in headless mode",
+                  ExitCode.NO_FILES);
         }
         else
         {
@@ -701,14 +693,10 @@ public class Commands
                 AppJmol jmol = (AppJmol) sview;
                 try
                 {
-                  whatNext wn = this.checksBeforeWritingToFile(avm, subVals,
-                          false, structureImageFilename, "structure image");
-                  if (wn == whatNext.ERROR)
-                  {
-                    isError = true;
-                    continue;
-                  }
-                  else if (wn == whatNext.CONTINUE)
+                  boolean success = this.checksBeforeWritingToFile(avm,
+                          subVals, false, structureImageFilename,
+                          "structure image", isError);
+                  if (!success)
                   {
                     continue;
                   }
@@ -803,7 +791,7 @@ public class Commands
       return false;
     }
 
-    boolean isError = false;
+    Boolean isError = Boolean.valueOf(false);
     if (avm.containsArg(Arg.IMAGE))
     {
       for (ArgValue av : avm.getArgValueList(Arg.IMAGE))
@@ -845,14 +833,9 @@ public class Commands
 
         Console.info("Writing " + file);
 
-        whatNext wn = this.checksBeforeWritingToFile(avm, subVal, false,
-                fileName, "image");
-        if (wn == whatNext.ERROR)
-        {
-          isError = true;
-          continue;
-        }
-        else if (wn == whatNext.CONTINUE)
+        boolean success = checksBeforeWritingToFile(avm, subVal, false,
+                fileName, "image", isError);
+        if (!success)
         {
           continue;
         }
@@ -930,7 +913,7 @@ public class Commands
       return false;
     }
 
-    boolean isError = false;
+    Boolean isError = Boolean.valueOf(false);
 
     if (avm.containsArg(Arg.OUTPUT))
     {
@@ -997,14 +980,9 @@ public class Commands
           }
         }
 
-        whatNext wn = this.checksBeforeWritingToFile(avm, subVals, true,
-                fileName, ff.getName());
-        if (wn == whatNext.ERROR)
-        {
-          isError = true;
-          continue;
-        }
-        else if (wn == whatNext.CONTINUE)
+        boolean success = checksBeforeWritingToFile(avm, subVals, true,
+                fileName, ff.getName(), isError);
+        if (!success)
         {
           continue;
         }
@@ -1122,14 +1100,9 @@ public class Commands
     errors.add(errorMessage);
   }
 
-  private enum whatNext
-  {
-    OKAY, CONTINUE, ERROR;
-  }
-
-  private whatNext checksBeforeWritingToFile(ArgValuesMap avm,
+  private boolean checksBeforeWritingToFile(ArgValuesMap avm,
           SubVals subVal, boolean includeBackups, String filename,
-          String adjective)
+          String adjective, Boolean isError)
   {
     File file = new File(filename);
 
@@ -1154,7 +1127,7 @@ public class Commands
               + Arg.OVERWRITE.argString()
               + (includeBackups ? " or " + Arg.BACKUPS.argString() : "")
               + " set");
-      return whatNext.CONTINUE;
+      return false;
     }
 
     boolean mkdirs = ArgParser.getFromSubValArgOrPref(avm, Arg.MKDIRS,
@@ -1167,9 +1140,27 @@ public class Commands
               + "' does not exist for " + adjective + " file '" + filename
               + "'."
               + (mkdirs ? "" : "  Try using " + Arg.MKDIRS.argString()));
-      return whatNext.ERROR;
+      isError = true;
+      return false;
     }
 
-    return whatNext.OKAY;
+    return true;
+  }
+
+  public List<String> getErrors()
+  {
+    return errors;
+  }
+
+  public String errorsToString()
+  {
+    StringBuilder sb = new StringBuilder();
+    for (String error : errors)
+    {
+      if (sb.length() > 0)
+        sb.append("\n");
+      sb.append("- " + error);
+    }
+    return sb.toString();
   }
 }