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_VIEW_ROW_LABELS
);
92 getApplication().enableMenuItem(TMenu
.MID_TABLE_VIEW_COLUMN_LABELS
);
93 getApplication().enableMenuItem(TMenu
.MID_TABLE_VIEW_HIGHLIGHT_ROW
);
94 getApplication().enableMenuItem(TMenu
.MID_TABLE_VIEW_HIGHLIGHT_COLUMN
);
95 getApplication().enableMenuItem(TMenu
.MID_TABLE_BORDER_NONE
);
96 getApplication().enableMenuItem(TMenu
.MID_TABLE_BORDER_ALL
);
97 getApplication().enableMenuItem(TMenu
.MID_TABLE_BORDER_RIGHT
);
98 getApplication().enableMenuItem(TMenu
.MID_TABLE_BORDER_LEFT
);
99 getApplication().enableMenuItem(TMenu
.MID_TABLE_BORDER_TOP
);
100 getApplication().enableMenuItem(TMenu
.MID_TABLE_BORDER_BOTTOM
);
101 getApplication().enableMenuItem(TMenu
.MID_TABLE_BORDER_DOUBLE_BOTTOM
);
102 getApplication().enableMenuItem(TMenu
.MID_TABLE_BORDER_THICK_BOTTOM
);
103 getApplication().enableMenuItem(TMenu
.MID_TABLE_DELETE_LEFT
);
104 getApplication().enableMenuItem(TMenu
.MID_TABLE_DELETE_UP
);
105 getApplication().enableMenuItem(TMenu
.MID_TABLE_DELETE_ROW
);
106 getApplication().enableMenuItem(TMenu
.MID_TABLE_DELETE_COLUMN
);
107 getApplication().enableMenuItem(TMenu
.MID_TABLE_INSERT_LEFT
);
108 getApplication().enableMenuItem(TMenu
.MID_TABLE_INSERT_RIGHT
);
109 getApplication().enableMenuItem(TMenu
.MID_TABLE_INSERT_ABOVE
);
110 getApplication().enableMenuItem(TMenu
.MID_TABLE_INSERT_BELOW
);
111 getApplication().enableMenuItem(TMenu
.MID_TABLE_COLUMN_NARROW
);
112 getApplication().enableMenuItem(TMenu
.MID_TABLE_COLUMN_WIDEN
);
113 getApplication().enableMenuItem(TMenu
.MID_TABLE_FILE_SAVE_CSV
);
114 getApplication().enableMenuItem(TMenu
.MID_TABLE_FILE_SAVE_TEXT
);
118 * Called by application.switchWindow() when another window gets the
121 public void onUnfocus() {
122 // Disable the table menu items.
123 getApplication().disableMenuItem(TMenu
.MID_CUT
);
124 getApplication().disableMenuItem(TMenu
.MID_TABLE_VIEW_ROW_LABELS
);
125 getApplication().disableMenuItem(TMenu
.MID_TABLE_VIEW_COLUMN_LABELS
);
126 getApplication().disableMenuItem(TMenu
.MID_TABLE_VIEW_HIGHLIGHT_ROW
);
127 getApplication().disableMenuItem(TMenu
.MID_TABLE_VIEW_HIGHLIGHT_COLUMN
);
128 getApplication().disableMenuItem(TMenu
.MID_TABLE_BORDER_NONE
);
129 getApplication().disableMenuItem(TMenu
.MID_TABLE_BORDER_ALL
);
130 getApplication().disableMenuItem(TMenu
.MID_TABLE_BORDER_RIGHT
);
131 getApplication().disableMenuItem(TMenu
.MID_TABLE_BORDER_LEFT
);
132 getApplication().disableMenuItem(TMenu
.MID_TABLE_BORDER_TOP
);
133 getApplication().disableMenuItem(TMenu
.MID_TABLE_BORDER_BOTTOM
);
134 getApplication().disableMenuItem(TMenu
.MID_TABLE_BORDER_DOUBLE_BOTTOM
);
135 getApplication().disableMenuItem(TMenu
.MID_TABLE_BORDER_THICK_BOTTOM
);
136 getApplication().disableMenuItem(TMenu
.MID_TABLE_DELETE_LEFT
);
137 getApplication().disableMenuItem(TMenu
.MID_TABLE_DELETE_UP
);
138 getApplication().disableMenuItem(TMenu
.MID_TABLE_DELETE_ROW
);
139 getApplication().disableMenuItem(TMenu
.MID_TABLE_DELETE_COLUMN
);
140 getApplication().disableMenuItem(TMenu
.MID_TABLE_INSERT_LEFT
);
141 getApplication().disableMenuItem(TMenu
.MID_TABLE_INSERT_RIGHT
);
142 getApplication().disableMenuItem(TMenu
.MID_TABLE_INSERT_ABOVE
);
143 getApplication().disableMenuItem(TMenu
.MID_TABLE_INSERT_BELOW
);
144 getApplication().disableMenuItem(TMenu
.MID_TABLE_COLUMN_NARROW
);
145 getApplication().disableMenuItem(TMenu
.MID_TABLE_COLUMN_WIDEN
);
146 getApplication().disableMenuItem(TMenu
.MID_TABLE_FILE_SAVE_CSV
);
147 getApplication().disableMenuItem(TMenu
.MID_TABLE_FILE_SAVE_TEXT
);
150 // ------------------------------------------------------------------------
151 // TWindow ----------------------------------------------------------------
152 // ------------------------------------------------------------------------
162 // Add borders on rows and columns.
167 * Handle mouse press events.
169 * @param mouse mouse button press event
172 public void onMouseDown(final TMouseEvent mouse
) {
173 // Use TWidget's code to pass the event to the children.
174 super.onMouseDown(mouse
);
176 if (mouseOnTable(mouse
)) {
177 // The table might have changed, update the scollbars.
180 setBottomValue(editField.getMaximumRowNumber());
181 setVerticalValue(editField.getVisibleRowNumber());
182 setRightValue(editField.getMaximumColumnNumber());
183 setHorizontalValue(editField.getEditingColumnNumber());
186 if (mouse
.isMouseWheelUp() || mouse
.isMouseWheelDown()) {
187 // Vertical scrollbar actions
189 // editField.setVisibleRowNumber(getVerticalValue());
195 * Handle mouse release events.
197 * @param mouse mouse button release event
200 public void onMouseUp(final TMouseEvent mouse
) {
201 // Use TWidget's code to pass the event to the children.
202 super.onMouseUp(mouse
);
204 if (mouse
.isMouse1() && mouseOnVerticalScroller(mouse
)) {
205 // Clicked on vertical scrollbar
207 // editField.setVisibleRowNumber(getVerticalValue());
210 // TODO: horizontal scrolling
214 * Method that subclasses can override to handle mouse movements.
216 * @param mouse mouse motion event
219 public void onMouseMotion(final TMouseEvent mouse
) {
220 // Use TWidget's code to pass the event to the children.
221 super.onMouseMotion(mouse
);
223 if (mouseOnTable(mouse
) && mouse
.isMouse1()) {
224 // The editor might have changed, update the scollbars.
227 setBottomValue(editField.getMaximumRowNumber());
228 setVerticalValue(editField.getVisibleRowNumber());
229 setRightValue(editField.getMaximumColumnNumber());
230 setHorizontalValue(editField.getEditingColumnNumber());
233 if (mouse
.isMouse1() && mouseOnVerticalScroller(mouse
)) {
234 // Clicked/dragged on vertical scrollbar
236 // editField.setVisibleRowNumber(getVerticalValue());
239 // TODO: horizontal scrolling
247 * @param keypress keystroke event
250 public void onKeypress(final TKeypressEvent keypress
) {
251 // Use TWidget's code to pass the event to the children.
252 super.onKeypress(keypress
);
254 // The editor might have changed, update the scollbars.
257 setBottomValue(editField.getMaximumRowNumber());
258 setVerticalValue(editField.getVisibleRowNumber());
259 setRightValue(editField.getMaximumColumnNumber());
260 setHorizontalValue(editField.getEditingColumnNumber());
265 * Handle window/screen resize events.
267 * @param event resize event
270 public void onResize(final TResizeEvent event
) {
271 if (event
.getType() == TResizeEvent
.Type
.WIDGET
) {
273 TResizeEvent tableSize
= new TResizeEvent(TResizeEvent
.Type
.WIDGET
,
274 event
.getWidth() - 2, event
.getHeight() - 2);
275 tableField
.onResize(tableSize
);
277 // Have TScrollableWindow handle the scrollbars
278 super.onResize(event
);
282 // Pass to children instead
283 for (TWidget widget
: getChildren()) {
284 widget
.onResize(event
);
288 // ------------------------------------------------------------------------
289 // TTableWindow -----------------------------------------------------------
290 // ------------------------------------------------------------------------
293 * Setup other fields after the table is created.
295 private void setupAfterTable() {
296 hScroller
= new THScroller(this, 17, getHeight() - 2, getWidth() - 20);
297 vScroller
= new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
298 setMinimumWindowWidth(25);
299 setMinimumWindowHeight(10);
301 // setBottomValue(editField.getMaximumRowNumber());
303 setRightValue(tableField
.getMaximumWidth());
305 statusBar
= newStatusBar(i18n
.getString("statusBar"));
306 statusBar
.addShortcutKeypress(kbF1
, cmHelp
,
307 i18n
.getString("statusBarHelp"));
309 statusBar.addShortcutKeypress(kbF2, cmSave,
310 i18n.getString("statusBarSave"));
311 statusBar.addShortcutKeypress(kbF3, cmOpen,
312 i18n.getString("statusBarOpen"));
314 statusBar
.addShortcutKeypress(kbF10
, cmMenu
,
315 i18n
.getString("statusBarMenu"));
319 * Check if a mouse press/release/motion event coordinate is over the
322 * @param mouse a mouse-based event
323 * @return whether or not the mouse is on the table
325 private boolean mouseOnTable(final TMouseEvent mouse
) {
326 if ((mouse
.getAbsoluteX() >= getAbsoluteX() + 1)
327 && (mouse
.getAbsoluteX() < getAbsoluteX() + getWidth() - 1)
328 && (mouse
.getAbsoluteY() >= getAbsoluteY() + 1)
329 && (mouse
.getAbsoluteY() < getAbsoluteY() + getHeight() - 1)