X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTTableWindow.java;h=766ceafdd901a0a4044105aba5114093cf7ef7a6;hb=HEAD;hp=47a5b9ec37368537e11c89afabb414c28f68a4bb;hpb=759cb83ebad2f861e50f39dab34f70eaafe6d6ed;p=fanfix.git diff --git a/src/jexer/TTableWindow.java b/src/jexer/TTableWindow.java index 47a5b9e..766ceaf 100644 --- a/src/jexer/TTableWindow.java +++ b/src/jexer/TTableWindow.java @@ -28,15 +28,18 @@ */ package jexer; +import java.io.File; import java.io.IOException; import java.text.MessageFormat; import java.util.ResourceBundle; import jexer.event.TCommandEvent; import jexer.event.TKeypressEvent; +import jexer.event.TMenuEvent; import jexer.event.TMouseEvent; import jexer.event.TResizeEvent; import jexer.menu.TMenu; +import jexer.menu.TMenuItem; import static jexer.TCommand.*; import static jexer.TKeypress.*; @@ -75,10 +78,30 @@ public class TTableWindow extends TScrollableWindow { super(parent, title, 0, 0, parent.getScreen().getWidth() / 2, parent.getScreen().getHeight() / 2 - 2, RESIZABLE | CENTERED); - tableField = new TTableWidget(this, 0, 0, getWidth() - 2, getHeight() - 2); + tableField = addTable(0, 0, getWidth() - 2, getHeight() - 2); setupAfterTable(); } + /** + * Public constructor loads a grid from a RFC4180 CSV file. + * + * @param parent the main application + * @param csvFile a File referencing the CSV data + * @throws IOException if a java.io operation throws + */ + public TTableWindow(final TApplication parent, + final File csvFile) throws IOException { + + super(parent, csvFile.getName(), 0, 0, + parent.getScreen().getWidth() / 2, + parent.getScreen().getHeight() / 2 - 2, + RESIZABLE | CENTERED); + + tableField = addTable(0, 0, getWidth() - 2, getHeight() - 2, 1, 1); + setupAfterTable(); + tableField.loadCsvFile(csvFile); + } + // ------------------------------------------------------------------------ // Event handlers --------------------------------------------------------- // ------------------------------------------------------------------------ @@ -89,13 +112,16 @@ public class TTableWindow extends TScrollableWindow { */ public void onFocus() { // Enable the table menu items. - getApplication().enableMenuItem(TMenu.MID_CUT); + getApplication().enableMenuItem(TMenu.MID_TABLE_RENAME_COLUMN); + getApplication().enableMenuItem(TMenu.MID_TABLE_RENAME_ROW); getApplication().enableMenuItem(TMenu.MID_TABLE_VIEW_ROW_LABELS); getApplication().enableMenuItem(TMenu.MID_TABLE_VIEW_COLUMN_LABELS); getApplication().enableMenuItem(TMenu.MID_TABLE_VIEW_HIGHLIGHT_ROW); getApplication().enableMenuItem(TMenu.MID_TABLE_VIEW_HIGHLIGHT_COLUMN); getApplication().enableMenuItem(TMenu.MID_TABLE_BORDER_NONE); getApplication().enableMenuItem(TMenu.MID_TABLE_BORDER_ALL); + getApplication().enableMenuItem(TMenu.MID_TABLE_BORDER_CELL_NONE); + getApplication().enableMenuItem(TMenu.MID_TABLE_BORDER_CELL_ALL); getApplication().enableMenuItem(TMenu.MID_TABLE_BORDER_RIGHT); getApplication().enableMenuItem(TMenu.MID_TABLE_BORDER_LEFT); getApplication().enableMenuItem(TMenu.MID_TABLE_BORDER_TOP); @@ -112,8 +138,30 @@ public class TTableWindow extends TScrollableWindow { getApplication().enableMenuItem(TMenu.MID_TABLE_INSERT_BELOW); getApplication().enableMenuItem(TMenu.MID_TABLE_COLUMN_NARROW); getApplication().enableMenuItem(TMenu.MID_TABLE_COLUMN_WIDEN); + getApplication().enableMenuItem(TMenu.MID_TABLE_FILE_OPEN_CSV); getApplication().enableMenuItem(TMenu.MID_TABLE_FILE_SAVE_CSV); getApplication().enableMenuItem(TMenu.MID_TABLE_FILE_SAVE_TEXT); + + if (tableField != null) { + + // Set the menu to match the flags. + TMenuItem menuItem = getApplication().getMenuItem(TMenu.MID_TABLE_VIEW_ROW_LABELS); + if (menuItem != null) { + menuItem.setChecked(tableField.getShowRowLabels()); + } + menuItem = getApplication().getMenuItem(TMenu.MID_TABLE_VIEW_COLUMN_LABELS); + if (menuItem != null) { + menuItem.setChecked(tableField.getShowColumnLabels()); + } + menuItem = getApplication().getMenuItem(TMenu.MID_TABLE_VIEW_HIGHLIGHT_ROW); + if (menuItem != null) { + menuItem.setChecked(tableField.getHighlightRow()); + } + menuItem = getApplication().getMenuItem(TMenu.MID_TABLE_VIEW_HIGHLIGHT_COLUMN); + if (menuItem != null) { + menuItem.setChecked(tableField.getHighlightColumn()); + } + } } /** @@ -122,13 +170,16 @@ public class TTableWindow extends TScrollableWindow { */ public void onUnfocus() { // Disable the table menu items. - getApplication().disableMenuItem(TMenu.MID_CUT); + getApplication().disableMenuItem(TMenu.MID_TABLE_RENAME_COLUMN); + getApplication().disableMenuItem(TMenu.MID_TABLE_RENAME_ROW); getApplication().disableMenuItem(TMenu.MID_TABLE_VIEW_ROW_LABELS); getApplication().disableMenuItem(TMenu.MID_TABLE_VIEW_COLUMN_LABELS); getApplication().disableMenuItem(TMenu.MID_TABLE_VIEW_HIGHLIGHT_ROW); getApplication().disableMenuItem(TMenu.MID_TABLE_VIEW_HIGHLIGHT_COLUMN); getApplication().disableMenuItem(TMenu.MID_TABLE_BORDER_NONE); getApplication().disableMenuItem(TMenu.MID_TABLE_BORDER_ALL); + getApplication().disableMenuItem(TMenu.MID_TABLE_BORDER_CELL_NONE); + getApplication().disableMenuItem(TMenu.MID_TABLE_BORDER_CELL_ALL); getApplication().disableMenuItem(TMenu.MID_TABLE_BORDER_RIGHT); getApplication().disableMenuItem(TMenu.MID_TABLE_BORDER_LEFT); getApplication().disableMenuItem(TMenu.MID_TABLE_BORDER_TOP); @@ -145,6 +196,7 @@ public class TTableWindow extends TScrollableWindow { getApplication().disableMenuItem(TMenu.MID_TABLE_INSERT_BELOW); getApplication().disableMenuItem(TMenu.MID_TABLE_COLUMN_NARROW); getApplication().disableMenuItem(TMenu.MID_TABLE_COLUMN_WIDEN); + getApplication().disableMenuItem(TMenu.MID_TABLE_FILE_OPEN_CSV); getApplication().disableMenuItem(TMenu.MID_TABLE_FILE_SAVE_CSV); getApplication().disableMenuItem(TMenu.MID_TABLE_FILE_SAVE_TEXT); } @@ -274,10 +326,7 @@ public class TTableWindow extends TScrollableWindow { String filename = fileOpenBox("."); if (filename != null) { try { - // TODO - if (false) { - tableField.saveToFilename(filename); - } + new TTableWindow(getApplication(), new File(filename)); } catch (IOException e) { messageBox(i18n.getString("errorDialogTitle"), MessageFormat.format(i18n. @@ -296,12 +345,12 @@ public class TTableWindow extends TScrollableWindow { try { String filename = fileSaveBox("."); if (filename != null) { - tableField.saveToFilename(filename); + tableField.saveToCsvFilename(filename); } } catch (IOException e) { messageBox(i18n.getString("errorDialogTitle"), MessageFormat.format(i18n. - getString("errorSavingFile"), e.getMessage())); + getString("errorWritingFile"), e.getMessage())); } return; } @@ -310,6 +359,164 @@ public class TTableWindow extends TScrollableWindow { super.onCommand(command); } + /** + * Handle posted menu events. + * + * @param menu menu event + */ + @Override + public void onMenu(final TMenuEvent menu) { + TInputBox inputBox = null; + String filename = null; + + switch (menu.getId()) { + case TMenu.MID_TABLE_RENAME_COLUMN: + inputBox = inputBox(i18n.getString("renameColumnInputTitle"), + i18n.getString("renameColumnInputCaption"), + tableField.getColumnLabel(tableField.getSelectedColumnNumber()), + TMessageBox.Type.OKCANCEL); + if (inputBox.isOk()) { + tableField.setColumnLabel(tableField.getSelectedColumnNumber(), + inputBox.getText()); + } + return; + case TMenu.MID_TABLE_RENAME_ROW: + inputBox = inputBox(i18n.getString("renameRowInputTitle"), + i18n.getString("renameRowInputCaption"), + tableField.getRowLabel(tableField.getSelectedRowNumber()), + TMessageBox.Type.OKCANCEL); + if (inputBox.isOk()) { + tableField.setRowLabel(tableField.getSelectedRowNumber(), + inputBox.getText()); + } + return; + case TMenu.MID_TABLE_VIEW_ROW_LABELS: + tableField.setShowRowLabels(getApplication().getMenuItem( + menu.getId()).getChecked()); + return; + case TMenu.MID_TABLE_VIEW_COLUMN_LABELS: + tableField.setShowColumnLabels(getApplication().getMenuItem( + menu.getId()).getChecked()); + return; + case TMenu.MID_TABLE_VIEW_HIGHLIGHT_ROW: + tableField.setHighlightRow(getApplication().getMenuItem( + menu.getId()).getChecked()); + return; + case TMenu.MID_TABLE_VIEW_HIGHLIGHT_COLUMN: + tableField.setHighlightColumn(getApplication().getMenuItem( + menu.getId()).getChecked()); + return; + case TMenu.MID_TABLE_BORDER_NONE: + tableField.setBorderAllNone(); + return; + case TMenu.MID_TABLE_BORDER_ALL: + tableField.setBorderAllSingle(); + return; + case TMenu.MID_TABLE_BORDER_CELL_NONE: + tableField.setBorderCellNone(); + return; + case TMenu.MID_TABLE_BORDER_CELL_ALL: + tableField.setBorderCellSingle(); + return; + case TMenu.MID_TABLE_BORDER_RIGHT: + tableField.setBorderColumnRightSingle(); + return; + case TMenu.MID_TABLE_BORDER_LEFT: + tableField.setBorderColumnLeftSingle(); + return; + case TMenu.MID_TABLE_BORDER_TOP: + tableField.setBorderRowAboveSingle(); + return; + case TMenu.MID_TABLE_BORDER_BOTTOM: + tableField.setBorderRowBelowSingle(); + return; + case TMenu.MID_TABLE_BORDER_DOUBLE_BOTTOM: + tableField.setBorderRowBelowDouble(); + return; + case TMenu.MID_TABLE_BORDER_THICK_BOTTOM: + tableField.setBorderRowBelowThick(); + return; + case TMenu.MID_TABLE_DELETE_LEFT: + tableField.deleteCellShiftLeft(); + return; + case TMenu.MID_TABLE_DELETE_UP: + tableField.deleteCellShiftUp(); + return; + case TMenu.MID_TABLE_DELETE_ROW: + tableField.deleteRow(tableField.getSelectedRowNumber()); + return; + case TMenu.MID_TABLE_DELETE_COLUMN: + tableField.deleteColumn(tableField.getSelectedColumnNumber()); + return; + case TMenu.MID_TABLE_INSERT_LEFT: + tableField.insertColumnLeft(tableField.getSelectedColumnNumber()); + return; + case TMenu.MID_TABLE_INSERT_RIGHT: + tableField.insertColumnRight(tableField.getSelectedColumnNumber()); + return; + case TMenu.MID_TABLE_INSERT_ABOVE: + tableField.insertRowAbove(tableField.getSelectedColumnNumber()); + return; + case TMenu.MID_TABLE_INSERT_BELOW: + tableField.insertRowBelow(tableField.getSelectedColumnNumber()); + return; + case TMenu.MID_TABLE_COLUMN_NARROW: + tableField.setColumnWidth(tableField.getSelectedColumnNumber(), + tableField.getColumnWidth(tableField.getSelectedColumnNumber()) - 1); + return; + case TMenu.MID_TABLE_COLUMN_WIDEN: + tableField.setColumnWidth(tableField.getSelectedColumnNumber(), + tableField.getColumnWidth(tableField.getSelectedColumnNumber()) + 1); + return; + case TMenu.MID_TABLE_FILE_OPEN_CSV: + try { + filename = fileOpenBox("."); + if (filename != null) { + try { + new TTableWindow(getApplication(), new File(filename)); + } catch (IOException e) { + messageBox(i18n.getString("errorDialogTitle"), + MessageFormat.format(i18n. + getString("errorReadingFile"), e.getMessage())); + } + } + } catch (IOException e) { + messageBox(i18n.getString("errorDialogTitle"), + MessageFormat.format(i18n. + getString("errorOpeningFileDialog"), e.getMessage())); + } + return; + case TMenu.MID_TABLE_FILE_SAVE_CSV: + try { + filename = fileSaveBox("."); + if (filename != null) { + tableField.saveToCsvFilename(filename); + } + } catch (IOException e) { + messageBox(i18n.getString("errorDialogTitle"), + MessageFormat.format(i18n. + getString("errorWritingFile"), e.getMessage())); + } + return; + case TMenu.MID_TABLE_FILE_SAVE_TEXT: + try { + filename = fileSaveBox("."); + if (filename != null) { + tableField.saveToTextFilename(filename); + } + } catch (IOException e) { + messageBox(i18n.getString("errorDialogTitle"), + MessageFormat.format(i18n. + getString("errorWritingFile"), e.getMessage())); + } + return; + default: + break; + } + + super.onMenu(menu); + } + // ------------------------------------------------------------------------ // TTableWindow ----------------------------------------------------------- // ------------------------------------------------------------------------ @@ -337,6 +544,9 @@ public class TTableWindow extends TScrollableWindow { i18n.getString("statusBarOpen")); statusBar.addShortcutKeypress(kbF10, cmMenu, i18n.getString("statusBarMenu")); + + // Synchronize the menu with tableField's flags. + onFocus(); } /**