Changeset 715


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

Move JS actions to Javascript2 class, handle java ClassNotFound exceptions caused by trying to access an non-existant class from javascript

Location:
trunk/src/org/expeditee/actions
Files:
2 edited

Legend:

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

    r708 r715  
    1717import org.expeditee.items.Item;
    1818import org.expeditee.items.ItemUtils;
     19import org.expeditee.items.Line;
    1920import org.expeditee.items.Text;
    2021
     
    3334        private static ScriptEngine se = sem.getEngineByMimeType("application/javascript");
    3435       
    35         public static synchronized void runFrame(Frame frame, boolean followLinks) throws Exception {
     36        public static void printJSFrame(Item item) {
     37                if(item.getChild() == null) {
     38                        // if the user clicked on the action without an item on their cursor
     39                if(item.hasAction()) {
     40                        boolean isThis = false;
     41                        for(String s : item.getAction()) {
     42                                if(s.equalsIgnoreCase("runJSFrame")) {
     43                                        isThis = true;
     44                                        break;
     45                                }
     46                        }
     47                        if(isThis) {
     48                                System.out.println(new Javascript2(item.getParentOrCurrentFrame(), true));
     49                                return;
     50                        }
     51                }
     52                MessageBay.warningMessage("Requires either an item with a link to a frame, or no item (will run the current frame)");
     53                } else {
     54                        System.out.println(new Javascript2(item.getChild(), true));
     55                }
     56        }
     57       
     58        public static void runJSFrame(Item item) throws Exception {
     59                if(item.getChild() == null) {
     60                        // if the user clicked on the action without an item on their cursor
     61                if(item.hasAction()) {
     62                        boolean isThis = false;
     63                        for(String s : item.getAction()) {
     64                                if(s.equalsIgnoreCase("runJSFrame")) {
     65                                        isThis = true;
     66                                        break;
     67                                }
     68                        }
     69                        if(isThis) {
     70                                Javascript2.runFrame(item.getParentOrCurrentFrame(), true);
     71                                return;
     72                        }
     73                }
     74                MessageBay.warningMessage("Requires either an item with a link to a frame, or no item (will run the current frame)");
     75                } else {
     76                        Javascript2.runFrame(item.getChild(), true);
     77                }
     78        }
     79       
     80        private static synchronized void runFrame(Frame frame, boolean followLinks) throws Exception {
    3681                Javascript2 js = new Javascript2(frame, followLinks);
    3782                try {
    3883            se.eval(js.toString());
    3984        } catch (ScriptException e) {
    40                 int lineNumber = e.getLineNumber();
    41                 if(lineNumber != -1) {
    42                         CodeLine cl = js.lines.get(lineNumber - 1);
    43                         Frame errorSourceFrame = cl.item.getParent();
    44                         if(errorSourceFrame == null) {
    45                                 throw new Exception("Failed to find frame on which error occurred");
    46                         }
    47                         Frame errorFrame;
    48                         String title = "Error parsing \"" + errorSourceFrame.getTitle() + "\" (" + errorSourceFrame.getName() + ")";
    49                         if(FrameIO.canAccessFrameset(ERROR_FRAMESET)) {
    50                                 errorFrame = FrameIO.CreateFrame(ERROR_FRAMESET, title, null);
    51                         } else {
    52                                 errorFrame = FrameIO.CreateFrameset(ERROR_FRAMESET, FrameIO.FRAME_PATH);
    53                                 errorFrame.setTitle(title);
    54                         }
    55                         Collection<Item> toAdd = errorSourceFrame.getAllItems();
    56                         toAdd.remove(errorSourceFrame.getTitleItem());
    57                         toAdd.remove(cl.item);
    58                         errorFrame.addAllItems(ItemUtils.CopyItems(toAdd));
    59                         String errorItemText = cl.item.getText().trim();
    60                         String[] errorItemLines = errorItemText.split("[\\n|\\r]+");
    61                         int errorLinePos = 0;
    62                         for(int i = 0; i < cl.line; i++) {
    63                                 errorLinePos += errorItemLines[i].length();
    64                         }
    65                         Text beforeErrorItem = errorFrame.addText(cl.item.getX(), cl.item.getY(),
    66                                         errorItemText.substring(0, errorLinePos), null);
    67                         Text errorItem;
    68                         errorItem = errorFrame.addText(cl.item.getX(),
    69                                         beforeErrorItem.getY() + beforeErrorItem.getBoundsHeight(),
    70                                         errorItemLines[cl.line], null);
    71                         errorItem.setBackgroundColor(Color.RED);
    72                         // todo set width to 80 characters worth
    73                         for(String line : e.getMessage().split("[\\n|\\r]+")) {
    74                                 errorItem.setTooltip("text: " + line);
    75                         }
    76                         errorItem.setTooltip("font: " + Text.MONOSPACED_FONT);
    77                         errorItem.setTooltip("width: " + 80 * 12);
    78                         errorLinePos += errorItemLines[cl.line].length();
    79                         if(++errorLinePos < errorItemText.length()) {
    80                                 errorFrame.addText(cl.item.getX(), errorItem.getY() + errorItem.getBoundsHeight(),
    81                                                 errorItemText.substring(errorLinePos + 1), null);
    82                         }
    83                         errorFrame.change();
    84                         FrameIO.SaveFrame(errorFrame);
    85                         MessageBay.displayMessage("Script failed at line " + lineNumber +  " - `" + cl.source + "`",
    86                                         errorFrame.getName(), MessageBay.ERROR_COLOR, true, null);
    87                         FrameUtils.DisplayFrame(errorFrame, true, true);
     85                js.handleError(e.getMessage(), e.getLineNumber());
     86        } catch (RuntimeException e) {
     87                // there doesn't seem to be a way to safely get the lineNumber on which the error occurred
     88                // so as a workaround we just parse the exception
     89                String detail = e.getCause().getStackTrace()[1].toString();
     90                int lastColon = detail.lastIndexOf(':');
     91                int lastBracket = detail.lastIndexOf(')');
     92                int lineNumber;
     93                if(lastColon == -1 || lastBracket == -1) {
     94                        lineNumber = -1;
     95                } else {
     96                        lineNumber = Integer.parseInt(detail.substring(lastColon + 1, lastBracket));
    8897                }
     98                js.handleError(e.getMessage(), lineNumber);
    8999        }
    90100        }
     
    134144       
    135145                // Finally, retrieve linear list of all Items, (ordered, Y by X, allowing for overlap, nested-boxing, and arrow flow)
    136                 List<Item> overlapping_y_ordered_items = toplevel_xgroup.getYXOverlappingItemList();   
     146                List<Item> overlapping_y_ordered_items = toplevel_xgroup.getYXOverlappingItemList(true);       
    137147               
    138148                // Loop through the items looking for code and links to new frames
     
    155165        }
    156166       
     167        private void handleError(String message, int lineNumber) throws Exception {
     168                // negative line number bad
     169                if(lineNumber < 0) {
     170                        MessageBay.errorMessage("Failed to determine the line on which the error occurred");
     171                        return;
     172                }
     173                // if for some reason the error is after the end of the code, assume it should be the last line
     174                if(lineNumber >= this.lines.size()) {
     175                        lineNumber = this.lines.size() - 1;
     176                }
     177                CodeLine cl = this.lines.get(lineNumber - 1);
     178                Frame errorSourceFrame = cl.item.getParent();
     179                if(errorSourceFrame == null) {
     180                        MessageBay.errorMessage("Failed to find frame on which the error occurred");
     181                        return;
     182                }
     183                Frame errorFrame;
     184                String title = "Error parsing \"" + errorSourceFrame.getTitle() + "\" (" + errorSourceFrame.getName() + ")";
     185                if(FrameIO.canAccessFrameset(ERROR_FRAMESET)) {
     186                        errorFrame = FrameIO.CreateFrame(ERROR_FRAMESET, title, null);
     187                } else {
     188                        errorFrame = FrameIO.CreateFrameset(ERROR_FRAMESET, FrameIO.FRAME_PATH);
     189                        errorFrame.setTitle(title);
     190                }
     191                Collection<Item> toAdd = errorSourceFrame.getAllItems();
     192                toAdd.remove(errorSourceFrame.getTitleItem());
     193                toAdd.remove(cl.item);
     194                errorFrame.addAllItems(ItemUtils.CopyItems(toAdd));
     195                String errorItemText = cl.item.getText().trim();
     196                String[] errorItemLines = errorItemText.split("[\\n|\\r]+");
     197                int errorLinePos = 0;
     198                int x = cl.item.getX();
     199                int y = cl.item.getY();
     200                if(cl.line != 0) {
     201                for(int i = 0; i < cl.line; i++) {
     202                        errorLinePos += errorItemLines[i].length();
     203                }
     204                Text beforeErrorItem = errorFrame.addText(x, y,
     205                                errorItemText.substring(0, errorLinePos), null);
     206                y = beforeErrorItem.getY() + beforeErrorItem.getBoundsHeight();
     207                }
     208                Text errorItem;
     209                errorItem = errorFrame.addText(x, y, errorItemLines[cl.line], null);
     210                errorItem.setBackgroundColor(Color.RED);
     211                for(String line : message.split("[\\n|\\r]+")) {
     212                        errorItem.setTooltip("text: " + line);
     213                }
     214                errorItem.setTooltip("font: " + Text.MONOSPACED_FONT);
     215                errorItem.setTooltip("width: " + 80 * 12);
     216                errorLinePos += errorItemLines[cl.line].length();
     217                if(++errorLinePos < errorItemText.length()) {
     218                        errorFrame.addText(cl.item.getX(), errorItem.getY() + errorItem.getBoundsHeight(),
     219                                        errorItemText.substring(errorLinePos + 1), null);
     220                }
     221                errorFrame.change();
     222                FrameIO.SaveFrame(errorFrame);
     223                MessageBay.displayMessage("Script failed at line " + lineNumber +  " - `" + cl.source + "`",
     224                                errorFrame.getName(), MessageBay.ERROR_COLOR, true, null);
     225                FrameUtils.DisplayFrame(errorFrame, true, true);
     226        }
     227       
    157228        public String toString() {
    158229                return this.sb.toString();
  • trunk/src/org/expeditee/actions/Misc.java

    r708 r715  
    12451245        }
    12461246       
    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)");
    1264                 } else {
    1265                         Javascript2.runFrame(item.getChild(), true);
    1266                 }
    1267         }
    1268        
    12691247        public static void parsePage(Item text) throws Exception {
    12701248                if (!(text instanceof Text) || !FreeItems.textOnlyAttachedToCursor()) {
Note: See TracChangeset for help on using the changeset viewer.