table CSV and text file
[fanfix.git] / src / jexer / TTableWindow.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2019 Kevin Lamonte
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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.
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29 package jexer;
30
31 import java.io.File;
32 import java.io.IOException;
33 import java.text.MessageFormat;
34 import java.util.ResourceBundle;
35
36 import jexer.event.TCommandEvent;
37 import jexer.event.TKeypressEvent;
38 import jexer.event.TMenuEvent;
39 import jexer.event.TMouseEvent;
40 import jexer.event.TResizeEvent;
41 import jexer.menu.TMenu;
42 import jexer.menu.TMenuItem;
43 import static jexer.TCommand.*;
44 import static jexer.TKeypress.*;
45
46 /**
47 * TTableWindow is used to display and edit regular two-dimensional tables of
48 * cells.
49 */
50 public class TTableWindow extends TScrollableWindow {
51
52 /**
53 * Translated strings.
54 */
55 private static final ResourceBundle i18n = ResourceBundle.getBundle(TTableWindow.class.getName());
56
57 // ------------------------------------------------------------------------
58 // Variables --------------------------------------------------------------
59 // ------------------------------------------------------------------------
60
61 /**
62 * The table widget.
63 */
64 private TTableWidget tableField;
65
66 // ------------------------------------------------------------------------
67 // Constructors -----------------------------------------------------------
68 // ------------------------------------------------------------------------
69
70 /**
71 * Public constructor sets window title.
72 *
73 * @param parent the main application
74 * @param title the window title
75 */
76 public TTableWindow(final TApplication parent, final String title) {
77
78 super(parent, title, 0, 0, parent.getScreen().getWidth() / 2,
79 parent.getScreen().getHeight() / 2 - 2, RESIZABLE | CENTERED);
80
81 tableField = addTable(0, 0, getWidth() - 2, getHeight() - 2);
82 setupAfterTable();
83 }
84
85 /**
86 * Public constructor loads a grid from a RFC4180 CSV file.
87 *
88 * @param parent the main application
89 * @param csvFile a File referencing the CSV data
90 * @throws IOException if a java.io operation throws
91 */
92 public TTableWindow(final TApplication parent,
93 final File csvFile) throws IOException {
94
95 super(parent, csvFile.getName(), 0, 0,
96 parent.getScreen().getWidth() / 2,
97 parent.getScreen().getHeight() / 2 - 2,
98 RESIZABLE | CENTERED);
99
100 tableField = addTable(0, 0, getWidth() - 2, getHeight() - 2, 1, 1);
101 setupAfterTable();
102 tableField.loadCsvFile(csvFile);
103 }
104
105 // ------------------------------------------------------------------------
106 // Event handlers ---------------------------------------------------------
107 // ------------------------------------------------------------------------
108
109 /**
110 * Called by application.switchWindow() when this window gets the
111 * focus, and also by application.addWindow().
112 */
113 public void onFocus() {
114 // Enable the table menu items.
115 getApplication().enableMenuItem(TMenu.MID_CUT);
116 getApplication().enableMenuItem(TMenu.MID_TABLE_RENAME_COLUMN);
117 getApplication().enableMenuItem(TMenu.MID_TABLE_RENAME_ROW);
118 getApplication().enableMenuItem(TMenu.MID_TABLE_VIEW_ROW_LABELS);
119 getApplication().enableMenuItem(TMenu.MID_TABLE_VIEW_COLUMN_LABELS);
120 getApplication().enableMenuItem(TMenu.MID_TABLE_VIEW_HIGHLIGHT_ROW);
121 getApplication().enableMenuItem(TMenu.MID_TABLE_VIEW_HIGHLIGHT_COLUMN);
122 getApplication().enableMenuItem(TMenu.MID_TABLE_BORDER_NONE);
123 getApplication().enableMenuItem(TMenu.MID_TABLE_BORDER_ALL);
124 getApplication().enableMenuItem(TMenu.MID_TABLE_BORDER_CELL_NONE);
125 getApplication().enableMenuItem(TMenu.MID_TABLE_BORDER_CELL_ALL);
126 getApplication().enableMenuItem(TMenu.MID_TABLE_BORDER_RIGHT);
127 getApplication().enableMenuItem(TMenu.MID_TABLE_BORDER_LEFT);
128 getApplication().enableMenuItem(TMenu.MID_TABLE_BORDER_TOP);
129 getApplication().enableMenuItem(TMenu.MID_TABLE_BORDER_BOTTOM);
130 getApplication().enableMenuItem(TMenu.MID_TABLE_BORDER_DOUBLE_BOTTOM);
131 getApplication().enableMenuItem(TMenu.MID_TABLE_BORDER_THICK_BOTTOM);
132 getApplication().enableMenuItem(TMenu.MID_TABLE_DELETE_LEFT);
133 getApplication().enableMenuItem(TMenu.MID_TABLE_DELETE_UP);
134 getApplication().enableMenuItem(TMenu.MID_TABLE_DELETE_ROW);
135 getApplication().enableMenuItem(TMenu.MID_TABLE_DELETE_COLUMN);
136 getApplication().enableMenuItem(TMenu.MID_TABLE_INSERT_LEFT);
137 getApplication().enableMenuItem(TMenu.MID_TABLE_INSERT_RIGHT);
138 getApplication().enableMenuItem(TMenu.MID_TABLE_INSERT_ABOVE);
139 getApplication().enableMenuItem(TMenu.MID_TABLE_INSERT_BELOW);
140 getApplication().enableMenuItem(TMenu.MID_TABLE_COLUMN_NARROW);
141 getApplication().enableMenuItem(TMenu.MID_TABLE_COLUMN_WIDEN);
142 getApplication().enableMenuItem(TMenu.MID_TABLE_FILE_OPEN_CSV);
143 getApplication().enableMenuItem(TMenu.MID_TABLE_FILE_SAVE_CSV);
144 getApplication().enableMenuItem(TMenu.MID_TABLE_FILE_SAVE_TEXT);
145
146 if (tableField != null) {
147
148 // Set the menu to match the flags.
149 TMenuItem menuItem = getApplication().getMenuItem(TMenu.MID_TABLE_VIEW_ROW_LABELS);
150 if (menuItem != null) {
151 menuItem.setChecked(tableField.getShowRowLabels());
152 }
153 menuItem = getApplication().getMenuItem(TMenu.MID_TABLE_VIEW_COLUMN_LABELS);
154 if (menuItem != null) {
155 menuItem.setChecked(tableField.getShowColumnLabels());
156 }
157 menuItem = getApplication().getMenuItem(TMenu.MID_TABLE_VIEW_HIGHLIGHT_ROW);
158 if (menuItem != null) {
159 menuItem.setChecked(tableField.getHighlightRow());
160 }
161 menuItem = getApplication().getMenuItem(TMenu.MID_TABLE_VIEW_HIGHLIGHT_COLUMN);
162 if (menuItem != null) {
163 menuItem.setChecked(tableField.getHighlightColumn());
164 }
165 }
166 }
167
168 /**
169 * Called by application.switchWindow() when another window gets the
170 * focus.
171 */
172 public void onUnfocus() {
173 // Disable the table menu items.
174 getApplication().disableMenuItem(TMenu.MID_CUT);
175 getApplication().disableMenuItem(TMenu.MID_TABLE_RENAME_COLUMN);
176 getApplication().disableMenuItem(TMenu.MID_TABLE_RENAME_ROW);
177 getApplication().disableMenuItem(TMenu.MID_TABLE_VIEW_ROW_LABELS);
178 getApplication().disableMenuItem(TMenu.MID_TABLE_VIEW_COLUMN_LABELS);
179 getApplication().disableMenuItem(TMenu.MID_TABLE_VIEW_HIGHLIGHT_ROW);
180 getApplication().disableMenuItem(TMenu.MID_TABLE_VIEW_HIGHLIGHT_COLUMN);
181 getApplication().disableMenuItem(TMenu.MID_TABLE_BORDER_NONE);
182 getApplication().disableMenuItem(TMenu.MID_TABLE_BORDER_ALL);
183 getApplication().disableMenuItem(TMenu.MID_TABLE_BORDER_CELL_NONE);
184 getApplication().disableMenuItem(TMenu.MID_TABLE_BORDER_CELL_ALL);
185 getApplication().disableMenuItem(TMenu.MID_TABLE_BORDER_RIGHT);
186 getApplication().disableMenuItem(TMenu.MID_TABLE_BORDER_LEFT);
187 getApplication().disableMenuItem(TMenu.MID_TABLE_BORDER_TOP);
188 getApplication().disableMenuItem(TMenu.MID_TABLE_BORDER_BOTTOM);
189 getApplication().disableMenuItem(TMenu.MID_TABLE_BORDER_DOUBLE_BOTTOM);
190 getApplication().disableMenuItem(TMenu.MID_TABLE_BORDER_THICK_BOTTOM);
191 getApplication().disableMenuItem(TMenu.MID_TABLE_DELETE_LEFT);
192 getApplication().disableMenuItem(TMenu.MID_TABLE_DELETE_UP);
193 getApplication().disableMenuItem(TMenu.MID_TABLE_DELETE_ROW);
194 getApplication().disableMenuItem(TMenu.MID_TABLE_DELETE_COLUMN);
195 getApplication().disableMenuItem(TMenu.MID_TABLE_INSERT_LEFT);
196 getApplication().disableMenuItem(TMenu.MID_TABLE_INSERT_RIGHT);
197 getApplication().disableMenuItem(TMenu.MID_TABLE_INSERT_ABOVE);
198 getApplication().disableMenuItem(TMenu.MID_TABLE_INSERT_BELOW);
199 getApplication().disableMenuItem(TMenu.MID_TABLE_COLUMN_NARROW);
200 getApplication().disableMenuItem(TMenu.MID_TABLE_COLUMN_WIDEN);
201 getApplication().disableMenuItem(TMenu.MID_TABLE_FILE_OPEN_CSV);
202 getApplication().disableMenuItem(TMenu.MID_TABLE_FILE_SAVE_CSV);
203 getApplication().disableMenuItem(TMenu.MID_TABLE_FILE_SAVE_TEXT);
204 }
205
206 // ------------------------------------------------------------------------
207 // TWindow ----------------------------------------------------------------
208 // ------------------------------------------------------------------------
209
210 /**
211 * Handle mouse press events.
212 *
213 * @param mouse mouse button press event
214 */
215 @Override
216 public void onMouseDown(final TMouseEvent mouse) {
217 // Use TWidget's code to pass the event to the children.
218 super.onMouseDown(mouse);
219
220 if (mouseOnTable(mouse)) {
221 // The table might have changed, update the scollbars.
222 setBottomValue(tableField.getRowCount() - 1);
223 setVerticalValue(tableField.getSelectedRowNumber());
224 setRightValue(tableField.getColumnCount() - 1);
225 setHorizontalValue(tableField.getSelectedColumnNumber());
226 }
227 }
228
229 /**
230 * Handle mouse release events.
231 *
232 * @param mouse mouse button release event
233 */
234 @Override
235 public void onMouseUp(final TMouseEvent mouse) {
236 // Use TWidget's code to pass the event to the children.
237 super.onMouseUp(mouse);
238
239 if (mouse.isMouse1() && mouseOnVerticalScroller(mouse)) {
240 // Clicked/dragged on vertical scrollbar.
241 tableField.setSelectedRowNumber(getVerticalValue());
242 }
243 if (mouse.isMouse1() && mouseOnHorizontalScroller(mouse)) {
244 // Clicked/dragged on horizontal scrollbar.
245 tableField.setSelectedColumnNumber(getHorizontalValue());
246 }
247 }
248
249 /**
250 * Method that subclasses can override to handle mouse movements.
251 *
252 * @param mouse mouse motion event
253 */
254 @Override
255 public void onMouseMotion(final TMouseEvent mouse) {
256 // Use TWidget's code to pass the event to the children.
257 super.onMouseMotion(mouse);
258
259 if (mouseOnTable(mouse) && mouse.isMouse1()) {
260 // The table might have changed, update the scollbars.
261 setBottomValue(tableField.getRowCount() - 1);
262 setVerticalValue(tableField.getSelectedRowNumber());
263 setRightValue(tableField.getColumnCount() - 1);
264 setHorizontalValue(tableField.getSelectedColumnNumber());
265 } else {
266 if (mouse.isMouse1() && mouseOnVerticalScroller(mouse)) {
267 // Clicked/dragged on vertical scrollbar.
268 tableField.setSelectedRowNumber(getVerticalValue());
269 }
270 if (mouse.isMouse1() && mouseOnHorizontalScroller(mouse)) {
271 // Clicked/dragged on horizontal scrollbar.
272 tableField.setSelectedColumnNumber(getHorizontalValue());
273 }
274 }
275
276 }
277
278 /**
279 * Handle keystrokes.
280 *
281 * @param keypress keystroke event
282 */
283 @Override
284 public void onKeypress(final TKeypressEvent keypress) {
285 // Use TWidget's code to pass the event to the children.
286 super.onKeypress(keypress);
287
288 // The table might have changed, update the scollbars.
289 setBottomValue(tableField.getRowCount() - 1);
290 setVerticalValue(tableField.getSelectedRowNumber());
291 setRightValue(tableField.getColumnCount() - 1);
292 setHorizontalValue(tableField.getSelectedColumnNumber());
293 }
294
295 /**
296 * Handle window/screen resize events.
297 *
298 * @param event resize event
299 */
300 @Override
301 public void onResize(final TResizeEvent event) {
302 if (event.getType() == TResizeEvent.Type.WIDGET) {
303 // Resize the table
304 TResizeEvent tableSize = new TResizeEvent(TResizeEvent.Type.WIDGET,
305 event.getWidth() - 2, event.getHeight() - 2);
306 tableField.onResize(tableSize);
307
308 // Have TScrollableWindow handle the scrollbars
309 super.onResize(event);
310 return;
311 }
312
313 // Pass to children instead
314 for (TWidget widget: getChildren()) {
315 widget.onResize(event);
316 }
317 }
318
319 /**
320 * Method that subclasses can override to handle posted command events.
321 *
322 * @param command command event
323 */
324 @Override
325 public void onCommand(final TCommandEvent command) {
326 if (command.equals(cmOpen)) {
327 try {
328 String filename = fileOpenBox(".");
329 if (filename != null) {
330 try {
331 new TTableWindow(getApplication(), new File(filename));
332 } catch (IOException e) {
333 messageBox(i18n.getString("errorDialogTitle"),
334 MessageFormat.format(i18n.
335 getString("errorReadingFile"), e.getMessage()));
336 }
337 }
338 } catch (IOException e) {
339 messageBox(i18n.getString("errorDialogTitle"),
340 MessageFormat.format(i18n.
341 getString("errorOpeningFileDialog"), e.getMessage()));
342 }
343 return;
344 }
345
346 // Didn't handle it, let children get it instead
347 super.onCommand(command);
348 }
349
350 /**
351 * Handle posted menu events.
352 *
353 * @param menu menu event
354 */
355 @Override
356 public void onMenu(final TMenuEvent menu) {
357 TInputBox inputBox = null;
358 String filename = null;
359
360 switch (menu.getId()) {
361 case TMenu.MID_TABLE_RENAME_COLUMN:
362 inputBox = inputBox(i18n.getString("renameColumnInputTitle"),
363 i18n.getString("renameColumnInputCaption"),
364 tableField.getColumnLabel(tableField.getSelectedColumnNumber()),
365 TMessageBox.Type.OKCANCEL);
366 if (inputBox.isOk()) {
367 tableField.setColumnLabel(tableField.getSelectedColumnNumber(),
368 inputBox.getText());
369 }
370 return;
371 case TMenu.MID_TABLE_RENAME_ROW:
372 inputBox = inputBox(i18n.getString("renameRowInputTitle"),
373 i18n.getString("renameRowInputCaption"),
374 tableField.getRowLabel(tableField.getSelectedRowNumber()),
375 TMessageBox.Type.OKCANCEL);
376 if (inputBox.isOk()) {
377 tableField.setRowLabel(tableField.getSelectedRowNumber(),
378 inputBox.getText());
379 }
380 return;
381 case TMenu.MID_TABLE_VIEW_ROW_LABELS:
382 tableField.setShowRowLabels(getApplication().getMenuItem(
383 menu.getId()).getChecked());
384 return;
385 case TMenu.MID_TABLE_VIEW_COLUMN_LABELS:
386 tableField.setShowColumnLabels(getApplication().getMenuItem(
387 menu.getId()).getChecked());
388 return;
389 case TMenu.MID_TABLE_VIEW_HIGHLIGHT_ROW:
390 tableField.setHighlightRow(getApplication().getMenuItem(
391 menu.getId()).getChecked());
392 return;
393 case TMenu.MID_TABLE_VIEW_HIGHLIGHT_COLUMN:
394 tableField.setHighlightColumn(getApplication().getMenuItem(
395 menu.getId()).getChecked());
396 return;
397 case TMenu.MID_TABLE_BORDER_NONE:
398 tableField.setBorderAllNone();
399 return;
400 case TMenu.MID_TABLE_BORDER_ALL:
401 tableField.setBorderAllSingle();
402 return;
403 case TMenu.MID_TABLE_BORDER_CELL_NONE:
404 tableField.setBorderCellNone();
405 return;
406 case TMenu.MID_TABLE_BORDER_CELL_ALL:
407 tableField.setBorderCellSingle();
408 return;
409 case TMenu.MID_TABLE_BORDER_RIGHT:
410 tableField.setBorderColumnRightSingle();
411 return;
412 case TMenu.MID_TABLE_BORDER_LEFT:
413 tableField.setBorderColumnLeftSingle();
414 return;
415 case TMenu.MID_TABLE_BORDER_TOP:
416 tableField.setBorderRowAboveSingle();
417 return;
418 case TMenu.MID_TABLE_BORDER_BOTTOM:
419 tableField.setBorderRowBelowSingle();
420 return;
421 case TMenu.MID_TABLE_BORDER_DOUBLE_BOTTOM:
422 tableField.setBorderRowBelowDouble();
423 return;
424 case TMenu.MID_TABLE_BORDER_THICK_BOTTOM:
425 tableField.setBorderRowBelowThick();
426 return;
427 case TMenu.MID_TABLE_DELETE_LEFT:
428 tableField.deleteCellShiftLeft();
429 return;
430 case TMenu.MID_TABLE_DELETE_UP:
431 tableField.deleteCellShiftUp();
432 return;
433 case TMenu.MID_TABLE_DELETE_ROW:
434 tableField.deleteRow(tableField.getSelectedRowNumber());
435 return;
436 case TMenu.MID_TABLE_DELETE_COLUMN:
437 tableField.deleteColumn(tableField.getSelectedColumnNumber());
438 return;
439 case TMenu.MID_TABLE_INSERT_LEFT:
440 tableField.insertColumnLeft(tableField.getSelectedColumnNumber());
441 return;
442 case TMenu.MID_TABLE_INSERT_RIGHT:
443 tableField.insertColumnRight(tableField.getSelectedColumnNumber());
444 return;
445 case TMenu.MID_TABLE_INSERT_ABOVE:
446 tableField.insertRowAbove(tableField.getSelectedColumnNumber());
447 return;
448 case TMenu.MID_TABLE_INSERT_BELOW:
449 tableField.insertRowBelow(tableField.getSelectedColumnNumber());
450 return;
451 case TMenu.MID_TABLE_COLUMN_NARROW:
452 tableField.setColumnWidth(tableField.getSelectedColumnNumber(),
453 tableField.getColumnWidth(tableField.getSelectedColumnNumber()) - 1);
454 return;
455 case TMenu.MID_TABLE_COLUMN_WIDEN:
456 tableField.setColumnWidth(tableField.getSelectedColumnNumber(),
457 tableField.getColumnWidth(tableField.getSelectedColumnNumber()) + 1);
458 return;
459 case TMenu.MID_TABLE_FILE_OPEN_CSV:
460 try {
461 filename = fileOpenBox(".");
462 if (filename != null) {
463 try {
464 new TTableWindow(getApplication(), new File(filename));
465 } catch (IOException e) {
466 messageBox(i18n.getString("errorDialogTitle"),
467 MessageFormat.format(i18n.
468 getString("errorReadingFile"), e.getMessage()));
469 }
470 }
471 } catch (IOException e) {
472 messageBox(i18n.getString("errorDialogTitle"),
473 MessageFormat.format(i18n.
474 getString("errorOpeningFileDialog"), e.getMessage()));
475 }
476 return;
477 case TMenu.MID_TABLE_FILE_SAVE_CSV:
478 try {
479 filename = fileSaveBox(".");
480 if (filename != null) {
481 tableField.saveToCsvFilename(filename);
482 }
483 } catch (IOException e) {
484 messageBox(i18n.getString("errorDialogTitle"),
485 MessageFormat.format(i18n.
486 getString("errorWritingFile"), e.getMessage()));
487 }
488 return;
489 case TMenu.MID_TABLE_FILE_SAVE_TEXT:
490 try {
491 filename = fileSaveBox(".");
492 if (filename != null) {
493 tableField.saveToTextFilename(filename);
494 }
495 } catch (IOException e) {
496 messageBox(i18n.getString("errorDialogTitle"),
497 MessageFormat.format(i18n.
498 getString("errorWritingFile"), e.getMessage()));
499 }
500 return;
501 default:
502 break;
503 }
504
505 super.onMenu(menu);
506 }
507
508 // ------------------------------------------------------------------------
509 // TTableWindow -----------------------------------------------------------
510 // ------------------------------------------------------------------------
511
512 /**
513 * Setup other fields after the table is created.
514 */
515 private void setupAfterTable() {
516 hScroller = new THScroller(this, 17, getHeight() - 2, getWidth() - 20);
517 vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
518 setMinimumWindowWidth(25);
519 setMinimumWindowHeight(10);
520 setTopValue(tableField.getSelectedRowNumber());
521 setBottomValue(tableField.getRowCount() - 1);
522 setLeftValue(tableField.getSelectedColumnNumber());
523 setRightValue(tableField.getColumnCount() - 1);
524
525 statusBar = newStatusBar(i18n.getString("statusBar"));
526 statusBar.addShortcutKeypress(kbF1, cmHelp,
527 i18n.getString("statusBarHelp"));
528
529 statusBar.addShortcutKeypress(kbF2, cmSave,
530 i18n.getString("statusBarSave"));
531 statusBar.addShortcutKeypress(kbF3, cmOpen,
532 i18n.getString("statusBarOpen"));
533 statusBar.addShortcutKeypress(kbF10, cmMenu,
534 i18n.getString("statusBarMenu"));
535
536 // Synchronize the menu with tableField's flags.
537 onFocus();
538 }
539
540 /**
541 * Check if a mouse press/release/motion event coordinate is over the
542 * table.
543 *
544 * @param mouse a mouse-based event
545 * @return whether or not the mouse is on the table
546 */
547 private boolean mouseOnTable(final TMouseEvent mouse) {
548 if ((mouse.getAbsoluteX() >= getAbsoluteX() + 1)
549 && (mouse.getAbsoluteX() < getAbsoluteX() + getWidth() - 1)
550 && (mouse.getAbsoluteY() >= getAbsoluteY() + 1)
551 && (mouse.getAbsoluteY() < getAbsoluteY() + getHeight() - 1)
552 ) {
553 return true;
554 }
555 return false;
556 }
557
558 }