X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTEditorWindow.java;h=495131106e7b9fad3fa38a1f3ddadf159238d776;hb=a69ed767c9c07cf35cf1c5f7821fc009cfe79cd2;hp=5bf664d254e515f95d41418ef383ae01559dcdb4;hpb=fe0770f988e64fc0ccafd3d3b086b4a0eb559d3b;p=nikiroo-utils.git diff --git a/src/jexer/TEditorWindow.java b/src/jexer/TEditorWindow.java index 5bf664d..4951311 100644 --- a/src/jexer/TEditorWindow.java +++ b/src/jexer/TEditorWindow.java @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (C) 2017 Kevin Lamonte + * Copyright (C) 2019 Kevin Lamonte * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -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; @@ -52,6 +54,15 @@ import static jexer.TKeypress.*; */ public class TEditorWindow extends TScrollableWindow { + /** + * Translated strings. + */ + private static final ResourceBundle i18n = ResourceBundle.getBundle(TEditorWindow.class.getName()); + + // ------------------------------------------------------------------------ + // Variables -------------------------------------------------------------- + // ------------------------------------------------------------------------ + /** * Hang onto my TEditor so I can resize it with the window. */ @@ -62,25 +73,9 @@ public class TEditorWindow extends TScrollableWindow { */ private String filename = ""; - /** - * Setup other fields after the editor is created. - */ - private void setupAfterEditor() { - hScroller = new THScroller(this, 17, getHeight() - 2, getWidth() - 20); - vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2); - setMinimumWindowWidth(25); - setMinimumWindowHeight(10); - setTopValue(1); - setBottomValue(editField.getMaximumRowNumber()); - 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"); - } + // ------------------------------------------------------------------------ + // Constructors ----------------------------------------------------------- + // ------------------------------------------------------------------------ /** * Public constructor sets window title. @@ -140,41 +135,12 @@ public class TEditorWindow extends TScrollableWindow { * @param parent the main application */ public TEditorWindow(final TApplication parent) { - 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(); - } + this(parent, i18n.getString("newTextDocument")); } - /** - * 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)); - } + // ------------------------------------------------------------------------ + // TWindow ---------------------------------------------------------------- + // ------------------------------------------------------------------------ /** * Draw the window. @@ -197,24 +163,6 @@ public class TEditorWindow extends TScrollableWindow { } } - /** - * Check if a mouse press/release/motion event coordinate is over the - * editor. - * - * @param mouse a mouse-based event - * @return whether or not the mouse is on the editor - */ - private final boolean mouseOnEditor(final TMouseEvent mouse) { - if ((mouse.getAbsoluteX() >= getAbsoluteX() + 1) - && (mouse.getAbsoluteX() < getAbsoluteX() + getWidth() - 1) - && (mouse.getAbsoluteY() >= getAbsoluteY() + 1) - && (mouse.getAbsoluteY() < getAbsoluteY() + getHeight() - 1) - ) { - return true; - } - return false; - } - /** * Handle mouse press events. * @@ -228,13 +176,13 @@ public class TEditorWindow extends TScrollableWindow { if (mouseOnEditor(mouse)) { // The editor might have changed, update the scollbars. setBottomValue(editField.getMaximumRowNumber()); - setVerticalValue(editField.getEditingRowNumber()); + setVerticalValue(editField.getVisibleRowNumber()); setRightValue(editField.getMaximumColumnNumber()); setHorizontalValue(editField.getEditingColumnNumber()); } else { if (mouse.isMouseWheelUp() || mouse.isMouseWheelDown()) { // Vertical scrollbar actions - editField.setEditingRowNumber(getVerticalValue()); + editField.setVisibleRowNumber(getVerticalValue()); } } } @@ -251,7 +199,7 @@ public class TEditorWindow extends TScrollableWindow { if (mouse.isMouse1() && mouseOnVerticalScroller(mouse)) { // Clicked on vertical scrollbar - editField.setEditingRowNumber(getVerticalValue()); + editField.setVisibleRowNumber(getVerticalValue()); } // TODO: horizontal scrolling @@ -270,13 +218,13 @@ public class TEditorWindow extends TScrollableWindow { if (mouseOnEditor(mouse) && mouse.isMouse1()) { // The editor might have changed, update the scollbars. setBottomValue(editField.getMaximumRowNumber()); - setVerticalValue(editField.getEditingRowNumber()); + setVerticalValue(editField.getVisibleRowNumber()); setRightValue(editField.getMaximumColumnNumber()); setHorizontalValue(editField.getEditingColumnNumber()); } else { if (mouse.isMouse1() && mouseOnVerticalScroller(mouse)) { // Clicked/dragged on vertical scrollbar - editField.setEditingRowNumber(getVerticalValue()); + editField.setVisibleRowNumber(getVerticalValue()); } // TODO: horizontal scrolling @@ -296,7 +244,7 @@ public class TEditorWindow extends TScrollableWindow { // The editor might have changed, update the scollbars. setBottomValue(editField.getMaximumRowNumber()); - setVerticalValue(editField.getEditingRowNumber()); + setVerticalValue(editField.getVisibleRowNumber()); setRightValue(editField.getMaximumColumnNumber()); setHorizontalValue(editField.getEditingColumnNumber()); } @@ -340,13 +288,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; } @@ -356,7 +306,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; @@ -366,4 +318,83 @@ public class TEditorWindow extends TScrollableWindow { super.onCommand(command); } + // ------------------------------------------------------------------------ + // TEditorWindow ---------------------------------------------------------- + // ------------------------------------------------------------------------ + + /** + * Setup other fields after the editor is created. + */ + private void setupAfterEditor() { + hScroller = new THScroller(this, 17, getHeight() - 2, getWidth() - 20); + vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2); + setMinimumWindowWidth(25); + setMinimumWindowHeight(10); + setTopValue(1); + setBottomValue(editField.getMaximumRowNumber()); + setLeftValue(1); + setRightValue(editField.getMaximumColumnNumber()); + + 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")); + } + + /** + * 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)); + } + + /** + * Check if a mouse press/release/motion event coordinate is over the + * editor. + * + * @param mouse a mouse-based event + * @return whether or not the mouse is on the editor + */ + private final boolean mouseOnEditor(final TMouseEvent mouse) { + if ((mouse.getAbsoluteX() >= getAbsoluteX() + 1) + && (mouse.getAbsoluteX() < getAbsoluteX() + getWidth() - 1) + && (mouse.getAbsoluteY() >= getAbsoluteY() + 1) + && (mouse.getAbsoluteY() < getAbsoluteY() + getHeight() - 1) + ) { + return true; + } + return false; + } + }