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]
31 import java
.io
.IOException
;
32 import java
.text
.MessageFormat
;
33 import java
.util
.ResourceBundle
;
35 import jexer
.event
.TCommandEvent
;
36 import jexer
.event
.TKeypressEvent
;
37 import jexer
.event
.TMouseEvent
;
38 import jexer
.event
.TResizeEvent
;
39 import jexer
.menu
.TMenu
;
40 import static jexer
.TCommand
.*;
41 import static jexer
.TKeypress
.*;
44 * TTableWindow is used to display and edit regular two-dimensional tables of
47 public class TTableWindow
extends TScrollableWindow
{
52 private static final ResourceBundle i18n
= ResourceBundle
.getBundle(TTableWindow
.class.getName());
54 // ------------------------------------------------------------------------
55 // Variables --------------------------------------------------------------
56 // ------------------------------------------------------------------------
61 private TTableWidget tableField
;
63 // ------------------------------------------------------------------------
64 // Constructors -----------------------------------------------------------
65 // ------------------------------------------------------------------------
68 * Public constructor sets window title.
70 * @param parent the main application
71 * @param title the window title
73 public TTableWindow(final TApplication parent
, final String title
) {
75 super(parent
, title
, 0, 0, parent
.getScreen().getWidth() / 2,
76 parent
.getScreen().getHeight() / 2 - 2, RESIZABLE
| CENTERED
);
78 tableField
= new TTableWidget(this, 0, 0, getWidth() - 2, getHeight() - 2);
82 // ------------------------------------------------------------------------
83 // Event handlers ---------------------------------------------------------
84 // ------------------------------------------------------------------------
87 * Called by application.switchWindow() when this window gets the
88 * focus, and also by application.addWindow().
90 public void onFocus() {
91 // Enable the table menu items.
92 getApplication().enableMenuItem(TMenu
.MID_CUT
);
93 getApplication().enableMenuItem(TMenu
.MID_TABLE_VIEW_ROW_LABELS
);
94 getApplication().enableMenuItem(TMenu
.MID_TABLE_VIEW_COLUMN_LABELS
);
95 getApplication().enableMenuItem(TMenu
.MID_TABLE_VIEW_HIGHLIGHT_ROW
);
96 getApplication().enableMenuItem(TMenu
.MID_TABLE_VIEW_HIGHLIGHT_COLUMN
);
97 getApplication().enableMenuItem(TMenu
.MID_TABLE_BORDER_NONE
);
98 getApplication().enableMenuItem(TMenu
.MID_TABLE_BORDER_ALL
);
99 getApplication().enableMenuItem(TMenu
.MID_TABLE_BORDER_RIGHT
);
100 getApplication().enableMenuItem(TMenu
.MID_TABLE_BORDER_LEFT
);
101 getApplication().enableMenuItem(TMenu
.MID_TABLE_BORDER_TOP
);
102 getApplication().enableMenuItem(TMenu
.MID_TABLE_BORDER_BOTTOM
);
103 getApplication().enableMenuItem(TMenu
.MID_TABLE_BORDER_DOUBLE_BOTTOM
);
104 getApplication().enableMenuItem(TMenu
.MID_TABLE_BORDER_THICK_BOTTOM
);
105 getApplication().enableMenuItem(TMenu
.MID_TABLE_DELETE_LEFT
);
106 getApplication().enableMenuItem(TMenu
.MID_TABLE_DELETE_UP
);
107 getApplication().enableMenuItem(TMenu
.MID_TABLE_DELETE_ROW
);
108 getApplication().enableMenuItem(TMenu
.MID_TABLE_DELETE_COLUMN
);
109 getApplication().enableMenuItem(TMenu
.MID_TABLE_INSERT_LEFT
);
110 getApplication().enableMenuItem(TMenu
.MID_TABLE_INSERT_RIGHT
);
111 getApplication().enableMenuItem(TMenu
.MID_TABLE_INSERT_ABOVE
);
112 getApplication().enableMenuItem(TMenu
.MID_TABLE_INSERT_BELOW
);
113 getApplication().enableMenuItem(TMenu
.MID_TABLE_COLUMN_NARROW
);
114 getApplication().enableMenuItem(TMenu
.MID_TABLE_COLUMN_WIDEN
);
115 getApplication().enableMenuItem(TMenu
.MID_TABLE_FILE_SAVE_CSV
);
116 getApplication().enableMenuItem(TMenu
.MID_TABLE_FILE_SAVE_TEXT
);
120 * Called by application.switchWindow() when another window gets the
123 public void onUnfocus() {
124 // Disable the table menu items.
125 getApplication().disableMenuItem(TMenu
.MID_CUT
);
126 getApplication().disableMenuItem(TMenu
.MID_TABLE_VIEW_ROW_LABELS
);
127 getApplication().disableMenuItem(TMenu
.MID_TABLE_VIEW_COLUMN_LABELS
);
128 getApplication().disableMenuItem(TMenu
.MID_TABLE_VIEW_HIGHLIGHT_ROW
);
129 getApplication().disableMenuItem(TMenu
.MID_TABLE_VIEW_HIGHLIGHT_COLUMN
);
130 getApplication().disableMenuItem(TMenu
.MID_TABLE_BORDER_NONE
);
131 getApplication().disableMenuItem(TMenu
.MID_TABLE_BORDER_ALL
);
132 getApplication().disableMenuItem(TMenu
.MID_TABLE_BORDER_RIGHT
);
133 getApplication().disableMenuItem(TMenu
.MID_TABLE_BORDER_LEFT
);
134 getApplication().disableMenuItem(TMenu
.MID_TABLE_BORDER_TOP
);
135 getApplication().disableMenuItem(TMenu
.MID_TABLE_BORDER_BOTTOM
);
136 getApplication().disableMenuItem(TMenu
.MID_TABLE_BORDER_DOUBLE_BOTTOM
);
137 getApplication().disableMenuItem(TMenu
.MID_TABLE_BORDER_THICK_BOTTOM
);
138 getApplication().disableMenuItem(TMenu
.MID_TABLE_DELETE_LEFT
);
139 getApplication().disableMenuItem(TMenu
.MID_TABLE_DELETE_UP
);
140 getApplication().disableMenuItem(TMenu
.MID_TABLE_DELETE_ROW
);
141 getApplication().disableMenuItem(TMenu
.MID_TABLE_DELETE_COLUMN
);
142 getApplication().disableMenuItem(TMenu
.MID_TABLE_INSERT_LEFT
);
143 getApplication().disableMenuItem(TMenu
.MID_TABLE_INSERT_RIGHT
);
144 getApplication().disableMenuItem(TMenu
.MID_TABLE_INSERT_ABOVE
);
145 getApplication().disableMenuItem(TMenu
.MID_TABLE_INSERT_BELOW
);
146 getApplication().disableMenuItem(TMenu
.MID_TABLE_COLUMN_NARROW
);
147 getApplication().disableMenuItem(TMenu
.MID_TABLE_COLUMN_WIDEN
);
148 getApplication().disableMenuItem(TMenu
.MID_TABLE_FILE_SAVE_CSV
);
149 getApplication().disableMenuItem(TMenu
.MID_TABLE_FILE_SAVE_TEXT
);
152 // ------------------------------------------------------------------------
153 // TWindow ----------------------------------------------------------------
154 // ------------------------------------------------------------------------
157 * Handle mouse press events.
159 * @param mouse mouse button press event
162 public void onMouseDown(final TMouseEvent mouse
) {
163 // Use TWidget's code to pass the event to the children.
164 super.onMouseDown(mouse
);
166 if (mouseOnTable(mouse
)) {
167 // The table might have changed, update the scollbars.
168 setBottomValue(tableField
.getRowCount() - 1);
169 setVerticalValue(tableField
.getSelectedRowNumber());
170 setRightValue(tableField
.getColumnCount() - 1);
171 setHorizontalValue(tableField
.getSelectedColumnNumber());
176 * Handle mouse release events.
178 * @param mouse mouse button release event
181 public void onMouseUp(final TMouseEvent mouse
) {
182 // Use TWidget's code to pass the event to the children.
183 super.onMouseUp(mouse
);
185 if (mouse
.isMouse1() && mouseOnVerticalScroller(mouse
)) {
186 // Clicked/dragged on vertical scrollbar.
187 tableField
.setSelectedRowNumber(getVerticalValue());
189 if (mouse
.isMouse1() && mouseOnHorizontalScroller(mouse
)) {
190 // Clicked/dragged on horizontal scrollbar.
191 tableField
.setSelectedColumnNumber(getHorizontalValue());
196 * Method that subclasses can override to handle mouse movements.
198 * @param mouse mouse motion event
201 public void onMouseMotion(final TMouseEvent mouse
) {
202 // Use TWidget's code to pass the event to the children.
203 super.onMouseMotion(mouse
);
205 if (mouseOnTable(mouse
) && mouse
.isMouse1()) {
206 // The table might have changed, update the scollbars.
207 setBottomValue(tableField
.getRowCount() - 1);
208 setVerticalValue(tableField
.getSelectedRowNumber());
209 setRightValue(tableField
.getColumnCount() - 1);
210 setHorizontalValue(tableField
.getSelectedColumnNumber());
212 if (mouse
.isMouse1() && mouseOnVerticalScroller(mouse
)) {
213 // Clicked/dragged on vertical scrollbar.
214 tableField
.setSelectedRowNumber(getVerticalValue());
216 if (mouse
.isMouse1() && mouseOnHorizontalScroller(mouse
)) {
217 // Clicked/dragged on horizontal scrollbar.
218 tableField
.setSelectedColumnNumber(getHorizontalValue());
227 * @param keypress keystroke event
230 public void onKeypress(final TKeypressEvent keypress
) {
231 // Use TWidget's code to pass the event to the children.
232 super.onKeypress(keypress
);
234 // The table might have changed, update the scollbars.
235 setBottomValue(tableField
.getRowCount() - 1);
236 setVerticalValue(tableField
.getSelectedRowNumber());
237 setRightValue(tableField
.getColumnCount() - 1);
238 setHorizontalValue(tableField
.getSelectedColumnNumber());
242 * Handle window/screen resize events.
244 * @param event resize event
247 public void onResize(final TResizeEvent event
) {
248 if (event
.getType() == TResizeEvent
.Type
.WIDGET
) {
250 TResizeEvent tableSize
= new TResizeEvent(TResizeEvent
.Type
.WIDGET
,
251 event
.getWidth() - 2, event
.getHeight() - 2);
252 tableField
.onResize(tableSize
);
254 // Have TScrollableWindow handle the scrollbars
255 super.onResize(event
);
259 // Pass to children instead
260 for (TWidget widget
: getChildren()) {
261 widget
.onResize(event
);
266 * Method that subclasses can override to handle posted command events.
268 * @param command command event
271 public void onCommand(final TCommandEvent command
) {
272 if (command
.equals(cmOpen
)) {
274 String filename
= fileOpenBox(".");
275 if (filename
!= null) {
279 tableField
.saveToFilename(filename
);
281 } catch (IOException e
) {
282 messageBox(i18n
.getString("errorDialogTitle"),
283 MessageFormat
.format(i18n
.
284 getString("errorReadingFile"), e
.getMessage()));
287 } catch (IOException e
) {
288 messageBox(i18n
.getString("errorDialogTitle"),
289 MessageFormat
.format(i18n
.
290 getString("errorOpeningFileDialog"), e
.getMessage()));
295 if (command
.equals(cmSave
)) {
297 String filename
= fileSaveBox(".");
298 if (filename
!= null) {
299 tableField
.saveToFilename(filename
);
301 } catch (IOException e
) {
302 messageBox(i18n
.getString("errorDialogTitle"),
303 MessageFormat
.format(i18n
.
304 getString("errorSavingFile"), e
.getMessage()));
309 // Didn't handle it, let children get it instead
310 super.onCommand(command
);
313 // ------------------------------------------------------------------------
314 // TTableWindow -----------------------------------------------------------
315 // ------------------------------------------------------------------------
318 * Setup other fields after the table is created.
320 private void setupAfterTable() {
321 hScroller
= new THScroller(this, 17, getHeight() - 2, getWidth() - 20);
322 vScroller
= new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
323 setMinimumWindowWidth(25);
324 setMinimumWindowHeight(10);
325 setTopValue(tableField
.getSelectedRowNumber());
326 setBottomValue(tableField
.getRowCount() - 1);
327 setLeftValue(tableField
.getSelectedColumnNumber());
328 setRightValue(tableField
.getColumnCount() - 1);
330 statusBar
= newStatusBar(i18n
.getString("statusBar"));
331 statusBar
.addShortcutKeypress(kbF1
, cmHelp
,
332 i18n
.getString("statusBarHelp"));
334 statusBar
.addShortcutKeypress(kbF2
, cmSave
,
335 i18n
.getString("statusBarSave"));
336 statusBar
.addShortcutKeypress(kbF3
, cmOpen
,
337 i18n
.getString("statusBarOpen"));
338 statusBar
.addShortcutKeypress(kbF10
, cmMenu
,
339 i18n
.getString("statusBarMenu"));
343 * Check if a mouse press/release/motion event coordinate is over the
346 * @param mouse a mouse-based event
347 * @return whether or not the mouse is on the table
349 private boolean mouseOnTable(final TMouseEvent mouse
) {
350 if ((mouse
.getAbsoluteX() >= getAbsoluteX() + 1)
351 && (mouse
.getAbsoluteX() < getAbsoluteX() + getWidth() - 1)
352 && (mouse
.getAbsoluteY() >= getAbsoluteY() + 1)
353 && (mouse
.getAbsoluteY() < getAbsoluteY() + getHeight() - 1)