checkstyle sweep
[fanfix.git] / src / jexer / demos / DemoApplication.java
index 22609620b5f2d56979b53f9c4d2588710b76cf92..f0bdacd3fab241f82b4922287ada4259048c5a5c 100644 (file)
@@ -31,6 +31,7 @@
 package jexer.demos;
 
 import java.io.*;
+import java.util.*;
 
 import jexer.*;
 import jexer.event.*;
@@ -39,7 +40,7 @@ import jexer.menu.*;
 /**
  * The demo application itself.
  */
-class DemoApplication extends TApplication {
+public class DemoApplication extends TApplication {
 
     /**
      * Add all the widgets of the demo.
@@ -75,7 +76,7 @@ class DemoApplication extends TApplication {
 
         addWindowMenu();
     }
-    
+
     /**
      * Public constructor.
      *
@@ -94,14 +95,54 @@ class DemoApplication extends TApplication {
         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(final 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 {
+    public DemoApplication(final BackendType backendType) throws Exception {
         super(backendType);
         addAllWidgets();
     }