// send a netlogo command asynchronously; receive notice of completion at this callback public void asynchronousCommand(final String cmd, final String callback) { final JSObject window = JSObject.getWindow(this); if (SwingUtilities.isEventDispatchThread()) { Thread t = new Thread("command thread off of EDT") { public void run() { try { panel().command(cmd); window.call(callback, null); } catch (CompilerException e) { e.printStackTrace(); } } }; t.start(); } else { try { panel().command(cmd); window.call(callback, null); } catch (CompilerException e) { e.printStackTrace(); } } }
// evaluate a netlogo report asynchronously; return the result to this callback as the argument public void asynchronousReport(final String cmd, final String callback) { final JSObject window = JSObject.getWindow(this); if (SwingUtilities.isEventDispatchThread()) { Thread t = new Thread("reporter thread off of EDT") { public void run() { Object retval = null; try { retval = panel().report(cmd); Object[] args = {retval}; window.call(callback, args); } catch (CompilerException e) { e.printStackTrace(); } } }; t.start(); } else { Object retval = null; try { retval = panel().report(cmd); Object[] args = {retval}; window.call(callback, args); } catch (CompilerException e) { e.printStackTrace(); } } }
public void alert(String s) { if (jso != null) try { jso.call("alert", new String[] {s}); } catch (Exception ex) { ex.printStackTrace(); } }
@Override public void switchChanged(String name, boolean value, boolean arg2) { String val = "NO"; if (value) { val = "YES"; } Object[] args = {"switch-changed", name, val}; window.call(callback, args); }
@FXML private void setCode() { if (submission.getSelectionModel().isEmpty()) return; try { obj.call( "setCode", api.getCode( submission.getSelectionModel().getSelectedItem().getId(), contest.getSelectionModel().getSelectedItem().getValue())); } catch (Exception e) { getAlert(e, "提出コードの取得に失敗しました。").show(); return; } languageLabel.setText(submission.getSelectionModel().getSelectedItem().getLanguage()); Extension lang = Extension.of(languageLabel.getText()); if (Stream.of(Extension.C, Extension.CPP).anyMatch(e -> e.equals(lang))) obj.call("setMode", "c_cpp"); else obj.call("setMode", lang.toString().toLowerCase()); }
public void setString(String newval) { JSObject firstChild = null; // used in SPAN try { if (nodeName == OBJECT_NAME || nodeName == APPLET_NAME) { Object[] args = {newval}; jsObject.call("setString", args); return; } if (nodeName == SPAN_NAME) { Object[] args = {newval}; firstChild = (JSObject) jsObject.getMember("firstChild"); if (firstChild == null) { // FIX create the text child node System.err.println("FIX: no child for " + jsObject); } firstChild.setMember("nodeValue", newval); return; } if (nodeName == INPUT_NAME) { String inputType = jsObject.getMember("type").toString().intern(); if (inputType.equals(RADIO_INPUT)) { JSObject radioGroup = groupOfRadioInput(jsObject); int groupLength = ((Double) radioGroup.getMember("length")).intValue(); for (int i = 0; i < groupLength; i += 1) { JSObject radioInGroup = (JSObject) radioGroup.getSlot(i); String radiosVal = (String) radioInGroup.getMember("value"); if (radiosVal.equals(newval)) radioInGroup.setMember("checked", Boolean.TRUE); else radioInGroup.removeMember("checked"); } return; } else { // default INPUT control jsObject.setMember("value", newval); return; } } if (nodeName == TEXTAREA_NAME) { jsObject.setMember("value", newval); return; } } catch (Exception ex) { System.err.println("setString error on " + jsObject); ex.printStackTrace(); return; } // no matches throw new IllegalStateException("setString does not support nodeName " + nodeName); }
/** Redirects to screenbird homepage. */ private void redirectWebPage() { try { JSObject win = JSObject.getWindow(this); System.out.println("Redirecting window"); win.call("redirectHome", new Object[] {}); System.out.println("Redirected window"); } catch (Exception e) { System.err.println(e); System.err.println("Window already closed"); } }
@Override public void sliderChanged( String name, double value, double min, double incr, double max, boolean arg5, boolean arg6) { Object[] args = {"slider-changed", name, value, min, incr, max}; window.call(callback, args); }
/** * Using the Javascript located on the web page which opened this applet, we close the web page; * thus closing the applet with it. */ public boolean closeApplet() { try { JSObject win = JSObject.getWindow(this); System.out.println("Closing window"); win.call("closeRecorderForm", new Object[] {}); System.out.println("Closed window"); } catch (Exception e) { System.err.println(e); System.err.println("Window already closed"); } return false; }
@FXML private void saveCode() { FileChooser fc = new FileChooser(); fc.setTitle("保存先を指定"); fc.setInitialFileName(contest.getSelectionModel().getSelectedItem().getValue()); fc.getExtensionFilters().addAll(Extension.getFilterList()); fc.setSelectedExtensionFilter(Extension.of(languageLabel.getText()).getFilter()); Optional.ofNullable(fc.showSaveDialog(root.getScene().getWindow())) .ifPresent( f -> { try { Files.write(f.toPath(), ((String) obj.call("getCode")).getBytes()); } catch (IOException e) { } }); }
public String getString() { JSObject firstChild = null; // used in SPAN System.out.println(" ..getString() on " + this); try { if (nodeName == OBJECT_NAME || nodeName == APPLET_NAME) { Object[] args = {}; return (String) jsObject.call("getString", args); } if (nodeName == SPAN_NAME) { firstChild = (JSObject) jsObject.getMember("firstChild"); return firstChild.getMember("nodeValue").toString(); } if (nodeName == INPUT_NAME) { String inputType = jsObject.getMember("type").toString().intern(); if (inputType.equals(RADIO_INPUT)) { JSObject radioGroup = groupOfRadioInput(jsObject); int groupLength = ((Double) radioGroup.getMember("length")).intValue(); for (int i = 0; i < groupLength; i += 1) { JSObject radioInGroup = (JSObject) radioGroup.getSlot(i); Boolean checked = (Boolean) radioInGroup.getMember("checked"); if (checked.booleanValue()) return (String) radioInGroup.getMember("value"); } return "*noselection*"; } else { // default INPUT control return jsObject.getMember("value").toString(); } } if (nodeName == TEXTAREA_NAME) { return jsObject.getMember("value").toString(); } } catch (Exception ex) { System.err.println("getString error on " + jsObject); ex.printStackTrace(); return ERROR_VAL; } // no matches throw new IllegalStateException("getString does not support nodeName " + nodeName); }
@Override public Object apply(Object context, Object datum, int index) { Object applyResult = wrappedJsObject.call("apply", datum, index); return applyResult; }
public void addTab(Path path, Runnable... runnables) { ObservableList<String> recentFiles = controller.getRecentFilesList(); if (Files.notExists(path)) { recentFiles.remove(path.toString()); return; } ObservableList<Tab> tabs = controller.getTabPane().getTabs(); for (Tab tab : tabs) { MyTab myTab = (MyTab) tab; Path currentPath = myTab.getPath(); if (Objects.nonNull(currentPath)) if (currentPath.equals(path)) { myTab.select(); // Select already added tab return; } } AnchorPane anchorPane = new AnchorPane(); EditorPane editorPane = webviewService.createWebView(); MyTab tab = createTab(); tab.setEditorPane(editorPane); tab.setTabText(path.getFileName().toString()); editorPane.confirmHandler( param -> { if ("command:ready".equals(param)) { JSObject window = editorPane.getWindow(); window.setMember("afx", controller); window.call("updateOptions", new Object[] {}); Map<String, String> shortCuts = controller.getShortCuts(); Set<String> keySet = shortCuts.keySet(); for (String key : keySet) { window.call("addNewCommand", new Object[] {key, shortCuts.get(key)}); } if (Objects.isNull(path)) return true; threadService.runTaskLater( () -> { String content = IOHelper.readFile(path); threadService.runActionLater( () -> { window.call("changeEditorMode", path.toUri().toString()); window.call("setInitialized"); window.call("setEditorValue", new Object[] {content}); for (Runnable runnable : runnables) { runnable.run(); } }); }); } return false; }); threadService.runActionLater( () -> { TabPane tabPane = controller.getTabPane(); tabPane.getTabs().add(tab); tab.select(); }); Node editorVBox = editorService.createEditorVBox(editorPane, tab); controller.fitToParent(editorVBox); anchorPane.getChildren().add(editorVBox); tab.setContent(anchorPane); tab.setPath(path); Tooltip tip = new Tooltip(path.toString()); Tooltip.install(tab.getGraphic(), tip); recentFiles.remove(path.toString()); recentFiles.add(0, path.toString()); editorPane.focus(); }
@Override public void inputBoxChanged(String name, Object value, boolean arg2) { Object[] args = {"input-box-changed", name, value}; window.call(callback, args); }
@Override public void chooserChanged(String name, Object value, boolean arg2) { Object[] args = {"chooser-changed", name, value}; window.call(callback, args); }
@Override public void tickCounterChanged(double value) { Object[] args = {"tick-counter-changed", value}; window.call(callback, args); }
public void _initKeyboard(double sec) { log("> initKeyboard"); // javascript entry point to discover the keyboard if (!isSecure(sec)) return; if (charMap != null) { dohrobot.call("_onKeyboard", new Object[] {}); return; } AccessController.doPrivileged( new PrivilegedAction() { public Object run() { charMap = new HashMap(); KeyEvent event = new KeyEvent(applet(), 0, 0, 0, KeyEvent.VK_SPACE, ' '); charMap.put(new Integer(32), event); try { // a-zA-Z0-9 + 29 others vkKeys = new Vector(); for (char i = 'a'; i <= 'z'; i++) { vkKeys.add( new Integer( KeyEvent.class .getField("VK_" + Character.toUpperCase((char) i)) .getInt(null))); } for (char i = '0'; i <= '9'; i++) { vkKeys.add( new Integer( KeyEvent.class .getField("VK_" + Character.toUpperCase((char) i)) .getInt(null))); } int[] mykeys = new int[] { KeyEvent.VK_COMMA, KeyEvent.VK_MINUS, KeyEvent.VK_PERIOD, KeyEvent.VK_SLASH, KeyEvent.VK_SEMICOLON, KeyEvent.VK_LEFT_PARENTHESIS, KeyEvent.VK_NUMBER_SIGN, KeyEvent.VK_PLUS, KeyEvent.VK_RIGHT_PARENTHESIS, KeyEvent.VK_UNDERSCORE, KeyEvent.VK_EXCLAMATION_MARK, KeyEvent.VK_DOLLAR, KeyEvent.VK_CIRCUMFLEX, KeyEvent.VK_AMPERSAND, KeyEvent.VK_ASTERISK, KeyEvent.VK_QUOTEDBL, KeyEvent.VK_LESS, KeyEvent.VK_GREATER, KeyEvent.VK_BRACELEFT, KeyEvent.VK_BRACERIGHT, KeyEvent.VK_COLON, KeyEvent.VK_BACK_QUOTE, KeyEvent.VK_QUOTE, KeyEvent.VK_OPEN_BRACKET, KeyEvent.VK_BACK_SLASH, KeyEvent.VK_CLOSE_BRACKET, KeyEvent.VK_EQUALS }; for (int i = 0; i < mykeys.length; i++) { vkKeys.add(new Integer(mykeys[i])); } } catch (Exception e) { e.printStackTrace(); } Thread thread = new Thread() { public void run() { robot.setAutoDelay(0); log("< initKeyboard"); pressNext(); } }; thread.start(); return null; } }); }
@Override public void buttonStopped(String buttonName) { Object[] args = {"button-stopped", buttonName}; window.call(callback, args); }