Changeset 708


Ignore:
Timestamp:
01/16/14 15:34:01 (10 years ago)
Author:
jts21
Message:

Changes to javascript interpreter, implement detailed error messages

Location:
trunk/src/org/expeditee/actions
Files:
1 added
1 edited

Legend:

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

    r689 r708  
    2525import javax.script.ScriptEngine;
    2626import javax.script.ScriptEngineManager;
     27import javax.script.ScriptException;
    2728
    2829import org.expeditee.gui.AttributeUtils;
     
    12441245        }
    12451246       
    1246         // Javascript stuff. Probably completely pointless since there is already a Javascript class,
    1247         // but I couldn't get the existing Javascript engine to work for some reason
    1248         private static ScriptEngineManager sem = new ScriptEngineManager();
    1249         private static ScriptEngine se = sem.getEngineByMimeType("application/javascript");
    1250        
    1251         private static class JSParser {
    1252                 private List<Frame> seen = new LinkedList<Frame>();
    1253                 private StringBuffer sb = new StringBuffer();
    1254                
    1255                 public JSParser(Frame frame, boolean followLinks) throws Exception {
    1256                         parseScript(frame, followLinks);
    1257                 }
    1258                
    1259                 private void parseScript(Frame frame, boolean followLinks) throws Exception {
    1260                        
    1261                         if(frame == null) {
    1262                                 return;
    1263                         }
    1264                        
    1265                         // make sure we don't get into an infinite loop
    1266                         // TODO: find a smarter way to do this that allows reusing frames but still stops infinite loops?
    1267                         seen.add(frame);
    1268                        
    1269                         // get all items on the frame
    1270                         List<Item> y_ordered_items = (List<Item>)frame.getItems();
    1271                         // remove the title item
    1272                         y_ordered_items.remove(frame.getTitleItem());
    1273                        
    1274                         XGroupItem toplevel_xgroup = new XGroupItem(frame,y_ordered_items);
    1275                         // ... following on from Steps 1 and 2 in the Constructor in XGroupItem ...
    1276                        
    1277                         // Step 3: Reposition any 'out-of-flow' XGroupItems
    1278                         toplevel_xgroup.repositionOutOfFlowGroups(toplevel_xgroup);
    1279                        
    1280                         // Step 4: Now add in the remaining (nested) XGroupItems
    1281                         List<XGroupItem> grouped_item_list = toplevel_xgroup.getGroupedItemList();
    1282                         toplevel_xgroup.mapInXGroupItemsRecursive(grouped_item_list);
    1283                
    1284                         // Finally, retrieve linear list of all Items, (ordered, Y by X, allowing for overlap, nested-boxing, and arrow flow)
    1285                         List<Item> overlapping_y_ordered_items = toplevel_xgroup.getYXOverlappingItemList();   
    1286                        
    1287                         // Loop through the items looking for code and links to new frames
    1288                         for(Item i : overlapping_y_ordered_items) {
    1289                                 if(followLinks && i.hasLink()) {
    1290                                         Frame child = i.getChild();
    1291                                         if(child != null && !seen.contains(child)) {
    1292                                                 this.parseScript(child, true);
    1293                                         }
    1294                                 }
    1295                                 if(i instanceof Text && !i.isAnnotation()) {
    1296                                         sb.append(((Text)i).getText() + "\n");
    1297                                 }
    1298                         }
    1299                 }
    1300                
    1301                 public String getScript() {
    1302                         System.out.println("*****\n" + sb.toString() + "\n*****");
    1303                         return sb.toString();
    1304                 }
    1305         }
    1306        
    1307         public static void runJSFrame(Frame frame) throws Exception {
    1308                 se.eval(new JSParser(frame, true).getScript());
    1309         }
    1310        
    1311         public static void runJSItem(Item item) throws Exception {
    1312                 // if the user clicked on the action without an item on their cursor
    1313                 if(item.hasAction()) {
    1314                         for(String s : item.getAction()) {
    1315                                 if(!s.equalsIgnoreCase("runJSItem")) {
    1316                                         return;
    1317                                 }
    1318                         }
    1319                         item.getParent().getNonAnnotationText(true);
    1320                         // Check if there is an arrow pointing from the action to a link
    1321                         // 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
    1322                         Collection<Item> itemsWithin = item.getParentOrCurrentFrame().getItemsWithin(item.getPolygon());
    1323                         for(Item i : itemsWithin) {
    1324                                 if(i instanceof Line && itemsWithin.contains(((Line)i).getStartItem())) {
    1325                                         Polygon p = ((Line)i).getEndItem().getPolygon();
    1326                                         for(Item j : ((Line)i).getEndItem().getParent().getAllItems()) {
    1327                                                 if(p.intersects(j.getArea().getBounds2D()) && j instanceof Text && j.hasLink()) {
    1328                                                         Frame child = j.getChild();
    1329                                                         if(child == null) {
    1330                                                                 continue;
    1331                                                         }
    1332                                                         runJSFrame(child);
    1333                                                         return;
    1334                                                 }
    1335                                         }
    1336                                 }
    1337                         }
    1338                        
    1339                         runJSFrame(item.getParentOrCurrentFrame());
    1340                         return;
    1341                 }
    1342                 Frame frame = item.getChild();
    1343                 if(frame == null) {
    1344                         frame = FrameIO.CreateNewFrame(item);
    1345                         item.setLink("" + frame.getNumber());
    1346                         frame.addText(100, 100, "print(\"test\");", null);
    1347                         FrameIO.SaveFrame(frame);
     1247        public static void runJSFrame(Item item) throws Exception {
     1248                if(item.getChild() == null) {
     1249                        // if the user clicked on the action without an item on their cursor
     1250                if(item.hasAction()) {
     1251                        boolean isThis = false;
     1252                        for(String s : item.getAction()) {
     1253                                if(s.equalsIgnoreCase("runJSFrame")) {
     1254                                        isThis = true;
     1255                                        break;
     1256                                }
     1257                        }
     1258                        if(isThis) {
     1259                                Javascript2.runFrame(item.getParentOrCurrentFrame(), true);
     1260                                return;
     1261                        }
     1262                }
     1263                MessageBay.warningMessage("Requires either an item with a link to a frame, or no item (will run the current frame)");
    13481264                } else {
    1349                         runJSFrame(frame);
     1265                        Javascript2.runFrame(item.getChild(), true);
    13501266                }
    13511267        }
Note: See TracChangeset for help on using the changeset viewer.