source: trunk/src/org/expeditee/actions/Python.java@ 1102

Last change on this file since 1102 was 1102, checked in by davidb, 6 years ago

Reworking of the code-base to separate logic from graphics. This version of Expeditee now supports a JFX graphics as an alternative to SWING

File size: 4.0 KB
Line 
1/**
2 * Python.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.util.List;
22
23import javax.script.Invocable;
24
25import org.expeditee.gui.Frame;
26import org.expeditee.gui.MessageBay;
27import org.expeditee.items.Item;
28import org.expeditee.items.Text;
29
30/**
31 * Javascript parser.
32 * Works differently to the other Javascript class in that it
33 * parses a frame as a whole rather than parsing individual text items as separate statements
34 *
35 * @author jts21
36 * @author davidb
37 */
38public class Python extends ScriptBase {
39
40 protected void init() {
41 ERROR_FRAMESET = "PythonErrors";
42 scriptEngine = scriptEngineManager.getEngineByMimeType("application/python");
43 scriptEngine.put("invocable", (Invocable) scriptEngine);
44 }
45
46 public static void printPythonFrame(Item item) {
47 if(item.getChild() == null) {
48 // if the user clicked on the action without an item on their cursor
49 if(item.hasAction()) {
50 boolean isThis = false;
51 for(String s : item.getAction()) {
52 if(s.equalsIgnoreCase("printPythonFrame")) {
53 isThis = true;
54 break;
55 }
56 }
57 if(isThis) {
58 System.out.println(new Python(item.getParentOrCurrentFrame(), true));
59 return;
60 }
61 }
62 MessageBay.warningMessage("Requires either an item with a link to a frame, or no item (will run the current frame)");
63 } else {
64 System.out.println(new Python(item.getChild(), true));
65 }
66 }
67
68 public static void runPythonFrame(Item item) throws Exception {
69 if(item.getChild() == null) {
70 // if the user clicked on the action without an item on their cursor
71 if(item.hasAction()) {
72 boolean isThis = false;
73 for(String s : item.getAction()) {
74 if(s.equalsIgnoreCase("runPythonFrame")) {
75 isThis = true;
76 break;
77 }
78 }
79 if(isThis) {
80 Python.runFrameFollow(item.getParentOrCurrentFrame());
81 return;
82 }
83 }
84 MessageBay.warningMessage("Requires either an item with a link to a frame, or no item (will run the current frame)");
85 } else {
86 Python.runFrameFollow(item.getChild());
87 }
88 }
89
90
91 private static synchronized void runFrameFollow(Frame frame) throws Exception {
92 Python python = new Python(frame, true);
93 python.runFrame(frame,true);
94 }
95
96 private Python(Frame frame, boolean followLinks) {
97 super(frame,followLinks);
98 }
99
100
101 protected void processItems(List<Item> overlapping_y_ordered_items, boolean followLinks, int depth) {
102
103 // Loop through the items looking for code and links to new frames
104 for(Item i : overlapping_y_ordered_items) {
105 if(followLinks && i.hasLink()) {
106 Frame child = i.getChild();
107 if(child != null && !seen.contains(child)) {
108 this.parseFrame(child, true, depth);
109 }
110 }
111 if(i instanceof Text && !i.isAnnotation()) {
112 String text = ((Text)i).getText();
113 if (i == org.expeditee.io.flowlayout.XGroupItem.GROUPSEP_START) {
114 text = null;
115 depth++;
116 }
117 else if (i == org.expeditee.io.flowlayout.XGroupItem.GROUPSEP_END) {
118 text = null;
119 depth--;
120 }
121
122 if (text != null) {
123 int lineNumber = 0;
124 for(String line : text.trim().split("[\\n\\r]+")) {
125 if (depth>0) {
126 sb.append(String.format("%1$" + (4*depth) + "s", "")); // indent with 'depth' x 4 spaces
127 }
128 sb.append(line).append("\n");
129 lines.add(new CodeLine((Text)i, lineNumber++, line));
130 }
131 }
132 }
133 }
134
135
136 }
137
138}
Note: See TracBrowser for help on using the repository browser.