Bug 13051 fixed: SciNotes restoration could block desktop one 53/14053/2
Calixte DENIZET [Tue, 18 Mar 2014 09:55:22 +0000 (10:55 +0100)]
Change-Id: Id6ddee2d777d1eb7a000eef552041fc20cad0d54

scilab/CHANGES_5.5.X
scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/SciNotes.java
scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/SciNotesAutosave.java
scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/actions/RestoreOpenedFilesAction.java
scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/utils/SciNotesContents.java
scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/utils/ScilabTabbedPane.java

index c5c7384..2a65f7d 100644 (file)
@@ -633,6 +633,8 @@ Scilab Bug Fixes
 
 * Bug #13050 fixed - The result of mvvacov was not symmetric.
 
+* Bug #13051 fixed - SciNotes restoration could block desktop one.
+
 * Bug #13053 fixed - datatipCreate did not return datatip handle.
 
 * Bug #13055 fixed - Array indexing did not follow Scilab convention in JIMS.
index 3949c52..1991975 100644 (file)
@@ -16,6 +16,8 @@ package org.scilab.modules.scinotes;
 import java.awt.Color;
 import java.awt.Component;
 import java.awt.Font;
+import java.awt.event.FocusEvent;
+import java.awt.event.FocusListener;
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
@@ -45,6 +47,7 @@ import javax.swing.JComponent;
 import javax.swing.JFileChooser;
 import javax.swing.JFrame;
 import javax.swing.JOptionPane;
+import javax.swing.JPanel;
 import javax.swing.JSplitPane;
 import javax.swing.KeyStroke;
 import javax.swing.SwingUtilities;
@@ -581,7 +584,7 @@ public class SciNotes extends SwingScilabDockablePanel {
         ScilabLexer.update();
         launchSciNotes();
         ScilabEditorPane theTextPane;
-        if (editor.getTabPane().getTabCount() != 0) {
+        if (editor.getTabPane().getTabCount() != 0 && editor.getTextPane(0) != null) {
             String name = editor.getTextPane(0).getName();
             if (name == null) {
                 theTextPane = editor.getTextPane(0);
@@ -630,7 +633,7 @@ public class SciNotes extends SwingScilabDockablePanel {
         ScilabEditorPane sep = editor.getTextPane();
 
         if (currentSep.getName() != null) {
-            editor.getTabPane().setTitleAt(0, title);
+            sep.setTitle(title);
             editor.setTitle(winTitle);
         }
 
@@ -642,6 +645,8 @@ public class SciNotes extends SwingScilabDockablePanel {
         editor.enableUndoButton(false);
         editor.enableRedoButton(false);
         ConfigSciNotesManager.saveToOpenFiles(sep.getName(), editor, editor.getTextPane());
+
+        editor.activeRestoreTab();
     }
 
     /**
@@ -699,29 +704,9 @@ public class SciNotes extends SwingScilabDockablePanel {
             return;
         }
 
-        SwingUtilities.invokeLater(new Runnable() {
-            @Override
-            public void run() {
-                RestoreOpenedFilesAction.displayDialog((JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, SciNotes.this), getUUID().toString());
-                List<File> list = RestoreOpenedFilesAction.getSelectedFiles();
-
-                if (list != null && list.size() != 0) {
-                    for (File f : list) {
-                        openFile(f.getPath(), 0, null);
-                    }
-                } else if (getTabPane().getTabCount() == 0) {
-                    openFile(null, 0, null);
-                }
-
-                setWindowIcon("accessories-text-editor");
-
-                if (navigator != null) {
-                    navigator.updateTree();
-                }
-
-                WindowsConfigurationManager.restorationFinished(SciNotes.this);
-            }
-        });
+        addRestoreTab();
+        setWindowIcon("accessories-text-editor");
+        WindowsConfigurationManager.restorationFinished(SciNotes.this);
     }
 
     /**
@@ -784,6 +769,7 @@ public class SciNotes extends SwingScilabDockablePanel {
 
         if (filePath == null) {
             addEmptyTab();
+            activeRestoreTab();
             return;
         }
 
@@ -791,12 +777,14 @@ public class SciNotes extends SwingScilabDockablePanel {
         if (!f.getParentFile().exists()) {
             JOptionPane.showMessageDialog(SciNotes.this, SciNotesMessages.OPEN_ERROR);
             addEmptyTab();
+            activeRestoreTab();
             return;
         }
 
         if (f.isDirectory()) { /* Bug 5131 */
             ConfigManager.saveLastOpenedDirectory(f.getPath());
             addEmptyTab();
+            activeRestoreTab();
             return;
         }
 
@@ -814,6 +802,15 @@ public class SciNotes extends SwingScilabDockablePanel {
             ConfigSciNotesManager.saveToRecentOpenedFiles(filePath);
             RecentFileAction.updateRecentOpenedFilesMenu(this);
         }
+
+        activeRestoreTab();
+    }
+
+    public void activeRestoreTab() {
+        if (getTextPane(0) == null) {
+            tabPane.setSelectedIndex(0);
+            getInfoBar().setText("");
+        }
     }
 
     /**
@@ -822,7 +819,9 @@ public class SciNotes extends SwingScilabDockablePanel {
     public void closeSciNotes() {
         for (int i = 0; i < getTabPane().getTabCount(); i++) {
             ScilabEditorPane textPaneAt = getTextPane(i);
-            textPaneAt.destroy();
+            if (textPaneAt != null) {
+                textPaneAt.destroy();
+            }
         }
 
         FindAction.close();
@@ -985,7 +984,7 @@ public class SciNotes extends SwingScilabDockablePanel {
          * Test for modification added after bug 5103 fix: do not ask the user
          * for an Untitled not-modified file saving when closing SciNotes
          */
-        if (((ScilabDocument) textPaneAt.getDocument()).isContentModified()) {
+        if (textPaneAt != null && ((ScilabDocument) textPaneAt.getDocument()).isContentModified()) {
             if (!save(index, false, false)) {
                 return false;
             }
@@ -1433,6 +1432,17 @@ public class SciNotes extends SwingScilabDockablePanel {
         return sep;
     }
 
+    public void addRestoreTab() {
+        try {
+            JPanel panel = RestoreOpenedFilesAction.getTab(this, getUUID().toString());
+            if (panel != null) {
+                tabPane.addTab("Restore", panel);
+            }
+        } catch (Exception e) {
+            System.err.println(e);
+        }
+    }
+
     /**
      * Init a pane
      *
@@ -1765,7 +1775,7 @@ public class SciNotes extends SwingScilabDockablePanel {
         int index = -1;
         for (int i = 0; i < tabPane.getTabCount(); i++) {
             ScilabEditorPane textPaneAt = getTextPane(i);
-            if (f.getAbsolutePath().equals(textPaneAt.getName())) {
+            if (textPaneAt != null && f.getAbsolutePath().equals(textPaneAt.getName())) {
                 /* File is already opened */
                 tabPane.setSelectedIndex(i);
                 if (f.lastModified() > textPaneAt.getLastModified()) {
@@ -1831,18 +1841,20 @@ public class SciNotes extends SwingScilabDockablePanel {
      */
     public ScilabEditorPane getTextPane() {
         try {
-            EditorComponent c = (EditorComponent) tabPane.getSelectedComponent();
-            ScilabEditorPane pane = c.getEditorPane();
-            if (ScilabEditorPane.getFocusedPane() == pane.getOtherPaneInSplit()) {
-                return pane.getOtherPaneInSplit();
-            }
+            if (tabPane.getSelectedComponent() instanceof EditorComponent) {
+                EditorComponent c = (EditorComponent) tabPane.getSelectedComponent();
+                ScilabEditorPane pane = c.getEditorPane();
+                if (ScilabEditorPane.getFocusedPane() == pane.getOtherPaneInSplit()) {
+                    return pane.getOtherPaneInSplit();
+                }
 
-            return pane;
+                return pane;
+            }
         } catch (NullPointerException e) {
-            return null;
         } catch (ArrayIndexOutOfBoundsException e) {
-            return null;
         }
+
+        return null;
     }
 
     /**
@@ -1854,18 +1866,20 @@ public class SciNotes extends SwingScilabDockablePanel {
      */
     public ScilabEditorPane getTextPane(int index) {
         try {
-            EditorComponent c = (EditorComponent) tabPane.getComponentAt(index);
-            ScilabEditorPane pane = c.getEditorPane();
-            if (ScilabEditorPane.getFocusedPane() == pane.getOtherPaneInSplit()) {
-                return pane.getOtherPaneInSplit();
-            }
+            if (tabPane.getComponentAt(index) instanceof EditorComponent) {
+                EditorComponent c = (EditorComponent) tabPane.getComponentAt(index);
+                ScilabEditorPane pane = c.getEditorPane();
+                if (ScilabEditorPane.getFocusedPane() == pane.getOtherPaneInSplit()) {
+                    return pane.getOtherPaneInSplit();
+                }
 
-            return pane;
+                return pane;
+            }
         } catch (NullPointerException e) {
-            return null;
         } catch (ArrayIndexOutOfBoundsException e) {
-            return null;
         }
+
+        return null;
     }
 
     /**
@@ -2315,7 +2329,7 @@ public class SciNotes extends SwingScilabDockablePanel {
 
             if (getTabPane().getTabCount() == 2) {
                 ScilabEditorPane pane = getTextPane(0);
-                if (pane.getName() == null && !((ScilabDocument) pane.getDocument()).isContentModified()) {
+                if (pane != null && pane.getName() == null && !((ScilabDocument) pane.getDocument()).isContentModified()) {
                     closeTabAt(0);
                 }
             }
@@ -2469,8 +2483,8 @@ public class SciNotes extends SwingScilabDockablePanel {
      *            the SciNotes editor
      */
     private static void setKeyStrokeAction(ScilabEditorPane sep, SciNotes ed) {
-        if (ed.getTextPane(0) != sep) {
-            ScilabEditorPane s = ed.getTextPane(0);
+        ScilabEditorPane s = ed.getTextPane(0);
+        if (s != null && s != sep) {
             sep.setInputMap(JComponent.WHEN_FOCUSED, s.getInputMap());
             return;
         }
index 02bcf21..94a9b78 100644 (file)
@@ -174,7 +174,7 @@ public class SciNotesAutosave implements ActionListener {
                 int n = ed.getTabPane().getTabCount();
                 for (int i = 0; i < n; i++) {
                     ScilabEditorPane sep = ed.getTextPane(i);
-                    if (sep.getName() != null && !sep.getName().isEmpty() && !sep.checkExternalModif() && ((ScilabDocument) sep.getDocument()).isContentModified() && ((ScilabDocument) sep.getDocument()).isContentModifiedSinceBackup()) {
+                    if (sep != null && sep.getName() != null && !sep.getName().isEmpty() && !sep.checkExternalModif() && ((ScilabDocument) sep.getDocument()).isContentModified() && ((ScilabDocument) sep.getDocument()).isContentModifiedSinceBackup()) {
                         String name = sep.getName();
                         File file = getBackupFile(name);
                         boolean identic = false;
index ab91d10..6ea17ae 100644 (file)
@@ -15,7 +15,6 @@ package org.scilab.modules.scinotes.actions;
 
 import java.awt.Color;
 import java.awt.Component;
-import java.awt.Dialog.ModalityType;
 import java.awt.Dimension;
 import java.awt.Font;
 import java.awt.GridBagConstraints;
@@ -29,7 +28,9 @@ import java.awt.event.MouseEvent;
 import java.io.File;
 import java.util.ArrayList;
 import java.util.EventObject;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.UUID;
 import java.util.Vector;
 
@@ -59,6 +60,8 @@ import javax.swing.tree.TreePath;
 
 import org.scilab.modules.commons.gui.FindIconHelper;
 import org.scilab.modules.gui.checkboxmenuitem.CheckBoxMenuItem;
+import org.scilab.modules.gui.bridge.menubar.SwingScilabMenuBar;
+import org.scilab.modules.gui.bridge.toolbar.SwingScilabToolBar;
 import org.scilab.modules.scinotes.SciNotes;
 import org.scilab.modules.scinotes.ScilabEditorPane;
 import org.scilab.modules.scinotes.utils.ConfigSciNotesManager;
@@ -76,6 +79,7 @@ public class RestoreOpenedFilesAction extends DefaultCheckAction {
     private static final String ESCAPE = "ESCAPE";
     private static final Icon SCILAB_ICON = new ImageIcon(FindIconHelper.findIcon("scilab"));
     private static List<File> selectedFiles;
+    private static Map<SciNotes, List<Component>> listOfComponents;
 
     /**
      * Constructor
@@ -115,31 +119,51 @@ public class RestoreOpenedFilesAction extends DefaultCheckAction {
         return selectedFiles;
     }
 
-    /**
-     * Display the JDialog
-     * @param owner the owner
-     * @param uuid the editor uuid
-     */
-    public static void displayDialog(JFrame owner, final String uuid) {
-        selectedFiles = null;
-        int dimX = 450;
-        int dimY = 300;
+    private static void saveEnabledComponents(SciNotes ed) {
+        List<Component> list = new ArrayList<Component>();
+        SwingScilabMenuBar mb = (SwingScilabMenuBar) ed.getMenuBar().getAsSimpleMenuBar();
+        SwingScilabToolBar tb = (SwingScilabToolBar) ed.getToolBar().getAsSimpleToolBar();
+        for (int i = 0; i < mb.getMenuCount(); i++) {
+            if (mb.getMenu(i).isEnabled()) {
+                list.add(mb.getMenu(i));
+                mb.getMenu(i).setEnabled(false);
+            }
+        }
+        for (int i = 0; i < tb.getComponentCount(); i++) {
+            if (tb.getComponent(i).isEnabled()) {
+                list.add(tb.getComponent(i));
+                tb.getComponent(i).setEnabled(false);
+            }
+        }
 
-        final JTree tree = fillTree(uuid);
-        if (tree == null) {
-            return;
+        if (listOfComponents == null) {
+            listOfComponents = new HashMap<SciNotes, List<Component>>();
         }
+        listOfComponents.put(ed, list);
+    }
 
-        final JDialog dialog = new JDialog(owner);
-        dialog.getRootPane().getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE , 0), ESCAPE);
-        dialog.getRootPane().getActionMap().put(ESCAPE, new AbstractAction() {
-            @Override
-            public void actionPerformed(ActionEvent e) {
-                dialog.dispose();
+    public static void restoreEnabledComponents(SciNotes ed) {
+        if (listOfComponents != null && listOfComponents.containsKey(ed)) {
+            SwingScilabMenuBar mb = (SwingScilabMenuBar) ed.getMenuBar().getAsSimpleMenuBar();
+            SwingScilabToolBar tb = (SwingScilabToolBar) ed.getToolBar().getAsSimpleToolBar();
+            List<Component> l = listOfComponents.get(ed);
+            for (Component c : l) {
+                c.setEnabled(true);
             }
-        });
 
-        dialog.setPreferredSize(new Dimension(dimX, dimY));
+            l.clear();
+            listOfComponents.remove(ed);
+            if (listOfComponents.isEmpty()) {
+                listOfComponents = null;
+            }
+        }
+    }
+
+    public static JPanel getTab(final SciNotes ed, final String uuid) {
+        final JTree tree = fillTree(uuid);
+        if (tree == null) {
+            return null;
+        }
 
         JButton ok = new JButton(SciNotesMessages.OK);
         JButton cancel = new JButton(SciNotesMessages.CANCEL);
@@ -148,8 +172,21 @@ public class RestoreOpenedFilesAction extends DefaultCheckAction {
         ok.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
-                selectedFiles = getOpenedFiles(tree, uuid);
-                dialog.dispose();
+                List<File> list = getOpenedFiles(tree, uuid);
+                ed.getTabPane().remove(0);
+                if (list != null && list.size() != 0) {
+                    for (File f : list) {
+                        ed.openFile(f.getPath(), 0, null);
+                    }
+                } else if (ed.getTabPane().getTabCount() == 0) {
+                    ed.addEmptyTab();
+                }
+
+                if (ed.getNavigator() != null) {
+                    ed.getNavigator().updateTree();
+                }
+
+                restoreEnabledComponents(ed);
             }
         });
 
@@ -164,7 +201,11 @@ public class RestoreOpenedFilesAction extends DefaultCheckAction {
                         break;
                     }
                 }
-                dialog.dispose();
+                ed.getTabPane().remove(0);
+                if (ed.getTabPane().getTabCount() == 0) {
+                    ed.addEmptyTab();
+                }
+                restoreEnabledComponents(ed);
             }
         });
 
@@ -178,38 +219,30 @@ public class RestoreOpenedFilesAction extends DefaultCheckAction {
         }
 
         JOptionPane jop = new JOptionPane(SciNotesMessages.RESTORE_FILES_QUERY, JOptionPane.QUESTION_MESSAGE, JOptionPane.CANCEL_OPTION, null, buttons);
-        JPanel panel = new JPanel();
-        panel.setPreferredSize(new Dimension(dimX, dimY));
+        final JPanel panel = new JPanel();
         panel.setLayout(new GridBagLayout());
         GridBagConstraints c = new GridBagConstraints();
         c.gridheight = 1;
         c.gridwidth = 1;
         c.gridx = 0;
         c.gridy = 0;
-        c.weighty = 0.5;
+        c.weightx = 1;
+        c.weighty = 0.1;
         c.anchor = GridBagConstraints.PAGE_START;
         c.fill = GridBagConstraints.HORIZONTAL;
         panel.add(jop, c);
         c.gridx = 0;
         c.gridy = 1;
         c.gridheight = GridBagConstraints.REMAINDER;
-        c.fill = GridBagConstraints.HORIZONTAL;
+        c.weighty = 0.9;
+        c.fill = GridBagConstraints.BOTH;
         JScrollPane scroll = new JScrollPane(tree);
-        scroll.setMinimumSize(new Dimension(dimX - 2 * GAP, dimY / 2));
         panel.add(scroll, c);
-
-        dialog.setContentPane(panel);
-
-        dialog.pack();
-        dialog.setIconImage(((ImageIcon) SCILAB_ICON).getImage());
-        dialog.setTitle(SciNotesMessages.RESTORE_FILES_TITLE);
-        dialog.setModalityType(ModalityType.APPLICATION_MODAL);
-        dialog.setResizable(false);
-        dialog.setLocationRelativeTo(owner);
-
         ok.requestFocusInWindow();
 
-        dialog.setVisible(true);
+        saveEnabledComponents(ed);
+
+        return panel;
     }
 
     /**
index 2fc96b0..2fb2dea 100644 (file)
@@ -41,9 +41,10 @@ public class SciNotesContents extends JPanel {
         add(stp);
         addFocusListener(new FocusListener() {
             public void focusGained(FocusEvent e) {
-                editor.getTextPane().requestFocus();
+                if (editor.getTextPane() != null) {
+                    editor.getTextPane().requestFocus();
+                }
             }
-
             public void focusLost(FocusEvent e) { }
         });
     }
index f8c3e89..39a6609 100644 (file)
@@ -63,10 +63,12 @@ import org.scilab.modules.gui.bridge.menuitem.SwingScilabMenuItem;
 import org.scilab.modules.gui.bridge.tab.SwingScilabDockablePanel;
 import org.scilab.modules.gui.utils.ClosingOperationsManager;
 
+import org.scilab.modules.scinotes.EditorComponent;
 import org.scilab.modules.scinotes.SciNotes;
 import org.scilab.modules.scinotes.ScilabEditorPane;
 import org.scilab.modules.scinotes.actions.CloseAction;
 import org.scilab.modules.scinotes.actions.CloseAllButThisAction;
+import org.scilab.modules.scinotes.actions.RestoreOpenedFilesAction;
 import org.scilab.modules.scinotes.actions.SaveAction;
 
 /**
@@ -361,11 +363,13 @@ public class ScilabTabbedPane extends JTabbedPane implements DragGestureListener
         if (inputEvent instanceof MouseEvent) {
             MouseEvent mouseEvent = (MouseEvent) inputEvent;
             int index = indexAtLocation(mouseEvent.getX(), mouseEvent.getY());
-            currentWhenDragged = this;
+            if (index == -1 || (getComponentAt(index) instanceof EditorComponent)) {
+                currentWhenDragged = this;
 
-            if (index != -1) {
-                draggedIndex = index;
-                dge.startDrag(DragSource.DefaultMoveDrop, this, this);
+                if (index != -1) {
+                    draggedIndex = index;
+                    dge.startDrag(DragSource.DefaultMoveDrop, this, this);
+                }
             }
         }
     }
@@ -454,13 +458,22 @@ public class ScilabTabbedPane extends JTabbedPane implements DragGestureListener
         }
 
         public void closeTab() {
-            String name = editor.getTextPane(editor.getTabPane().indexOfTabComponent(this)).getName();
-            editor.closeTabAt(editor.getTabPane().indexOfTabComponent(this));
-            if (getTabCount() == 0) {
-                if (name != null) {
+            ScilabEditorPane sep = editor.getTextPane(editor.getTabPane().indexOfTabComponent(this));
+            if (sep != null) {
+                String name = sep.getName();
+                editor.closeTabAt(editor.getTabPane().indexOfTabComponent(this));
+                if (getTabCount() == 0) {
+                    if (name != null) {
+                        editor.addEmptyTab();
+                    } else {
+                        SciNotes.closeEditor(editor);
+                    }
+                }
+            } else if (editor.getTabPane().getTabComponentAt(editor.getTabPane().indexOfTabComponent(this)) != null) {
+                editor.getTabPane().remove(editor.getTabPane().indexOfTabComponent(this));
+                if (editor.getTabPane().getTabCount() == 0) {
                     editor.addEmptyTab();
-                } else {
-                    SciNotes.closeEditor(editor);
+                    RestoreOpenedFilesAction.restoreEnabledComponents(editor);
                 }
             }
         }