package bakatxt.core;

import java.awt.GraphicsEnvironment;
import java.util.Scanner;
import java.util.StringTokenizer;

import bakatxt.gui.BakaUI;

public class BakaTxtMain {

    enum CommandType {
        ADD, DELETE, DISPLAY, EXIT
    }

    private static final String MESSAGE_FILENAME = "Please input a filename: ";
    private static final String MESSAGE_INVALID_FORMAT = "Invalid command format";
    private static final String MESSAGE_WELCOME = "Welcome To BakaTxt!";
    private static final String MESSAGE_INVALID_COMMAND = "Unrecognized command type";
    private static final String MESSAGE_ENTER_COMMAND = "Please enter command: ";
    private static final String MESSAGE_BYEBYE = "Bye bye!";

    private static Scanner _sc;
    private static BakaTxtSession _session;
    private static String _filename;

    public static void main(String[] args) {
        String command;

        if (GraphicsEnvironment.isHeadless()) {
            _sc = new Scanner(System.in);
            initializeFile();
            while (true) {
                System.out.print(MESSAGE_ENTER_COMMAND);
                command = _sc.nextLine();
                String output = executeCommand(command);
                System.out.println(output);
            }
       }

        BakaUI.startGui();
    }

    private static void initializeFile() {
        System.out.print(MESSAGE_FILENAME);
        _filename = _sc.nextLine();

        while (!checkFileName()) {
            System.out.print(MESSAGE_FILENAME);
            _filename = _sc.nextLine();
        }

        _session = new BakaTxtSession(_filename);
        System.out.println(MESSAGE_WELCOME);
    }

    private static boolean checkFileName() {
        // TODO check filename grammar
        return true;
    }

    private static String getFirstWord(String command) {
        StringTokenizer tokenizedCommand = new StringTokenizer(command);
        String commandLine = tokenizedCommand.nextToken();
        return commandLine;
    }

    public static String executeCommand(String userCommand) {
        if (userCommand.trim().equals("")) {
            return invalidFormat(userCommand);
        }

        String output;
        String parsingCommand = getFirstWord(userCommand).toUpperCase();
        String contentString = removeFirstWord(userCommand);

        switch (CommandType.valueOf(parsingCommand)) {
            case ADD :
                output = _session.add(contentString);
                break;
            case DELETE :
                output = _session.delete(contentString);
                break;
            case DISPLAY :
                output = _session.display(contentString);
                break;
            case EXIT :
                System.out.println(MESSAGE_BYEBYE);
                _session.exit();
                System.exit(0);
                //$FALL-THROUGH$ (to remove fall-through warning in eclipse)
            default :
                output = MESSAGE_INVALID_COMMAND;
        }

        return output;
    }

    private static String invalidFormat(String userCommand) {
        return String.format(MESSAGE_INVALID_FORMAT, userCommand);
    }

    private static String removeFirstWord(String command) {
        return command.replace(getFirstWord(command), "").trim();
    }
}

@@ -987,16 +987,19 @@
         
+ //
  initial
@@ -1005,24 +1005,120 @@
 lizeFile();%0A
+            _filename = %22mytestfile.txt%22;%0A            _session = new BakaTxtSession(_filename);%0A
             
@@ -1346,16 +1346,17 @@
       %7D%0A
+ 
        %7D
@@ -1348,32 +1348,32 @@
     %7D%0A        %7D%0A
-
 %0A        BakaUI.
@@ -1386,32 +1386,35 @@
 ui();%0A    %7D%0A%0A   
+ //
  private static 
@@ -1437,27 +1437,26 @@
 ile() %7B%0A    
-   
+//
  System.out.
@@ -1480,27 +1480,26 @@
 ENAME);%0A    
-   
+//
  _filename =
@@ -1515,24 +1515,29 @@
 Line();%0A
-%0A
     
-   
+//%0A    //
  while (
@@ -1556,31 +1556,26 @@
 me()) %7B%0A    
-       
+//
  System.out.
@@ -1599,31 +1599,26 @@
 ENAME);%0A    
-       
+//
  _filename =
@@ -1638,30 +1638,34 @@
 ();%0A    
-    %7D%0A%0A
+// %7D%0A 
    
+//%0A
     
+//
  _sessio
@@ -1699,27 +1699,26 @@
 ename);%0A    
-   
+//
  System.out.
@@ -1742,24 +1742,27 @@
 ELCOME);%0A   
+ //
  %7D%0A%0A    priv

Add kludge for GUI integration