Bug fixes
[nikiroo-utils.git] / src / jexer / TEditorWindow.java
index f96b177f6f4d1ccbddd44a08ad5d87c5b73afe09..5bf664d254e515f95d41418ef383ae01559dcdb4 100644 (file)
@@ -41,6 +41,7 @@ import jexer.TWidget;
 import jexer.bits.CellAttributes;
 import jexer.bits.GraphicsChars;
 import jexer.event.TCommandEvent;
+import jexer.event.TKeypressEvent;
 import jexer.event.TMouseEvent;
 import jexer.event.TResizeEvent;
 import static jexer.TCommand.*;
@@ -114,6 +115,25 @@ public class TEditorWindow extends TScrollableWindow {
         setupAfterEditor();
     }
 
+    /**
+     * Public constructor opens a file.
+     *
+     * @param parent the main application
+     * @param file the file to open
+     * @throws IOException if a java.io operation throws
+     */
+    public TEditorWindow(final TApplication parent,
+        final File file) throws IOException {
+
+        super(parent, file.getName(), 0, 0, parent.getScreen().getWidth(),
+            parent.getScreen().getHeight() - 2, RESIZABLE);
+
+        filename = file.getName();
+        String contents = readFileData(file);
+        editField = addEditor(contents, 0, 0, getWidth() - 2, getHeight() - 2);
+        setupAfterEditor();
+    }
+
     /**
      * Public constructor.
      *
@@ -123,6 +143,39 @@ public class TEditorWindow extends TScrollableWindow {
         this(parent, "New Text Document");
     }
 
+    /**
+     * Read file data into a string.
+     *
+     * @param file the file to open
+     * @return the file contents
+     * @throws IOException if a java.io operation throws
+     */
+    private String readFileData(final File file) throws IOException {
+        StringBuilder fileContents = new StringBuilder();
+        Scanner scanner = new Scanner(file);
+        String EOL = System.getProperty("line.separator");
+
+        try {
+            while (scanner.hasNextLine()) {
+                fileContents.append(scanner.nextLine() + EOL);
+            }
+            return fileContents.toString();
+        } finally {
+            scanner.close();
+        }
+    }
+
+    /**
+     * Read file data into a string.
+     *
+     * @param filename the file to open
+     * @return the file contents
+     * @throws IOException if a java.io operation throws
+     */
+    private String readFileData(final String filename) throws IOException {
+        return readFileData(new File(filename));
+    }
+
     /**
      * Draw the window.
      */
@@ -149,7 +202,7 @@ public class TEditorWindow extends TScrollableWindow {
      * editor.
      *
      * @param mouse a mouse-based event
-     * @return whether or not the mouse is on the emulator
+     * @return whether or not the mouse is on the editor
      */
     private final boolean mouseOnEditor(final TMouseEvent mouse) {
         if ((mouse.getAbsoluteX() >= getAbsoluteX() + 1)
@@ -169,21 +222,83 @@ public class TEditorWindow extends TScrollableWindow {
      */
     @Override
     public void onMouseDown(final TMouseEvent mouse) {
+        // Use TWidget's code to pass the event to the children.
+        super.onMouseDown(mouse);
+
         if (mouseOnEditor(mouse)) {
-            editField.onMouseDown(mouse);
+            // The editor might have changed, update the scollbars.
             setBottomValue(editField.getMaximumRowNumber());
             setVerticalValue(editField.getEditingRowNumber());
             setRightValue(editField.getMaximumColumnNumber());
             setHorizontalValue(editField.getEditingColumnNumber());
         } else {
-            // Let the scrollbars get the event
-            super.onMouseDown(mouse);
-
             if (mouse.isMouseWheelUp() || mouse.isMouseWheelDown()) {
+                // Vertical scrollbar actions
+                editField.setEditingRowNumber(getVerticalValue());
+            }
+        }
+    }
+
+    /**
+     * Handle mouse release events.
+     *
+     * @param mouse mouse button release event
+     */
+    @Override
+    public void onMouseUp(final TMouseEvent mouse) {
+        // Use TWidget's code to pass the event to the children.
+        super.onMouseUp(mouse);
+
+        if (mouse.isMouse1() && mouseOnVerticalScroller(mouse)) {
+            // Clicked on vertical scrollbar
+            editField.setEditingRowNumber(getVerticalValue());
+        }
+
+        // TODO: horizontal scrolling
+    }
+
+    /**
+     * Method that subclasses can override to handle mouse movements.
+     *
+     * @param mouse mouse motion event
+     */
+    @Override
+    public void onMouseMotion(final TMouseEvent mouse) {
+        // Use TWidget's code to pass the event to the children.
+        super.onMouseMotion(mouse);
+
+        if (mouseOnEditor(mouse) && mouse.isMouse1()) {
+            // The editor might have changed, update the scollbars.
+            setBottomValue(editField.getMaximumRowNumber());
+            setVerticalValue(editField.getEditingRowNumber());
+            setRightValue(editField.getMaximumColumnNumber());
+            setHorizontalValue(editField.getEditingColumnNumber());
+        } else {
+            if (mouse.isMouse1() && mouseOnVerticalScroller(mouse)) {
+                // Clicked/dragged on vertical scrollbar
                 editField.setEditingRowNumber(getVerticalValue());
             }
+
             // TODO: horizontal scrolling
         }
+
+    }
+
+    /**
+     * Handle keystrokes.
+     *
+     * @param keypress keystroke event
+     */
+    @Override
+    public void onKeypress(final TKeypressEvent keypress) {
+        // Use TWidget's code to pass the event to the children.
+        super.onKeypress(keypress);
+
+        // The editor might have changed, update the scollbars.
+        setBottomValue(editField.getMaximumRowNumber());
+        setVerticalValue(editField.getEditingRowNumber());
+        setRightValue(editField.getMaximumColumnNumber());
+        setHorizontalValue(editField.getEditingColumnNumber());
     }
 
     /**
@@ -220,30 +335,18 @@ public class TEditorWindow extends TScrollableWindow {
         if (command.equals(cmOpen)) {
             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 TEditorWindow(getApplication(), filename,
-                                 fileContents.toString());
-                         } finally {
-                             scanner.close();
-                         }
-                     } catch (IOException e) {
-                         // TODO: make this a message box
-                         e.printStackTrace();
-                     }
-                 }
+                if (filename != null) {
+                    try {
+                        String contents = readFileData(filename);
+                        new TEditorWindow(getApplication(), filename, contents);
+                    } catch (IOException e) {
+                        messageBox("Error", "Error reading file: " +
+                            e.getMessage());
+                    }
+                }
             } catch (IOException e) {
-                // TODO: make this a message box
-                e.printStackTrace();
+                messageBox("Error", "Error opening file dialog: " +
+                    e.getMessage());
             }
             return;
         }
@@ -253,8 +356,7 @@ public class TEditorWindow extends TScrollableWindow {
                 try {
                     editField.saveToFilename(filename);
                 } catch (IOException e) {
-                    // TODO: make this a message box
-                    e.printStackTrace();
+                    messageBox("Error", "Error saving file: " + e.getMessage());
                 }
             }
             return;