2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2017 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());
63 * Hang onto my TEditor so I can resize it with the window.
65 private TEditorWidget editField
;
68 * The fully-qualified name of the file being edited.
70 private String filename
= "";
73 * Setup other fields after the editor is created.
75 private void setupAfterEditor() {
76 hScroller
= new THScroller(this, 17, getHeight() - 2, getWidth() - 20);
77 vScroller
= new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
78 setMinimumWindowWidth(25);
79 setMinimumWindowHeight(10);
81 setBottomValue(editField
.getMaximumRowNumber());
83 setRightValue(editField
.getMaximumColumnNumber());
85 statusBar
= newStatusBar(i18n
.getString("statusBar"));
86 statusBar
.addShortcutKeypress(kbF1
, cmHelp
,
87 i18n
.getString("statusBarHelp"));
88 statusBar
.addShortcutKeypress(kbF2
, cmSave
,
89 i18n
.getString("statusBarSave"));
90 statusBar
.addShortcutKeypress(kbF3
, cmOpen
,
91 i18n
.getString("statusBarOpen"));
92 statusBar
.addShortcutKeypress(kbF10
, cmMenu
,
93 i18n
.getString("statusBarMenu"));
97 * Public constructor sets window title.
99 * @param parent the main application
100 * @param title the window title
102 public TEditorWindow(final TApplication parent
, final String title
) {
104 super(parent
, title
, 0, 0, parent
.getScreen().getWidth(),
105 parent
.getScreen().getHeight() - 2, RESIZABLE
);
107 editField
= addEditor("", 0, 0, getWidth() - 2, getHeight() - 2);
112 * Public constructor sets window title and contents.
114 * @param parent the main application
115 * @param title the window title, usually a filename
116 * @param contents the data for the editing window, usually the file data
118 public TEditorWindow(final TApplication parent
, final String title
,
119 final String contents
) {
121 super(parent
, title
, 0, 0, parent
.getScreen().getWidth(),
122 parent
.getScreen().getHeight() - 2, RESIZABLE
);
125 editField
= addEditor(contents
, 0, 0, getWidth() - 2, getHeight() - 2);
130 * Public constructor opens a file.
132 * @param parent the main application
133 * @param file the file to open
134 * @throws IOException if a java.io operation throws
136 public TEditorWindow(final TApplication parent
,
137 final File file
) throws IOException
{
139 super(parent
, file
.getName(), 0, 0, parent
.getScreen().getWidth(),
140 parent
.getScreen().getHeight() - 2, RESIZABLE
);
142 filename
= file
.getName();
143 String contents
= readFileData(file
);
144 editField
= addEditor(contents
, 0, 0, getWidth() - 2, getHeight() - 2);
149 * Public constructor.
151 * @param parent the main application
153 public TEditorWindow(final TApplication parent
) {
154 this(parent
, i18n
.getString("newTextDocument"));
158 * Read file data into a string.
160 * @param file the file to open
161 * @return the file contents
162 * @throws IOException if a java.io operation throws
164 private String
readFileData(final File file
) throws IOException
{
165 StringBuilder fileContents
= new StringBuilder();
166 Scanner scanner
= new Scanner(file
);
167 String EOL
= System
.getProperty("line.separator");
170 while (scanner
.hasNextLine()) {
171 fileContents
.append(scanner
.nextLine() + EOL
);
173 return fileContents
.toString();
180 * Read file data into a string.
182 * @param filename the file to open
183 * @return the file contents
184 * @throws IOException if a java.io operation throws
186 private String
readFileData(final String filename
) throws IOException
{
187 return readFileData(new File(filename
));
198 // Add the row:col on the bottom row
199 CellAttributes borderColor
= getBorder();
200 String location
= String
.format(" %d:%d ",
201 editField
.getEditingRowNumber(),
202 editField
.getEditingColumnNumber());
203 int colon
= location
.indexOf(':');
204 putStringXY(10 - colon
, getHeight() - 1, location
, borderColor
);
206 if (editField
.isDirty()) {
207 putCharXY(2, getHeight() - 1, GraphicsChars
.OCTOSTAR
, borderColor
);
212 * Check if a mouse press/release/motion event coordinate is over the
215 * @param mouse a mouse-based event
216 * @return whether or not the mouse is on the editor
218 private final boolean mouseOnEditor(final TMouseEvent mouse
) {
219 if ((mouse
.getAbsoluteX() >= getAbsoluteX() + 1)
220 && (mouse
.getAbsoluteX() < getAbsoluteX() + getWidth() - 1)
221 && (mouse
.getAbsoluteY() >= getAbsoluteY() + 1)
222 && (mouse
.getAbsoluteY() < getAbsoluteY() + getHeight() - 1)
230 * Handle mouse press events.
232 * @param mouse mouse button press event
235 public void onMouseDown(final TMouseEvent mouse
) {
236 // Use TWidget's code to pass the event to the children.
237 super.onMouseDown(mouse
);
239 if (mouseOnEditor(mouse
)) {
240 // The editor might have changed, update the scollbars.
241 setBottomValue(editField
.getMaximumRowNumber());
242 setVerticalValue(editField
.getVisibleRowNumber());
243 setRightValue(editField
.getMaximumColumnNumber());
244 setHorizontalValue(editField
.getEditingColumnNumber());
246 if (mouse
.isMouseWheelUp() || mouse
.isMouseWheelDown()) {
247 // Vertical scrollbar actions
248 editField
.setVisibleRowNumber(getVerticalValue());
254 * Handle mouse release events.
256 * @param mouse mouse button release event
259 public void onMouseUp(final TMouseEvent mouse
) {
260 // Use TWidget's code to pass the event to the children.
261 super.onMouseUp(mouse
);
263 if (mouse
.isMouse1() && mouseOnVerticalScroller(mouse
)) {
264 // Clicked on vertical scrollbar
265 editField
.setVisibleRowNumber(getVerticalValue());
268 // TODO: horizontal scrolling
272 * Method that subclasses can override to handle mouse movements.
274 * @param mouse mouse motion event
277 public void onMouseMotion(final TMouseEvent mouse
) {
278 // Use TWidget's code to pass the event to the children.
279 super.onMouseMotion(mouse
);
281 if (mouseOnEditor(mouse
) && mouse
.isMouse1()) {
282 // The editor might have changed, update the scollbars.
283 setBottomValue(editField
.getMaximumRowNumber());
284 setVerticalValue(editField
.getVisibleRowNumber());
285 setRightValue(editField
.getMaximumColumnNumber());
286 setHorizontalValue(editField
.getEditingColumnNumber());
288 if (mouse
.isMouse1() && mouseOnVerticalScroller(mouse
)) {
289 // Clicked/dragged on vertical scrollbar
290 editField
.setVisibleRowNumber(getVerticalValue());
293 // TODO: horizontal scrolling
301 * @param keypress keystroke event
304 public void onKeypress(final TKeypressEvent keypress
) {
305 // Use TWidget's code to pass the event to the children.
306 super.onKeypress(keypress
);
308 // The editor might have changed, update the scollbars.
309 setBottomValue(editField
.getMaximumRowNumber());
310 setVerticalValue(editField
.getVisibleRowNumber());
311 setRightValue(editField
.getMaximumColumnNumber());
312 setHorizontalValue(editField
.getEditingColumnNumber());
316 * Handle window/screen resize events.
318 * @param event resize event
321 public void onResize(final TResizeEvent event
) {
322 if (event
.getType() == TResizeEvent
.Type
.WIDGET
) {
323 // Resize the text field
324 TResizeEvent editSize
= new TResizeEvent(TResizeEvent
.Type
.WIDGET
,
325 event
.getWidth() - 2, event
.getHeight() - 2);
326 editField
.onResize(editSize
);
328 // Have TScrollableWindow handle the scrollbars
329 super.onResize(event
);
333 // Pass to children instead
334 for (TWidget widget
: getChildren()) {
335 widget
.onResize(event
);
340 * Method that subclasses can override to handle posted command events.
342 * @param command command event
345 public void onCommand(final TCommandEvent command
) {
346 if (command
.equals(cmOpen
)) {
348 String filename
= fileOpenBox(".");
349 if (filename
!= null) {
351 String contents
= readFileData(filename
);
352 new TEditorWindow(getApplication(), filename
, contents
);
353 } catch (IOException e
) {
354 messageBox(i18n
.getString("errorDialogTitle"),
355 MessageFormat
.format(i18n
.
356 getString("errorReadingFile"), e
.getMessage()));
359 } catch (IOException e
) {
360 messageBox(i18n
.getString("errorDialogTitle"),
361 MessageFormat
.format(i18n
.
362 getString("errorOpeningFileDialog"), e
.getMessage()));
367 if (command
.equals(cmSave
)) {
368 if (filename
.length() > 0) {
370 editField
.saveToFilename(filename
);
371 } catch (IOException e
) {
372 messageBox(i18n
.getString("errorDialogTitle"),
373 MessageFormat
.format(i18n
.
374 getString("errorSavingFile"), e
.getMessage()));
380 // Didn't handle it, let children get it instead
381 super.onCommand(command
);