admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | package tai_ofa; |
| 2 | |
| 3 | import com.sun.org.apache.xerces.internal.parsers.IntegratedParserConfiguration; |
| 4 | import java.awt.TextArea; |
| 5 | import java.util.ArrayList; |
| 6 | import java.util.Locale; |
| 7 | import javafx.event.EventHandler; |
| 8 | import javafx.scene.Group; |
| 9 | import javafx.scene.Node; |
| 10 | import javafx.scene.control.Button; |
| 11 | import javafx.scene.control.ContextMenu; |
| 12 | import javafx.scene.control.MenuItem; |
| 13 | import javafx.scene.control.Tab; |
| 14 | import javafx.scene.input.MouseEvent; |
| 15 | import javafx.scene.layout.StackPane; |
| 16 | import javafx.scene.web.PopupFeatures; |
| 17 | import javafx.scene.web.WebEngine; |
| 18 | import javafx.scene.web.WebView; |
| 19 | import javafx.util.Callback; |
| 20 | import javax.swing.JOptionPane; |
| 21 | |
| 22 | |
| 23 | /* |
| 24 | * To change this template, choose Tools | Templates and open the template in |
| 25 | * the editor. |
| 26 | */ |
| 27 | /** |
| 28 | * |
| 29 | * @author Raghav Kashyap (raghavkashyap@paxterrasolutions.com) |
| 30 | |
| 31 | * TestON is free software: you can redistribute it and/or modify |
| 32 | * it under the terms of the GNU General Public License as published by |
| 33 | * the Free Software Foundation, either version 2 of the License, or |
| 34 | * (at your option) any later version. |
| 35 | |
| 36 | * TestON is distributed in the hope that it will be useful, |
| 37 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 38 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 39 | * GNU General Public License for more details. |
| 40 | |
| 41 | * You should have received a copy of the GNU General Public License |
| 42 | * along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 43 | |
| 44 | * /** |
| 45 | */ |
| 46 | public class CodeEditorParams extends StackPane { |
| 47 | |
| 48 | TAI_OFA OFAReference; |
| 49 | WebView webview = new WebView(); |
| 50 | private String editingCode; |
| 51 | ContextMenu contextMenu; |
| 52 | TAILocale label = new TAILocale(new Locale("en", "EN")); |
| 53 | String editorScriptsPath = label.OFAHarnessPath; |
| 54 | private final String editingTemplate = |
| 55 | "<!doctype html>" |
| 56 | + "<html>" |
| 57 | + "<head>" |
| 58 | + " <link rel=\"stylesheet\"href=\"file://editorScriptPath/codemirror.css\">".replace("editorScriptPath", editorScriptsPath+"/EditorScripts") |
| 59 | + " <script src=\"file://editorScriptPath/codemirror.js\"></script>".replace("editorScriptPath", editorScriptsPath+"/EditorScripts") |
| 60 | + " <script src=\"file://editorScriptPath/foldcode.js\"></script>".replace("editorScriptPath", editorScriptsPath+"/EditorScripts") |
| 61 | + " <script src=\"file://editorScriptPath/perl.js\"></script>".replace("editorScriptPath", editorScriptsPath+"/EditorScripts") |
| 62 | + " <script src=\"file://editorScriptPath/xml.js\"></script>".replace("editorScriptPath", editorScriptsPath+"/EditorScripts") |
| 63 | + " <style type=\"text/css\">" |
| 64 | + "</style>" |
| 65 | + "</head>" |
| 66 | + "<body>" |
| 67 | + "<form><textarea id=\"code\" name=\"code\">\n" |
| 68 | + "${code}" |
| 69 | + "</textarea></form>" |
| 70 | + "<script>" |
| 71 | + " var foldFunc = CodeMirror.newFoldFunction(CodeMirror.tagRangeFinder);" |
| 72 | + "var editor;" |
| 73 | + "editor = CodeMirror.fromTextArea(document.getElementById(\"code\"), {" |
| 74 | + "mode: \"perl\"," |
| 75 | + " lineNumbers: true," |
| 76 | + "onGutterClick: foldFunc," |
| 77 | + "extraKeys: {\"Ctrl-Q\" : function(cm){foldFunc(cm, cm.getCursor().line);}}" |
| 78 | + " });" |
| 79 | + "</script>" |
| 80 | + "</body>" |
| 81 | + "</html>"; |
| 82 | |
| 83 | /** |
| 84 | * applies the editing template to the editing code to create the |
| 85 | * html+javascript source for a code editor. |
| 86 | */ |
| 87 | private String applyEditingTemplate() { |
| 88 | editingTemplate.replace("${code}", editingCode); |
| 89 | return editingTemplate.replace("${code}", editingCode); |
| 90 | } |
| 91 | |
| 92 | public void setOFA(TAI_OFA reference) { |
| 93 | OFAReference = reference; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * sets the current code in the editor and creates an editing snapshot of |
| 98 | * the code which can be reverted to. |
| 99 | */ |
| 100 | public void setCode(String newCode) { |
| 101 | this.editingCode = newCode; |
| 102 | webview.getEngine().loadContent(applyEditingTemplate()); |
| 103 | } |
| 104 | |
| 105 | public String getCurrentLine() { |
| 106 | return (String) webview.getEngine().executeScript("editor.getLine(editor.getCursor().line);"); |
| 107 | } |
| 108 | |
| 109 | public String getCurrentLineNumber() { |
| 110 | return webview.getEngine().executeScript("editor.getLineNumber(editor.getCursor().line);").toString(); |
| 111 | |
| 112 | } |
| 113 | |
| 114 | public String getCurrentLine(int lineNumber) { |
| 115 | Integer lines = lineNumber; |
| 116 | return (String) webview.getEngine().executeScript("editor.getLine(line);".replace("line", lines.toString())); |
| 117 | |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * returns the current code in the editor and updates an editing snapshot of |
| 122 | * the code which can be reverted to. |
| 123 | */ |
| 124 | public String getCodeAndSnapshot() { |
| 125 | this.editingCode = (String) webview.getEngine().executeScript("editor.getValue();"); |
| 126 | return editingCode; |
| 127 | } |
| 128 | |
| 129 | public void alert() { |
| 130 | webview.getEngine().executeScript("editor.myFunction();"); |
| 131 | } |
| 132 | |
| 133 | public int cursorPosfromTop() { |
| 134 | return (Integer) webview.getEngine().executeScript("editor.cursorTopPos();"); |
| 135 | } |
| 136 | |
| 137 | public int cursorPosfromLeft() { |
| 138 | return (Integer) webview.getEngine().executeScript("editor.cursorLeftPos();"); |
| 139 | } |
| 140 | |
| 141 | public void clearMarker(String line) { |
| 142 | int lineNumber = Integer.parseInt(line) - 1; |
| 143 | String lineNumberString = "editor.clearMarker(clearGutter);".replace("clearGutter", String.valueOf(lineNumber)); |
| 144 | webview.getEngine().executeScript(lineNumberString); |
| 145 | } |
| 146 | |
| 147 | public void SetError(String line, final String errorType) { |
| 148 | |
| 149 | final String tooltip = "editor.setMarker(line-1, \"<a id='error' title='errorType \" + \"'><img src='file://editorScriptPath/Delete.png'/></a>%N%\"); ".replace("editorScriptPath", editorScriptsPath+"/EditorScripts"); |
| 150 | Integer lineCount = (Integer) webview.getEngine().executeScript("editor.lineCount();"); |
| 151 | int i; |
| 152 | for (i = 1; i <= lineCount; i++) { |
| 153 | String tooltipToExcute = tooltip.replace("line", line).replace("errorType", errorType); |
| 154 | if (!"".equals(errorType)) { |
| 155 | webview.getEngine().executeScript(tooltipToExcute); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | } |
| 160 | |
| 161 | public void SetWarning(String line, final String errorType) { |
| 162 | |
| 163 | final String tooltip = "editor.setMarker(line-1, \"<a id='error' title='errorType \" + \"'><img src='file://editorScriptPath/Warning.png'/></a>%N%\"); ".replace("editorScriptPath", editorScriptsPath+"/EditorScripts"); |
| 164 | Integer lineCount = (Integer) webview.getEngine().executeScript("editor.lineCount();"); |
| 165 | int i; |
| 166 | for (i = 1; i <= lineCount; i++) { |
| 167 | String tooltipToExcute = tooltip.replace("line", line).replace("errorType", errorType); |
| 168 | if (!"".equals(errorType)) { |
| 169 | webview.getEngine().executeScript(tooltipToExcute); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | } |
| 174 | |
| 175 | public void SetInfo(String line, final String errorType) { |
| 176 | |
| 177 | final String tooltip = "editor.setMarker(line-1, \"<a id='error' title='errorType \" + \"'><img src='file://editorScriptPath/info.jpg'/></a>%N%\"); ".replace("editorScriptPath", editorScriptsPath+"/EditorScripts"); |
| 178 | Integer lineCount = (Integer) webview.getEngine().executeScript("editor.lineCount();"); |
| 179 | int i; |
| 180 | for (i = 1; i <= lineCount; i++) { |
| 181 | String tooltipToExcute = tooltip.replace("line", line).replace("errorType", errorType); |
| 182 | if (!"".equals(errorType)) { |
| 183 | webview.getEngine().executeScript(tooltipToExcute); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * revert edits of the code to the last edit snapshot taken. |
| 191 | */ |
| 192 | public void revertEdits() { |
| 193 | setCode(editingCode); |
| 194 | } |
| 195 | |
| 196 | CodeEditorParams(String editingCode) { |
| 197 | this.editingCode = editingCode; |
| 198 | webview.getEngine().loadContent(applyEditingTemplate()); |
| 199 | this.getChildren().add(webview); |
| 200 | } |
| 201 | |
| 202 | public void contextMenu() { |
| 203 | contextMenu = new ContextMenu(); |
| 204 | MenuItem myMenuItem = new MenuItem(); |
| 205 | contextMenu.getItems().add(myMenuItem); |
| 206 | } |
| 207 | } |