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
.util
.Scanner
;
35 import jexer
.TApplication
;
36 import jexer
.TEditorWidget
;
37 import jexer
.THScroller
;
38 import jexer
.TScrollableWindow
;
39 import jexer
.TVScroller
;
41 import jexer
.bits
.CellAttributes
;
42 import jexer
.bits
.GraphicsChars
;
43 import jexer
.event
.TCommandEvent
;
44 import jexer
.event
.TMouseEvent
;
45 import jexer
.event
.TResizeEvent
;
46 import static jexer
.TCommand
.*;
47 import static jexer
.TKeypress
.*;
50 * TEditorWindow is a basic text file editor.
52 public class TEditorWindow
extends TScrollableWindow
{
55 * Hang onto my TEditor so I can resize it with the window.
57 private TEditorWidget editField
;
60 * The fully-qualified name of the file being edited.
62 private String filename
= "";
65 * Setup other fields after the editor is created.
67 private void setupAfterEditor() {
68 hScroller
= new THScroller(this, 17, getHeight() - 2, getWidth() - 20);
69 vScroller
= new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
70 setMinimumWindowWidth(25);
71 setMinimumWindowHeight(10);
73 setBottomValue(editField
.getMaximumRowNumber());
75 setRightValue(editField
.getMaximumColumnNumber());
77 statusBar
= newStatusBar("Editor");
78 statusBar
.addShortcutKeypress(kbF1
, cmHelp
, "Help");
79 statusBar
.addShortcutKeypress(kbF2
, cmSave
, "Save");
80 statusBar
.addShortcutKeypress(kbF3
, cmOpen
, "Open");
81 statusBar
.addShortcutKeypress(kbF10
, cmMenu
, "Menu");
85 * Public constructor sets window title.
87 * @param parent the main application
88 * @param title the window title
90 public TEditorWindow(final TApplication parent
, final String title
) {
92 super(parent
, title
, 0, 0, parent
.getScreen().getWidth(),
93 parent
.getScreen().getHeight() - 2, RESIZABLE
);
95 editField
= addEditor("", 0, 0, getWidth() - 2, getHeight() - 2);
100 * Public constructor sets window title and contents.
102 * @param parent the main application
103 * @param title the window title, usually a filename
104 * @param contents the data for the editing window, usually the file data
106 public TEditorWindow(final TApplication parent
, final String title
,
107 final String contents
) {
109 super(parent
, title
, 0, 0, parent
.getScreen().getWidth(),
110 parent
.getScreen().getHeight() - 2, RESIZABLE
);
113 editField
= addEditor(contents
, 0, 0, getWidth() - 2, getHeight() - 2);
118 * Public constructor opens a file.
120 * @param parent the main application
121 * @param file the file to open
122 * @throws IOException if a java.io operation throws
124 public TEditorWindow(final TApplication parent
,
125 final File file
) throws IOException
{
127 super(parent
, file
.getName(), 0, 0, parent
.getScreen().getWidth(),
128 parent
.getScreen().getHeight() - 2, RESIZABLE
);
130 filename
= file
.getName();
131 String contents
= readFileData(file
);
132 editField
= addEditor(contents
, 0, 0, getWidth() - 2, getHeight() - 2);
137 * Public constructor.
139 * @param parent the main application
141 public TEditorWindow(final TApplication parent
) {
142 this(parent
, "New Text Document");
146 * Read file data into a string.
148 * @param file the file to open
149 * @return the file contents
150 * @throws IOException if a java.io operation throws
152 private String
readFileData(final File file
) throws IOException
{
153 StringBuilder fileContents
= new StringBuilder();
154 Scanner scanner
= new Scanner(file
);
155 String EOL
= System
.getProperty("line.separator");
158 while (scanner
.hasNextLine()) {
159 fileContents
.append(scanner
.nextLine() + EOL
);
161 return fileContents
.toString();
168 * Read file data into a string.
170 * @param filename the file to open
171 * @return the file contents
172 * @throws IOException if a java.io operation throws
174 private String
readFileData(final String filename
) throws IOException
{
175 return readFileData(new File(filename
));
186 // Add the row:col on the bottom row
187 CellAttributes borderColor
= getBorder();
188 String location
= String
.format(" %d:%d ",
189 editField
.getEditingRowNumber(),
190 editField
.getEditingColumnNumber());
191 int colon
= location
.indexOf(':');
192 putStringXY(10 - colon
, getHeight() - 1, location
, borderColor
);
194 if (editField
.isDirty()) {
195 putCharXY(2, getHeight() - 1, GraphicsChars
.OCTOSTAR
, borderColor
);
200 * Check if a mouse press/release/motion event coordinate is over the
203 * @param mouse a mouse-based event
204 * @return whether or not the mouse is on the emulator
206 private final boolean mouseOnEditor(final TMouseEvent mouse
) {
207 if ((mouse
.getAbsoluteX() >= getAbsoluteX() + 1)
208 && (mouse
.getAbsoluteX() < getAbsoluteX() + getWidth() - 1)
209 && (mouse
.getAbsoluteY() >= getAbsoluteY() + 1)
210 && (mouse
.getAbsoluteY() < getAbsoluteY() + getHeight() - 1)
218 * Handle mouse press events.
220 * @param mouse mouse button press event
223 public void onMouseDown(final TMouseEvent mouse
) {
224 // Use TWidget's code to pass the event to the children.
225 super.onMouseDown(mouse
);
227 if (mouseOnEditor(mouse
)) {
228 // The editor might have changed, update the scollbars.
229 setBottomValue(editField
.getMaximumRowNumber());
230 setVerticalValue(editField
.getEditingRowNumber());
231 setRightValue(editField
.getMaximumColumnNumber());
232 setHorizontalValue(editField
.getEditingColumnNumber());
234 if (mouse
.isMouseWheelUp() || mouse
.isMouseWheelDown()) {
235 // Vertical scrollbar actions
236 editField
.setEditingRowNumber(getVerticalValue());
238 // TODO: horizontal scrolling
243 * Handle window/screen resize events.
245 * @param event resize event
248 public void onResize(final TResizeEvent event
) {
249 if (event
.getType() == TResizeEvent
.Type
.WIDGET
) {
250 // Resize the text field
251 TResizeEvent editSize
= new TResizeEvent(TResizeEvent
.Type
.WIDGET
,
252 event
.getWidth() - 2, event
.getHeight() - 2);
253 editField
.onResize(editSize
);
255 // Have TScrollableWindow handle the scrollbars
256 super.onResize(event
);
260 // Pass to children instead
261 for (TWidget widget
: getChildren()) {
262 widget
.onResize(event
);
267 * Method that subclasses can override to handle posted command events.
269 * @param command command event
272 public void onCommand(final TCommandEvent command
) {
273 if (command
.equals(cmOpen
)) {
275 String filename
= fileOpenBox(".");
276 if (filename
!= null) {
278 String contents
= readFileData(filename
);
279 new TEditorWindow(getApplication(), filename
, contents
);
280 } catch (IOException e
) {
281 messageBox("Error", "Error reading file: " +
285 } catch (IOException e
) {
286 messageBox("Error", "Error opening file dialog: " +
292 if (command
.equals(cmSave
)) {
293 if (filename
.length() > 0) {
295 editField
.saveToFilename(filename
);
296 } catch (IOException e
) {
297 messageBox("Error", "Error saving file: " + e
.getMessage());
303 // Didn't handle it, let children get it instead
304 super.onCommand(command
);