Merge branch 'subtree'
[fanfix.git] / src / jexer / TTableWindow.java
index 88fca16092740e0897fd021f96ab25520278c4d4..766ceafdd901a0a4044105aba5114093cf7ef7a6 100644 (file)
@@ -28,6 +28,7 @@
  */
 package jexer;
 
+import java.io.File;
 import java.io.IOException;
 import java.text.MessageFormat;
 import java.util.ResourceBundle;
@@ -81,6 +82,26 @@ public class TTableWindow extends TScrollableWindow {
         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 ---------------------------------------------------------
     // ------------------------------------------------------------------------
@@ -91,7 +112,6 @@ 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);
@@ -150,7 +170,6 @@ 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);
@@ -307,10 +326,7 @@ public class TTableWindow extends TScrollableWindow {
                 String filename = fileOpenBox(".");
                 if (filename != null) {
                     try {
-                        // TODO
-                        if (false) {
-                            tableField.saveToCsvFilename(filename);
-                        }
+                        new TTableWindow(getApplication(), new File(filename));
                     } catch (IOException e) {
                         messageBox(i18n.getString("errorDialogTitle"),
                             MessageFormat.format(i18n.
@@ -325,6 +341,20 @@ public class TTableWindow extends TScrollableWindow {
             return;
         }
 
+        if (command.equals(cmSave)) {
+            try {
+                String filename = fileSaveBox(".");
+                if (filename != null) {
+                    tableField.saveToCsvFilename(filename);
+                }
+            } catch (IOException e) {
+                messageBox(i18n.getString("errorDialogTitle"),
+                    MessageFormat.format(i18n.
+                        getString("errorWritingFile"), e.getMessage()));
+            }
+            return;
+        }
+
         // Didn't handle it, let children get it instead
         super.onCommand(command);
     }
@@ -336,7 +366,8 @@ public class TTableWindow extends TScrollableWindow {
      */
     @Override
     public void onMenu(final TMenuEvent menu) {
-        TInputBox inputBox;
+        TInputBox inputBox = null;
+        String filename = null;
 
         switch (menu.getId()) {
         case TMenu.MID_TABLE_RENAME_COLUMN:
@@ -438,13 +469,46 @@ public class TTableWindow extends TScrollableWindow {
                 tableField.getColumnWidth(tableField.getSelectedColumnNumber()) + 1);
             return;
         case TMenu.MID_TABLE_FILE_OPEN_CSV:
-            // TODO
+            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:
-            // TODO
+            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:
-            // TODO
+            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;