Example usage for javax.script ScriptEngine get

List of usage examples for javax.script ScriptEngine get

Introduction

In this page you can find the example usage for javax.script ScriptEngine get.

Prototype

public Object get(String key);

Source Link

Document

Retrieves a value set in the state of this engine.

Usage

From source file:org.samjoey.gui.GraphicalViewer.java

private void selectedPGN(File file) {

    String fileLoc = file.getAbsolutePath();
    //fileLoc = "File:" + fileLoc.substring(2);
    if (fileLoc.substring(fileLoc.length() - 3).equals("pgn")) {
        for (int i = 0; i < fileLoc.length(); i++) {
            if (fileLoc.substring(i, i + 1).equals("/")) {
                fileLoc = fileLoc.substring(0, i) + "\\" + fileLoc.substring(i + 1);
            }//from   w  ww  .  ja v a  2 s  .c  om
        }
        try {
            // Below: Talk to JavaScript
            ScriptEngineManager factory = new ScriptEngineManager();
            // create JavaScript engine
            final ScriptEngine engine = factory.getEngineByName("JavaScript");
            // evaluate JavaScript code from given file - specified by first argument
            engine.put("engine", engine);
            ClassLoader cl = GraphicalViewer.class.getClassLoader();
            // Talk to GameLooper_1
            URL url = cl.getResource("\\org\\samjoey\\gameLooper\\GameLooper_1.js");
            String loopLoc = url.toString().substring(5);
            for (int i = 0; i < loopLoc.length() - 3; i++) {
                if (loopLoc.substring(i, i + 3).equals("%5c")) {
                    loopLoc = loopLoc.substring(0, i) + "/" + loopLoc.substring(i + 3);
                }
            }
            engine.put("loopLoc", loopLoc);
            // Talk to calcDefs_1
            url = cl.getResource("\\org\\samjoey\\calculator\\calcDefs_1.js");
            String defsLoc = url.toString().substring(5);
            for (int i = 0; i < defsLoc.length() - 3; i++) {
                if (defsLoc.substring(i, i + 3).equals("%5c")) {
                    defsLoc = defsLoc.substring(0, i) + "/" + defsLoc.substring(i + 3);
                }
            }
            engine.put("defsLoc", defsLoc);
            // Talk to Calculator
            url = cl.getResource("\\org\\samjoey\\calculator\\Calculator.js");
            String calcLoc = url.toString().substring(5);
            for (int i = 0; i < calcLoc.length() - 3; i++) {
                if (calcLoc.substring(i, i + 3).equals("%5c")) {
                    calcLoc = calcLoc.substring(0, i) + "/" + calcLoc.substring(i + 3);
                }
            }
            engine.put("calcLoc", calcLoc);
            String args[] = { "-g:false", fileLoc };
            engine.put("arguments", args);

            // Create a Thread to update the parser's progress bar
            parserProgress.setStringPainted(true);
            running = false;
            running = true;
            Thread thread = new Thread() {
                @Override
                public void run() {
                    long last = 0l;
                    boolean print = true;
                    double p = .01;
                    while (engine.get("progress") == null || engine.get("size") == null
                            || Integer.parseInt((String) engine.get("progress")) != Integer
                                    .parseInt((String) engine.get("size"))) {
                        //if (Parser.numGames > 0 && Parser.progress == Parser.numGames) {
                        try {
                            parserProgress.setValue(Integer.parseInt((String) engine.get("progress")));
                            parserProgress.setMaximum(Integer.parseInt((String) engine.get("size")));
                            if (last == 0l) {
                                last = System.nanoTime();
                            }
                            if ((double) parserProgress.getValue() / (double) parserProgress.getMaximum() > p
                                    && print) {
                                //System.out.println(p + ": " + (System.nanoTime() - last));
                                //print = false;
                                p += .01;
                            }
                        } catch (Exception e) {
                        }
                        //} else {
                        //    parserProgress.setMaximum(Parser.numGames - 1);
                        //    parserProgress.setMinimum(0);
                        //    parserProgress.setValue(Parser.progress);
                        //}
                    }
                    // finally
                    parserProgress.setValue(parserProgress.getMaximum());
                }
            };
            thread.start();

            // I have no clue what's here!??
            engine.eval(new java.io.FileReader(
                    GraphicalViewer.class.getClassLoader().getResource("driver_1.js").toString().substring(5)));
            while (games == null || games.get((int) (Math.random() * games.size())).getVarData().size() < 20) {
                games = (LinkedList<Game>) engine.get("gameList");
            }
        } catch (ScriptException | FileNotFoundException ex) {
            Logger.getLogger(GraphicalViewer.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else if (fileLoc.substring(fileLoc.length() - 4).equals("jsca")) {
        JSCAParser jsca = new JSCAParser(fileLoc);
        games = (LinkedList<Game>) jsca.getGamesList();
    }

    Set<String> keys = games.get(0).getVarData().keySet();
    for (String key : keys) {
        Variable_Chooser.addItem(key);
    }
    graphs = GraphUtility.getGraphs(games);

    setViewer(0, 0);
}