blob: c645aac40b5acec8b01bf5cf96f18a24aa35bb23 [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001package tai_ofa;
2
3import java.util.Locale;
4import javafx.scene.layout.StackPane;
5import javafx.scene.web.WebView;
6import javax.print.Doc;
7import javax.swing.tree.DefaultTreeCellEditor;
8import org.w3c.dom.Document;
9import org.w3c.dom.Element;
10
11/*
12
13 * TestON is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation, either version 2 of the License, or
16 * (at your option) any later version.
17
18 * TestON is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22
23 * You should have received a copy of the GNU General Public License
24 * along with TestON. If not, see <http://www.gnu.org/licenses/>.
25
26
27*/
28
29public class CodeEditor extends StackPane {
30
31 /**
32 * a webview used to encapsulate the JavaScript.
33 */
34 final WebView webview = new WebView();
35 /**
36 * a snapshot of the code to be edited kept for easy initialization and
37 * reversion of editable code.
38 */
39 private String editingCode;
40 /**
41 * a template for editing code - this can be changed to any template derived
42 * from the supported modes at java to allow syntax highlighted editing of a
43 * wide variety of languages.
44 */
45 TAILocale label = new TAILocale(new Locale("en", "EN"));
46 String editorScriptsPath = label.OFAHarnessPath;
47 private final String editingTemplate =
48 "<!doctype html>"
49 + "<html>"
50 + "<head>"
51 + " <link rel=\"stylesheet\" href=\"file://editorScriptPath/codemirror.css\">".replace("editorScriptPath", editorScriptsPath + "/EditorScripts")
52 + " <script src=\"file://editorScriptPath/codemirror.js\"></script>".replace("editorScriptPath", editorScriptsPath + "/EditorScripts")
53 + " <script src=\"file://editorScriptPath/clike.js\"></script>".replace("editorScriptPath", editorScriptsPath + "/EditorScripts")
54 + " <script src=\"file://editorScriptPath/javascript-hint.js\"></script>".replace("editorScriptPath", editorScriptsPath + "/EditorScripts")
55 + " <script src=\"file://editorScriptPath/search.js\"></script>".replace("editorScriptPath", editorScriptsPath + "/EditorScripts")
56 + " <script src=\"file://editorScriptPath/dialog.js\"></script>".replace("editorScriptPath", editorScriptsPath + "/EditorScripts")
57 + " <script src=\"file://editorScriptPath/searchcursor.js\"></script>".replace("editorScriptPath", editorScriptsPath + "/EditorScripts")
58 + " <script src=\"file://editorScriptPath/simple-hint.js\"></script>".replace("editorScriptPath", editorScriptsPath + "/EditorScripts")
59 + " <link rel=\"stylesheet\" href=\"file://editorScriptPath/simple-hint.css\">".replace("editorScriptPath", editorScriptsPath + "/EditorScripts")
60 + " <script src=\"file://editorScriptPath/javascript-hint.js\"></script>".replace("editorScriptPath", editorScriptsPath + "/EditorScripts")
61 + " <script src=\"file://editorScriptPath/foldcode.js\"></script>".replace("editorScriptPath", editorScriptsPath + "/EditorScripts")
62 + " <script src=\"file://editorScriptPath/perl.js\"></script>".replace("editorScriptPath", editorScriptsPath + "/EditorScripts")
63 + " <script src=\"file://editorScriptPath/xml.js\"></script>".replace("editorScriptPath", editorScriptsPath + "/EditorScripts")
64 + "</head>"
65 + "<body>"
66 + "<form><textarea id=\"code\" name=\"code\">\n"
67 + "${code}"
68 + "</textarea></form>"
69 + "<script>"
70 + "var editor;"
71 + "editor = CodeMirror.fromTextArea(document.getElementById(\"code\"), {"
72 + "mode: \"perl\","
73 + "lineNumbers: true,"
74 + " });"
75 + "</script>"
76 + "</body>"
77 + "</html>";
78
79 /**
80 * applies the editing template to the editing code to create the
81 * html+javascript source for a code editor.
82 */
83 private String applyEditingTemplate() {
84
85 editingTemplate.replace("${code}", editingCode);
86 return editingTemplate.replace("${code}", editingCode);
87
88 }
89
90 /**
91 * sets the current code in the editor and creates an editing snapshot of
92 * the code which can be reverted to.
93 */
94 public void setCode(String newCode) {
95 this.editingCode = newCode;
96 webview.getEngine().loadContent(applyEditingTemplate());
97
98 // webview.getStylesheets().add("eclipse.css");
99 }
100
101 public String getCurrentLine() {
102 return (String) webview.getEngine().executeScript("editor.getLine(editor.getCursor().line);");
103 }
104
105 public String getCurrentLineNumber() {
106
107 return webview.getEngine().executeScript("editor.getLineNumber(editor.getCursor().line);").toString();
108
109 }
110
111 public String getCurrentLine(int lineNumber) {
112 Integer lines = lineNumber;
113 return (String) webview.getEngine().executeScript("editor.getLine(line);".replace("line", lines.toString()));
114
115 }
116
117 public void setLine(int lineNumber, String text) {
118 String lineToSet = "editor.setLine(" + lineNumber + ",'lineText');";
119 webview.getEngine().executeScript(lineToSet.replace("lineText", text));
120 }
121
122 /**
123 * returns the current code in the editor and updates an editing snapshot of
124 * the code which can be reverted to.
125 */
126 public String getCodeAndSnapshot() {
127 //Document doc = webview.getEngine().getDocument();
128// Element el = doc.getElementById("code");
129 webview.getEngine().executeScript("editor.refresh();");
130 this.editingCode = (String) webview.getEngine().executeScript("editor.getValue();");
131
132 return editingCode;
133 }
134
135 public int cursorPosfromTop() {
136 return (Integer) webview.getEngine().executeScript("editor.cursorTopPos();");
137
138 }
139
140 public int cursorPosfromLeft() {
141 return (Integer) webview.getEngine().executeScript("editor.cursorLeftPos();");
142
143 }
144
145 public String test() {
146 return (String) webview.getEngine().executeScript("editor.find();");
147
148 }
149
150 public void clearMarker(String line) {
151 int lineNumber = Integer.parseInt(line) - 1;
152 String lineNumberString = "editor.clearMarker(clearGutter);".replace("clearGutter", String.valueOf(lineNumber));
153 webview.getEngine().executeScript(lineNumberString);
154 }
155
156 public void SetError(String line, final String errorType) {
157
158 final String tooltip = "editor.setMarker(line-1, \"<a id='error' title='errorType \" + \"'><img src='file://editorScriptPath/Delete.png'/></a>%N%\"); ".replace("editorScriptPath", editorScriptsPath + "/EditorScripts");
159 Integer lineCount = (Integer) webview.getEngine().executeScript("editor.lineCount();");
160 int i;
161 for (i = 1; i <= lineCount; i++) {
162 String tooltipToExcute = tooltip.replace("line", line).replace("errorType", errorType);
163 if (!"".equals(errorType)) {
164 webview.getEngine().executeScript(tooltipToExcute);
165 }
166 }
167
168 }
169
170 public void SetWarning(String line, final String errorType) {
171
172 final String tooltip = "editor.setMarker(line-1, \"<a id='error' title='errorType \" + \"'><img src='file://editorScriptPath/Warning.png'/></a>%N%\"); ".replace("editorScriptPath", editorScriptsPath + "/EditorScripts");
173 Integer lineCount = (Integer) webview.getEngine().executeScript("editor.lineCount();");
174 int i;
175 for (i = 1; i <= lineCount; i++) {
176 String tooltipToExcute = tooltip.replace("line", line).replace("errorType", errorType);
177 if (!"".equals(errorType)) {
178 webview.getEngine().executeScript(tooltipToExcute);
179 }
180 }
181
182 }
183
184 public void SetInfo(String line, final String errorType) {
185
186 final String tooltip = "editor.setMarker(line-1, \"<a id='error' title='errorType \" + \"'><img src='file://editorScriptPath/info.jpg'/></a>%N%\"); ".replace("editorScriptPath", editorScriptsPath + "/EditorScripts");
187 Integer lineCount = (Integer) webview.getEngine().executeScript("editor.lineCount();");
188 int i;
189 for (i = 1; i <= lineCount; i++) {
190 String tooltipToExcute = tooltip.replace("line", line).replace("errorType", errorType);
191 if (!"".equals(errorType)) {
192 webview.getEngine().executeScript(tooltipToExcute);
193 }
194 }
195
196 }
197
198 /**
199 * revert edits of the code to the last edit snapshot taken.
200 */
201 public void revertEdits() {
202 setCode(editingCode);
203 }
204
205 /**
206 * Create a new code editor.
207 *
208 * @param editingCode the initial code to be edited in the code editor.
209 */
210 CodeEditor(String editingCode) {
211 this.editingCode = editingCode;
212
213 // webview.setPrefSize(650, 325);
214 // webview.setMinSize(150, 325);
215 webview.getEngine().loadContent(applyEditingTemplate());
216
217
218 this.getChildren().add(webview);
219 }
220}