📄 javascriptglobal.java
字号:
} } /** * Create all non-function properties for this global. Sub-classes should override this method to add their own * non-function properties to this global */ protected void createProperties() { // create standard error stream, cache, and add to global this._err = new JavaScriptPrintStream(this, System.err); this.defineProperty("err", _err, READONLY | PERMANENT); //$NON-NLS-1$ // get standard out stream and add to global MessageConsoleStream stream = getConsoleStream(); this.defineProperty("out", stream, READONLY | PERMANENT); //$NON-NLS-1$ } /** * Load a script into a currently executing library * * @param cx * The scripting context * @param thisObj * The object that activated this function call * @param args * The arguments passed to this function call * @param funObj * The function object that invoked this method */ public static void include(Context cx, Scriptable thisObj, Object[] args, Function funObj) { if (args.length > 0) { String libraryName = Context.toString(args[0]); java.io.File library = new java.io.File(libraryName); if (library.exists() == false) { String location = Context.toString(thisObj.get(LOCATION_PROPERTY, thisObj)); java.io.File parent = new java.io.File(location).getParentFile(); String name = parent.getAbsolutePath() + java.io.File.separator + libraryName; library = new java.io.File(name); } if (library.exists()) { String absolutePath = library.getAbsolutePath(); Scriptable includes; if (thisObj.has(INCLUDES_PROPERTY, thisObj)) { includes = (Scriptable) thisObj.get(INCLUDES_PROPERTY, thisObj); } else { includes = cx.newObject(thisObj); ((ScriptableObject) thisObj).defineProperty(INCLUDES_PROPERTY, includes, READONLY | PERMANENT); } // only compile and execute script if we haven't already if (includes.has(absolutePath, includes) == false) { try { // grab the script's source String source = JavaScriptGlobal.getText(new FileInputStream(library)); // compile the script Script script = cx.compileString(source, absolutePath, 1, null); // tag as loaded includes.put(absolutePath, includes, ""); // exec script.exec(cx, thisObj); } catch (IOException e) { // I/O error reading library } } } else { // cannot locate include } } else { // no include file defined } } /** * Load the specified bundle and add it to the active JS class loader * * @param cx * The scripting context * @param thisObj * The object that activated this function call * @param args * The arguments passed to this function call * @param funObj * The function object that invoked this method */ public static void loadBundle(Context cx, Scriptable thisObj, Object[] args, Function funObj) { if (args.length > 0) { String bundleName = Context.toString(args[0]); Bundle bundle = Platform.getBundle(bundleName); if (bundle == null) { throw new RuntimeException("Global_Bundle_Not_Found: " + bundleName); } ClassLoader c = cx.getApplicationClassLoader(); if (c instanceof JavaScriptClassLoader) { JavaScriptClassLoader classLoader = (JavaScriptClassLoader) c; classLoader.addBundle(bundle); } else { throw new RuntimeException("JavaScriptClassLoader not the application Classloader."); } } } /** * Parse the specified XML string and return a W3C DOM as a result * * @param xml * The source XML string * @return Document The resulting Document element or null */ public Document parseXML(String xml) { Document result = null; if (xml != null && xml.length() > 0) { try { // get the document builder factory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // create the document builder DocumentBuilder builder = factory.newDocumentBuilder(); // create a stream from our XML string InputStream in = new ByteArrayInputStream(xml.getBytes()); // parse XML to create our document element result = builder.parse(in); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return result; } /** * Popup an prompt box with the specified message * * @param cx * The scripting context * @param thisObj * The object that activated this function call * @param args * The message to display in the prompt dialog * @param funObj * The function object that invoked this method * @return boolean */ public static Object prompt(Context cx, Scriptable thisObj, Object[] args, Function funObj) { final Display currentDisplay = Display.getCurrent(); String messageArg = "Prompt"; String defaultValueArg = ""; if (args.length > 0) { messageArg = Context.toString(args[0]); } if (args.length > 1) { defaultValueArg = Context.toString(args[1]); } /** * Answer */ class Answer { public Object result = ""; } final String message = messageArg; final String defaultValue = defaultValueArg; final Answer a = new Answer(); if (currentDisplay != null) { currentDisplay.syncExec(new Runnable() { public void run() { Shell shell = currentDisplay.getActiveShell(); if (shell != null) { InputDialog dialog = new InputDialog(null, "Eclipse Monkey Prompt", message, defaultValue, null); //$NON-NLS-1$ int dialogResult = dialog.open(); if (dialogResult == Window.OK) { a.result = dialog.getValue(); } else { a.result = Context.getUndefinedValue(); } } } }); } return a.result; } /** * Execute the specified function on the UI thread * * @param cx * The scripting context * @param thisObj * The object that activated this function call * @param args * The message to display in the prompt dialog * @param funObj * The function object that invoked this method */ public static void runOnUIThread(Context cx, Scriptable thisObj, Object[] args, Function funObj) { if (args.length > 0 && args[0] instanceof Function) { Function f = (Function) args[0]; // get display final IWorkbench workbench = PlatformUI.getWorkbench(); Display display = workbench.getDisplay(); // execute callback in the correct thread display.asyncExec(new JavaScriptThread(f.getParentScope(), f, new Object[0])); } } /** * setClassLoader * * @param cx * @param thisObj * @param args * @param funObj * @return new class loader or class loader passed into method */ public static ClassLoader setClassLoader(Context cx, Scriptable thisObj, Object[] args, Function funObj) { ClassLoader result = null; if (args.length > 0) { Object arg = args[0]; if (arg instanceof ClassLoader) { result = cx.getApplicationClassLoader(); cx.setApplicationClassLoader((ClassLoader) arg); } }// else// {// result = cx.getApplicationClassLoader();// // cx.setApplicationClassLoader(new JavaScriptClassLoader());// } return result; } /** * setTimeout * * @param function * The function to invoke once this timer completes * @param timeout * The amount of time to wait in milliseconds before firing the specified function * @return int Returns a handle to this specific timeout instance. This handle can be used in clearTimeout to cancel * the timer before it fires */ public int setTimeout(final Function function, final int timeout) { final JavaScriptGlobal self = this; final int timeoutIndex = self._setTimeoutIndex++; Thread thread = new Thread(new Runnable() { public void run() { boolean active = true; try { // sleep Thread.sleep(timeout); // check if this was canceled while sleeping synchronized (self._runningSetTimeouts) { active = self._runningSetTimeouts.containsKey(new Integer(timeoutIndex)); } // call the associated function if this is still active if (active) { // get display final IWorkbench workbench = PlatformUI.getWorkbench(); if (workbench == null) { return; } Display display = workbench.getDisplay(); if (display == null || display.isDisposed()) { return; } // execute callback in the correct thread display.asyncExec(new Runnable() { public void run() { Scriptable scope = function.getParentScope(); Context cx = Context.enter(); function.call(cx, scope, scope, new Object[0]); Context.exit(); self.clearTimeout(timeoutIndex); } }); } } catch (InterruptedException e) { e.printStackTrace(); } } }, "Eclipse Monkey JavaScript setTimeout"); //$NON-NLS-1$ // save reference to this thread to indicate that it should run. Note that clearTimeout will remove this entry // and if that is done before the thread wakes, then the action in the thread will not execute this._runningSetTimeouts.put(new Integer(timeoutIndex), thread); thread.setDaemon(true); // start the thread thread.start(); // return the timeout index in case the thread wants to abort this timeout return timeoutIndex; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -