SciNotes: improve error message 16/10516/1
Calixte DENIZET [Mon, 18 Feb 2013 13:58:56 +0000 (14:58 +0100)]
Change-Id: I738359e6a10a249077bb2e8d78e6413eb4bdba98

scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/SciNotes.java
scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/utils/SciNotesMessages.java

index a4be301..a632aec 100644 (file)
@@ -20,6 +20,7 @@ import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.InputStreamReader;
 import java.io.IOException;
@@ -177,11 +178,11 @@ public class SciNotes extends SwingScilabTab {
         ConfigSciNotesManager.createUserCopy();
         ScilabTabFactory.getInstance().addTabFactory(SciNotesTabFactory.getInstance());
         Scilab.registerInitialHook(new Runnable() {
-            @Override
-            public void run() {
-                updateSciNotes();
-            }
-        });
+                @Override
+                public void run() {
+                    updateSciNotes();
+                }
+            });
 
         Document doc = ScilabXMLUtilities.readDocument(System.getenv("SCI") + "/modules/console/etc/Actions-Configuration.xml");
         actionToName = XConfiguration.get(doc, "name", String.class, "action", String.class, XPATH_SCINOTES_ACTION);
@@ -205,23 +206,23 @@ public class SciNotes extends SwingScilabTab {
         contentPane = new SciNotesContents(this);
         tabPane = contentPane.getScilabTabbedPane();
         tabPane.addChangeListener(new ChangeListener() {
-            @Override
-            public void stateChanged(ChangeEvent e) {
-                if (getTextPane() != null) {
-                    // updateUI();
-                    getTextPane().updateInfosWhenFocused();
-                    getTextPane().requestFocus();
-                    getTextPane().highlightWords(IncrementalSearchAction.getWord(SciNotes.this), IncrementalSearchAction.getExact(SciNotes.this));
-
-                    // Update encoding menu
-                    EncodingAction.updateEncodingMenu((ScilabDocument) getTextPane().getDocument());
-
-                    // Update End Of Line menu
-                    EndOfLineAction.updateEolMenu((ScilabDocument) getTextPane().getDocument());
-                    setTitle(getTextPane().getTitle());
+                @Override
+                public void stateChanged(ChangeEvent e) {
+                    if (getTextPane() != null) {
+                        // updateUI();
+                        getTextPane().updateInfosWhenFocused();
+                        getTextPane().requestFocus();
+                        getTextPane().highlightWords(IncrementalSearchAction.getWord(SciNotes.this), IncrementalSearchAction.getExact(SciNotes.this));
+
+                        // Update encoding menu
+                        EncodingAction.updateEncodingMenu((ScilabDocument) getTextPane().getDocument());
+
+                        // Update End Of Line menu
+                        EndOfLineAction.updateEolMenu((ScilabDocument) getTextPane().getDocument());
+                        setTitle(getTextPane().getTitle());
+                    }
                 }
-            }
-        });
+            });
         this.setContentPane(contentPane);
     }
 
@@ -439,15 +440,15 @@ public class SciNotes extends SwingScilabTab {
         try {
             SwingUtilities.invokeAndWait(new Runnable() {
 
-                @Override
-                public void run() {
-                    launchSciNotes();
-                    // Open an empty file if no tabs were opened at launch.
-                    if (editor.getTabPane().getTabCount() == 0) {
-                        editor.openFile(null, 0, null);
+                    @Override
+                    public void run() {
+                        launchSciNotes();
+                        // Open an empty file if no tabs were opened at launch.
+                        if (editor.getTabPane().getTabCount() == 0) {
+                            editor.openFile(null, 0, null);
+                        }
                     }
-                }
-            });
+                });
         } catch (InterruptedException e) {
             Logger.getLogger(SciNotes.class.getName()).severe(e.toString());
             throw new RuntimeException(e);
@@ -471,12 +472,12 @@ public class SciNotes extends SwingScilabTab {
         try {
             SwingUtilities.invokeAndWait(new Runnable() {
 
-                @Override
-                public void run() {
-                    launchSciNotes();
-                    editor.openFile(filePath, 0, null);
-                }
-            });
+                    @Override
+                    public void run() {
+                        launchSciNotes();
+                        editor.openFile(filePath, 0, null);
+                    }
+                });
         } catch (InterruptedException e) {
             Logger.getLogger(SciNotes.class.getName()).severe(e.toString());
             throw new RuntimeException(e);
@@ -502,12 +503,12 @@ public class SciNotes extends SwingScilabTab {
         try {
             SwingUtilities.invokeAndWait(new Runnable() {
 
-                @Override
-                public void run() {
-                    launchSciNotes();
-                    editor.openFile(filePath, lineNumber, functionName);
-                }
-            });
+                    @Override
+                    public void run() {
+                        launchSciNotes();
+                        editor.openFile(filePath, lineNumber, functionName);
+                    }
+                });
         } catch (InterruptedException e) {
             Logger.getLogger(SciNotes.class.getName()).severe(e.toString());
             throw new RuntimeException(e);
@@ -528,13 +529,15 @@ public class SciNotes extends SwingScilabTab {
      *
      *            This method *must not* be called on the EDT thread.
      */
-    public static void scinotes(final String filePath, final String[] options) {
+    public static void scinotes(final String filePath, final String[] options) throws Exception {
         boolean hasAction = false;
         if (options != null && options.length != 0) {
             try {
                 hasAction = executeAction(filePath, options);
+            } catch (FileNotFoundException e) {
+                throw new Exception(String.format(SciNotesMessages.INVALID_FILE, filePath));
             } catch (IOException e) {
-                System.err.println(e);
+                throw new Exception(String.format(SciNotesMessages.IO_EXCEPTION, e.getLocalizedMessage()));
             }
         }
 
@@ -546,16 +549,16 @@ public class SciNotes extends SwingScilabTab {
         try {
             SwingUtilities.invokeAndWait(new Runnable() {
 
-                @Override
-                public void run() {
-                    launchSciNotes();
-                    if (options != null && options.length != 0) {
-                        editor.openFile(filePath, 0, options[0]);
-                    } else {
-                        editor.openFile(filePath, 0, "");
+                    @Override
+                    public void run() {
+                        launchSciNotes();
+                        if (options != null && options.length != 0) {
+                            editor.openFile(filePath, 0, options[0]);
+                        } else {
+                            editor.openFile(filePath, 0, "");
+                        }
                     }
-                }
-            });
+                });
         } catch (InterruptedException e) {
             Logger.getLogger(SciNotes.class.getName()).severe(e.toString());
             throw new RuntimeException(e);
@@ -661,11 +664,11 @@ public class SciNotes extends SwingScilabTab {
             try {
                 SwingUtilities.invokeAndWait(new Runnable() {
 
-                    @Override
-                    public void run() {
-                        restorePreviousSession();
-                    }
-                });
+                        @Override
+                        public void run() {
+                            restorePreviousSession();
+                        }
+                    });
             } catch (InterruptedException e) {
                 Logger.getLogger(SciNotes.class.getName()).severe(e.toString());
                 throw new RuntimeException(e);
@@ -694,30 +697,30 @@ public class SciNotes extends SwingScilabTab {
         }
 
         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 || getTextPane(0).getName() == null) {
-                        openFile(null, 0, null);
+                @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 || getTextPane(0).getName() == null) {
+                            openFile(null, 0, null);
+                        }
                     }
-                }
 
-                setWindowIcon("accessories-text-editor");
+                    setWindowIcon("accessories-text-editor");
 
-                if (navigator != null) {
-                    navigator.updateTree();
-                }
+                    if (navigator != null) {
+                        navigator.updateTree();
+                    }
 
-                WindowsConfigurationManager.restorationFinished(SciNotes.this);
-            }
-        });
+                    WindowsConfigurationManager.restorationFinished(SciNotes.this);
+                }
+            });
     }
 
     /**
@@ -1070,14 +1073,14 @@ public class SciNotes extends SwingScilabTab {
             }
 
             switch (answer) {
-                case YES_OPTION: // Yes, continue
-                    break;
-                case NO_OPTION:// No, exit and returns true
-                    return true;
-                case CANCEL_OPTION: // Cancel, exit and return false
-                    return false;
-                default:
-                    break;
+            case YES_OPTION: // Yes, continue
+                break;
+            case NO_OPTION:// No, exit and returns true
+                return true;
+            case CANCEL_OPTION: // Cancel, exit and return false
+                return false;
+            default:
+                break;
             }
         }
 
@@ -1278,7 +1281,7 @@ public class SciNotes extends SwingScilabTab {
             String fileName = f.getName();
             if (fileName.lastIndexOf(DOT) != -1) {
                 if (fileName.substring(fileName.lastIndexOf(DOT), fileName.length()).length() >= 2
-                        && fileName.substring(fileName.lastIndexOf(DOT), fileName.length()).length() <= 4) {
+                    && fileName.substring(fileName.lastIndexOf(DOT), fileName.length()).length() <= 4) {
                     hasNoExtension = false;
                 }
             }
@@ -1770,22 +1773,22 @@ public class SciNotes extends SwingScilabTab {
                     messageBox.displayAndWait(); // Waits for a user action
 
                     switch (messageBox.getSelectedButton()) {
-                        case 1: // Reload
-                            if ((i == 0) && (getTabPane().getTabCount() == 1)) {
-                                for (int j = 0; j < tabPane.getChangeListeners().length; j++) {
-                                    tabPane.removeChangeListener(tabPane.getChangeListeners()[j]);
-                                }
+                    case 1: // Reload
+                        if ((i == 0) && (getTabPane().getTabCount() == 1)) {
+                            for (int j = 0; j < tabPane.getChangeListeners().length; j++) {
+                                tabPane.removeChangeListener(tabPane.getChangeListeners()[j]);
                             }
-                            tabPane.remove(i);
-                            f = new File(textPaneAt.getName());
-                            index = i;
-                            break;
-                        case 2: // Overwrite 2
-                            overwrite(i);
-                            alreadyOpened = true;
-                            break;
-                        default: // Ignore
-                            alreadyOpened = true;
+                        }
+                        tabPane.remove(i);
+                        f = new File(textPaneAt.getName());
+                        index = i;
+                        break;
+                    case 2: // Overwrite 2
+                        overwrite(i);
+                        alreadyOpened = true;
+                        break;
+                    default: // Ignore
+                        alreadyOpened = true;
                     }
                 } else {
                     alreadyOpened = true;
@@ -2317,7 +2320,7 @@ public class SciNotes extends SwingScilabTab {
         ScilabEditorPane theTextPane = addEmptyTab();
         ScilabDocument styleDocument = null;
         int choice = JOptionPane.showConfirmDialog(SciNotes.this, String.format(SciNotesMessages.FILE_DOESNT_EXIST, f.getName()), SCINOTES,
-                     JOptionPane.YES_NO_OPTION);
+                                                   JOptionPane.YES_NO_OPTION);
         if (choice == 0) { // OK
             styleDocument = (ScilabDocument) theTextPane.getDocument();
             styleDocument.disableUndoManager();
@@ -2412,16 +2415,16 @@ public class SciNotes extends SwingScilabTab {
         try {
             SwingUtilities.invokeAndWait(new Runnable() {
 
-                @Override
-                public void run() {
-                    SciNotes[] arr = scinotesList.toArray(new SciNotes[0]);
-                    for (int i = 0; i < arr.length; i++) {
-                        arr[i].setProtectOpenFileList(true);
-                        ExitAction.doExit(arr[i]);
+                    @Override
+                    public void run() {
+                        SciNotes[] arr = scinotesList.toArray(new SciNotes[0]);
+                        for (int i = 0; i < arr.length; i++) {
+                            arr[i].setProtectOpenFileList(true);
+                            ExitAction.doExit(arr[i]);
+                        }
+                        scinotesList.clear();
                     }
-                    scinotesList.clear();
-                }
-            });
+                });
         } catch (InterruptedException e) {
             Logger.getLogger(SciNotes.class.getName()).severe(e.toString());
         } catch (InvocationTargetException e) {
@@ -2541,21 +2544,21 @@ public class SciNotes extends SwingScilabTab {
     public static boolean executeAction(String fileName, final String[] actionsName) throws IOException {
         final boolean[] hasAction = new boolean[] { false };
         ActionOnDocument action = new ActionOnDocument() {
-            public void actionOn(ScilabDocument doc) throws IOException {
-                for (String act : actionsName) {
-                    if (act.equalsIgnoreCase("indent")) {
-                        hasAction[0] = true;
-                        org.scilab.modules.scinotes.actions.IndentAction.getActionOnDocument().actionOn(doc);
-                    } else if (act.equalsIgnoreCase("trailing")) {
-                        hasAction[0] = true;
-                        org.scilab.modules.scinotes.actions.RemoveTrailingWhiteAction.getActionOnDocument().actionOn(doc);
-                    } else if (act.equalsIgnoreCase("quote")) {
-                        hasAction[0] = true;
-                        org.scilab.modules.scinotes.actions.DoubleQuoteStringAction.getActionOnDocument().actionOn(doc);
+                public void actionOn(ScilabDocument doc) throws IOException {
+                    for (String act : actionsName) {
+                        if (act.equalsIgnoreCase("indent")) {
+                            hasAction[0] = true;
+                            org.scilab.modules.scinotes.actions.IndentAction.getActionOnDocument().actionOn(doc);
+                        } else if (act.equalsIgnoreCase("trailing")) {
+                            hasAction[0] = true;
+                            org.scilab.modules.scinotes.actions.RemoveTrailingWhiteAction.getActionOnDocument().actionOn(doc);
+                        } else if (act.equalsIgnoreCase("quote")) {
+                            hasAction[0] = true;
+                            org.scilab.modules.scinotes.actions.DoubleQuoteStringAction.getActionOnDocument().actionOn(doc);
+                        }
                     }
                 }
-            }
-        };
+            };
 
         executeAction(fileName, action);
 
index 81718bd..60e2a1c 100644 (file)
@@ -30,6 +30,9 @@ public class SciNotesMessages {
     public static final String SCILAB_EDITOR = Messages.gettext("SciNotes");
     public static final String ERROR_WITH_STRING = Messages.gettext("Error while reading the String");
 
+    public static final String INVALID_FILE = Messages.gettext("File not found: %s");
+    public static final String IO_EXCEPTION = Messages.gettext("I/O error: %s");
+
     /** FILE MENU */
     public static final String FILE = Messages.gettext("File");
     public static final String NEW = Messages.gettext("New") + DOTS;