/*
 * Copyright (C) 2011 0xlab - http://0xlab.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Authored by Kan-Ru Chen <kanru@0xlab.org>
 */

package org.zeroxlab.aster;

import java.io.IOException;
import java.lang.ArrayIndexOutOfBoundsException;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import org.zeroxlab.aster.cmds.AsterCommandManager;
import org.zeroxlab.aster.cmds.AsterCommand.ExecutionResult;

public class AsterMain {

    public static void main(String[] args) {
        if (args.length > 0) {
            try {
                if ("-run".equals(args[0])) {
                    startCLI(args[1]);
                }
            } catch(ArrayIndexOutOfBoundsException e) {
                usage();
            }
        } else {
            startGUI();
        }
    }

    public static void startCLI(String script) {
        ExecutionResult result = null;
        try {
            result = (new AsterCommandManager()).run(script);
        } catch(IOException e) {
            System.err.printf(e.toString());
            System.exit(1);
        }
        if (result.mSuccess) {
            System.out.println("\nFinished\n");
            System.exit(0);
        } else {
            System.out.println("\nFailed\n");
            System.exit(1);
        }
    }

    public static void startGUI() {
        trySetupLookFeel();
        JFrame f = new JFrame("Aster");
        AsterMainPanel p = new AsterMainPanel();
        f.setContentPane(p);
        f.setJMenuBar(p.createMenuBar());
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }

    private static void trySetupLookFeel() {
        System.setProperty("awt.useSystemAAFontSettings","on");
        System.setProperty("swing.aatext", "true");
        try {
            // Set System L&F
            UIManager.setLookAndFeel(
                "com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
        } catch (UnsupportedLookAndFeelException e) {
            // handle exception
        } catch (ClassNotFoundException e) {
            // handle exception
        } catch (InstantiationException e) {
            // handle exception
        } catch (IllegalAccessException e) {
            // handle exception
        }
    }

    private static void usage() {
        System.out.printf("Usage: aster [-run TEST.ast]\n\n");
    }
}

@@ -871,16 +871,72 @@
 ption;%0A%0A
+import org.zeroxlab.aster.CmdConnection.SnapshotDrawer;%0A
 import o
@@ -1076,345 +1076,151 @@
    p
-ublic static void main(String%5B%5D args) %7B%0A        if (args.length %3E 0) %7B%0A            try %7B%0A                if (%22-run%22.equals(args%5B0%5D)) %7B%0A                    startCLI(args%5B1%5D);%0A                %7D%0A            %7D catch(ArrayIndexOutOfBoundsException e) %7B%0A                usage();%0A            %7D%0A        %7D else %7B%0A            startGUI();%0A        %7D
+rivate AsterCommandManager mCmdMgr;%0A    private CmdConnection mCmdConn;%0A%0A    public AsterMain() %7B%0A        mCmdMgr = new AsterCommandManager();%0A
 %0A   
@@ -1226,39 +1226,32 @@
   %7D%0A%0A    public 
-static 
 void startCLI(St
@@ -1343,35 +1343,15 @@
 t = 
-(new AsterCommandManager())
+mCmdMgr
 .run
@@ -1702,23 +1702,16 @@
  public 
-static 
 void sta
@@ -1716,24 +1716,72 @@
 tartGUI() %7B%0A
+        mCmdConn = new CmdConnection(mCmdMgr);%0A%0A
         tryS
@@ -1882,16 +1882,33 @@
 inPanel(
+mCmdMgr, mCmdConn
 );%0A     
@@ -2076,16 +2076,178 @@
 e(true);
+%0A%0A        SnapshotDrawer drawer = p.getSnapshotDrawer();%0A        mCmdConn.setDrawer(drawer);%0A        Thread thread = new Thread(mCmdConn);%0A        thread.start();
 %0A    %7D%0A%0A
@@ -2989,10 +2989,422 @@
 ;%0A    %7D%0A
+%0A    public static void main(String%5B%5D args) %7B%0A        AsterMain aster = new AsterMain();%0A        if (args.length %3E 0) %7B%0A            try %7B%0A                if (%22-run%22.equals(args%5B0%5D)) %7B%0A                    aster.startCLI(args%5B1%5D);%0A                %7D%0A            %7D catch(ArrayIndexOutOfBoundsException e) %7B%0A                aster.usage();%0A            %7D%0A        %7D else %7B%0A            aster.startGUI();%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Create AsterMain instance