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 |
|
---|
19 | package org.expeditee.actions;
|
---|
20 |
|
---|
21 | import java.awt.Color;
|
---|
22 | import java.util.Collection;
|
---|
23 | import java.util.LinkedList;
|
---|
24 | import java.util.List;
|
---|
25 |
|
---|
26 | import javax.script.Bindings;
|
---|
27 | import javax.script.Invocable;
|
---|
28 | import javax.script.ScriptContext;
|
---|
29 | import javax.script.ScriptEngine;
|
---|
30 | import javax.script.ScriptEngineManager;
|
---|
31 | import javax.script.ScriptException;
|
---|
32 | import javax.script.SimpleScriptContext;
|
---|
33 |
|
---|
34 | import org.expeditee.gui.Frame;
|
---|
35 | import org.expeditee.gui.FrameIO;
|
---|
36 | import org.expeditee.gui.FrameUtils;
|
---|
37 | import org.expeditee.gui.MessageBay;
|
---|
38 | import org.expeditee.io.flowlayout.XGroupItem;
|
---|
39 | import org.expeditee.items.Item;
|
---|
40 | import org.expeditee.items.ItemUtils;
|
---|
41 | import 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 | */
|
---|
50 | public 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 | }
|
---|