source: trunk/src/org/expeditee/actions/Javascript2.java@ 1080

Last change on this file since 1080 was 1080, checked in by davidb, 7 years ago

Updates to cope with changes in Java ScriptEngine

File size: 3.4 KB
Line 
1/**
2 * Javascript2.java
3 * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package org.expeditee.actions;
20
21import java.awt.Color;
22import java.util.Collection;
23import java.util.LinkedList;
24import java.util.List;
25
26import javax.script.Bindings;
27import javax.script.Invocable;
28import javax.script.ScriptContext;
29import javax.script.ScriptEngine;
30import javax.script.ScriptEngineManager;
31import javax.script.ScriptException;
32import javax.script.SimpleScriptContext;
33
34import org.expeditee.gui.Frame;
35import org.expeditee.gui.FrameIO;
36import org.expeditee.gui.FrameUtils;
37import org.expeditee.gui.MessageBay;
38import org.expeditee.io.flowlayout.XGroupItem;
39import org.expeditee.items.Item;
40import org.expeditee.items.ItemUtils;
41import org.expeditee.items.Text;
42
43/**
44 * Javascript parser.
45 * Works differently to the other Javascript class in that it
46 * parses a frame as a whole rather than parsing individual text items as separate statements
47 *
48 * @author jts21
49 */
50public class Javascript2 extends ScriptBase {
51
52 protected void init() {
53 ERROR_FRAMESET = "JavascriptErrors";
54 scriptEngine = scriptEngineManager.getEngineByMimeType("application/javascript");
55 scriptEngine.put("invocable", (Invocable) scriptEngine);
56 }
57
58 public static void printJSFrame(Item item) {
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("printJSFrame")) {
65 isThis = true;
66 break;
67 }
68 }
69 if(isThis) {
70 System.out.println(new Javascript2(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 System.out.println(new Javascript2(item.getChild(), true));
77 }
78 }
79
80 public static void runJSFrame(Item item) throws Exception {
81 if(item.getChild() == null) {
82 // if the user clicked on the action without an item on their cursor
83 if(item.hasAction()) {
84 boolean isThis = false;
85 for(String s : item.getAction()) {
86 if(s.equalsIgnoreCase("runJSFrame")) {
87 isThis = true;
88 break;
89 }
90 }
91 if(isThis) {
92 Javascript2.runFrameFollow(item.getParentOrCurrentFrame());
93 return;
94 }
95 }
96 MessageBay.warningMessage("Requires either an item with a link to a frame, or no item (will run the current frame)");
97 } else {
98 Javascript2.runFrameFollow(item.getChild());
99 }
100 }
101
102
103 private static synchronized void runFrameFollow(Frame frame) throws Exception {
104 Javascript2 js = new Javascript2(frame, true);
105 js.runFrame(frame,true);
106 }
107
108
109 private Javascript2(Frame frame, boolean followLinks) {
110 super(frame,followLinks);
111 }
112
113}
Note: See TracBrowser for help on using the repository browser.