improved undo/redo , able to load xpad command into scilab -nw
Allan SIMON [Thu, 17 Sep 2009 15:23:33 +0000 (17:23 +0200)]
scilab/modules/xpad/src/java/org/scilab/modules/xpad/Xpad.java
scilab/modules/xpad/src/java/org/scilab/modules/xpad/actions/LoadIntoScilabAction.java
scilab/modules/xpad/src/java/org/scilab/modules/xpad/style/ScilabStyleDocument.java

index 8624261..584dc92 100644 (file)
@@ -462,23 +462,30 @@ public class Xpad extends SwingScilabTab implements Tab {
 
        public void undo() {
                UndoManager undo = ((ScilabStyleDocument) getTextPane().getStyledDocument()).getUndoManager();
-               try {
-                       System.err.println("Will undo "+undo.getUndoPresentationName());
-                       undo.undo();
-               } catch (CannotUndoException ex) {
-                       System.out.println("Unable to undo: " + ex);
-                       ex.printStackTrace();
-               }
+               
+               if(undo.canUndo()){
+                       try {
+                                       System.out.println(undo.canUndo());
+                                       System.err.println("Will undo "+undo.getUndoPresentationName());
+                                       undo.undo();
+                               
+                       } catch (CannotUndoException ex) {
+                               System.out.println("Unable to undo: " + ex);
+                               ex.printStackTrace();
+                       }
+               };
        }
 
        public void redo() {
                UndoManager redo = ((ScilabStyleDocument) getTextPane().getStyledDocument()).getUndoManager();
-               try {
-                       System.err.println("Will redo "+redo.getRedoPresentationName());
-                       redo.redo();
-               } catch (CannotRedoException ex) {
-                       System.out.println("Unable to redo: " + ex);
-                       ex.printStackTrace();
+               if ( redo.canRedo()){
+                       try {
+                               System.err.println("Will redo "+redo.getRedoPresentationName());
+                               redo.redo();
+                       } catch (CannotRedoException ex) {
+                               System.out.println("Unable to redo: " + ex);
+                               ex.printStackTrace();
+                       }
                }
        }
 
index 85fa408..446cd53 100644 (file)
 
 package org.scilab.modules.xpad.actions;
 
+import java.util.ArrayList;
+
+import org.scilab.modules.action_binding.InterpreterManagement;
 import org.scilab.modules.gui.console.ScilabConsole;
 import org.scilab.modules.gui.menuitem.MenuItem;
 import org.scilab.modules.xpad.Xpad;
+import org.scilab.modules.xpad.style.ScilabStyleDocument;
 
 public class LoadIntoScilabAction extends DefaultAction {
 
@@ -24,8 +28,15 @@ public class LoadIntoScilabAction extends DefaultAction {
 
        public void doAction() {
            /* Will do the job as if it was copy / paste in scilab Console */
-           //InterpreterManagement.requestScilabExec(getEditor().getTextPane().getText());
-           ScilabConsole.getConsole().getAsSimpleConsole().sendCommandsToScilab(getEditor().getTextPane().getText(), true, false);
+           
+           try {
+               ScilabConsole.getConsole().getAsSimpleConsole().sendCommandsToScilab(getEditor().getTextPane().getText(), true, false);
+           }catch (NoClassDefFoundError noClass){
+               InterpreterManagement.requestScilabExec(  getEditor().getTextPane().getText().replaceAll("\n", ","));
+               
+           }
+               
+               
        }
        
        public static MenuItem createMenu(Xpad editor) {
index 98db667..d1897ce 100644 (file)
@@ -40,14 +40,16 @@ public class ScilabStyleDocument extends DefaultStyledDocument implements Docume
 
        private UndoManager undo = new UndoManager() {
                public void undoableEditHappened(UndoableEditEvent e) {
-                       //          System.out.println("UndoableEditEvent ="+e.getClass().getCanonicalName());
-                       //          System.out.println("UndoableEdit ="+e.getEdit().toString());
-                       //          System.out.println("Source = "+e.getSource().toString());
-                       //          System.out.println("filter = "+(e.getEdit() instanceof AttributeUndoableEdit));
-                       //                      if(e.getEdit().getPresentationName().compareTo("addition") == 0
-                       //                                      || e.getEdit().getPresentationName().compareTo("deletion") == 0) {
-                       undo.addEdit(e.getEdit());
-                       //                      }
+                                       
+                       if ( (EventType.equals(DocumentEvent.EventType.INSERT.toString()) || EventType.equals(DocumentEvent.EventType.REMOVE.toString()) ) && (e.getEdit().canUndo()) ){
+                               
+                               System.out.println("add edit");
+                               undo.addEdit(e.getEdit());
+                               
+                               EventType = "" ;
+                       }
+
+                                       
                }
        };
 
@@ -57,6 +59,8 @@ public class ScilabStyleDocument extends DefaultStyledDocument implements Docume
        private boolean indentInprogress = false;
        private boolean updaterDisabled = false;
 
+       private String EventType ;
+       
        //private final String[] quotations = {"[^A-Z](\"|')[^{\n}]*?(\"|')"};
        private final String[] quotations = {"(\"|')[^{\n}]*?(\"|')"};
        private final String[] bools = {"%T", "%F", "%t", "%f"};
@@ -103,7 +107,8 @@ public class ScilabStyleDocument extends DefaultStyledDocument implements Docume
 
        public ScilabStyleDocument(Xpad editor) {
                super();
-
+               EventType = new String();
+               
                this.editor = editor ;
                Hashtable< String, Color>stylesColorsTable =  ConfigXpadManager.getAllForegroundColors();
                Hashtable< String, Boolean>stylesIsBoldTable = ConfigXpadManager.getAllisBold()  ;
@@ -1392,6 +1397,8 @@ public class ScilabStyleDocument extends DefaultStyledDocument implements Docume
        }
 
        public void insertUpdate(DocumentEvent e) {
+               
+               EventType = e.getType().toString();
                //System.err.println("--- Calling insertUpdate");
                if (!updaterDisabled) {
                        SwingUtilities.invokeLater(new Runnable() {
@@ -1446,6 +1453,8 @@ public class ScilabStyleDocument extends DefaultStyledDocument implements Docume
                }
        }
        public void removeUpdate(DocumentEvent e) {
+               
+               EventType = e.getType().toString() ;
                //System.err.println("--- Calling ScilabStyleDocument.removeUpdate");
                if (!updaterDisabled) {
                        SwingUtilities.invokeLater(new Runnable() {
@@ -1510,6 +1519,8 @@ public class ScilabStyleDocument extends DefaultStyledDocument implements Docume
        }
 
        public void changedUpdate(DocumentEvent arg0) {
+               
+               EventType = arg0.getType().toString() ;
                // TODO Auto-generated method stub
        }