now prompt if trying to overwrite a file, change title of xpad depending of the file...
Allan SIMON [Mon, 21 Sep 2009 11:02:03 +0000 (13:02 +0200)]
scilab/modules/gui/src/java/org/scilab/modules/gui/utils/SciFileFilter.java
scilab/modules/xpad/src/java/org/scilab/modules/xpad/Xpad.java
scilab/modules/xpad/src/java/org/scilab/modules/xpad/actions/OpenAction.java
scilab/modules/xpad/src/java/org/scilab/modules/xpad/actions/RecentFileAction.java
scilab/modules/xpad/src/java/org/scilab/modules/xpad/style/ScilabStyleDocument.java

index cac77fb..834a8e5 100644 (file)
@@ -40,15 +40,15 @@ public class SciFileFilter extends FileFilter {
                if (maskdescription == null) {
 
                        if (fileMask.equals("*.sci")) {
-                               description = "Scilab SCI files";
+                               description = "Scilab SCI files (*.sci)";
                        } else if (fileMask.equals("*.sce")) {
-                               description = "Scilab SCE files";
+                               description = "Scilab SCE files (*.sce)";
                        } else if (fileMask.equals("*.bin")) {
-                               description = "Scilab binary files";
+                               description = "Scilab binary files (*.bin)";
                        } else if (fileMask.equals("*.sc*")) {
-                               description = "All Scilab files";
+                               description = "All Scilab files (*.sc*)";
                        } else if (fileMask.equals("*.cos*")) {
-                               description = "Scicos files";
+                               description = "Scicos files (*.cos*)";
                        } else {
                                description = "All " + fileMask + " files";
                        }
index 584dc92..05896df 100644 (file)
@@ -13,6 +13,9 @@
 package org.scilab.modules.xpad;
 
 import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
@@ -20,11 +23,14 @@ import java.util.ArrayList;
 import java.util.Scanner;
 
 import javax.swing.BorderFactory;
+import javax.swing.JButton;
 import javax.swing.JFileChooser;
 import javax.swing.JOptionPane;
 import javax.swing.JScrollPane;
 import javax.swing.JTabbedPane;
 import javax.swing.JTextPane;
+import javax.swing.event.ChangeEvent;
+import javax.swing.event.ChangeListener;
 import javax.swing.undo.CannotRedoException;
 import javax.swing.undo.CannotUndoException;
 import javax.swing.undo.UndoManager;
@@ -44,6 +50,7 @@ import org.scilab.modules.gui.textbox.ScilabTextBox;
 import org.scilab.modules.gui.textbox.TextBox;
 import org.scilab.modules.gui.toolbar.ScilabToolBar;
 import org.scilab.modules.gui.toolbar.ToolBar;
+import org.scilab.modules.gui.utils.SciFileFilter;
 import org.scilab.modules.gui.window.ScilabWindow;
 import org.scilab.modules.gui.window.Window;
 import org.scilab.modules.xpad.actions.ASCIIEncodingAction;
@@ -97,7 +104,10 @@ public class Xpad extends SwingScilabTab implements Tab {
        private JTextPane textPane;
        private JScrollPane scrollingText;
        private XpadLineNumberPanel xln;
-       private  Menu recentsMenu ;
+       private Menu recentsMenu ;
+       private int numberOfUntitled;
+       private String lastSaveDir ;
+
 
        private static Xpad editor = null;
 
@@ -290,8 +300,24 @@ public class Xpad extends SwingScilabTab implements Tab {
        public Xpad(Window parentWindow) {
                super("Xpad");
                this.parentWindow = parentWindow;
-               recentsMenu = ScilabMenu.createMenu(); ;
+               recentsMenu = ScilabMenu.createMenu();
+               numberOfUntitled =0 ;
                tabPane = new JTabbedPane();
+               tabPane.addChangeListener(new ChangeListener() {
+                     public void stateChanged(ChangeEvent e) {
+                         String path = new String("") ;
+                         if (getTextPane()!= null ) {
+                                 if ( getTextPane().getName() != null)
+                                         path  =  " ( " + getTextPane().getName() + " )" ;
+                                         
+                                 
+                                 setTitle(tabPane.getTitleAt(tabPane.getSelectedIndex()) + path + " - Xpad") ;
+                                 updateUI() ;
+                         }
+                                 
+                     }
+               });
+       
                this.setContentPane(tabPane);
        }
 
@@ -360,16 +386,29 @@ public class Xpad extends SwingScilabTab implements Tab {
        public boolean saveAs (JTextPane textPane){
                
                boolean isSuccess = false ;
+               String extension = new String() ;
                
+               if (lastSaveDir == null)
+                       lastSaveDir = System.getProperty("user.dir");
                
-               String[] mask = Juigetfile.DEFAULT_MASK ;
-               
+               //SciFileFilter allFilter = new SciFileFilter("" , null , 0);
+               SciFileFilter sceFilter = new SciFileFilter("*.sce" , null , 1);
+               SciFileFilter scxFilter = new SciFileFilter("*.scx" , null , 2);
+               SciFileFilter cosFilter = new SciFileFilter("*.cos" , null , 3);
+               SciFileFilter sciFilter = new SciFileFilter("*.sci" , null , 4);
+
                SwingScilabFileChooser _fileChooser = ((SwingScilabFileChooser) ScilabFileChooser.createFileChooser().getAsSimpleFileChooser());
                 
                 _fileChooser .setAcceptAllFileFilterUsed(true);
-                _fileChooser .addMask(mask , new String[0]);           
-                _fileChooser .setInitialDirectory( System.getProperty("user.dir"));            
+                //_fileChooser .addMask(mask , new String[0]);         
+                _fileChooser .setInitialDirectory( lastSaveDir);               
                 _fileChooser .setUiDialogType(Juigetfile.SAVE_DIALOG);         
+                //_fileChooser.addChoosableFileFilter(allFilter);
+                _fileChooser.addChoosableFileFilter(sceFilter);
+                _fileChooser.addChoosableFileFilter(scxFilter);
+                _fileChooser.addChoosableFileFilter(cosFilter);
+                _fileChooser.addChoosableFileFilter(sciFilter);
+                
                //ssfc.displayAndWait();        
                
                
@@ -378,17 +417,30 @@ public class Xpad extends SwingScilabTab implements Tab {
                //JTextPane textPane =(JTextPane) ((JScrollPane) tabPane.getTabComponentAt(indexTab)).getViewport().getComponent(0) ;
                
                if (retval == JFileChooser.APPROVE_OPTION) {
-                       File f = _fileChooser.getSelectedFile() ;
+                       File f = _fileChooser.getSelectedFile();
+                       lastSaveDir = f.getPath() ;
+                       if (f.exists()) {
+                               int actionDialog = JOptionPane.showConfirmDialog(this, "Replace existing file?", "File already exist", JOptionPane.YES_NO_OPTION);
+                               if (actionDialog == JOptionPane.NO_OPTION){
+                                       this.saveAs (this.getTextPane());
+                                       return true ;
+                                       }
+                       
+                       }
+                       
                        try {
-
+                               
+                               
                                String doc = this.getTextPane().getText();
                                
                                
                                /*we test if the file has already a scilab extension*/
                                boolean hasNoExtension = true ;
                                
+                               
+                               
                                for ( int i = 0 ; i < Juigetfile.DEFAULT_MASK.length ; i++ ){
-                                       if (f.getName().endsWith(Juigetfile.DEFAULT_MASK[i])){
+                                       if (f.getName().endsWith(".sci") || f.getName().endsWith(".sce")   )  {
                                                hasNoExtension = false;
                                                break ;
                                        }
@@ -396,7 +448,19 @@ public class Xpad extends SwingScilabTab implements Tab {
                                }
                                /*if no extension , we add it */
                                if ( hasNoExtension ){
-                                       f = new File (f.getPath() + ".sci");
+                                       
+                                       if ( _fileChooser.getFileFilter() == sciFilter) {
+                                               extension = ".sci";
+                                       } else if (_fileChooser.getFileFilter() == sceFilter) {
+                                               extension = ".sce";
+                                       } else if (_fileChooser.getFileFilter() == scxFilter) {
+                                               extension = ".sce";
+                                       } else if (_fileChooser.getFileFilter() == cosFilter) {
+                                               extension = ".cos";
+                                       } else {
+                                               extension = "";
+                                       }
+                                       f = new File (f.getPath() + extension);
                                }
                                
                                FileWriter writer = new FileWriter(f);
@@ -415,6 +479,7 @@ public class Xpad extends SwingScilabTab implements Tab {
                                isSuccess = true ;
                                
                        } catch (Exception ioex) {
+                               ioex.printStackTrace();
                            JOptionPane.showMessageDialog(this, ioex);
                        }
                        
@@ -453,7 +518,15 @@ public class Xpad extends SwingScilabTab implements Tab {
        }
 
        public JTextPane addEmptyTab() {
-               return addTab("Untitled");
+               
+               if (numberOfUntitled != 0)
+                       return addTab("Untitled " + numberOfUntitled++);
+               else{
+                       numberOfUntitled++;
+                       return addTab("Untitled ");
+               }
+                       
+
        }
 
        public void setAutoIndent(boolean b) {
@@ -468,6 +541,7 @@ public class Xpad extends SwingScilabTab implements Tab {
                                        System.out.println(undo.canUndo());
                                        System.err.println("Will undo "+undo.getUndoPresentationName());
                                        undo.undo();
+                                       repaint();
                                
                        } catch (CannotUndoException ex) {
                                System.out.println("Unable to undo: " + ex);
@@ -536,7 +610,11 @@ public class Xpad extends SwingScilabTab implements Tab {
        }
 
        public JTextPane getTextPane() {
-               return (JTextPane) ((JScrollPane) tabPane.getSelectedComponent()).getViewport().getComponent(0);
+               try{
+                       return (JTextPane) ((JScrollPane) tabPane.getSelectedComponent()).getViewport().getComponent(0);
+               }catch( NullPointerException e){
+                       return null ;
+               }
        }
 
        public SimpleTab getAsSimpleTab() {
@@ -603,5 +681,37 @@ public class Xpad extends SwingScilabTab implements Tab {
                        }
                        
        }
-
+       
+       private class TabButton extends JButton implements ActionListener {
+        public TabButton() {
+            int size = 17;
+            setPreferredSize(new Dimension(size, size));
+            setToolTipText("close this tab");
+            
+            setUI(new BasicButtonUI());
+            //Make it transparent
+            setContentAreaFilled(false);
+            //No need to be focusable
+            setFocusable(false);
+            setBorder(BorderFactory.createEtchedBorder());
+            setBorderPainted(false);
+            setRolloverEnabled(true);
+            //Close the proper tab by clicking the button
+            addActionListener(this);            
+        }
+
+        public void actionPerformed(ActionEvent e) {
+            /*e.getSource();
+            if (i != -1) {
+               tabPane.remove(i);
+            }
+            */
+        }
+
+        //we don't want to update UI for this button
+        public void updateUI() {
+        }
+       }
+       
+*/
 }
index 199bc97..b8db62f 100644 (file)
@@ -36,7 +36,7 @@ public class OpenAction extends DefaultAction {
 
     public void doAction() {
        
-               String[] mask = Juigetfile.DEFAULT_MASK ;
+               String[] mask = new String[]{ "*.sce",  "*.sc*", "*.cos*", "*.sci",}; 
                
                SwingScilabFileChooser _fileChooser = ((SwingScilabFileChooser) ScilabFileChooser.createFileChooser().getAsSimpleFileChooser());
                 
@@ -50,6 +50,7 @@ public class OpenAction extends DefaultAction {
                    File f = _fileChooser.getSelectedFile();
                    getEditor().readFile(f);
 
+               getEditor().setTitle(f.getPath() + " - Xpad");
            ConfigXpadManager.saveToRecentOpenedFiles(f.getPath());
            getEditor().updateRecentOpenedFilesMenu();
            
index 52d800d..01c24d3 100644 (file)
@@ -28,6 +28,7 @@ public class RecentFileAction extends DefaultAction {
        public void doAction() {
                ConfigXpadManager.saveToRecentOpenedFiles(recentFile.getPath());
                getEditor().readFile(recentFile);
+               getEditor().setTitle(recentFile.getPath() + " - Xpad");
        };
 
        public static MenuItem createMenu(Xpad editor, File f) {
index d1897ce..e62c255 100644 (file)
@@ -26,6 +26,8 @@ import javax.swing.event.DocumentListener;
 import javax.swing.event.UndoableEditEvent;
 import javax.swing.text.BadLocationException;
 import javax.swing.text.DefaultStyledDocument;
+import javax.swing.text.Document;
+import javax.swing.text.Element;
 import javax.swing.text.Style;
 import javax.swing.text.StyleConstants;
 import javax.swing.undo.UndoManager;
@@ -42,11 +44,17 @@ public class ScilabStyleDocument extends DefaultStyledDocument implements Docume
                public void undoableEditHappened(UndoableEditEvent e) {
                                        
                        if ( (EventType.equals(DocumentEvent.EventType.INSERT.toString()) || EventType.equals(DocumentEvent.EventType.REMOVE.toString()) ) && (e.getEdit().canUndo()) ){
-                               
-                               System.out.println("add edit");
+                               /*
+                               if ( EventType.equals(DocumentEvent.EventType.REMOVE.toString())){
+                                       System.out.println("remove");
+                                       System.out.println(indentInprogress) ;
+                               }
+                               */
+                               if (!indentInprogress){ 
                                undo.addEdit(e.getEdit());
                                
                                EventType = "" ;
+                               }
                        }
 
                                        
@@ -1397,8 +1405,11 @@ public class ScilabStyleDocument extends DefaultStyledDocument implements Docume
        }
 
        public void insertUpdate(DocumentEvent e) {
+               if (e != null){
+                       EventType = e.getType().toString();
+                       //Element[] pouet =  e.getChange( this.getParagraphElement(editor.getTextPane().getCaretPosition())).getChildrenAdded();
+               }
                
-               EventType = e.getType().toString();
                //System.err.println("--- Calling insertUpdate");
                if (!updaterDisabled) {
                        SwingUtilities.invokeLater(new Runnable() {