X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2Fdemos%2FDemoApplication.java;h=d9bbeb765db92d45aca5537fe274f9b288c42c92;hb=a043164fd1cc1b38f03bb104f8b5240cdf5705c6;hp=4c5ba319cc97669a7bee02c2e93408017bc83188;hpb=e3dfbd233442a877d5efa1bc177c3d357771e5cb;p=fanfix.git diff --git a/src/jexer/demos/DemoApplication.java b/src/jexer/demos/DemoApplication.java index 4c5ba31..d9bbeb7 100644 --- a/src/jexer/demos/DemoApplication.java +++ b/src/jexer/demos/DemoApplication.java @@ -30,6 +30,9 @@ */ package jexer.demos; +import java.io.*; +import java.util.*; + import jexer.*; import jexer.event.*; import jexer.menu.*; @@ -37,16 +40,12 @@ import jexer.menu.*; /** * The demo application itself. */ -class DemoApplication extends TApplication { +public class DemoApplication extends TApplication { /** - * Public constructor. - * - * @param backendType one of the TApplication.BackendType values - * @throws Exception if TApplication can't instantiate the Backend. + * Add all the widgets of the demo. */ - public DemoApplication(BackendType backendType) throws Exception { - super(backendType); + private void addAllWidgets() { new DemoMainWindow(this); // Add the menus @@ -76,6 +75,75 @@ class DemoApplication extends TApplication { item = subMenu.addItem(2002, "&Normal (sub)"); addWindowMenu(); + } + + /** + * Public constructor. + * + * @param input an InputStream connected to the remote user, or null for + * System.in. If System.in is used, then on non-Windows systems it will + * be put in raw mode; shutdown() will (blindly!) put System.in in cooked + * mode. input is always converted to a Reader with UTF-8 encoding. + * @param output an OutputStream connected to the remote user, or null + * for System.out. output is always converted to a Writer with UTF-8 + * encoding. + * @throws UnsupportedEncodingException if an exception is thrown when + * creating the InputStreamReader + */ + public DemoApplication(final InputStream input, + final OutputStream output) throws UnsupportedEncodingException { + super(input, output); + addAllWidgets(); + } + /** + * Handle menu events. + * + * @param menu menu event + * @return if true, the event was processed and should not be passed onto + * a window + */ + @Override + public boolean onMenu(TMenuEvent menu) { + if (menu.getId() == TMenu.MID_OPEN_FILE) { + try { + String filename = fileOpenBox("."); + if (filename != null) { + try { + File file = new File(filename); + StringBuilder fileContents = new StringBuilder(); + Scanner scanner = new Scanner(file); + String EOL = System.getProperty("line.separator"); + + try { + while(scanner.hasNextLine()) { + fileContents.append(scanner.nextLine() + EOL); + } + new DemoTextWindow(this, filename, + fileContents.toString()); + } finally { + scanner.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + return true; + } + return super.onMenu(menu); + } + + /** + * Public constructor. + * + * @param backendType one of the TApplication.BackendType values + * @throws Exception if TApplication can't instantiate the Backend. + */ + public DemoApplication(BackendType backendType) throws Exception { + super(backendType); + addAllWidgets(); } }