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
.util
.ResourceBundle
;
33 import jexer
.event
.TCommandEvent
;
34 import jexer
.event
.TKeypressEvent
;
35 import jexer
.event
.TMouseEvent
;
36 import jexer
.event
.TResizeEvent
;
37 import jexer
.menu
.TMenu
;
38 import static jexer
.TCommand
.*;
39 import static jexer
.TKeypress
.*;
42 * TTableWindow is used to display and edit regular two-dimensional tables of
45 public class TTableWindow
extends TScrollableWindow
{
50 private static final ResourceBundle i18n
= ResourceBundle
.getBundle(TTableWindow
.class.getName());
52 // ------------------------------------------------------------------------
53 // Variables --------------------------------------------------------------
54 // ------------------------------------------------------------------------
59 private TTableWidget tableField
;
61 // ------------------------------------------------------------------------
62 // Constructors -----------------------------------------------------------
63 // ------------------------------------------------------------------------
66 * Public constructor sets window title.
68 * @param parent the main application
69 * @param title the window title
71 public TTableWindow(final TApplication parent
, final String title
) {
73 super(parent
, title
, 0, 0, parent
.getScreen().getWidth() / 2,
74 parent
.getScreen().getHeight() / 2 - 2, RESIZABLE
| CENTERED
);
76 tableField
= new TTableWidget(this, 0, 0, getWidth() - 2, getHeight() - 2);
80 // ------------------------------------------------------------------------
81 // Event handlers ---------------------------------------------------------
82 // ------------------------------------------------------------------------
85 * Called by application.switchWindow() when this window gets the
86 * focus, and also by application.addWindow().
88 public void onFocus() {
89 // Enable the table menu items.
90 getApplication().enableMenuItem(TMenu
.MID_CUT
);
91 getApplication().enableMenuItem(TMenu
.MID_TABLE_BORDER_NONE
);
92 getApplication().enableMenuItem(TMenu
.MID_TABLE_BORDER_ALL
);
93 getApplication().enableMenuItem(TMenu
.MID_TABLE_BORDER_RIGHT
);
94 getApplication().enableMenuItem(TMenu
.MID_TABLE_BORDER_LEFT
);
95 getApplication().enableMenuItem(TMenu
.MID_TABLE_BORDER_TOP
);
96 getApplication().enableMenuItem(TMenu
.MID_TABLE_BORDER_BOTTOM
);
97 getApplication().enableMenuItem(TMenu
.MID_TABLE_BORDER_DOUBLE_BOTTOM
);
98 getApplication().enableMenuItem(TMenu
.MID_TABLE_BORDER_THICK_BOTTOM
);
99 getApplication().enableMenuItem(TMenu
.MID_TABLE_DELETE_LEFT
);
100 getApplication().enableMenuItem(TMenu
.MID_TABLE_DELETE_UP
);
101 getApplication().enableMenuItem(TMenu
.MID_TABLE_DELETE_ROW
);
102 getApplication().enableMenuItem(TMenu
.MID_TABLE_DELETE_COLUMN
);
103 getApplication().enableMenuItem(TMenu
.MID_TABLE_INSERT_LEFT
);
104 getApplication().enableMenuItem(TMenu
.MID_TABLE_INSERT_RIGHT
);
105 getApplication().enableMenuItem(TMenu
.MID_TABLE_INSERT_ABOVE
);
106 getApplication().enableMenuItem(TMenu
.MID_TABLE_INSERT_BELOW
);
107 getApplication().enableMenuItem(TMenu
.MID_TABLE_COLUMN_NARROW
);
108 getApplication().enableMenuItem(TMenu
.MID_TABLE_COLUMN_WIDEN
);
109 getApplication().enableMenuItem(TMenu
.MID_TABLE_FILE_SAVE_CSV
);
110 getApplication().enableMenuItem(TMenu
.MID_TABLE_FILE_SAVE_TEXT
);
114 * Called by application.switchWindow() when another window gets the
117 public void onUnfocus() {
118 // Disable the table menu items.
119 getApplication().disableMenuItem(TMenu
.MID_CUT
);
120 getApplication().disableMenuItem(TMenu
.MID_TABLE_BORDER_NONE
);
121 getApplication().disableMenuItem(TMenu
.MID_TABLE_BORDER_ALL
);
122 getApplication().disableMenuItem(TMenu
.MID_TABLE_BORDER_RIGHT
);
123 getApplication().disableMenuItem(TMenu
.MID_TABLE_BORDER_LEFT
);
124 getApplication().disableMenuItem(TMenu
.MID_TABLE_BORDER_TOP
);
125 getApplication().disableMenuItem(TMenu
.MID_TABLE_BORDER_BOTTOM
);
126 getApplication().disableMenuItem(TMenu
.MID_TABLE_BORDER_DOUBLE_BOTTOM
);
127 getApplication().disableMenuItem(TMenu
.MID_TABLE_BORDER_THICK_BOTTOM
);
128 getApplication().disableMenuItem(TMenu
.MID_TABLE_DELETE_LEFT
);
129 getApplication().disableMenuItem(TMenu
.MID_TABLE_DELETE_UP
);
130 getApplication().disableMenuItem(TMenu
.MID_TABLE_DELETE_ROW
);
131 getApplication().disableMenuItem(TMenu
.MID_TABLE_DELETE_COLUMN
);
132 getApplication().disableMenuItem(TMenu
.MID_TABLE_INSERT_LEFT
);
133 getApplication().disableMenuItem(TMenu
.MID_TABLE_INSERT_RIGHT
);
134 getApplication().disableMenuItem(TMenu
.MID_TABLE_INSERT_ABOVE
);
135 getApplication().disableMenuItem(TMenu
.MID_TABLE_INSERT_BELOW
);
136 getApplication().disableMenuItem(TMenu
.MID_TABLE_COLUMN_NARROW
);
137 getApplication().disableMenuItem(TMenu
.MID_TABLE_COLUMN_WIDEN
);
138 getApplication().disableMenuItem(TMenu
.MID_TABLE_FILE_SAVE_CSV
);
139 getApplication().disableMenuItem(TMenu
.MID_TABLE_FILE_SAVE_TEXT
);
142 // ------------------------------------------------------------------------
143 // TWindow ----------------------------------------------------------------
144 // ------------------------------------------------------------------------
154 // Add borders on rows and columns.
159 * Handle mouse press events.
161 * @param mouse mouse button press event
164 public void onMouseDown(final TMouseEvent mouse
) {
165 // Use TWidget's code to pass the event to the children.
166 super.onMouseDown(mouse
);
168 if (mouseOnTable(mouse
)) {
169 // The table might have changed, update the scollbars.
172 setBottomValue(editField.getMaximumRowNumber());
173 setVerticalValue(editField.getVisibleRowNumber());
174 setRightValue(editField.getMaximumColumnNumber());
175 setHorizontalValue(editField.getEditingColumnNumber());
178 if (mouse
.isMouseWheelUp() || mouse
.isMouseWheelDown()) {
179 // Vertical scrollbar actions
181 // editField.setVisibleRowNumber(getVerticalValue());
187 * Handle mouse release events.
189 * @param mouse mouse button release event
192 public void onMouseUp(final TMouseEvent mouse
) {
193 // Use TWidget's code to pass the event to the children.
194 super.onMouseUp(mouse
);
196 if (mouse
.isMouse1() && mouseOnVerticalScroller(mouse
)) {
197 // Clicked on vertical scrollbar
199 // editField.setVisibleRowNumber(getVerticalValue());
202 // TODO: horizontal scrolling
206 * Method that subclasses can override to handle mouse movements.
208 * @param mouse mouse motion event
211 public void onMouseMotion(final TMouseEvent mouse
) {
212 // Use TWidget's code to pass the event to the children.
213 super.onMouseMotion(mouse
);
215 if (mouseOnTable(mouse
) && mouse
.isMouse1()) {
216 // The editor might have changed, update the scollbars.
219 setBottomValue(editField.getMaximumRowNumber());
220 setVerticalValue(editField.getVisibleRowNumber());
221 setRightValue(editField.getMaximumColumnNumber());
222 setHorizontalValue(editField.getEditingColumnNumber());
225 if (mouse
.isMouse1() && mouseOnVerticalScroller(mouse
)) {
226 // Clicked/dragged on vertical scrollbar
228 // editField.setVisibleRowNumber(getVerticalValue());
231 // TODO: horizontal scrolling
239 * @param keypress keystroke event
242 public void onKeypress(final TKeypressEvent keypress
) {
243 // Use TWidget's code to pass the event to the children.
244 super.onKeypress(keypress
);
246 // The editor might have changed, update the scollbars.
249 setBottomValue(editField.getMaximumRowNumber());
250 setVerticalValue(editField.getVisibleRowNumber());
251 setRightValue(editField.getMaximumColumnNumber());
252 setHorizontalValue(editField.getEditingColumnNumber());
257 * Handle window/screen resize events.
259 * @param event resize event
262 public void onResize(final TResizeEvent event
) {
263 if (event
.getType() == TResizeEvent
.Type
.WIDGET
) {
265 TResizeEvent tableSize
= new TResizeEvent(TResizeEvent
.Type
.WIDGET
,
266 event
.getWidth() - 2, event
.getHeight() - 2);
267 tableField
.onResize(tableSize
);
269 // Have TScrollableWindow handle the scrollbars
270 super.onResize(event
);
274 // Pass to children instead
275 for (TWidget widget
: getChildren()) {
276 widget
.onResize(event
);
280 // ------------------------------------------------------------------------
281 // TTableWindow -----------------------------------------------------------
282 // ------------------------------------------------------------------------
285 * Setup other fields after the table is created.
287 private void setupAfterTable() {
288 hScroller
= new THScroller(this, 17, getHeight() - 2, getWidth() - 20);
289 vScroller
= new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
290 setMinimumWindowWidth(25);
291 setMinimumWindowHeight(10);
293 // setBottomValue(editField.getMaximumRowNumber());
295 // setRightValue(editField.getMaximumColumnNumber());
297 statusBar
= newStatusBar(i18n
.getString("statusBar"));
298 statusBar
.addShortcutKeypress(kbF1
, cmHelp
,
299 i18n
.getString("statusBarHelp"));
301 statusBar.addShortcutKeypress(kbF2, cmSave,
302 i18n.getString("statusBarSave"));
303 statusBar.addShortcutKeypress(kbF3, cmOpen,
304 i18n.getString("statusBarOpen"));
306 statusBar
.addShortcutKeypress(kbF10
, cmMenu
,
307 i18n
.getString("statusBarMenu"));
311 * Check if a mouse press/release/motion event coordinate is over the
314 * @param mouse a mouse-based event
315 * @return whether or not the mouse is on the table
317 private boolean mouseOnTable(final TMouseEvent mouse
) {
318 if ((mouse
.getAbsoluteX() >= getAbsoluteX() + 1)
319 && (mouse
.getAbsoluteX() < getAbsoluteX() + getWidth() - 1)
320 && (mouse
.getAbsoluteY() >= getAbsoluteY() + 1)
321 && (mouse
.getAbsoluteY() < getAbsoluteY() + getHeight() - 1)