added warning message when trying to close with unsaved modified document in Xpad...
Allan SIMON [Tue, 15 Sep 2009 14:50:37 +0000 (16:50 +0200)]
scilab/modules/xpad/src/java/org/scilab/modules/xpad/Xpad.java
scilab/modules/xpad/src/java/org/scilab/modules/xpad/actions/ExitAction.java
scilab/modules/xpad/src/java/org/scilab/modules/xpad/actions/FindAction.java
scilab/modules/xpad/src/java/org/scilab/modules/xpad/actions/GotoLineAction.java
scilab/modules/xpad/src/java/org/scilab/modules/xpad/actions/OpenAction.java
scilab/modules/xpad/src/java/org/scilab/modules/xpad/actions/SaveAction.java
scilab/modules/xpad/src/java/org/scilab/modules/xpad/actions/SaveAsAction.java
scilab/modules/xpad/src/java/org/scilab/modules/xpad/actions/SetColorsAction.java
scilab/modules/xpad/src/java/org/scilab/modules/xpad/style/ScilabStyleDocument.java

index 6b29247..6afed27 100644 (file)
@@ -20,6 +20,7 @@ import java.util.ArrayList;
 import java.util.Scanner;
 
 import javax.swing.BorderFactory;
+import javax.swing.JFileChooser;
 import javax.swing.JOptionPane;
 import javax.swing.JScrollPane;
 import javax.swing.JTabbedPane;
@@ -119,6 +120,12 @@ public class Xpad extends SwingScilabTab implements Tab {
        }
 
        public static void closeXpad() {
+               for ( int i = 0 ; i < editor.getTabPane().getComponentCount() ; i++){
+               
+                       editor.closeTabAt(i);
+                       
+               }
+                       
                editor = null;
        }
 
@@ -282,7 +289,102 @@ public class Xpad extends SwingScilabTab implements Tab {
        }
 
        public void closeCurrentTab() {
-               tabPane.remove(tabPane.getSelectedComponent());
+               closeTabAt( tabPane.getSelectedIndex() );
+
+       }
+
+       public void closeTabAt (int indexTab ){
+//             JScrollPane pouet  = (JScrollPane) tabPane.getTabComponentAt(indexTab) ;
+//              pouet.getViewport().getComponent(0);
+               JTextPane textPane = (JTextPane) ((JScrollPane) tabPane.getComponentAt(indexTab)).getViewport().getComponent(0) ;
+               
+               if (  ((ScilabStyleDocument) textPane.getStyledDocument()).isContentModified() ){
+                       int choice = JOptionPane.showConfirmDialog(this, "This file has been modified since last save\nsave it?");
+                       
+                       if (choice == 0){
+                                       save (textPane);
+                       }else if (choice == 1){
+                               tabPane.remove(tabPane.getSelectedComponent());
+                               
+                       }else if (choice == 2){
+                               // if cancel we do nothing
+                       }
+                       
+               }else {
+                       tabPane.remove(tabPane.getSelectedComponent());
+               }
+               
+       }
+       
+       
+       public boolean save ( JTextPane textPane ) {
+               
+               //JTextPane textPane =(JTextPane) ((JScrollPane) tabPane.getTabComponentAt(indexTab)).getViewport().getComponent(0) ;
+               boolean isSuccess = false ;
+               if (textPane.getName() != null ){
+                       try {
+                               File newSavedFiled = new File(textPane.getName());
+                               
+                               String doc = textPane.getText();
+
+                               FileWriter writer = new FileWriter(newSavedFiled);
+                               writer.write(doc);
+                               writer.flush();
+                               writer.close();
+                               
+                               ((ScilabStyleDocument) textPane.getStyledDocument()).setContentModified(false);
+                               
+                               int index = getTabPane().getSelectedIndex();
+                   getTabPane().setTitleAt( index  , newSavedFiled.getName() );
+
+                   isSuccess = true ;
+
+                       } catch (Exception ioex) {
+                           JOptionPane.showMessageDialog(this, ioex);
+                       }
+               }else{
+                        isSuccess = saveAs (textPane ); 
+                       
+               }
+               
+               return isSuccess ;
+       }
+       
+       public boolean saveAs (JTextPane textPane){
+               
+               boolean isSuccess = false ;
+               JFileChooser _fileChooser = new JFileChooser();
+               int retval = _fileChooser.showSaveDialog(this);
+               
+               //JTextPane textPane =(JTextPane) ((JScrollPane) tabPane.getTabComponentAt(indexTab)).getViewport().getComponent(0) ;
+               
+               if (retval == JFileChooser.APPROVE_OPTION) {
+                       File f = _fileChooser.getSelectedFile();
+                       try {
+
+                               String doc = this.getTextPane().getText();
+
+                               FileWriter writer = new FileWriter(f);
+                               writer.write(doc);
+                               writer.flush();
+                               writer.close();
+                               
+                               
+                               ConfigXpadManager.saveToRecentOpenedFiles(f.getPath());
+                               textPane.setName(f.getPath());
+                               getTabPane().setTitleAt(getTabPane().getSelectedIndex() , f.getName());
+                               updateRecentOpenedFilesMenu();
+                               
+                               ((ScilabStyleDocument) textPane.getStyledDocument()).setContentModified(false);
+                               
+                               isSuccess = true ;
+                               
+                       } catch (Exception ioex) {
+                           JOptionPane.showMessageDialog(this, ioex);
+                       }
+                       
+               }
+               return isSuccess ;
        }
 
        public JTextPane addTab(String title) {
@@ -307,7 +409,7 @@ public class Xpad extends SwingScilabTab implements Tab {
 
                textPane.setBackground(ConfigXpadManager.getXpadBackgroundColor());
                textPane.setCaretColor(Color.BLACK);
-               textPane.setStyledDocument(new ScilabStyleDocument());
+               textPane.setStyledDocument(new ScilabStyleDocument(this));
                textPane.setCharacterAttributes(textPane.getStyle("Default"), true);
 
                textPane.setFocusable(true);
index 3cc32ea..22bac0c 100644 (file)
@@ -16,12 +16,16 @@ package org.scilab.modules.xpad.actions;
 import java.awt.event.ActionEvent;
 import java.awt.event.KeyEvent;
 
+import javax.swing.JOptionPane;
+import javax.swing.JScrollPane;
+import javax.swing.JTextPane;
 import javax.swing.KeyStroke;
 
 import org.scilab.modules.gui.menuitem.MenuItem;
 import org.scilab.modules.gui.utils.UIElementMapper;
 import org.scilab.modules.gui.window.ScilabWindow;
 import org.scilab.modules.xpad.Xpad;
+import org.scilab.modules.xpad.style.ScilabStyleDocument;
 
 public class ExitAction extends DefaultAction {
 
@@ -32,6 +36,27 @@ public class ExitAction extends DefaultAction {
     public void doAction() {
        ScilabWindow xpadWindow = (ScilabWindow) UIElementMapper.getCorrespondingUIElement(getEditor().getParentWindowId());
 
+       int numberOfTab = getEditor().getTabPane().getComponentCount();
+       for ( int i = 0 ; i < numberOfTab ; i++){
+               
+       JTextPane textPane = (JTextPane) ((JScrollPane) getEditor().getTabPane().getComponentAt(i)).getViewport().getComponent(0) ;
+               
+               if (  ((ScilabStyleDocument) textPane.getStyledDocument()).isContentModified() ){
+                       int choice = JOptionPane.showConfirmDialog(getEditor(),getEditor().getTabPane().getTitleAt(i) + " has been modified since last save\nsave it?");
+                       
+                       if (choice == 0){// ok choose
+                               getEditor().save(textPane);
+                       }else if (choice == 1){// cancel choose , we dont'save
+                               
+                               
+                       }else if (choice == 2){
+                               return ;// if cancel we stop closing xpad
+                       }
+                       
+               }
+               
+       }
+       
        xpadWindow.getAsSimpleWindow().removeTab(getEditor());
        Xpad.closeXpad();
     }
index d8edfab..69d2a10 100644 (file)
@@ -23,6 +23,8 @@ import java.awt.event.FocusEvent;
 import java.awt.event.FocusListener;
 import java.awt.event.KeyEvent;
 import java.awt.event.KeyListener;
+import java.awt.event.WindowEvent;
+import java.awt.event.WindowListener;
 import java.util.ArrayList;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -111,7 +113,7 @@ public class FindAction extends DefaultAction {
 
                //Find & Replace Frame
                frame = new JFrame();
-               frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
+               frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                frame.setPreferredSize(new Dimension(300, 650));
                frame.setTitle("Find/Replace");
                frame.pack();
@@ -421,7 +423,36 @@ public class FindAction extends DefaultAction {
             }
                });
                
+               frame.addWindowListener( new WindowListener(){
+                       public void windowClosed(WindowEvent arg0) {
+                               // TODO Auto-generated method stub
                                
+                       }
+                       public void windowDeiconified(WindowEvent arg0) {
+                               // TODO Auto-generated method stub
+                               
+                       }
+                       public void windowActivated(WindowEvent arg0) {
+                               // TODO Auto-generated method stub
+                               
+                       }
+                       public void windowClosing(WindowEvent arg0) {
+                               FindAction.windowAlreadyExist = false ;
+                               frame.dispose();
+                               
+                       }
+                       public void windowDeactivated(WindowEvent arg0) {
+                               // TODO Auto-generated method stub
+                               
+                       }
+                       public void windowIconified(WindowEvent arg0) {
+                               
+                       }
+                       public void windowOpened(WindowEvent arg0) {
+                               // TODO Auto-generated method stub
+                               
+                       }
+               });
 
        }
        private void findText(){
index 1bb038b..c58a8f9 100644 (file)
@@ -19,6 +19,8 @@ import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.KeyEvent;
 import java.awt.event.KeyListener;
+import java.awt.event.WindowEvent;
+import java.awt.event.WindowListener;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -152,7 +154,41 @@ public class GotoLineAction extends DefaultAction {
                        
                
                //display the frame and set some properties
-               mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
+                       
+                       mainFrame.addWindowListener( new WindowListener(){
+                               public void windowClosed(WindowEvent arg0) {
+                                       // TODO Auto-generated method stub
+                                       
+                               }
+                               public void windowDeiconified(WindowEvent arg0) {
+                                       // TODO Auto-generated method stub
+                                       
+                               }
+                               public void windowActivated(WindowEvent arg0) {
+                                       // TODO Auto-generated method stub
+                                       
+                               }
+                               public void windowClosing(WindowEvent arg0) {
+                                       GotoLineAction.windowAlreadyExist = false ;
+                                       mainFrame.dispose();
+                                       
+                               }
+                               public void windowDeactivated(WindowEvent arg0) {
+                                       // TODO Auto-generated method stub
+                                       
+                               }
+                               public void windowIconified(WindowEvent arg0) {
+                                       
+                               };
+                               public void windowOpened(WindowEvent arg0) {
+                                       // TODO Auto-generated method stub
+                                       
+                               }
+                               
+                       } );
+                       
+                       
+               mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                mainFrame.setTitle("Go to line ...");
                mainFrame.pack();
                mainFrame.setLocationRelativeTo(null);
index 7ee69bf..38428dc 100644 (file)
@@ -22,6 +22,7 @@ import javax.swing.KeyStroke;
 import org.scilab.modules.gui.menuitem.MenuItem;
 import org.scilab.modules.gui.pushbutton.PushButton;
 import org.scilab.modules.xpad.Xpad;
+import org.scilab.modules.xpad.style.ScilabStyleDocument;
 import org.scilab.modules.xpad.utils.ConfigXpadManager;
 
 public class OpenAction extends DefaultAction {
@@ -39,7 +40,8 @@ public class OpenAction extends DefaultAction {
 
            ConfigXpadManager.saveToRecentOpenedFiles(f.getPath());
            getEditor().updateRecentOpenedFilesMenu();
-           
+           getEditor().getTabPane().setTitleAt( getEditor().getTabPane().getSelectedIndex() , f.getName());
+           ((ScilabStyleDocument) getEditor().getTextPane().getStyledDocument()).setContentModified(false);
            
        }
     }
index a72a941..dc4b08c 100644 (file)
@@ -24,6 +24,7 @@ import javax.swing.KeyStroke;
 import org.scilab.modules.gui.menuitem.MenuItem;
 import org.scilab.modules.gui.pushbutton.PushButton;
 import org.scilab.modules.xpad.Xpad;
+import org.scilab.modules.xpad.style.ScilabStyleDocument;
 import org.scilab.modules.xpad.utils.ConfigXpadManager;
 
 public class SaveAction extends DefaultAction {
@@ -45,7 +46,13 @@ public class SaveAction extends DefaultAction {
        public void doAction() {
                // TODO Auto-generated method stub
                //super.doAction();
-               if ( getEditor().getTextPane().getName() != null ){
+        getEditor().save(getEditor().getTextPane() );
+               
+       }
+}
+
+/*
+       if ( getEditor().getTextPane().getName() != null ){
                        try {
                                File newSavedFiled = new File(getEditor().getTextPane().getName());
                                
@@ -56,6 +63,12 @@ public class SaveAction extends DefaultAction {
                                writer.flush();
                                writer.close();
                                
+                               ((ScilabStyleDocument) getEditor().getTextPane().getStyledDocument()).setContentModified(false);
+                               
+                               int index = getEditor().getTabPane().getSelectedIndex();
+                   getEditor().getTabPane().setTitleAt( index  , newSavedFiled.getName() );
+
+                               
 
                        } catch (Exception ioex) {
                            JOptionPane.showMessageDialog(getEditor(), ioex);
@@ -79,13 +92,13 @@ public class SaveAction extends DefaultAction {
                                        getEditor().getTextPane().setName(f.getPath());
                                        getEditor().getTabPane().setTitleAt( getEditor().getTabPane().getSelectedIndex() , f.getName());
                                        getEditor().updateRecentOpenedFilesMenu();
+                                       
+                                       ((ScilabStyleDocument) getEditor().getTextPane().getStyledDocument()).setContentModified(false);
+                                       
                                } catch (Exception ioex) {
                                    JOptionPane.showMessageDialog(getEditor(), ioex);
                                }
                        }
                        
                }
-               
-               
-       }
-}
+               */
index 6a6d405..90b7878 100644 (file)
@@ -33,29 +33,9 @@ public class SaveAsAction extends DefaultAction {
        }
 
        public void doAction() {
-               JFileChooser _fileChooser = new JFileChooser();
-               int retval = _fileChooser.showSaveDialog(getEditor());
-               if (retval == JFileChooser.APPROVE_OPTION) {
-                       File f = _fileChooser.getSelectedFile();
-                       try {
-
-                               String doc = getEditor().getTextPane().getText();
-
-                               FileWriter writer = new FileWriter(f);
-                               writer.write(doc);
-                               writer.flush();
-                               writer.close();
-                               
-                               ConfigXpadManager.saveToRecentOpenedFiles(f.getPath());
-                               
-                               getEditor().getTextPane().setName(f.getPath());
-                               getEditor().getTabPane().setTitleAt( getEditor().getTabPane().getSelectedIndex() , f.getName());
-                               getEditor().updateRecentOpenedFilesMenu();
-
-                       } catch (Exception ioex) {
-                           JOptionPane.showMessageDialog(getEditor(), ioex);
-                       }
-               }
+               
+                getEditor().saveAs(getEditor().getTextPane() );
+                       
        }
        
         public static MenuItem createMenu(Xpad editor) {
index ca57b19..1072419 100644 (file)
@@ -17,6 +17,8 @@ import java.awt.GridBagLayout;
 import java.awt.Insets;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.awt.event.WindowEvent;
+import java.awt.event.WindowListener;
 import java.util.ArrayList;
 import java.util.Hashtable;
 
@@ -69,6 +71,8 @@ public class SetColorsAction extends DefaultAction {
                JPanel panel = new JPanel(new GridBagLayout( ));
                frame.setContentPane(panel);
                
+               
+               
                JPanel changePanel = new JPanel(new GridBagLayout());
                JPanel validationPanel = new JPanel(new GridBagLayout());
                
@@ -244,7 +248,40 @@ public class SetColorsAction extends DefaultAction {
                
                
                //display the frame and set some properties
-               frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
+               
+               frame.addWindowListener( new WindowListener(){
+                       public void windowClosed(WindowEvent arg0) {
+                               // TODO Auto-generated method stub
+                               
+                       }
+                       public void windowDeiconified(WindowEvent arg0) {
+                               // TODO Auto-generated method stub
+                               
+                       }
+                       public void windowActivated(WindowEvent arg0) {
+                               // TODO Auto-generated method stub
+                               
+                       }
+                       public void windowClosing(WindowEvent arg0) {
+                               SetColorsAction.windowAlreadyExist = false ;
+                               frame.dispose();
+                               
+                       }
+                       public void windowDeactivated(WindowEvent arg0) {
+                               // TODO Auto-generated method stub
+                               
+                       }
+                       public void windowIconified(WindowEvent arg0) {
+                               
+                       };
+                       public void windowOpened(WindowEvent arg0) {
+                               // TODO Auto-generated method stub
+                               
+                       }
+                       
+               } );
+               
+               frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                frame.setTitle("Change Colors");
                frame.pack();
                frame.setLocationRelativeTo(null);
index 003d339..a7ec4d4 100644 (file)
@@ -26,6 +26,7 @@ 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.Style;
 import javax.swing.text.StyleConstants;
 import javax.swing.undo.UndoManager;
@@ -83,6 +84,10 @@ public class ScilabStyleDocument extends DefaultStyledDocument implements Docume
        private boolean singleLine = false;
        private int currentLine;
        
+       private boolean contentModified ;
+       
+       //private XpadStyles xpadStyles ; 
+       
        Hashtable<String, String[]> keywords;
        String[] commands;
        String[] functions;
@@ -96,13 +101,15 @@ public class ScilabStyleDocument extends DefaultStyledDocument implements Docume
        private Style defaultStyle;
 
 
-       public ScilabStyleDocument() {
+       public ScilabStyleDocument(Xpad editor) {
                super();
 
+               this.editor = editor ;
                Hashtable< String, Color>stylesColorsTable =  ConfigXpadManager.getAllForegroundColors();
                Hashtable< String, Boolean>stylesIsBoldTable = ConfigXpadManager.getAllisBold()  ;
                listStylesName  =  ConfigXpadManager.getAllStyleName();
 
+               //xpadStyles = XpadStyles.getInstance() ;
                addDocumentListener(this);
                addUndoableEditListener(undo);
                defaultStyle = this.addStyle("Default", null);
@@ -121,8 +128,37 @@ public class ScilabStyleDocument extends DefaultStyledDocument implements Docume
                }
                
                loadingsForColorisation();
+               setContentModified(false );
+               
+               this.addDocumentListener( new DocumentListener(){
+                       
+                        public void changedUpdate(DocumentEvent documentEvent) {
+
+                             }
+                             public void insertUpdate(DocumentEvent documentEvent) {
+                               handleEvent(documentEvent);
+                             }
+                             public void removeUpdate(DocumentEvent documentEvent) {
+                                 handleEvent(documentEvent);
+                             }
+                             private void handleEvent(DocumentEvent documentEvent) {
+                               DocumentEvent.EventType type = documentEvent.getType();
+                               if (type.equals(DocumentEvent.EventType.INSERT) || type.equals(DocumentEvent.EventType.REMOVE) ) {
+                                
+                                       int index = getEditor().getTabPane().getSelectedIndex();
+                                       if ( ! isContentModified()){
+                                               getEditor().getTabPane().setTitleAt( index  , "*" + getEditor().getTabPane().getTitleAt(index ) );
+                                       }
+                                       setContentModified(true);
+                               }  
+
+                             }
+               });
+               
        }
 
+
+       
        /**
         * DOCUMENT COLORISATION START
         */
@@ -205,9 +241,14 @@ public class ScilabStyleDocument extends DefaultStyledDocument implements Docume
                                colorizeInprogress = false;
                        }
                }
-
        }
-       
+       /*
+       public Style getStyle(String styleName ){
+               Style plop =  xpadStyles.getStyle(styleName);
+               
+               return plop ;
+       }
+       */
        private void resetStyle() {
                // Reset Color
                this.removeUndoableEditListener(undo);
@@ -1313,7 +1354,15 @@ public class ScilabStyleDocument extends DefaultStyledDocument implements Docume
        }
 
        
-
+       public boolean isContentModified(){
+               return contentModified ;
+       }
+       
+       public void setContentModified(boolean contentModified){
+               this.contentModified = contentModified ;
+       }
+       
+       
        public boolean isSingleLine() {
                return singleLine;
        }