editvar propose direct save to the CSV format of the whole variable or the selection. 79/10379/6
Sylvestre Ledru [Thu, 31 Jan 2013 23:12:34 +0000 (00:12 +0100)]
Change-Id: I2a903fe864d188099d2d0ea2d79f8ed094a6e37e

scilab/CHANGES_5.4.X
scilab/modules/ui_data/src/java/org/scilab/modules/ui_data/utils/UiDataMessages.java
scilab/modules/ui_data/src/java/org/scilab/modules/ui_data/variableeditor/SwingScilabVariableEditor.java
scilab/modules/ui_data/src/java/org/scilab/modules/ui_data/variableeditor/actions/DuplicateVariableAction.java
scilab/modules/ui_data/src/java/org/scilab/modules/ui_data/variableeditor/actions/ExportToCsvAction.java [new file with mode: 0644]
scilab/modules/ui_data/src/java/org/scilab/modules/ui_data/variableeditor/actions/ExportToCsvFromSelectionAction.java [new file with mode: 0644]

index b4e9d69..4b807cb 100644 (file)
@@ -32,6 +32,9 @@ Improvements
 * Search is now possible in the console. Shortcut F3 or CTRL+F enables it.
   See Bug #11841.
 
+* editvar propose direct save to the CSV format of the whole variable or the
+  selection.
+
 
 Removed functions
 ==================
index 7439469..b2ef2ec 100644 (file)
@@ -94,6 +94,8 @@ public final class UiDataMessages {
     public static final String VARNAME = Messages.gettext("Variable name");
     public static final String CREATENEWVAR = Messages.gettext("Create new variable");
     public static final String DUPLICATE = Messages.gettext("Duplicate the variable");
+    public static final String EXPORTCSV = Messages.gettext("Export to CSV");
+    public static final String EXPORTSELECTIONCSV = Messages.gettext("Export the selection to CSV");
     public static final String NUMROWS = Messages.gettext("Rows number");
     public static final String NUMCOLS = Messages.gettext("Columns number");
     public static final String FILLWITH = Messages.gettext("Fill with");
index e17e277..8aeb1ac 100644 (file)
@@ -52,6 +52,8 @@ import org.scilab.modules.ui_data.variableeditor.actions.CreateNewVariableAction
 import org.scilab.modules.ui_data.variableeditor.actions.CreateVariableFromSelectionAction;
 import org.scilab.modules.ui_data.variableeditor.actions.CutAction;
 import org.scilab.modules.ui_data.variableeditor.actions.DuplicateVariableAction;
+import org.scilab.modules.ui_data.variableeditor.actions.ExportToCsvAction;
+import org.scilab.modules.ui_data.variableeditor.actions.ExportToCsvFromSelectionAction;
 import org.scilab.modules.ui_data.variableeditor.actions.PasteAction;
 import org.scilab.modules.ui_data.variableeditor.actions.PlotAction;
 import org.scilab.modules.ui_data.variableeditor.actions.RedoAction;
@@ -97,7 +99,7 @@ public class SwingScilabVariableEditor extends SwingScilabTab implements SimpleV
      */
     public SwingScilabVariableEditor(String name, String type, Object[][] data) {
         super(UiDataMessages.VARIABLE_EDITOR);
-       setAssociatedXMLIDForHelp("editvar");
+        setAssociatedXMLIDForHelp("editvar");
         setWindowIcon("rrze_table");
         refreshButton = RefreshAction.createButton(this, UiDataMessages.REFRESH);
         undoButton = UndoAction.createButton(this, UiDataMessages.UNDO);
@@ -133,8 +135,8 @@ public class SwingScilabVariableEditor extends SwingScilabTab implements SimpleV
         setData(name, type, data);
         WindowsConfigurationManager.restorationFinished(this);
 
-       addToolBar(createToolBar());
-       addMenuBar(createMenuBar());
+        addToolBar(createToolBar());
+        addMenuBar(createMenuBar());
     }
 
     /**
@@ -234,11 +236,13 @@ public class SwingScilabVariableEditor extends SwingScilabTab implements SimpleV
         popup.addSeparator();
         popup.add(CopySelectionAsScilabCommandAction.createJMenuItem(this, UiDataMessages.COPYASSCILABCOM));
         popup.add(CopySelectionAsScilabMatrixAction.createJMenuItem(this, UiDataMessages.COPYASSCILABMAT));
-       popup.add(CopySelectionAsScilabMatrixWithCRAction.createJMenuItem(this, UiDataMessages.COPYASSCILABMATWITHCR));
+        popup.add(CopySelectionAsScilabMatrixWithCRAction.createJMenuItem(this, UiDataMessages.COPYASSCILABMATWITHCR));
+        popup.add(ExportToCsvFromSelectionAction.createJMenuItem(this, UiDataMessages.EXPORTSELECTIONCSV));
         popup.addSeparator();
         popup.add(CreateVariableFromSelectionAction.createJMenuItem(this, UiDataMessages.CREATE));
         popup.add(CreateNewVariableAction.createJMenuItem(this, UiDataMessages.CREATENEWVAR));
         popup.add(DuplicateVariableAction.createJMenuItem(this, UiDataMessages.DUPLICATE));
+        popup.add(ExportToCsvAction.createJMenuItem(this, UiDataMessages.EXPORTCSV));
 
         popup.addSeparator();
 
@@ -284,6 +288,7 @@ public class SwingScilabVariableEditor extends SwingScilabTab implements SimpleV
         file.add(CreateVariableFromSelectionAction.createMenuItem(this, UiDataMessages.CREATE));
         file.add(CreateNewVariableAction.createMenuItem(this, UiDataMessages.CREATENEWVAR));
         file.add(DuplicateVariableAction.createMenuItem(this, UiDataMessages.DUPLICATE));
+        file.add(ExportToCsvAction.createMenuItem(this, UiDataMessages.EXPORTCSV));
         file.addSeparator();
         file.add(CloseAction.createMenuItem(this, UiDataMessages.CLOSE));
 
@@ -299,7 +304,7 @@ public class SwingScilabVariableEditor extends SwingScilabTab implements SimpleV
         edition.addSeparator();
         edition.add(CopySelectionAsScilabCommandAction.createMenuItem(this, UiDataMessages.COPYASSCILABCOM));
         edition.add(CopySelectionAsScilabMatrixAction.createMenuItem(this, UiDataMessages.COPYASSCILABMAT));
-       edition.add(CopySelectionAsScilabMatrixWithCRAction.createMenuItem(this, UiDataMessages.COPYASSCILABMATWITHCR));
+        edition.add(CopySelectionAsScilabMatrixWithCRAction.createMenuItem(this, UiDataMessages.COPYASSCILABMATWITHCR));
         edition.addSeparator();
         edition.add(SizeColumnsToFitAction.createMenuItem(this, UiDataMessages.FIT));
         bar.add(edition);
index b3096ac..f3c4d3e 100644 (file)
@@ -43,7 +43,7 @@ import org.scilab.modules.ui_data.utils.UiDataMessages;
 import org.scilab.modules.ui_data.variableeditor.SwingScilabVariableEditor;
 
 /**
- * RefreshAction class
+ * DuplicateVariableAction class
  * @author Calixte DENIZET
  */
 @SuppressWarnings(value = { "serial" })
diff --git a/scilab/modules/ui_data/src/java/org/scilab/modules/ui_data/variableeditor/actions/ExportToCsvAction.java b/scilab/modules/ui_data/src/java/org/scilab/modules/ui_data/variableeditor/actions/ExportToCsvAction.java
new file mode 100644 (file)
index 0000000..2d65a56
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) 2013 - S/E - Sylvestre Ledru
+ *
+ * This file must be used under the terms of the CeCILL.
+ * This source file is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution.  The terms
+ * are also available at
+ * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+ *
+ */
+
+package org.scilab.modules.ui_data.variableeditor.actions;
+
+import javax.swing.JTable;
+
+import org.scilab.modules.commons.gui.ScilabKeyStroke;
+import org.scilab.modules.gui.bridge.menuitem.SwingScilabMenuItem;
+import org.scilab.modules.gui.events.callback.CommonCallBack;
+import org.scilab.modules.gui.menuitem.MenuItem;
+import org.scilab.modules.gui.menuitem.ScilabMenuItem;
+import org.scilab.modules.gui.utils.ScilabSwingUtilities;
+import org.scilab.modules.ui_data.UI_data;
+import org.scilab.modules.ui_data.datatable.SwingEditvarTableModel;
+import org.scilab.modules.ui_data.utils.UiDataMessages;
+import org.scilab.modules.ui_data.variableeditor.SwingScilabVariableEditor;
+
+/**
+ * Export to Csv class
+ * @author Sylvestre Ledru
+ */
+@SuppressWarnings(value = { "serial" })
+public final class ExportToCsvAction extends CommonCallBack {
+
+    private static final String KEY = "OSSCKEY S";
+    private static final String CREATE = "Export to CSV";
+    private static final int GAP = 5;
+
+    private final SwingScilabVariableEditor editor;
+
+    /**
+     * Constructor
+     * @param editor the editor
+     * @param name the name of the action
+     */
+    private ExportToCsvAction(SwingScilabVariableEditor editor, String name) {
+        super(name);
+        this.editor = editor;
+    }
+
+    /**
+     * @param editor the editor
+     * @param table where to put the action
+     */
+    public static void registerAction(SwingScilabVariableEditor editor, JTable table) {
+        table.getActionMap().put(CREATE, new ExportToCsvAction(editor, CREATE));
+        table.getInputMap().put(ScilabKeyStroke.getKeyStroke(KEY), CREATE);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void callBack() {
+        JTable table = editor.getCurrentTable();
+        SwingEditvarTableModel model = (SwingEditvarTableModel) table.getModel();
+        model.execCommand("__export__csv__=uiputfile();if __export__csv__<>'' then csvWrite(" + model.getVarName() + ", __export__csv__);clear __export__csv__; end;");
+    }
+
+    /**
+     * Create a menu item
+     * @param editor the associated editor
+     * @param title the menu title
+     * @return the menu item
+     */
+    public static MenuItem createMenuItem(SwingScilabVariableEditor editor, String title) {
+        MenuItem menu = ScilabMenuItem.createMenuItem();
+        menu.setCallback(new ExportToCsvAction(editor, title));
+        menu.setText(title);
+        ((SwingScilabMenuItem) menu.getAsSimpleMenuItem()).setAccelerator(ScilabKeyStroke.getKeyStroke(KEY));
+
+        return menu;
+    }
+
+    /**
+     * Create a menu item as a SwingScilabMenuItem
+     * @param editor the associated editor
+     * @param title the menu title
+     * @return the menu item
+     */
+    public static SwingScilabMenuItem createJMenuItem(SwingScilabVariableEditor editor, String title) {
+        return (SwingScilabMenuItem) createMenuItem(editor, title).getAsSimpleMenuItem();
+    }
+}
diff --git a/scilab/modules/ui_data/src/java/org/scilab/modules/ui_data/variableeditor/actions/ExportToCsvFromSelectionAction.java b/scilab/modules/ui_data/src/java/org/scilab/modules/ui_data/variableeditor/actions/ExportToCsvFromSelectionAction.java
new file mode 100644 (file)
index 0000000..1aeddf0
--- /dev/null
@@ -0,0 +1,101 @@
+/*
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) 2013 - S/E - Sylvestre Ledru
+ *
+ * This file must be used under the terms of the CeCILL.
+ * This source file is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution.  The terms
+ * are also available at
+ * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+ *
+ */
+
+package org.scilab.modules.ui_data.variableeditor.actions;
+
+import javax.swing.JTable;
+
+import org.scilab.modules.commons.gui.ScilabKeyStroke;
+import org.scilab.modules.gui.bridge.menuitem.SwingScilabMenuItem;
+import org.scilab.modules.gui.events.callback.CommonCallBack;
+import org.scilab.modules.gui.menuitem.MenuItem;
+import org.scilab.modules.gui.menuitem.ScilabMenuItem;
+import org.scilab.modules.gui.utils.ScilabSwingUtilities;
+import org.scilab.modules.ui_data.UI_data;
+import org.scilab.modules.ui_data.datatable.SwingEditvarTableModel;
+import org.scilab.modules.ui_data.utils.UiDataMessages;
+import org.scilab.modules.ui_data.variableeditor.SwingScilabVariableEditor;
+
+/**
+ * Export to Csv class
+ * @author Sylvestre Ledru
+ */
+@SuppressWarnings(value = { "serial" })
+public final class ExportToCsvFromSelectionAction extends CommonCallBack {
+
+    private static final String KEY = "OSSCKEY S";
+    private static final String CREATE = "Export to CSV";
+    private static final int GAP = 5;
+
+    private final SwingScilabVariableEditor editor;
+
+    /**
+     * Constructor
+     * @param editor the editor
+     * @param name the name of the action
+     */
+    private ExportToCsvFromSelectionAction(SwingScilabVariableEditor editor, String name) {
+        super(name);
+        this.editor = editor;
+    }
+
+    /**
+     * @param editor the editor
+     * @param table where to put the action
+     */
+    public static void registerAction(SwingScilabVariableEditor editor, JTable table) {
+        table.getActionMap().put(CREATE, new ExportToCsvFromSelectionAction(editor, CREATE));
+        table.getInputMap().put(ScilabKeyStroke.getKeyStroke(KEY), CREATE);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void callBack() {
+        JTable table = editor.getCurrentTable();
+        int[] cols = table.getSelectedColumns();
+        int[] rows = table.getSelectedRows();
+        if (cols.length > 0 && rows.length > 0) {
+            int[] rowSize = new int[1];
+            int[] colSize = new int[1];
+            SwingEditvarTableModel model = (SwingEditvarTableModel) table.getModel();
+            String datas = model.getScilabSubMatrix(rows, cols, rowSize, colSize);
+            model.execCommand("__export__csv__=uiputfile();__var_to_export__=" + datas + "; if __export__csv__<>'' then csvWrite(__var_to_export__ , __export__csv__);clear __export__csv__ __var_to_export__; end;");
+        }
+    }
+
+    /**
+     * Create a menu item
+     * @param editor the associated editor
+     * @param title the menu title
+     * @return the menu item
+     */
+    public static MenuItem createMenuItem(SwingScilabVariableEditor editor, String title) {
+        MenuItem menu = ScilabMenuItem.createMenuItem();
+        menu.setCallback(new ExportToCsvFromSelectionAction(editor, title));
+        menu.setText(title);
+        ((SwingScilabMenuItem) menu.getAsSimpleMenuItem()).setAccelerator(ScilabKeyStroke.getKeyStroke(KEY));
+
+        return menu;
+    }
+
+    /**
+     * Create a menu item as a SwingScilabMenuItem
+     * @param editor the associated editor
+     * @param title the menu title
+     * @return the menu item
+     */
+    public static SwingScilabMenuItem createJMenuItem(SwingScilabVariableEditor editor, String title) {
+        return (SwingScilabMenuItem) createMenuItem(editor, title).getAsSimpleMenuItem();
+    }
+}