X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTEditorWindow.java;h=d78185c32f3096cd615f345d9731751d285fc3b6;hb=9892fdaafb368227aa9630ab3abe6ed10bb1d001;hp=f96b177f6f4d1ccbddd44a08ad5d87c5b73afe09;hpb=71a389c9810382e014682dde52e94d3f34e385fa;p=fanfix.git diff --git a/src/jexer/TEditorWindow.java b/src/jexer/TEditorWindow.java index f96b177..d78185c 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; @@ -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,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,24 +74,19 @@ public class TEditorWindow extends TScrollableWindow { private String filename = ""; /** - * Setup other fields after the editor is created. + * If true, hide the mouse after typing a keystroke. */ - 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()); + private boolean hideMouseWhenTyping = true; - statusBar = newStatusBar("Editor"); - statusBar.addShortcutKeypress(kbF1, cmHelp, "Help"); - statusBar.addShortcutKeypress(kbF2, cmSave, "Save"); - statusBar.addShortcutKeypress(kbF3, cmOpen, "Open"); - statusBar.addShortcutKeypress(kbF10, cmMenu, "Menu"); - } + /** + * If true, the mouse should not be displayed because a keystroke was + * typed. + */ + private boolean typingHidMouse = false; + + // ------------------------------------------------------------------------ + // Constructors ----------------------------------------------------------- + // ------------------------------------------------------------------------ /** * Public constructor sets window title. @@ -90,7 +97,7 @@ public class TEditorWindow extends TScrollableWindow { public TEditorWindow(final TApplication parent, final String title) { super(parent, title, 0, 0, parent.getScreen().getWidth(), - parent.getScreen().getHeight() - 2, RESIZABLE); + parent.getDesktopBottom() - parent.getDesktopTop(), RESIZABLE); editField = addEditor("", 0, 0, getWidth() - 2, getHeight() - 2); setupAfterEditor(); @@ -107,22 +114,45 @@ public class TEditorWindow extends TScrollableWindow { final String contents) { super(parent, title, 0, 0, parent.getScreen().getWidth(), - parent.getScreen().getHeight() - 2, RESIZABLE); + parent.getDesktopBottom() - parent.getDesktopTop(), RESIZABLE); filename = title; editField = addEditor(contents, 0, 0, getWidth() - 2, getHeight() - 2); 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.getDesktopBottom() - parent.getDesktopTop(), RESIZABLE); + + filename = file.getName(); + String contents = readFileData(file); + editField = addEditor(contents, 0, 0, getWidth() - 2, getHeight() - 2); + setupAfterEditor(); + } + /** * Public constructor. * * @param parent the main application */ public TEditorWindow(final TApplication parent) { - this(parent, "New Text Document"); + this(parent, i18n.getString("newTextDocument")); } + // ------------------------------------------------------------------------ + // TWindow ---------------------------------------------------------------- + // ------------------------------------------------------------------------ + /** * Draw the window. */ @@ -145,45 +175,111 @@ public class TEditorWindow extends TScrollableWindow { } /** - * Check if a mouse press/release/motion event coordinate is over the - * editor. + * Handle mouse press events. * - * @param mouse a mouse-based event - * @return whether or not the mouse is on the emulator + * @param mouse mouse button press event */ - 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; + @Override + public void onMouseDown(final TMouseEvent mouse) { + // Use TWidget's code to pass the event to the children. + super.onMouseDown(mouse); + + if (hideMouseWhenTyping) { + typingHidMouse = false; + } + + if (mouseOnEditor(mouse)) { + // The editor might have changed, update the scollbars. + setBottomValue(editField.getMaximumRowNumber()); + setVerticalValue(editField.getVisibleRowNumber()); + setRightValue(editField.getMaximumColumnNumber()); + setHorizontalValue(editField.getEditingColumnNumber()); + } else { + if (mouse.isMouseWheelUp() || mouse.isMouseWheelDown()) { + // Vertical scrollbar actions + editField.setVisibleRowNumber(getVerticalValue()); + } } - return false; } /** - * Handle mouse press events. + * Handle mouse release events. * - * @param mouse mouse button press event + * @param mouse mouse button release event */ @Override - public void onMouseDown(final TMouseEvent mouse) { - if (mouseOnEditor(mouse)) { - editField.onMouseDown(mouse); + public void onMouseUp(final TMouseEvent mouse) { + // Use TWidget's code to pass the event to the children. + super.onMouseUp(mouse); + + if (hideMouseWhenTyping) { + typingHidMouse = false; + } + + if (mouse.isMouse1() && mouseOnVerticalScroller(mouse)) { + // Clicked on vertical scrollbar + editField.setVisibleRowNumber(getVerticalValue()); + } + if (mouse.isMouse1() && mouseOnHorizontalScroller(mouse)) { + // Clicked on horizontal scrollbar + editField.setVisibleColumnNumber(getHorizontalValue()); + setHorizontalValue(editField.getVisibleColumnNumber()); + } + } + + /** + * 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 (hideMouseWhenTyping) { + typingHidMouse = false; + } + + 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 { - // Let the scrollbars get the event - super.onMouseDown(mouse); - - if (mouse.isMouseWheelUp() || mouse.isMouseWheelDown()) { - editField.setEditingRowNumber(getVerticalValue()); + if (mouse.isMouse1() && mouseOnVerticalScroller(mouse)) { + // Clicked/dragged on vertical scrollbar + editField.setVisibleRowNumber(getVerticalValue()); + } + if (mouse.isMouse1() && mouseOnHorizontalScroller(mouse)) { + // Clicked/dragged on horizontal scrollbar + editField.setVisibleColumnNumber(getHorizontalValue()); + setHorizontalValue(editField.getVisibleColumnNumber()); } - // TODO: horizontal scrolling } + + } + + /** + * Handle keystrokes. + * + * @param keypress keystroke event + */ + @Override + public void onKeypress(final TKeypressEvent keypress) { + if (hideMouseWhenTyping) { + typingHidMouse = true; + } + + // 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.getVisibleRowNumber()); + setRightValue(editField.getMaximumColumnNumber()); + setHorizontalValue(editField.getEditingColumnNumber()); } /** @@ -220,30 +316,20 @@ 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(i18n.getString("errorDialogTitle"), + MessageFormat.format(i18n. + getString("errorReadingFile"), e.getMessage())); + } + } } catch (IOException e) { - // TODO: make this a message box - e.printStackTrace(); + messageBox(i18n.getString("errorDialogTitle"), + MessageFormat.format(i18n. + getString("errorOpeningFileDialog"), e.getMessage())); } return; } @@ -253,8 +339,9 @@ public class TEditorWindow extends TScrollableWindow { try { editField.saveToFilename(filename); } catch (IOException e) { - // TODO: make this a message box - e.printStackTrace(); + messageBox(i18n.getString("errorDialogTitle"), + MessageFormat.format(i18n. + getString("errorSavingFile"), e.getMessage())); } } return; @@ -264,4 +351,102 @@ public class TEditorWindow extends TScrollableWindow { super.onCommand(command); } + /** + * Returns true if this window does not want the application-wide mouse + * cursor drawn over it. + * + * @return true if this window does not want the application-wide mouse + * cursor drawn over it + */ + @Override + public boolean hasHiddenMouse() { + return (super.hasHiddenMouse() || typingHidMouse); + } + + // ------------------------------------------------------------------------ + // 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")); + + // Hide mouse when typing option + if (System.getProperty("jexer.TEditor.hideMouseWhenTyping", + "true").equals("false")) { + + hideMouseWhenTyping = false; + } + } + + /** + * 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 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; + } + }