Package abstrasy

Source Code of abstrasy.Main

package abstrasy;


import abstrasy.consoleui.FrameWorkConsole;
import abstrasy.consoleui.MinimalConsole;
import abstrasy.consoleui.PromptParser;

import java.awt.Dimension;
import java.awt.Toolkit;

import java.util.Scanner;

import javax.swing.UIManager;


/**
* Abstrasy Interpreter
*
* Copyright : Copyright (c) 2006-2012, Luc Bruninx.
*
* Concédée sous licence EUPL, version 1.1 uniquement (la «Licence»).
*
* Vous ne pouvez utiliser la présente oeuvre que conformément à la Licence.
* Vous pouvez obtenir une copie de la Licence à l’adresse suivante:
*
*   http://www.osor.eu/eupl
*
* Sauf obligation légale ou contractuelle écrite, le logiciel distribué sous
* la Licence est distribué "en l’état", SANS GARANTIES OU CONDITIONS QUELLES
* QU’ELLES SOIENT, expresses ou implicites.
*
* Consultez la Licence pour les autorisations et les restrictions
* linguistiques spécifiques relevant de la Licence.
*
*
* @author Luc Bruninx
* @version 1.0
*/


public class Main {

    boolean packFrame = false;

    public static boolean onOpenJDK = (System.getProperty("java.vm.name").indexOf("OpenJDK") >= 0);
    //Construire l'application

    public Main() {

        FrameWorkConsole frame = new FrameWorkConsole();
        //Valider les cadres ayant des tailles prédéfinies
        //Compacter les cadres ayant des infos de taille préférées - ex. depuis leur disposition
        if (packFrame) {
            frame.pack();
        }
        else {
            frame.validate();
        }
        //Centrer la fenêtre
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = frame.getSize();
        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }
        frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
        frame.setVisible(true);
    }

    public static String arg_file = null;

    public static boolean arg_ide = false;

    public static boolean arg_noconsole = false;

    public static boolean arg_prompt = false;

    public static Node arg_argv = Node.createCList();

    //Méthode main

    private static String prompt_level(int level){
        String r="";
        for(int i=level;i>0;i--)
            r+="  ";
        return r;
    }
   
    private static String prompt(MinimalConsole console) {
        String rd = "";
        int level = 0;
        boolean goodcode=true;
        do {
            if (console != null) {
                try {
                    console.getOutputTextArea().write(level==0 ? "$ ": "  "+prompt_level(level));
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    if (rd.length() > 0)
                        rd += "\n";
                    rd += console.getOutputTextArea().readln();
                }
                catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            else {
                System.out.print(level==0 ? "$ ": "  "+prompt_level(level));
                try {
                    Scanner in = new Scanner(System.in);
                    if (rd.length() > 0)
                        rd += "\n";
                    rd += in.nextLine();
                    //in.close();       *** ATTENTION NE PAS FERMER LE FICHIER System.in !!!!
                }
                catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            PromptParser pparser = new PromptParser(rd);
            level = pparser.getPar() + pparser.getBra() + pparser.getCur() + pparser.getChe() + pparser.getStr();
            goodcode=!(pparser.hasParErrors()||pparser.hasBraErrors()||pparser.hasCurErrors()||pparser.hasCheErrors()||pparser.hasStrErrors());
        }
        while (goodcode && (level > 0));
        return rd;
    }

    /*
    * Removed by l.bruninx, 2012-06-05. (bug: Launchpad#1009077).
    *
    private final static Node createList_IfNull(Node xnode) {
        Node res = xnode;
        if (res == null) {
            res = Node.createEmptyList();
        }
        return res;
    }
    *
    */

    public static void main(String[] args) {
        String[] args2 = args;
        try {
            if ((System.getProperty("os.name").compareTo("Linux") == 0)) {
                boolean reussi = false;
                /*try {
                    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                        if ("Nimbus".equals(info.getName())) {
                            UIManager.setLookAndFeel(info.getClassName());
                            reussi=true;
                            break;
                        }
                    }
                } catch (Exception e) {
                        reussi=false;
                }*/
                //if (!reussi) {
                UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
                //}

            }
            else {
                boolean reussi = false;
                /*try {
                    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                        if ("Nimbus".equals(info.getName())) {
                            UIManager.setLookAndFeel(info.getClassName());
                            reussi=true;
                            break;
                        }
                    }
                } catch (Exception e) {
                        reussi=false;
                }*/
                if (!reussi) {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                }
            }

        }
        catch (Exception e) {
            e.printStackTrace();
        }


        /**
     * traitement des arguments...
     */
        int cntArgs = 0;
        if (args2 != null) {
            cntArgs = args2.length;
            boolean file_unset = true;
            for (int i = 0; i < args2.length; i++) {
                Interpreter.Log("ARGS[" + i + "]:\"" + args2[i] + "\"");
                if (file_unset && !args2[i].startsWith("--")) {
                    arg_file = args2[i];
                    file_unset = false;
                }
                else if (file_unset && args2[i].startsWith("--")) {
                    if (args2[i].equalsIgnoreCase("--ide")) {
                        arg_ide = true;
                    }
                    else if (args2[i].equalsIgnoreCase("--no-console")) {
                        arg_noconsole = true;
                    }
                    else if (args2[i].equalsIgnoreCase("--prompt")) {
                        arg_prompt = true;
                    }
                }
                else if (!file_unset) {
                    try {
                        arg_argv.append(new Node(args2[i]));
                    }
                    catch (Exception e) {
                        e.printStackTrace();
                        System.exit(1);
                    }
                }
            }
        }

        String source = null;
        Interpreter lastInterpreter;

        if (arg_ide || cntArgs == 0) {
            // ide only

            new Main();

        }
        else if (arg_prompt) {
            //System.out.println("prompt");
            MinimalConsole console = (arg_noconsole ? null: MinimalConsole.createConsole("Abstrasy Virtual Console"));
            int exitCode = 0;
            try {
                lastInterpreter = Interpreter.interpr_getSuperInterpreter(Interpreter.GETSUPERINTERPRETER_HARDMODE);
                if (console != null) {
                    lastInterpreter.setOutputTextArea(console.getOutputTextArea());
                    lastInterpreter.setLogTextArea(console.getOutputTextArea());
                }
                Interpreter.setDebugMode(false);


                if (console != null) {
                    console.getOutputTextArea().write(Interpreter.TITLE_APP + "\n<close this window or enter ''(exit)'' to exit>\nReady...\n");
                }
                else {
                    System.out.println(Interpreter.TITLE_APP);
                    System.out.println("<enter ''(exit)'' to exit> ");
                    System.out.println("Ready...");
                }

                String prd = "";
                boolean continuer = true;
                while (continuer) {
                    prd = prompt(console);
                    lastInterpreter.setSource(prd);
                    try {
                        lastInterpreter.start();
                        lastInterpreter.join();
                    }
                    catch (Exception ex) {
                        ex.printStackTrace();
                    }
                    if (!Interpreter.hasExitCode()) {
                        lastInterpreter = Interpreter.interpr_cloneInterpreter(lastInterpreter);
                    }
                    else {
                        exitCode = Interpreter.getExitCode();
                        continuer = false;
                    }
                }

            }
            catch (Exception ex) {
                System.out.println("Remote exception : " + ex.getMessage());
                ex.printStackTrace();
            }
            System.exit(exitCode);

        }
        else {

            // no-ide

            if (arg_file != null) {
                try {
                    SourceFile sf = new SourceFile(arg_file);
                    sf.load();
                    source = sf.getSource();
                }
                catch (Exception e) {
                    System.out.println("Erreur lors du chargement de " + arg_file);
                    e.printStackTrace();
                }
            }

            MinimalConsole console = (arg_noconsole ? null: MinimalConsole.createConsole("Abstrasy Virtual Console"));

            if (source != null) {
                int exitCode = 0;
                try {
                    lastInterpreter = Interpreter.interpr_getSuperInterpreter(Interpreter.GETSUPERINTERPRETER_HARDMODE);

                    if (console != null) {
                        lastInterpreter.setOutputTextArea(console.getOutputTextArea());
                        lastInterpreter.setLogTextArea(console.getOutputTextArea());
                    }

                    Interpreter.setDebugMode(false);
                    if (arg_file != null) {
                        Interpreter.registerSourceAbsolutePath(arg_file);
                    }
                    lastInterpreter.setSource(source);

                    lastInterpreter.setInterpreterArgs(arg_argv);
                    //System.out.println(arg_argv);

                    lastInterpreter.start();
                    lastInterpreter.join();
                    if (Interpreter.hasExitCode()) {
                        exitCode = Interpreter.getExitCode();
                        if (console != null) {
                            console.getOutputTextArea().write("Process has an exit code (" + Interpreter.getExitCode() + ")...\n");
                        }
                    }
                    if (console != null && console.isVisible()) {
                        console.getOutputTextArea().write("\n. . . <close this window to exit> . . .");
                        return; // on arrête là si on est en mode graphique...
                    }
                }
                catch (Exception ex) {
                    System.out.println("Remote exception : " + ex.getMessage());
                    ex.printStackTrace();
                }
                System.exit(exitCode);
            }

            // no-ide
        }

    }

}
TOP

Related Classes of abstrasy.Main

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.