Localize strings
[fanfix.git] / src / jexer / TEditorWindow.java
index 69e254b9df70f2b4303edc9a9806b64bacd90eb1..d474db2fa3e623e155c90a93183ac898b0a4b530 100644 (file)
@@ -30,6 +30,8 @@ package jexer;
 
 import java.io.File;
 import java.io.IOException;
+import java.text.MessageFormat;
+import java.util.ResourceBundle;
 import java.util.Scanner;
 
 import jexer.TApplication;
@@ -41,6 +43,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.*;
@@ -51,6 +54,11 @@ import static jexer.TKeypress.*;
  */
 public class TEditorWindow extends TScrollableWindow {
 
+    /**
+     * Translated strings.
+     */
+    private static final ResourceBundle i18n = ResourceBundle.getBundle(TEditorWindow.class.getName());
+
     /**
      * Hang onto my TEditor so I can resize it with the window.
      */
@@ -74,11 +82,15 @@ public class TEditorWindow extends TScrollableWindow {
         setLeftValue(1);
         setRightValue(editField.getMaximumColumnNumber());
 
-        statusBar = newStatusBar("Editor");
-        statusBar.addShortcutKeypress(kbF1, cmHelp, "Help");
-        statusBar.addShortcutKeypress(kbF2, cmSave, "Save");
-        statusBar.addShortcutKeypress(kbF3, cmOpen, "Open");
-        statusBar.addShortcutKeypress(kbF10, cmMenu, "Menu");
+        statusBar = newStatusBar(i18n.getString("statusBar"));
+        statusBar.addShortcutKeypress(kbF1, cmHelp,
+            i18n.getString("statusBarHelp"));
+        statusBar.addShortcutKeypress(kbF2, cmSave,
+            i18n.getString("statusBarSave"));
+        statusBar.addShortcutKeypress(kbF3, cmOpen,
+            i18n.getString("statusBarOpen"));
+        statusBar.addShortcutKeypress(kbF10, cmMenu,
+            i18n.getString("statusBarMenu"));
     }
 
     /**
@@ -139,7 +151,7 @@ public class TEditorWindow extends TScrollableWindow {
      * @param parent the main application
      */
     public TEditorWindow(final TApplication parent) {
-        this(parent, "New Text Document");
+        this(parent, i18n.getString("newTextDocument"));
     }
 
     /**
@@ -201,7 +213,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)
@@ -235,8 +247,69 @@ public class TEditorWindow extends TScrollableWindow {
                 // 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());
     }
 
     /**
@@ -278,13 +351,15 @@ public class TEditorWindow extends TScrollableWindow {
                         String contents = readFileData(filename);
                         new TEditorWindow(getApplication(), filename, contents);
                     } catch (IOException e) {
-                        messageBox("Error", "Error reading file: " +
-                            e.getMessage());
+                        messageBox(i18n.getString("errorDialogTitle"),
+                            MessageFormat.format(i18n.
+                                getString("errorReadingFile"), e.getMessage()));
                     }
                 }
             } catch (IOException e) {
-                messageBox("Error", "Error opening file dialog: " +
-                    e.getMessage());
+                messageBox(i18n.getString("errorDialogTitle"),
+                    MessageFormat.format(i18n.
+                        getString("errorOpeningFileDialog"), e.getMessage()));
             }
             return;
         }
@@ -294,7 +369,9 @@ public class TEditorWindow extends TScrollableWindow {
                 try {
                     editField.saveToFilename(filename);
                 } catch (IOException e) {
-                    messageBox("Error", "Error saving file: " + e.getMessage());
+                messageBox(i18n.getString("errorDialogTitle"),
+                    MessageFormat.format(i18n.
+                        getString("errorSavingFile"), e.getMessage()));
                 }
             }
             return;