2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
32 import java
.io
.IOException
;
33 import java
.text
.MessageFormat
;
34 import java
.util
.ResourceBundle
;
35 import java
.util
.Scanner
;
37 import jexer
.TApplication
;
38 import jexer
.TEditorWidget
;
39 import jexer
.THScroller
;
40 import jexer
.TScrollableWindow
;
41 import jexer
.TVScroller
;
43 import jexer
.bits
.CellAttributes
;
44 import jexer
.bits
.GraphicsChars
;
45 import jexer
.event
.TCommandEvent
;
46 import jexer
.event
.TKeypressEvent
;
47 import jexer
.event
.TMouseEvent
;
48 import jexer
.event
.TResizeEvent
;
49 import static jexer
.TCommand
.*;
50 import static jexer
.TKeypress
.*;
53 * TEditorWindow is a basic text file editor.
55 public class TEditorWindow
extends TScrollableWindow
{
60 private static final ResourceBundle i18n
= ResourceBundle
.getBundle(TEditorWindow
.class.getName());
62 // ------------------------------------------------------------------------
63 // Variables --------------------------------------------------------------
64 // ------------------------------------------------------------------------
67 * Hang onto my TEditor so I can resize it with the window.
69 private TEditorWidget editField
;
72 * The fully-qualified name of the file being edited.
74 private String filename
= "";
77 * If true, hide the mouse after typing a keystroke.
79 private boolean hideMouseWhenTyping
= true;
82 * If true, the mouse should not be displayed because a keystroke was
85 private boolean typingHidMouse
= false;
87 // ------------------------------------------------------------------------
88 // Constructors -----------------------------------------------------------
89 // ------------------------------------------------------------------------
92 * Public constructor sets window title.
94 * @param parent the main application
95 * @param title the window title
97 public TEditorWindow(final TApplication parent
, final String title
) {
99 super(parent
, title
, 0, 0, parent
.getScreen().getWidth(),
100 parent
.getScreen().getHeight() - 2, RESIZABLE
);
102 editField
= addEditor("", 0, 0, getWidth() - 2, getHeight() - 2);
107 * Public constructor sets window title and contents.
109 * @param parent the main application
110 * @param title the window title, usually a filename
111 * @param contents the data for the editing window, usually the file data
113 public TEditorWindow(final TApplication parent
, final String title
,
114 final String contents
) {
116 super(parent
, title
, 0, 0, parent
.getScreen().getWidth(),
117 parent
.getScreen().getHeight() - 2, RESIZABLE
);
120 editField
= addEditor(contents
, 0, 0, getWidth() - 2, getHeight() - 2);
125 * Public constructor opens a file.
127 * @param parent the main application
128 * @param file the file to open
129 * @throws IOException if a java.io operation throws
131 public TEditorWindow(final TApplication parent
,
132 final File file
) throws IOException
{
134 super(parent
, file
.getName(), 0, 0, parent
.getScreen().getWidth(),
135 parent
.getScreen().getHeight() - 2, RESIZABLE
);
137 filename
= file
.getName();
138 String contents
= readFileData(file
);
139 editField
= addEditor(contents
, 0, 0, getWidth() - 2, getHeight() - 2);
144 * Public constructor.
146 * @param parent the main application
148 public TEditorWindow(final TApplication parent
) {
149 this(parent
, i18n
.getString("newTextDocument"));
152 // ------------------------------------------------------------------------
153 // TWindow ----------------------------------------------------------------
154 // ------------------------------------------------------------------------
164 // Add the row:col on the bottom row
165 CellAttributes borderColor
= getBorder();
166 String location
= String
.format(" %d:%d ",
167 editField
.getEditingRowNumber(),
168 editField
.getEditingColumnNumber());
169 int colon
= location
.indexOf(':');
170 putStringXY(10 - colon
, getHeight() - 1, location
, borderColor
);
172 if (editField
.isDirty()) {
173 putCharXY(2, getHeight() - 1, GraphicsChars
.OCTOSTAR
, borderColor
);
178 * Handle mouse press events.
180 * @param mouse mouse button press event
183 public void onMouseDown(final TMouseEvent mouse
) {
184 // Use TWidget's code to pass the event to the children.
185 super.onMouseDown(mouse
);
187 if (hideMouseWhenTyping
) {
188 typingHidMouse
= false;
191 if (mouseOnEditor(mouse
)) {
192 // The editor might have changed, update the scollbars.
193 setBottomValue(editField
.getMaximumRowNumber());
194 setVerticalValue(editField
.getVisibleRowNumber());
195 setRightValue(editField
.getMaximumColumnNumber());
196 setHorizontalValue(editField
.getEditingColumnNumber());
198 if (mouse
.isMouseWheelUp() || mouse
.isMouseWheelDown()) {
199 // Vertical scrollbar actions
200 editField
.setVisibleRowNumber(getVerticalValue());
206 * Handle mouse release events.
208 * @param mouse mouse button release event
211 public void onMouseUp(final TMouseEvent mouse
) {
212 // Use TWidget's code to pass the event to the children.
213 super.onMouseUp(mouse
);
215 if (hideMouseWhenTyping
) {
216 typingHidMouse
= false;
219 if (mouse
.isMouse1() && mouseOnVerticalScroller(mouse
)) {
220 // Clicked on vertical scrollbar
221 editField
.setVisibleRowNumber(getVerticalValue());
224 // TODO: horizontal scrolling
228 * Method that subclasses can override to handle mouse movements.
230 * @param mouse mouse motion event
233 public void onMouseMotion(final TMouseEvent mouse
) {
234 // Use TWidget's code to pass the event to the children.
235 super.onMouseMotion(mouse
);
237 if (hideMouseWhenTyping
) {
238 typingHidMouse
= false;
241 if (mouseOnEditor(mouse
) && mouse
.isMouse1()) {
242 // The editor might have changed, update the scollbars.
243 setBottomValue(editField
.getMaximumRowNumber());
244 setVerticalValue(editField
.getVisibleRowNumber());
245 setRightValue(editField
.getMaximumColumnNumber());
246 setHorizontalValue(editField
.getEditingColumnNumber());
248 if (mouse
.isMouse1() && mouseOnVerticalScroller(mouse
)) {
249 // Clicked/dragged on vertical scrollbar
250 editField
.setVisibleRowNumber(getVerticalValue());
253 // TODO: horizontal scrolling
261 * @param keypress keystroke event
264 public void onKeypress(final TKeypressEvent keypress
) {
265 if (hideMouseWhenTyping
) {
266 typingHidMouse
= true;
269 // Use TWidget's code to pass the event to the children.
270 super.onKeypress(keypress
);
272 // The editor might have changed, update the scollbars.
273 setBottomValue(editField
.getMaximumRowNumber());
274 setVerticalValue(editField
.getVisibleRowNumber());
275 setRightValue(editField
.getMaximumColumnNumber());
276 setHorizontalValue(editField
.getEditingColumnNumber());
280 * Handle window/screen resize events.
282 * @param event resize event
285 public void onResize(final TResizeEvent event
) {
286 if (event
.getType() == TResizeEvent
.Type
.WIDGET
) {
287 // Resize the text field
288 TResizeEvent editSize
= new TResizeEvent(TResizeEvent
.Type
.WIDGET
,
289 event
.getWidth() - 2, event
.getHeight() - 2);
290 editField
.onResize(editSize
);
292 // Have TScrollableWindow handle the scrollbars
293 super.onResize(event
);
297 // Pass to children instead
298 for (TWidget widget
: getChildren()) {
299 widget
.onResize(event
);
304 * Method that subclasses can override to handle posted command events.
306 * @param command command event
309 public void onCommand(final TCommandEvent command
) {
310 if (command
.equals(cmOpen
)) {
312 String filename
= fileOpenBox(".");
313 if (filename
!= null) {
315 String contents
= readFileData(filename
);
316 new TEditorWindow(getApplication(), filename
, contents
);
317 } catch (IOException e
) {
318 messageBox(i18n
.getString("errorDialogTitle"),
319 MessageFormat
.format(i18n
.
320 getString("errorReadingFile"), e
.getMessage()));
323 } catch (IOException e
) {
324 messageBox(i18n
.getString("errorDialogTitle"),
325 MessageFormat
.format(i18n
.
326 getString("errorOpeningFileDialog"), e
.getMessage()));
331 if (command
.equals(cmSave
)) {
332 if (filename
.length() > 0) {
334 editField
.saveToFilename(filename
);
335 } catch (IOException e
) {
336 messageBox(i18n
.getString("errorDialogTitle"),
337 MessageFormat
.format(i18n
.
338 getString("errorSavingFile"), e
.getMessage()));
344 // Didn't handle it, let children get it instead
345 super.onCommand(command
);
349 * Returns true if this window does not want the application-wide mouse
350 * cursor drawn over it.
352 * @return true if this window does not want the application-wide mouse
353 * cursor drawn over it
356 public boolean hasHiddenMouse() {
357 return (super.hasHiddenMouse() || typingHidMouse
);
360 // ------------------------------------------------------------------------
361 // TEditorWindow ----------------------------------------------------------
362 // ------------------------------------------------------------------------
365 * Setup other fields after the editor is created.
367 private void setupAfterEditor() {
368 hScroller
= new THScroller(this, 17, getHeight() - 2, getWidth() - 20);
369 vScroller
= new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
370 setMinimumWindowWidth(25);
371 setMinimumWindowHeight(10);
373 setBottomValue(editField
.getMaximumRowNumber());
375 setRightValue(editField
.getMaximumColumnNumber());
377 statusBar
= newStatusBar(i18n
.getString("statusBar"));
378 statusBar
.addShortcutKeypress(kbF1
, cmHelp
,
379 i18n
.getString("statusBarHelp"));
380 statusBar
.addShortcutKeypress(kbF2
, cmSave
,
381 i18n
.getString("statusBarSave"));
382 statusBar
.addShortcutKeypress(kbF3
, cmOpen
,
383 i18n
.getString("statusBarOpen"));
384 statusBar
.addShortcutKeypress(kbF10
, cmMenu
,
385 i18n
.getString("statusBarMenu"));
387 // Hide mouse when typing option
388 if (System
.getProperty("jexer.TEditor.hideMouseWhenTyping",
389 "true").equals("false")) {
391 hideMouseWhenTyping
= false;
396 * Read file data into a string.
398 * @param file the file to open
399 * @return the file contents
400 * @throws IOException if a java.io operation throws
402 private String
readFileData(final File file
) throws IOException
{
403 StringBuilder fileContents
= new StringBuilder();
404 Scanner scanner
= new Scanner(file
);
405 String EOL
= System
.getProperty("line.separator");
408 while (scanner
.hasNextLine()) {
409 fileContents
.append(scanner
.nextLine() + EOL
);
411 return fileContents
.toString();
418 * Read file data into a string.
420 * @param filename the file to open
421 * @return the file contents
422 * @throws IOException if a java.io operation throws
424 private String
readFileData(final String filename
) throws IOException
{
425 return readFileData(new File(filename
));
429 * Check if a mouse press/release/motion event coordinate is over the
432 * @param mouse a mouse-based event
433 * @return whether or not the mouse is on the editor
435 private boolean mouseOnEditor(final TMouseEvent mouse
) {
436 if ((mouse
.getAbsoluteX() >= getAbsoluteX() + 1)
437 && (mouse
.getAbsoluteX() < getAbsoluteX() + getWidth() - 1)
438 && (mouse
.getAbsoluteY() >= getAbsoluteY() + 1)
439 && (mouse
.getAbsoluteY() < getAbsoluteY() + getHeight() - 1)