Ace Editor as a Syntax Highlighter Widget

« back to the blog

This page shows how to use Ace as a syntax highlighter (read-only), so that it doesn't have any scroll bars. Try resizing the window to see line wrapping in action. Everything needed is on this page, so just view the source.

The code shown is from

// This example is from the book _JavaScript: The Definitive Guide_. // Written by David Flanagan. Copyright (c) 1996 O'Reilly & Associates. // This example is provided WITHOUT WARRANTY either expressed or implied. // You may study, use, modify, and distribute it for any purpose. // This is a Java code, not JavaScript code. import netscape.javascript.JSObject; // these are the classes we'll use import java.applet.Applet; import; // An output stream that sends HTML text to a newly created Web browser window. public class HTMLOutputStream extends OutputStream { JSObject main_window; // the initial browser window JSObject window; // the new window we create JSObject document; // the document of that new window static int window_num = 0; // used to give each new window a unique name // To create a new HTMLOutputStream, you must specify the applet that // will use it (this specifies a browser window) and the desired size // for the new window. public HTMLOutputStream(Applet applet, int width, int height) { // get main browser window from the applet with JSObject.getWindow() main_window = JSObject.getWindow(applet); // use JSObject.eval() to create a new window window = (JSObject) main_window.eval("''," + "'HTMLOutputStream" + window_num++ + "'," + "'menubar,status,resizable,scrollbars," + "width=" + width + ",height=" + height + "')"); // use JSObject.getMember() to get the document of this new window document = (JSObject) window.getMember("document"); // Then use to open this document."open", null); } // This is the write() method required for all OutputStream subclasses. public void write(byte[] chars, int offset, int length) { // Create a string from the specified bytes String s = new String(chars, 0, offset, length); // Store the string in an array for use with Object[] args = { s }; // check to see if the window has been closed boolean closed = ((Boolean)window.getMember("closed")).booleanValue(); // If not, use to invoke document.write() if (!closed)"write", args); } // Here are two variants on the above method, also required. public void write(byte[] chars) { write(chars, 0, chars.length); } public void write(int c) { byte[] chars = {(byte)c}; write(chars, 0, 1); } // When the stream is closed, use to call Document.close public void close() {"close", null); } // This method is unique to HTMLOutputStream. If the new window is // still open, use to invoke Window.close() to close it. public void close_window() { boolean closed = ((Boolean)window.getMember("closed")).booleanValue(); if (!closed)"close", null); } }

Have some text, just to prove that the editor has ended

« back to the blog