Ignore:
Timestamp:
12/05/13 16:55:01 (11 years ago)
Author:
jts21
Message:

Add action to run javascript code. Probably completely pointless since there is already a Javascript class, but I couldn't get the existing Javascript engine to work and wanted to try scripting anyway

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/actions/Misc.java

    r573 r574  
    44import java.awt.Desktop;
    55import java.awt.Image;
     6import java.awt.Polygon;
    67import java.awt.image.BufferedImage;
    78import java.awt.image.VolatileImage;
     
    910import java.io.FileNotFoundException;
    1011import java.io.IOException;
     12import java.io.StringWriter;
     13import java.io.Writer;
    1114import java.lang.reflect.Method;
    1215import java.util.ArrayList;
     
    1619
    1720import javax.imageio.ImageIO;
     21import javax.script.ScriptEngine;
     22import javax.script.ScriptEngineManager;
    1823
    1924import org.expeditee.gui.AttributeUtils;
     
    3035import org.expeditee.gui.TimeKeeper;
    3136import org.expeditee.importer.FrameDNDTransferHandler;
     37import org.expeditee.io.JavaWriter;
    3238import org.expeditee.items.Item;
    3339import org.expeditee.items.ItemUtils;
     
    11861192       
    11871193        public static void run(String command) throws Exception {
     1194                if(command == null) {
     1195                        MessageBay.warningMessage("Please provide a command to run");
     1196                        return;
     1197                }
    11881198                int firstSpace = command.indexOf(" ");
    11891199                if(firstSpace == -1) {
     
    12141224       
    12151225        public static void run(Item item) throws Exception {
     1226                if(item == null) {
     1227                        MessageBay.warningMessage("Please provide a command to run");
     1228                        return;
     1229                }
    12161230                run(((Text)item).getText());
    12171231        }
     1232       
     1233        // Javascript stuff. Probably completely pointless since there is already a Javascript class,
     1234        // but I couldn't get the existing Javascript engine to work for some reason
     1235        private static ScriptEngineManager sem = new ScriptEngineManager();
     1236        private static ScriptEngine se = sem.getEngineByMimeType("application/javascript");
     1237       
     1238        private static String getScript(Frame frame) throws Exception {
     1239                Writer writer = new StringWriter();
     1240                JavaWriter jw = new JavaWriter();
     1241                jw.writeFrame(frame, writer);
     1242                writer.flush();
     1243                String script = writer.toString();
     1244                writer.close();
     1245                return script;
     1246        }
     1247       
     1248        public static void runJSFrame(Frame frame) throws Exception {
     1249                se.eval(getScript(frame));
     1250        }
     1251       
     1252        public static void runJSItem(Item item) throws Exception {
     1253                // if the user clicked on the action without an item on their cursor
     1254                if(item.hasAction()) {
     1255                        for(String s : item.getAction()) {
     1256                                if(!s.equalsIgnoreCase("runJSItem")) {
     1257                                        return;
     1258                                }
     1259                        }
     1260                       
     1261                        // Check if there is an arrow pointing from the action to a link
     1262                        // Seemed like a cool idea before I made it, but the arrows kind of get in the way when trying to click on the action
     1263                        Collection<Item> itemsWithin = item.getParentOrCurrentFrame().getItemsWithin(item.getPolygon());
     1264                        for(Item i : itemsWithin) {
     1265                                if(i instanceof Line && itemsWithin.contains(((Line)i).getStartItem())) {
     1266                                        Polygon p = ((Line)i).getEndItem().getPolygon();
     1267                                        for(Item j : ((Line)i).getEndItem().getParent().getAllItems()) {
     1268                                                if(p.intersects(j.getArea().getBounds2D()) && j instanceof Text && j.hasLink()) {
     1269                                                        Frame child = j.getChild();
     1270                                                        if(child == null) {
     1271                                                                continue;
     1272                                                        }
     1273                                                        runJSFrame(child);
     1274                                                        return;
     1275                                                }
     1276                                        }
     1277                                }
     1278                        }
     1279                       
     1280                        runJSFrame(item.getParentOrCurrentFrame());
     1281                        return;
     1282                }
     1283                Frame frame = item.getChild();
     1284                if(frame == null) {
     1285                        frame = FrameIO.CreateNewFrame(item);
     1286                        item.setLink("" + frame.getNumber());
     1287                        frame.addText(100, 100, "print(\"test\");", null);
     1288                        FrameIO.SaveFrame(frame);
     1289                } else {
     1290                        se.eval(getScript(frame));
     1291                }
     1292        }
    12181293}
Note: See TracChangeset for help on using the changeset viewer.