2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009 - DIGITEO - Bruno JOFRET
4 * Copyright (C) 2010 - 2011 - Calixte DENIZET
6 * This file must be used under the terms of the CeCILL.
7 * This source file is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution. The terms
9 * are also available at
10 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
14 package org.scilab.modules.scinotes;
16 import java.awt.Color;
17 import java.awt.Component;
19 import java.awt.event.FocusEvent;
20 import java.awt.event.FocusListener;
21 import java.io.BufferedReader;
22 import java.io.BufferedWriter;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.FileOutputStream;
27 import java.io.InputStreamReader;
28 import java.io.IOException;
29 import java.io.OutputStreamWriter;
30 import java.io.StringReader;
31 import java.lang.reflect.Constructor;
32 import java.lang.reflect.InvocationTargetException;
33 import java.nio.charset.CharacterCodingException;
34 import java.nio.charset.Charset;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.Iterator;
39 import java.util.List;
41 import java.util.StringTokenizer;
42 import java.util.UUID;
43 import java.util.logging.Logger;
45 import javax.swing.BorderFactory;
46 import javax.swing.JButton;
47 import javax.swing.JComponent;
48 import javax.swing.JFileChooser;
49 import javax.swing.JFrame;
50 import javax.swing.JOptionPane;
51 import javax.swing.JPanel;
52 import javax.swing.JSplitPane;
53 import javax.swing.KeyStroke;
54 import javax.swing.SwingUtilities;
55 import javax.swing.event.ChangeEvent;
56 import javax.swing.event.ChangeListener;
57 import javax.swing.text.BadLocationException;
58 import javax.swing.text.EditorKit;
59 import javax.swing.text.View;
60 import javax.swing.undo.UndoManager;
62 import org.w3c.dom.Document;
64 import org.flexdock.docking.event.DockingEvent;
65 import org.scilab.modules.commons.CommonFileUtils;
66 import org.scilab.modules.commons.gui.ScilabKeyStroke;
67 import org.scilab.modules.commons.xml.ScilabXMLUtilities;
68 import org.scilab.modules.commons.xml.XConfiguration;
69 import static org.scilab.modules.commons.xml.XConfiguration.XConfAttribute;
70 import org.scilab.modules.core.Scilab;
71 import org.scilab.modules.gui.bridge.filechooser.SwingScilabFileChooser;
72 import org.scilab.modules.gui.bridge.tab.SwingScilabDockablePanel;
73 import org.scilab.modules.gui.bridge.window.SwingScilabWindow;
74 import org.scilab.modules.gui.filechooser.Juigetfile;
75 import org.scilab.modules.gui.filechooser.ScilabFileChooser;
76 import org.scilab.modules.gui.menubar.MenuBar;
77 import org.scilab.modules.gui.messagebox.MessageBox;
78 import org.scilab.modules.gui.messagebox.ScilabMessageBox;
79 import org.scilab.modules.gui.messagebox.ScilabModalDialog;
80 import org.scilab.modules.gui.messagebox.ScilabModalDialog.AnswerOption;
81 import org.scilab.modules.gui.messagebox.ScilabModalDialog.ButtonType;
82 import org.scilab.modules.gui.messagebox.ScilabModalDialog.IconType;
83 import org.scilab.modules.gui.tab.SimpleTab;
84 import org.scilab.modules.gui.tabfactory.ScilabTabFactory;
85 import org.scilab.modules.gui.textbox.TextBox;
86 import org.scilab.modules.gui.toolbar.ToolBar;
87 import org.scilab.modules.gui.utils.ClosingOperationsManager;
88 import org.scilab.modules.gui.utils.ConfigManager;
89 import org.scilab.modules.gui.utils.Position;
90 import org.scilab.modules.gui.utils.SciFileFilter;
91 import org.scilab.modules.gui.utils.Size;
92 import org.scilab.modules.gui.utils.WindowsConfigurationManager;
94 import org.scilab.modules.scinotes.actions.DoubleQuoteStringAction;
95 import org.scilab.modules.scinotes.actions.EncodingAction;
96 import org.scilab.modules.scinotes.actions.EndOfLineAction;
97 import org.scilab.modules.scinotes.actions.ExitAction;
98 import org.scilab.modules.scinotes.actions.FindAction;
99 import org.scilab.modules.scinotes.actions.IncrementalSearchAction;
100 import org.scilab.modules.scinotes.actions.IndentAction;
101 import org.scilab.modules.scinotes.actions.InsertOverwriteAction;
102 import org.scilab.modules.scinotes.actions.LineBeautifierAction;
103 import org.scilab.modules.scinotes.actions.OpenSourceFileOnKeywordAction;
104 import org.scilab.modules.scinotes.actions.RecentFileAction;
105 import org.scilab.modules.scinotes.actions.RegisterFavoriteDirsAction;
106 import org.scilab.modules.scinotes.actions.RemoveTrailingWhiteAction;
107 import org.scilab.modules.scinotes.actions.RestoreOpenedFilesAction;
108 import org.scilab.modules.scinotes.actions.SciNotesCompletionAction;
109 import org.scilab.modules.scinotes.actions.SearchWordInFilesAction;
110 import org.scilab.modules.scinotes.tabfactory.CodeNavigatorTab;
111 import org.scilab.modules.scinotes.tabfactory.SciNotesTab;
112 import org.scilab.modules.scinotes.tabfactory.SciNotesTabFactory;
113 import org.scilab.modules.scinotes.tabfactory.SearchInFilesTab;
114 import org.scilab.modules.scinotes.utils.ConfigSciNotesManager;
115 import org.scilab.modules.scinotes.utils.DropFilesListener;
116 import org.scilab.modules.scinotes.utils.NavigatorWindow;
117 import org.scilab.modules.scinotes.utils.SaveFile;
118 import org.scilab.modules.scinotes.utils.SciNotesContents;
119 import org.scilab.modules.scinotes.utils.SciNotesMessages;
120 import org.scilab.modules.scinotes.utils.ScilabTabbedPane;
121 import org.scilab.modules.scinotes.utils.SearchFile;
124 * Main SciNotes class.
126 * @author Bruno JOFRET
127 * @author Calixte DENIZET
129 public class SciNotes extends SwingScilabDockablePanel {
131 private static final long serialVersionUID = -6410183357490518676L;
133 private static final String XPATH_SCINOTES_KEY = "//general/shortcuts/body/actions/action-folder[@name='Scinotes']/action";
134 private static final String XPATH_SCINOTES_ACTION = "/map/scinotes/entry";
136 private static final String SCINOTES = "SciNotes";
137 private static final String SCI_EXTENSION = ".sci";
138 private static final String SCE_EXTENSION = ".sce";
139 private static final String TST_EXTENSION = ".tst";
140 private static final String QUIT_EXTENSION = ".quit";
141 private static final String DEM_EXTENSION = ".dem";
142 private static final String START_EXTENSION = ".start";
143 private static final String ALL_TST_FILES = "*.tst";
144 private static final String ALL_QUIT_FILES = "*.quit";
145 private static final String ALL_START_FILES = "*.start";
146 private static final String ALL_SCI_FILES = "*.sci";
147 private static final String ALL_SCE_FILES = "*.sce";
148 private static final String ALL_DEM_FILES = "*.dem";
149 private static final String ALL_SCX_FILES = "*.sc*";
150 private static final String ALL_SCILAB = "all";
151 private static final String ALL_FILES = "*.*";
152 private static final String DOT = ".";
154 private static final String DEFAULTACTIONPATH = "org.scilab.modules.scinotes.actions";
156 private static final Map<String, String> actionToName;
157 private static Map<String, KeyStroke> actionKeys;
159 private static List<SciNotes> scinotesList = new ArrayList<SciNotes>();
160 private static SciNotes editor;
161 private static boolean mustWrapLines;
163 private SwingScilabWindow parentWindow;
166 private ScilabTabbedPane tabPane;
167 private final SciNotesContents contentPane;
168 private NavigatorWindow navigator;
169 private SearchFile searchInFiles;
171 private int numberOfUntitled;
172 private EditorKit editorKit;
174 private JButton undoButton;
175 private JButton redoButton;
177 private boolean protectOpenFileList;
178 private boolean restored;
179 private boolean firstOpen = true;
181 private final List<Integer> tabList = new ArrayList<Integer>();
182 private final List<Integer> closedTabList = new ArrayList<Integer>();
185 ConfigSciNotesManager.createUserCopy();
186 ScilabTabFactory.getInstance().addTabFactory(SciNotesTabFactory.getInstance());
187 Scilab.registerInitialHook(new Runnable() {
194 Document doc = ScilabXMLUtilities.readDocument(System.getenv("SCI") + "/modules/console/etc/Actions-Configuration.xml");
195 actionToName = XConfiguration.get(doc, "name", String.class, "action", String.class, XPATH_SCINOTES_ACTION);
196 XConfiguration.addXConfigurationListener(new SciNotesConfiguration());
200 * Create SciNotes instance
202 public SciNotes(String uuid) {
203 super(SCINOTES, uuid);
204 setWindowIcon("accessories-text-editor");
205 setAssociatedXMLIDForHelp("scinotes");
206 this.uuid = UUID.fromString(uuid);
207 SciNotesAutosave.autosave();
209 scinotesList.add(this);
210 numberOfUntitled = 0;
211 mustWrapLines = SciNotesOptions.getSciNotesDisplay().wrapLines;
212 editorKit = new ScilabEditorKit(!mustWrapLines);
213 protectOpenFileList = false;
214 contentPane = new SciNotesContents(this);
215 tabPane = contentPane.getScilabTabbedPane();
216 tabPane.addChangeListener(new ChangeListener() {
218 public void stateChanged(ChangeEvent e) {
219 if (getTextPane() != null) {
221 getTextPane().updateInfosWhenFocused();
222 getTextPane().requestFocus();
223 getTextPane().highlightWords(IncrementalSearchAction.getWord(SciNotes.this), IncrementalSearchAction.getExact(SciNotes.this));
225 // Update encoding menu
226 EncodingAction.updateEncodingMenu((ScilabDocument) getTextPane().getDocument());
228 // Update End Of Line menu
229 EndOfLineAction.updateEolMenu((ScilabDocument) getTextPane().getDocument());
230 setTitle(getTextPane().getTitle());
234 this.setContentPane(contentPane);
238 * Default constructor
241 this(UUID.randomUUID().toString());
244 public static void configurationChanged(SciNotesConfiguration.Conf conf) {
245 setWhereamiLineNumbering();
248 setDefaultTabulation();
251 setKeyStrokeActions();
255 SciNotesAutosave.stopAutosave();
256 SciNotesAutosave.autosave();
258 if (conf.preferences) {
259 RecentFileAction.updateRecentOpenedFilesMenu();
265 public static Map<String, KeyStroke> getActionKeys() {
266 if (actionKeys == null) {
267 Document doc = XConfiguration.getXConfigurationDocument();
268 actionKeys = XConfiguration.get(doc, "name", String.class, "key", KeyStroke.class, XPATH_SCINOTES_KEY);
274 public static Map<String, String> getActionName() {
279 * Get an opened editor from its uuid
283 * @return the corresponding editor
285 public static SciNotes getEditorFromUUID(String uuid) {
286 for (SciNotes ed : scinotesList) {
287 if (ed.getPersistentId().equals(uuid)) {
295 public void setParentWindow() {
296 this.parentWindow = SwingScilabWindow.createWindow(true);
297 parentWindow.setLocation(150, 50);
298 parentWindow.setSize(650, 550);
305 public void setTitle(String title) {
306 super.setTitle(title);
307 SwingScilabWindow window = (SwingScilabWindow) SwingUtilities.getAncestorOfClass(SwingScilabWindow.class, tabPane);
308 if (window != null) {
309 window.setTitle(title);
313 public void insertBottomComponent(Component c) {
314 contentPane.insertBottomComponent(c);
318 * @return the SwingScilabWindow containing this editor
320 public SwingScilabWindow getSwingParentWindow() {
321 return (SwingScilabWindow) SwingUtilities.getAncestorOfClass(SwingScilabWindow.class, this);
328 public void undockingComplete(DockingEvent evt) {
329 super.undockingComplete(evt);
330 if (navigator != null) {
331 navigator.addToolBar(null);
333 if (searchInFiles != null) {
334 searchInFiles.addToolBar(null);
342 public void dockingComplete(DockingEvent evt) {
343 super.dockingComplete(evt);
344 if (navigator != null) {
345 navigator.changeToolBar();
347 if (searchInFiles != null) {
348 searchInFiles.changeToolBar();
353 * Add a code navigator
355 public void addNavigator() {
356 if (navigator == null) {
357 String navUUID = ConfigSciNotesManager.getCodeNavigatorStateForEditor(getPersistentId());
358 boolean success = WindowsConfigurationManager.restoreUUID(navUUID);
360 navigator = CodeNavigatorTab.getCodeNavigatorInstance(this, null);
361 navigator.setParentWindow();
363 int count = getTabPane().getTabCount();
364 for (int i = 0; i < count; i++) {
365 navigator.addEditorPane(getTextPane(i));
371 * Add a code navigator
373 public void addNavigator(NavigatorWindow navigator) {
374 this.navigator = navigator;
378 * Remove the navigator
380 public void removeNavigator() {
385 * @return the navigator associated with this editor
387 public NavigatorWindow getNavigator() {
392 * Add a Search in files
394 public void addSearchInFiles() {
395 if (searchInFiles == null) {
396 String sfUUID = ConfigSciNotesManager.getSearchInFilesStateForEditor(getPersistentId());
397 boolean success = WindowsConfigurationManager.restoreUUID(sfUUID);
399 searchInFiles = SearchInFilesTab.getSearchInFilesTabInstance(this, null);
400 searchInFiles.setParentWindow();
406 * Add a Search In Files
408 public void addSearchInFiles(SearchFile sf) {
409 this.searchInFiles = sf;
413 * Remove Search in Files
415 public void removeSearchInFiles() {
416 searchInFiles = null;
420 * @return the Search In Files associated with this editor
422 public SearchFile getSearchInFiles() {
423 return searchInFiles;
429 public static void updateSciNotes() {
430 ScilabLexer.update();
431 for (SciNotes editor : scinotesList) {
432 if (editor.getTextPane() != null) {
433 editor.getTextPane().repaint();
439 * Launch SciNotes with an empty file
441 * This method *must not* be called on the EDT thread.
443 public static void scinotes() {
444 ScilabLexer.update();
446 SwingUtilities.invokeAndWait(new Runnable() {
451 // Open an empty file if no tabs were opened at launch.
452 if (editor.getTabPane().getTabCount() == 0) {
453 editor.openFile(null, 0, null);
457 } catch (InterruptedException e) {
458 Logger.getLogger(SciNotes.class.getName()).severe(e.toString());
459 throw new RuntimeException(e);
460 } catch (InvocationTargetException e) {
461 Logger.getLogger(SciNotes.class.getName()).severe(e.toString());
463 throw new RuntimeException(e);
468 * Launch SciNotes with a file name to open.
471 * the name of the file to open
473 * This method *must not* be called on the EDT thread.
475 public static void scinotes(final String filePath) {
476 ScilabLexer.update();
478 SwingUtilities.invokeAndWait(new Runnable() {
483 editor.openFile(filePath, 0, null);
486 } catch (InterruptedException e) {
487 Logger.getLogger(SciNotes.class.getName()).severe(e.toString());
488 throw new RuntimeException(e);
489 } catch (InvocationTargetException e) {
491 Logger.getLogger(SciNotes.class.getName()).severe(e.toString());
492 throw new RuntimeException(e);
497 * Launch SciNotes with a file name to open and a line to highlight.
500 * the name of the file to open
502 * the line to highlight
504 * This method *must not* be called on the EDT thread.
506 public static void scinotes(final String filePath, final int lineNumber, final String functionName) {
507 ScilabLexer.update();
509 SwingUtilities.invokeAndWait(new Runnable() {
514 editor.openFile(filePath, lineNumber, functionName);
517 } catch (InterruptedException e) {
518 Logger.getLogger(SciNotes.class.getName()).severe(e.toString());
519 throw new RuntimeException(e);
520 } catch (InvocationTargetException e) {
522 Logger.getLogger(SciNotes.class.getName()).severe(e.toString());
523 throw new RuntimeException(e);
528 * Launch SciNotes with a file name to open and a line to highlight.
531 * the name of the file to open
535 * This method *must not* be called on the EDT thread.
537 public static void scinotes(final String filePath, final String[] options) throws Exception {
538 boolean hasAction = false;
539 if (options != null && options.length != 0) {
541 hasAction = executeAction(filePath, options);
542 } catch (FileNotFoundException e) {
543 throw new Exception(String.format(SciNotesMessages.INVALID_FILE, filePath));
544 } catch (IOException e) {
545 throw new Exception(String.format(SciNotesMessages.IO_EXCEPTION, e.getLocalizedMessage()));
553 ScilabLexer.update();
555 SwingUtilities.invokeAndWait(new Runnable() {
560 if (options != null && options.length != 0) {
561 editor.openFile(filePath, 0, options[0]);
563 editor.openFile(filePath, 0, "");
567 } catch (InterruptedException e) {
568 Logger.getLogger(SciNotes.class.getName()).severe(e.toString());
569 throw new RuntimeException(e);
570 } catch (InvocationTargetException e) {
572 Logger.getLogger(SciNotes.class.getName()).severe(e.toString());
573 throw new RuntimeException(e);
578 * Launch SciNotes with a provided text (from the help for example).
581 * the text which should be modified
583 public static void scinotesWithText(String text) {
584 ScilabLexer.update();
586 ScilabEditorPane theTextPane;
587 if (editor.getTabPane().getTabCount() != 0 && editor.getTextPane(0) != null) {
588 String name = editor.getTextPane(0).getName();
590 theTextPane = editor.getTextPane(0);
592 theTextPane = editor.addEmptyTab();
595 theTextPane = editor.addEmptyTab();
598 ScilabDocument styleDocument = (ScilabDocument) theTextPane.getDocument();
600 editor.getEditorKit().read(new StringReader(text), styleDocument, theTextPane.getCaretPosition());
601 } catch (IOException e) {
602 System.err.println(SciNotesMessages.ERROR_WITH_STRING);
603 } catch (BadLocationException e) {
604 System.err.println(SciNotesMessages.ERROR_WITH_STRING);
609 * Clone the current tab and if b is true close the tab
612 * the editor where the tab is
616 public static void cloneAndCloseCurrentTab(SciNotes ed, boolean b) {
617 ScilabDocument cdoc = (ScilabDocument) ed.getTextPane().getDocument();
618 String title = ed.getTabPane().getScilabTitleAt(ed.getTabPane().getSelectedIndex());
619 ScilabEditorPane currentSep = ed.getTextPane();
620 String winTitle = ed.getTitle();
623 ed.closeTabAtWithoutConfirmation(ed.getTabPane().getSelectedIndex());
624 if (ed.getTabPane().getTabCount() == 0) {
625 ClosingOperationsManager.startClosingOperationWithoutSave((SwingScilabDockablePanel) ed);
626 ConfigSciNotesManager.removeEditorUUID(ed.uuid.toString());
632 scinotesWithText(cdoc.getText());
633 ScilabEditorPane sep = editor.getTextPane();
635 if (currentSep.getName() != null) {
637 editor.setTitle(winTitle);
640 currentSep.copyProps(sep);
641 ScilabDocument sdoc = (ScilabDocument) sep.getDocument();
642 sdoc.setContentModified(cdoc.isContentModified());
643 sdoc.getUndoManager().discardAllEdits();
644 sep.setCaretPosition(0);
645 editor.enableUndoButton(false);
646 editor.enableRedoButton(false);
647 ConfigSciNotesManager.saveToOpenFiles(sep.getName(), editor, editor.getTextPane());
649 editor.activeRestoreTab();
653 * Clone the current tab and if b is true close the tab
656 * the editor where the tab is
660 public static void cloneAndCloseCurrentTab(SciNotes ed, boolean b, int x, int y) {
661 cloneAndCloseCurrentTab(ed, b);
662 editor.getSwingParentWindow().setLocation(x, y);
666 * Execute after when the restoration is finished
669 public void endedRestoration() {
670 if (!SwingUtilities.isEventDispatchThread()) {
672 SwingUtilities.invokeAndWait(new Runnable() {
676 restorePreviousSession();
679 } catch (InterruptedException e) {
680 Logger.getLogger(SciNotes.class.getName()).severe(e.toString());
681 throw new RuntimeException(e);
682 } catch (InvocationTargetException e) {
683 Logger.getLogger(SciNotes.class.getName()).severe(e.toString());
684 throw new RuntimeException(e);
687 restorePreviousSession();
692 * Restore the previous session
694 public void restorePreviousSession() {
696 if (!SciNotesOptions.getSciNotesPreferences().restartOpen || !ConfigSciNotesManager.getRestoreOpenedFiles() || ConfigSciNotesManager.countExistingOpenFiles(getUUID()) == 0) {
697 if (getTabPane().getTabCount() == 0) {
698 openFile(null, 0, null);
701 WindowsConfigurationManager.restorationFinished(this);
707 WindowsConfigurationManager.restorationFinished(SciNotes.this);
711 * Create SciNotes instance
713 * @return the instance
715 public static void launchSciNotes() {
716 if (editor == null) {
717 List<String> uuids = ConfigSciNotesManager.getEditorsUUID();
718 boolean success = false;
719 if (!uuids.isEmpty()) {
720 for (String uuid : uuids) {
721 boolean exists = false;
722 for (SciNotes ed : scinotesList) {
723 if (ed.uuid.toString().equals(uuid)) {
729 success = WindowsConfigurationManager.restoreUUID(uuid);
733 ConfigSciNotesManager.removeEditorUUID(uuid);
738 editor = SciNotesTab.getEditorInstance(null);
739 editor.setParentWindow();
740 ConfigSciNotesManager.saveEditorUUID(editor.getPersistentId());
741 SciNotesGUI.init(editor.getParentWindow(), editor, SCINOTES);
742 WindowsConfigurationManager.unregisterEndedRestoration(editor);
746 if (!editor.restored) {
747 editor.restorePreviousSession();
754 * This method *must* be called on the EDT thread.
757 * the file path (may be null)
759 * the line number (use 0 for unspecified)
761 * the options (may be null)
763 public void openFile(String filePath, int lineNumber, String option) {
764 if (!SwingUtilities.isEventDispatchThread()) {
765 throw new RuntimeException("called outside the EDT thread.");
768 if (filePath == null) {
774 File f = fileToCanonicalFile(new File(filePath));
775 if (!f.getParentFile().exists()) {
776 JOptionPane.showMessageDialog(SciNotes.this, SciNotesMessages.OPEN_ERROR);
782 if (f.isDirectory()) { /* Bug 5131 */
783 ConfigManager.saveLastOpenedDirectory(f.getPath());
790 if (option == null || option.length() == 0 || "readonly".equals(option.toLowerCase())) {
791 getTextPane().scrollTextToLineNumber(lineNumber, true);
793 getTextPane().scrollTextToLineNumberInWhereami(lineNumber, option, true);
795 if ((option != null && "readonly".equals(option.toLowerCase()))) {
796 getTextPane().setReadOnly(true);
797 getInfoBar().setText(getTextPane().getInfoBarText());
798 ConfigSciNotesManager.removeFromOpenFiles(this, getTextPane());
800 ConfigSciNotesManager.saveToRecentOpenedFiles(filePath);
801 RecentFileAction.updateRecentOpenedFilesMenu(this);
807 public void activeRestoreTab() {
808 if (getTextPane(0) == null) {
809 tabPane.setSelectedIndex(0);
810 getInfoBar().setText("");
815 * Close SciNotes instance including all tabs.
817 public void closeSciNotes() {
818 for (int i = 0; i < getTabPane().getTabCount(); i++) {
819 ScilabEditorPane textPaneAt = getTextPane(i);
820 if (textPaneAt != null) {
821 textPaneAt.destroy();
826 IncrementalSearchAction.close(this);
827 RecentFileAction.close(this);
828 OpenSourceFileOnKeywordAction.closeOpenSourceWindow();
829 SearchWordInFilesAction.closeWindow();
832 * setProtectOpenFileList(true); while (getTabPane().getTabCount() > 0)
833 * { closeTabAt(0, true); } setProtectOpenFileList(false);
836 scinotesList.remove(this);
837 if (scinotesList.size() == 0) {
838 SciNotesAutosave.stopAutosave();
839 ScilabEditorPane.clean();
840 RegisterFavoriteDirsAction.close();
841 OpenSourceFileOnKeywordAction.close();
842 EncodingAction.close();
843 EndOfLineAction.close();
844 HelpOnTypingManager.close();
848 SciNotesGUI.clean(this);
850 ConfigSciNotesManager.resetDocument();
853 public boolean canClose() {
854 int numberOfTab = getTabPane().getTabCount();
855 setProtectOpenFileList(true);
857 for (int i = 0; i < numberOfTab; i++) {
858 boolean ans = checkToCloseTabAt(i);
860 setProtectOpenFileList(false);
864 setProtectOpenFileList(false);
869 public String askForClosing() {
870 int numberOfTab = getTabPane().getTabCount();
871 boolean isContentModified = false;
872 for (int i = 0; i < getTabPane().getTabCount(); i++) {
873 ScilabEditorPane textPaneAt = getTextPane(i);
874 if (textPaneAt != null && ((ScilabDocument) textPaneAt.getDocument()).isContentModified()) {
875 isContentModified = true;
880 if (isContentModified) {
887 public static void closeEditor(SciNotes ed) {
888 if (scinotesList.size() > 1) {
889 ClosingOperationsManager.startClosingOperationWithoutSave((SwingScilabDockablePanel) ed);
891 ClosingOperationsManager.startClosingOperation((SwingScilabDockablePanel) ed);
896 * Close a tab using its index.
899 * the index of the tab
900 * @return if the tab has been really closed
902 public boolean closeTabAtWithoutConfirmation(int indexTab) {
903 return closeTabAt(indexTab, false, false);
907 * Close a tab using its index.
910 * the index of the tab
911 * @return if the tab has been really closed
913 public boolean closeTabAt(int indexTab) {
914 return closeTabAt(indexTab, false, true);
918 * Close a tab using its index.
921 * the index of the tab
923 * if true, save dialog confirmation cannot be canceled
924 * @return if the tab has been really closed
926 public boolean closeTabAt(int indexTab, boolean scilabClose, boolean confirmation) {
927 ScilabEditorPane textPaneAt = getTextPane(indexTab);
930 * Test for modification added after bug 5103 fix: do not ask the user
931 * for an Untitled not-modified file saving when closing SciNotes
933 if (confirmation && ((ScilabDocument) textPaneAt.getDocument()).isContentModified()) {
934 if (!save(indexTab, false, scilabClose)) {
941 if (textPaneAt.getName() == null) {
942 String closedTabName = tabPane.getScilabTitleAt(indexTab);
943 String closedTabNameIndex = closedTabName.substring(closedTabName.length() - 1, closedTabName.length());
944 tabList.remove(Integer.valueOf(closedTabNameIndex));
945 closedTabList.add(Integer.valueOf(closedTabNameIndex));
948 // correction for bug 5404, closing the last tabPane generate an
950 // that's why we need to remove ChangeListeners before closing
951 if ((indexTab == 0) && (getTabPane().getTabCount() == 1)) {
952 for (int i = 0; i < tabPane.getChangeListeners().length; i++) {
953 tabPane.removeChangeListener(tabPane.getChangeListeners()[i]);
957 // Remove the filename associated with the tab from the list of open
959 if (!protectOpenFileList) {
960 ConfigSciNotesManager.removeFromOpenFiles(this, textPaneAt);
963 if (navigator != null) {
964 navigator.removePane(textPaneAt);
967 tabPane.remove(indexTab);
969 textPaneAt = getTextPane();
970 if (textPaneAt != null) {
971 if (navigator != null) {
972 navigator.update((ScilabDocument) textPaneAt.getDocument());
978 public boolean checkToCloseTabAt(int index) {
979 ScilabEditorPane textPaneAt = getTextPane(index);
982 * Test for modification added after bug 5103 fix: do not ask the user
983 * for an Untitled not-modified file saving when closing SciNotes
985 if (textPaneAt != null && ((ScilabDocument) textPaneAt.getDocument()).isContentModified()) {
986 if (!save(index, false, false)) {
995 * Set or unset protection of the currently open file list. Call before
996 * closing tabs if the tabs should be restored when scinotes restarts.
999 * Enables protection of the open file list if true.
1001 public void setProtectOpenFileList(boolean protect) {
1002 protectOpenFileList = protect;
1006 * Determines if this editor is the only editor instance.
1008 * @return true if this is the last editor instance.
1010 public boolean isOnlyInstance() {
1011 if (scinotesList.size() > 1) {
1019 * @return the list of the different openened editors
1021 public static List<SciNotes> getSciNotesList() {
1022 return scinotesList;
1029 * the textPane index containing the file contents
1030 * @return execution status
1032 public boolean save(int indexTab) {
1033 return save(indexTab, false, false);
1040 * the textPane index containing the file contents
1042 * : force save without confirmation dialog ( SaveAction )
1043 * @return execution status
1045 public boolean save(int indexTab, boolean force) {
1046 return save(indexTab, force, false);
1053 * the textPane index containing the file contents
1055 * : force save without confirmation dialog ( SaveAction )
1056 * @param scilabClose
1057 * : if true, save dialog confirmation cannot be canceled
1058 * @return execution status
1060 public boolean save(int indexTab, boolean force, boolean scilabClose) {
1061 ScilabEditorPane textPaneAt = getTextPane(indexTab);
1062 getTabPane().setSelectedIndex(indexTab);
1064 // if the file ( empty, new or loaded ) is not modified, exit save
1065 // process and return true
1066 if (!textPaneAt.checkExternalModif() && !((ScilabDocument) textPaneAt.getDocument()).isContentModified() && (textPaneAt.getName() != null)) {
1076 AnswerOption answer;
1078 answer = ScilabModalDialog.show(this, String.format(SciNotesMessages.MODIFIED, textPaneAt.getShortName()), SciNotesMessages.SCILAB_EDITOR,
1079 IconType.QUESTION_ICON, ButtonType.YES_NO);
1081 answer = ScilabModalDialog.show(this, String.format(SciNotesMessages.MODIFIED, textPaneAt.getShortName()), SciNotesMessages.SCILAB_EDITOR,
1082 IconType.QUESTION_ICON, ButtonType.YES_NO_CANCEL);
1086 case YES_OPTION: // Yes, continue
1088 case NO_OPTION:// No, exit and returns true
1090 case CANCEL_OPTION: // Cancel, exit and return false
1097 String fileToSave = textPaneAt.getName();
1098 if (fileToSave == null) {
1099 // need a filename, call chooseFileToSave
1100 fileToSave = chooseFileToSave(SciNotesMessages.SAVE);
1102 // check if the file has been modified by external software
1103 fileToSave = checkExternalModification(fileToSave);
1106 if (fileToSave == null || fileToSave.length() == 0) {
1108 * Bug 5189: The user cancels ==> do not want an error
1113 File newSavedFile = new File(fileToSave);
1115 if (!SaveFile.doSave(textPaneAt, indexTab, newSavedFile, editorKit)) {
1119 ScilabDocument styledDocument = (ScilabDocument) textPaneAt.getDocument();
1120 styledDocument.setContentModified(false);
1122 textPaneAt.setLastModified(newSavedFile.lastModified());
1124 if (textPaneAt.getName() == null) {
1125 String name = getTabPane().getScilabTitleAt(indexTab);
1126 String index = name.substring(name.length() - 1, name.length());
1127 tabList.remove(Integer.valueOf(index));
1128 closedTabList.add(Integer.valueOf(index));
1131 textPaneAt.setName(fileToSave);
1132 getTabPane().setTitleAt(indexTab, newSavedFile.getName());
1134 setTitle(textPaneAt.getTitle());
1135 ConfigSciNotesManager.saveToOpenFiles(fileToSave, this, textPaneAt);
1141 * Overwrite the file at a given index.
1144 * the textPane index containing the file contents
1145 * @return execution status
1147 public boolean overwrite(int indexTab) {
1148 ScilabEditorPane textPaneAt = getTextPane(indexTab);
1149 String fileToSave = textPaneAt.getName();
1150 if (fileToSave == null) {
1154 File newSavedFile = new File(fileToSave);
1155 if (!newSavedFile.exists()) {
1159 if (!SaveFile.doSave(textPaneAt, indexTab, newSavedFile, editorKit)) {
1163 ScilabDocument styledDocument = (ScilabDocument) textPaneAt.getDocument();
1164 styledDocument.setContentModified(false);
1166 getTabPane().setTitleAt(getTabPane().getSelectedIndex(), newSavedFile.getName());
1168 // Get current file path for Execute file into Scilab
1169 getTextPane().setLastModified(newSavedFile.lastModified());
1171 textPaneAt.setName(fileToSave);
1172 setTitle(textPaneAt.getTitle());
1178 * Check for external modification. If it is the case, propose to select a
1182 * The name of the file
1183 * @return the filename where to save
1185 public String checkExternalModification(String filename) {
1186 File newSavedFile = new File(filename);
1187 if (newSavedFile.lastModified() > getTextPane().getLastModified()) {
1188 if (ScilabModalDialog.show(this, String.format(SciNotesMessages.EXTERNAL_MODIFICATION, newSavedFile.getPath()),
1189 SciNotesMessages.REPLACE_FILE_TITLE, IconType.QUESTION_ICON, ButtonType.YES_NO) == AnswerOption.NO_OPTION) {
1190 return chooseFileToSave(SciNotesMessages.SAVE);
1197 * Return through a file selector the name of the selected file.
1200 * the title for JFileChooser
1201 * @return the file picked up by the user
1203 public String chooseFileToSave(String title) {
1204 return chooseFileToSave(title, null);
1208 * Return through a file selector the name of the selected file.
1211 * the title for JFileChooser
1213 * the path where to open the filechooser
1214 * @return the file picked up by the user
1216 public String chooseFileToSave(String title, String path) {
1217 String extension = new String();
1219 String initialDirectoryPath = path;
1220 if (initialDirectoryPath == null) {
1221 initialDirectoryPath = getTextPane().getName();
1223 if (initialDirectoryPath == null) {
1225 initialDirectoryPath = CommonFileUtils.getCWD();
1228 initialDirectoryPath = ConfigManager.getLastOpenedDirectory();
1232 SciFileFilter sceFilter = new SciFileFilter(ALL_SCE_FILES, null, 0);
1233 SciFileFilter sciFilter = new SciFileFilter(ALL_SCI_FILES, null, 1);
1234 SciFileFilter scxFilter = new SciFileFilter(ALL_SCX_FILES, null, 2);
1235 SciFileFilter tstFilter = new SciFileFilter(ALL_TST_FILES, null, 3);
1236 SciFileFilter startFilter = new SciFileFilter(ALL_START_FILES, null, 4);
1237 SciFileFilter quitFilter = new SciFileFilter(ALL_QUIT_FILES, null, 5);
1238 SciFileFilter demFilter = new SciFileFilter(ALL_DEM_FILES, null, 6);
1239 SciFileFilter allFilter = new SciFileFilter(ALL_FILES, null, 7);
1240 SciFileFilter allScilabFilter = new SciFileFilter(ALL_SCILAB, null, 8);
1242 SwingScilabFileChooser fileChooser = ((SwingScilabFileChooser) ScilabFileChooser.createFileChooser().getAsSimpleFileChooser());
1244 fileChooser.setAcceptAllFileFilterUsed(false);
1245 fileChooser.setInitialDirectory(initialDirectoryPath);
1246 fileChooser.setUiDialogType(Juigetfile.SAVE_DIALOG);
1248 // order is also important here
1249 fileChooser.addChoosableFileFilter(sceFilter);
1250 fileChooser.addChoosableFileFilter(sciFilter);
1251 fileChooser.addChoosableFileFilter(scxFilter);
1252 fileChooser.addChoosableFileFilter(tstFilter);
1253 fileChooser.addChoosableFileFilter(startFilter);
1254 fileChooser.addChoosableFileFilter(quitFilter);
1255 fileChooser.addChoosableFileFilter(demFilter);
1256 fileChooser.addChoosableFileFilter(allFilter);
1257 fileChooser.addChoosableFileFilter(allScilabFilter);
1259 // select default file type
1260 fileChooser.setFileFilter(sceFilter);
1261 fileChooser.setTitle(title);
1263 String name = getTextPane().getName();
1265 name = ((ScilabDocument) getTextPane().getDocument()).getFirstFunctionName();
1267 name += SCI_EXTENSION;
1272 fileChooser.setSelectedFile(new File(name));
1275 int retval = fileChooser.showSaveDialog(this);
1277 if (retval == JFileChooser.APPROVE_OPTION) {
1278 File f = fileToCanonicalFile(fileChooser.getSelectedFile());
1279 initialDirectoryPath = f.getPath();
1281 if (ScilabModalDialog.show(this, SciNotesMessages.REPLACE_FILE_TITLE, SciNotesMessages.FILE_ALREADY_EXIST, IconType.QUESTION_ICON,
1282 ButtonType.YES_NO) == AnswerOption.NO_OPTION) {
1283 return chooseFileToSave(SciNotesMessages.SAVE);
1287 /* we test if the file has already a scilab extension */
1288 boolean hasNoExtension = true;
1290 // if the file name is like this : any character , a dot , then
1291 // 2,3or 4 characters, then
1292 // we consider the file has already an extension
1293 // we previously only check for .sci and .sce extension, but what if
1294 // the user open a txt file
1295 String fileName = f.getName();
1296 if (fileName.lastIndexOf(DOT) != -1) {
1297 if (fileName.substring(fileName.lastIndexOf(DOT), fileName.length()).length() >= 2
1298 && fileName.substring(fileName.lastIndexOf(DOT), fileName.length()).length() <= 4) {
1299 hasNoExtension = false;
1303 /* if no extension , we add it */
1304 if (hasNoExtension) {
1305 if (fileChooser.getFileFilter() == sciFilter) {
1306 extension = SCI_EXTENSION;
1307 } else if (fileChooser.getFileFilter() == sceFilter) {
1308 extension = SCE_EXTENSION;
1309 } else if (fileChooser.getFileFilter() == scxFilter) {
1310 extension = SCE_EXTENSION;
1311 } else if (fileChooser.getFileFilter() == tstFilter) {
1312 extension = TST_EXTENSION;
1313 } else if (fileChooser.getFileFilter() == startFilter) {
1314 extension = START_EXTENSION;
1315 } else if (fileChooser.getFileFilter() == quitFilter) {
1316 extension = QUIT_EXTENSION;
1317 } else if (fileChooser.getFileFilter() == demFilter) {
1318 extension = DEM_EXTENSION;
1322 return f.getPath() + extension;
1325 if (initialDirectoryPath != null) {
1326 ConfigManager.saveLastOpenedDirectory(initialDirectoryPath);
1330 } else if (retval == JFileChooser.CANCEL_OPTION) {
1341 * the initial path where the filechooser will be open
1342 * @return execution status
1344 public boolean saveAs(String path) {
1345 String filename = chooseFileToSave(SciNotesMessages.SAVE_AS, path);
1346 if (filename == null || filename.length() == 0) {
1350 File f = new File(filename);
1351 ScilabDocument styledDocument = (ScilabDocument) getTextPane().getDocument();
1353 if (!SaveFile.doSave(getTextPane(), getTabPane().getSelectedIndex(), f, editorKit)) {
1357 if (getTextPane().getName() == null) {
1358 String name = getTabPane().getScilabTitleAt(getTabPane().getSelectedIndex());
1359 String index = name.substring(name.length() - 1, name.length());
1360 tabList.remove(Integer.valueOf(index));
1361 closedTabList.add(Integer.valueOf(index));
1365 ConfigManager.saveLastOpenedDirectory(f.getPath());
1366 ConfigSciNotesManager.saveToRecentOpenedFiles(f.getPath());
1367 ConfigSciNotesManager.renameOpenFilesItem(f.getPath(), this, getTextPane());
1368 getTextPane().setName(f.getPath());
1369 getTabPane().setTitleAt(getTabPane().getSelectedIndex(), f.getName());
1370 setTitle(getTextPane().getTitle());
1372 RecentFileAction.updateRecentOpenedFilesMenu(this);
1374 if (navigator != null) {
1375 navigator.update(styledDocument);
1378 styledDocument.setContentModified(false);
1379 getTextPane().setLastModified(f.lastModified());
1380 getTextPane().setReadOnly(false);
1381 getInfoBar().setText(getTextPane().getInfoBarText());
1387 * Create a new tab in SciNotes.
1390 * the title of the tab
1391 * @return the text component inside the tab
1393 public ScilabEditorPane addTab(String title) {
1394 return addTab(title, Integer.MAX_VALUE);
1398 * Create a new tab in SciNotes.
1401 * the title of the tab
1403 * the index where to put the new tab
1404 * @return the text component inside the tab
1406 public ScilabEditorPane addTab(String title, int index) {
1407 return addTab(title, index, 0);
1411 * Create a new tab in SciNotes.
1414 * the title of the tab
1416 * the index where to put the new tab
1417 * @return the text component inside the tab
1419 public ScilabEditorPane addTab(String title, int index, int caretPos) {
1420 ScilabEditorPane sep = new ScilabEditorPane(this);
1422 int ind = Math.min(Math.max(0, index), tabPane.getTabCount());
1423 tabPane.insertTab(title, null, sep.getEditorComponent(), "", ind);
1424 tabPane.setSelectedIndex(ind);
1427 getInfoBar().setText(sep.getInfoBarText());
1433 public void addRestoreTab() {
1435 JPanel panel = RestoreOpenedFilesAction.getTab(this, getUUID().toString());
1436 if (panel != null) {
1437 tabPane.addTab("Restore", panel);
1439 } catch (Exception e) {
1440 System.err.println(e);
1450 public void initPane(ScilabEditorPane pane) {
1451 initPane(pane, !SciNotesOptions.getSciNotesDisplay().wrapLines);
1455 * Init the EditorPane.
1460 * true for a plain view or false for a wrapped view
1462 public void initPane(ScilabEditorPane pane, boolean plain) {
1464 ScilabEditorKit kit = new ScilabEditorKit(plain);
1465 pane.setEditorKit(kit);
1467 // Panel of line number for the text pane
1468 pane.getXln().setWhereamiLineNumbering(SciNotesOptions.getSciNotesDisplay().showLineNumbers, SciNotesOptions.getSciNotesDisplay().whereami);
1469 activateHelpOnTyping(pane);
1471 pane.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
1472 pane.setFont(kit.getStylePreferences().tokenFonts[0]);
1474 pane.setBackground(SciNotesOptions.getSciNotesDisplay().backgroundColor);
1475 pane.setCaretColor(SciNotesOptions.getSciNotesDisplay().caretColor);
1477 pane.setFocusable(true);
1478 pane.setRequestFocusEnabled(true);
1479 pane.setDragEnabled(true); /* Bug 5497 */
1481 DropFilesListener dndTarget = new DropFilesListener(pane);
1483 pane.setComponentPopupMenu(SciNotesGUI.generateRightClickPopup(this));
1487 * Init the EditorPane with the InputMap found in ConfigSciNotesManager
1492 public void initInputMap(ScilabEditorPane pane) {
1493 setKeyStrokeAction(pane, this);
1494 LineBeautifierAction.putInInputMap(pane);
1495 InsertOverwriteAction.putInInputMap(pane);
1499 * Split the EditorPane
1502 * true for a vertical split
1504 public void splitTab(boolean vertical) {
1505 ScilabEditorPane pane = getTextPane();
1506 Component bottom = pane.getEditorComponent().getBottom();
1507 int state = pane.getXln().getState();
1508 ScilabEditorPane leftPane = new ScilabEditorPane(editor);
1509 ScilabEditorPane rightPane = new ScilabEditorPane(editor);
1510 if (navigator != null) {
1511 navigator.changePaneOnSplit(pane, leftPane);
1513 initPane(leftPane, true);
1514 initPane(rightPane, true);
1515 leftPane.setOtherPaneInSplit(rightPane);
1516 rightPane.setOtherPaneInSplit(leftPane);
1517 pane.copyProps(leftPane);
1518 pane.copyProps(rightPane);
1519 ScilabDocument doc = (ScilabDocument) pane.getDocument();
1520 leftPane.setDocument(doc);
1521 rightPane.setDocument(doc);
1522 leftPane.setCaretPosition(0);
1523 rightPane.setCaretPosition(0);
1526 split = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
1528 split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
1530 leftPane.getXln().setWhereamiLineNumbering(state);
1531 rightPane.getXln().setWhereamiLineNumbering(state);
1532 split.setLeftComponent(leftPane.getScrollPane());
1533 split.setRightComponent(rightPane.getScrollPane());
1534 split.setResizeWeight(0.5);
1535 rightPane.setEditorComponent(leftPane.getEditorComponent());
1536 leftPane.setSplitPane(split);
1537 rightPane.setSplitPane(split);
1539 activateHelpOnTyping(leftPane);
1540 activateHelpOnTyping(rightPane);
1541 initInputMap(leftPane);
1542 initInputMap(rightPane);
1543 if (doc.getBinary()) {
1544 leftPane.setBinary(true);
1545 rightPane.setBinary(true);
1547 getInfoBar().setText(leftPane.getInfoBarText());
1549 tabPane.setComponentAt(tabPane.getSelectedIndex(), leftPane.getEditorComponent());
1550 leftPane.getEditorComponent().insertBottomComponent(bottom);
1551 leftPane.requestFocus();
1557 public void removeSplit() {
1558 if (((EditorComponent) tabPane.getSelectedComponent()).isSplited()) {
1559 ScilabEditorPane pane = new ScilabEditorPane(editor);
1560 ScilabEditorPane textpane = getTextPane();
1561 Component bottom = textpane.getEditorComponent().getBottom();
1562 if (navigator != null) {
1563 navigator.changePaneOnSplit(textpane, pane);
1566 textpane.setOtherPaneInSplit(null);
1567 textpane.copyProps(pane);
1568 ScilabDocument doc = (ScilabDocument) textpane.getDocument();
1569 pane.setDocument(doc);
1570 pane.setCaretPosition(0);
1571 activateHelpOnTyping(pane);
1572 tabPane.setComponentAt(tabPane.getSelectedIndex(), pane.getEditorComponent());
1574 if (doc.getBinary()) {
1575 pane.setBinary(true);
1577 getInfoBar().setText(pane.getInfoBarText());
1579 pane.getEditorComponent().insertBottomComponent(bottom);
1580 pane.requestFocus();
1585 * Create an empty tab inside SciNotes.
1587 * @return the text component inside the tab
1589 public ScilabEditorPane addEmptyTab() {
1590 ScilabEditorPane sep;
1591 int n = getNumberForEmptyTab();
1592 sep = addTab(SciNotesMessages.UNTITLED + n);
1593 sep.setShortName(SciNotesMessages.UNTITLED + n);
1594 sep.setTitle(SciNotesMessages.UNTITLED + n);
1595 setTitle(sep.getTitle());
1596 SciNotesOptions.Header header = SciNotesOptions.getSciNotesHeader();
1597 if (header.header != null) {
1598 ((ScilabDocument) sep.getDocument()).disableUndoManager();
1600 sep.getDocument().insertString(0, header.header, null);
1601 } catch (BadLocationException e) { }
1602 sep.init(header.header.length());
1603 ((ScilabDocument) sep.getDocument()).setContentModified(false);
1604 ((ScilabDocument) sep.getDocument()).enableUndoManager();
1611 * Get an integer used for untitled document
1613 public int getNumberForEmptyTab() {
1614 if (closedTabList.size() > 0) {
1615 Integer n = Collections.min(closedTabList);
1616 closedTabList.remove(n);
1617 return n.intValue();
1620 tabList.add(Integer.valueOf(numberOfUntitled));
1621 return numberOfUntitled;
1626 * Add or remove '*' prefix in current tab tile according to
1627 * isContentModified().
1629 public void updateTabTitle() {
1630 StringBuffer newTitle = new StringBuffer();
1631 ScilabEditorPane currentTextPane = getTextPane();
1632 if (((ScilabDocument) currentTextPane.getDocument()).isContentModified()) {
1633 newTitle.append('*');
1636 String textPaneName = currentTextPane.getName();
1638 File f = new File(textPaneName);
1639 newTitle.append(f.getName());
1640 } catch (NullPointerException e) { // not a file name, no path prefix to
1641 // remove, but maybe a '*'
1642 textPaneName = getTabPane().getScilabTitleAt(getTabPane().getSelectedIndex());
1643 if (textPaneName.charAt(0) == '*') {
1644 newTitle.append(textPaneName.substring(1, textPaneName.length()));
1646 newTitle.append(textPaneName);
1649 getTabPane().setTitleAt(getTabPane().getSelectedIndex(), newTitle.toString());
1654 * the UndoButton used in this editor
1656 public void setUndoButton(JButton button) {
1657 undoButton = button;
1658 enableUndoButton(false);
1663 * true to enable the button
1665 public void enableUndoButton(boolean b) {
1666 if (undoButton != null) {
1667 undoButton.setEnabled(b);
1672 * Undo last modification.
1674 public void undo() {
1675 ScilabDocument doc = (ScilabDocument) getTextPane().getDocument();
1676 synchronized (doc) {
1677 doc.getUndoManager().undo();
1683 * the RedoButton used in this editor
1685 public void setRedoButton(JButton button) {
1686 redoButton = button;
1687 enableRedoButton(false);
1692 * true to enable the button
1694 public void enableRedoButton(boolean b) {
1695 if (redoButton != null) {
1696 redoButton.setEnabled(b);
1701 * Redo last modification.
1703 public void redo() {
1704 ScilabDocument doc = (ScilabDocument) getTextPane().getDocument();
1705 synchronized (doc) {
1706 doc.getUndoManager().redo();
1711 * Reload a file inside SciNotes.
1716 public void reload(int index) {
1717 ScilabEditorPane textPaneAt = getTextPane(index);
1718 if (textPaneAt != null && textPaneAt.getName() != null) {
1719 if ((index == 0) && (getTabPane().getTabCount() == 1)) {
1720 for (int j = 0; j < tabPane.getChangeListeners().length; j++) {
1721 tabPane.removeChangeListener(tabPane.getChangeListeners()[j]);
1724 ConfigSciNotesManager.removeFromOpenFiles(this, textPaneAt);
1725 tabPane.remove(index);
1726 File f = new File(textPaneAt.getName());
1736 * Load a file inside SciNotes.
1741 public void readFile(File f) {
1742 /** Is this file already opened */
1743 boolean alreadyOpened = false;
1744 for (int i = 0; i < tabPane.getTabCount(); i++) {
1745 ScilabEditorPane textPaneAt = getTextPane(i);
1746 if (f.getAbsolutePath().equals(textPaneAt.getName())) {
1747 /* File is already opnened */
1748 tabPane.setSelectedIndex(i);
1749 alreadyOpened = true;
1754 if (!alreadyOpened) {
1764 * Load a file inside SciNotes.
1769 public void readFileAndWait(File file) {
1770 /** Is this file already opened */
1772 boolean alreadyOpened = false;
1774 for (int i = 0; i < tabPane.getTabCount(); i++) {
1775 ScilabEditorPane textPaneAt = getTextPane(i);
1776 if (textPaneAt != null && f.getAbsolutePath().equals(textPaneAt.getName())) {
1777 /* File is already opened */
1778 tabPane.setSelectedIndex(i);
1779 if (f.lastModified() > textPaneAt.getLastModified()) {
1782 * Create a new messagebox to know what the user wants to do
1783 * if the file has been modified outside SciNotes
1785 MessageBox messageBox = ScilabMessageBox.createMessageBox();
1786 messageBox.setTitle(SciNotesMessages.REPLACE_FILE_TITLE);
1787 messageBox.setMessage(String.format(SciNotesMessages.EXTERNAL_MODIFICATION, textPaneAt.getName()));
1789 String[] labels = new String[] { SciNotesMessages.RELOAD, SciNotesMessages.OVERWRITE, SciNotesMessages.IGNORE };
1790 messageBox.setButtonsLabels(labels);
1792 messageBox.setIcon("question"); // Question icon
1794 messageBox.setParentForLocation(this); // Centered on
1798 messageBox.displayAndWait(); // Waits for a user action
1800 switch (messageBox.getSelectedButton()) {
1802 if ((i == 0) && (getTabPane().getTabCount() == 1)) {
1803 for (int j = 0; j < tabPane.getChangeListeners().length; j++) {
1804 tabPane.removeChangeListener(tabPane.getChangeListeners()[j]);
1808 f = new File(textPaneAt.getName());
1811 case 2: // Overwrite 2
1813 alreadyOpened = true;
1816 alreadyOpened = true;
1819 alreadyOpened = true;
1825 if (!alreadyOpened) {
1835 * Get current text component. If the window is splitted, then return the
1836 * focused text component.
1838 * @return the text component
1840 public ScilabEditorPane getTextPane() {
1842 if (tabPane.getSelectedComponent() instanceof EditorComponent) {
1843 EditorComponent c = (EditorComponent) tabPane.getSelectedComponent();
1844 ScilabEditorPane pane = c.getEditorPane();
1845 if (ScilabEditorPane.getFocusedPane() == pane.getOtherPaneInSplit()) {
1846 return pane.getOtherPaneInSplit();
1851 } catch (NullPointerException e) {
1852 } catch (ArrayIndexOutOfBoundsException e) {
1859 * Get text component at index.
1862 * the index of the textpane
1863 * @return the text component
1865 public ScilabEditorPane getTextPane(int index) {
1867 if (tabPane.getComponentAt(index) instanceof EditorComponent) {
1868 EditorComponent c = (EditorComponent) tabPane.getComponentAt(index);
1869 ScilabEditorPane pane = c.getEditorPane();
1870 if (ScilabEditorPane.getFocusedPane() == pane.getOtherPaneInSplit()) {
1871 return pane.getOtherPaneInSplit();
1876 } catch (NullPointerException e) {
1877 } catch (ArrayIndexOutOfBoundsException e) {
1884 * Set the keystroke actions
1886 public static void setAllMenus() {
1887 for (SciNotes ed : scinotesList) {
1888 SciNotesGUI.reinitMenus(ed);
1889 int n = ed.getTabPane().getTabCount();
1890 for (int i = 0; i < n; i++) {
1891 ScilabEditorPane sep = ed.getTextPane(i);
1892 sep.setComponentPopupMenu(SciNotesGUI.generateRightClickPopup(ed));
1893 if (sep.getOtherPaneInSplit() != null) {
1894 sep.getOtherPaneInSplit().setComponentPopupMenu(SciNotesGUI.generateRightClickPopup(ed));
1901 * Set the keystroke actions
1903 public static void setKeyStrokeActions() {
1904 for (SciNotes ed : scinotesList) {
1905 int n = ed.getTabPane().getTabCount();
1906 for (int i = 0; i < n; i++) {
1907 ScilabEditorPane sep = ed.getTextPane(i);
1908 ed.initInputMap(sep);
1909 if (sep.getOtherPaneInSplit() != null) {
1910 ed.initInputMap(sep.getOtherPaneInSplit());
1917 * Enable the whereami-line numbering
1922 public static void setWhereamiLineNumbering() {
1923 for (SciNotes ed : scinotesList) {
1924 int n = ed.getTabPane().getTabCount();
1925 for (int i = 0; i < n; i++) {
1926 ScilabEditorPane sep = ed.getTextPane(i);
1927 sep.getXln().setWhereamiLineNumbering(SciNotesOptions.getSciNotesDisplay().showLineNumbers, SciNotesOptions.getSciNotesDisplay().whereami);
1928 if (sep.getOtherPaneInSplit() != null) {
1929 sep.getOtherPaneInSplit().getXln().setWhereamiLineNumbering(SciNotesOptions.getSciNotesDisplay().showLineNumbers, SciNotesOptions.getSciNotesDisplay().whereami);
1937 * Auto-indent mode management
1940 * true to activate auto-indent mode
1942 public static void setAutoIndent() {
1943 for (SciNotes ed : scinotesList) {
1944 int n = ed.getTabPane().getTabCount();
1945 for (int i = 0; i < n; i++) {
1946 ScilabEditorPane sep = ed.getTextPane(i);
1947 ((ScilabDocument) sep.getDocument()).setAutoIndent(SciNotesOptions.getSciNotesDisplay().automaticIndent);
1948 if (sep.getOtherPaneInSplit() != null) {
1949 ((ScilabDocument) sep.getOtherPaneInSplit().getDocument()).setAutoIndent(SciNotesOptions.getSciNotesDisplay().automaticIndent);
1956 * Horizontal Wrap mode management
1959 * true to activate horizontal wrapping mode
1961 public static void setHorizontalWrap() {
1962 if (SciNotesOptions.getSciNotesDisplay().wrapLines != mustWrapLines) {
1963 mustWrapLines = SciNotesOptions.getSciNotesDisplay().wrapLines;
1964 for (SciNotes ed : scinotesList) {
1965 int n = ed.getTabPane().getTabCount();
1966 for (int i = 0; i < n; i++) {
1967 ScilabEditorPane sep = ed.getTextPane(i);
1968 if (sep.getOtherPaneInSplit() == null) {
1969 ScilabEditorPane pane = new ScilabEditorPane(editor);
1970 ed.initPane(pane, !mustWrapLines);
1971 sep.copyProps(pane);
1972 pane.setDocument(sep.getDocument());
1973 pane.setCaretPosition(sep.getCaretPosition());
1974 pane.getXln().setWhereamiLineNumbering(SciNotesOptions.getSciNotesDisplay().showLineNumbers, SciNotesOptions.getSciNotesDisplay().whereami);
1975 ed.tabPane.setComponentAt(i, pane.getEditorComponent());
1976 SciNotes.activateHelpOnTyping(pane);
1977 ed.initInputMap(pane);
1978 if (((ScilabDocument) sep.getDocument()).getBinary()) {
1979 pane.setBinary(true);
1981 ed.getInfoBar().setText(pane.getInfoBarText());
1989 * Set a line numbering compatible with the whereami function
1990 * @param state 0 for nothing, 1 for normal and 2 for whereami
1994 * Enable the highlighted line in this editor
1999 public static void enableHighlightedLine(boolean b) {
2000 for (SciNotes ed : scinotesList) {
2001 int n = ed.getTabPane().getTabCount();
2002 for (int i = 0; i < n; i++) {
2003 ScilabEditorPane sep = ed.getTextPane(i);
2004 sep.enableHighlightedLine(SciNotesOptions.getSciNotesDisplay().highlightCurrentLine);
2005 if (sep.getOtherPaneInSplit() != null) {
2006 sep.getOtherPaneInSplit().enableHighlightedLine(SciNotesOptions.getSciNotesDisplay().highlightCurrentLine);
2013 * Enable the help on typing in the current textPane
2018 public static void activateHelpOnTyping(ScilabEditorPane pane) {
2019 pane.activateHelpOnTyping();
2023 * Enable the help on typing in this editor
2025 public static void activateHelpOnTyping() {
2026 for (SciNotes ed : scinotesList) {
2027 int n = ed.getTabPane().getTabCount();
2028 for (int i = 0; i < n; i++) {
2029 ScilabEditorPane sep = ed.getTextPane(i);
2030 sep.activateHelpOnTyping();
2031 if (sep.getOtherPaneInSplit() != null) {
2032 sep.getOtherPaneInSplit().activateHelpOnTyping();
2039 * Set the color of the highlighted line in this editor
2044 public static void setDefaultTabulation() {
2045 TabManager.Tabulation tab = new TabManager.Tabulation();
2046 for (SciNotes ed : scinotesList) {
2047 int n = ed.getTabPane().getTabCount();
2048 for (int i = 0; i < n; i++) {
2049 ScilabEditorPane sep = ed.getTextPane(i);
2050 sep.getTabManager().setTabulation(tab);
2051 View view = ((ScilabDocument) sep.getDocument()).getView();
2053 if (view instanceof ScilabView) {
2054 ((ScilabView) view).setTabRepresentation(tab);
2055 ((ScilabView) view).reinitialize();
2057 ((ScilabPlainView) view).setTabRepresentation(tab);
2058 ((ScilabPlainView) view).reinitialize();
2066 * Set the color of the highlighted line in this editor
2071 public static void updatePanes(SciNotesConfiguration.Conf conf) {
2072 for (SciNotes ed : scinotesList) {
2073 int n = ed.getTabPane().getTabCount();
2074 for (int i = 0; i < n; i++) {
2075 ScilabEditorPane sep = ed.getTextPane(i);
2076 sep.configurationChanged(conf);
2077 if (sep.getOtherPaneInSplit() != null) {
2078 sep.getOtherPaneInSplit().configurationChanged(conf);
2085 public static void updateFontSize(int inc) {
2086 Font baseFont = null;
2087 for (SciNotes ed : scinotesList) {
2088 int n = ed.getTabPane().getTabCount();
2089 for (int i = 0; i < n; i++) {
2090 ScilabEditorPane sep = ed.getTextPane(i);
2091 ((ScilabEditorKit) sep.getEditorKit()).getStylePreferences().changeBaseFontSize(inc);
2092 if (baseFont == null) {
2093 baseFont = ((ScilabEditorKit) sep.getEditorKit()).getStylePreferences().getBaseFont();
2095 if (sep.getOtherPaneInSplit() != null) {
2096 ((ScilabEditorKit) sep.getOtherPaneInSplit().getEditorKit()).getStylePreferences().changeBaseFontSize(n);
2097 sep.getOtherPaneInSplit().resetFont();
2103 if (baseFont != null) {
2104 ScilabContext.saveFont(baseFont);
2109 * Set the color of the highlighted line in this editor
2114 public static void setHighlightedLineColor() {
2115 for (SciNotes ed : scinotesList) {
2116 int n = ed.getTabPane().getTabCount();
2117 for (int i = 0; i < n; i++) {
2118 ScilabEditorPane sep = ed.getTextPane(i);
2119 sep.setHighlightedLineColor(SciNotesOptions.getSciNotesDisplay().currentLineColor);
2120 if (sep.getOtherPaneInSplit() != null) {
2121 sep.getOtherPaneInSplit().setHighlightedLineColor(SciNotesOptions.getSciNotesDisplay().currentLineColor);
2128 * Set the color of the contour of the highlighted line in this editor
2133 public static void setHighlightedContourColor() {
2134 for (SciNotes ed : scinotesList) {
2135 int n = ed.getTabPane().getTabCount();
2136 for (int i = 0; i < n; i++) {
2137 ScilabEditorPane sep = ed.getTextPane(i);
2138 sep.setHighlightedContourColor(null);
2139 if (sep.getOtherPaneInSplit() != null) {
2140 sep.getOtherPaneInSplit().setHighlightedContourColor(null);
2147 * Set the highlighted line in this textPane
2152 public static void setHighlight(ScilabEditorPane sep) {
2153 sep.enableHighlightedLine(SciNotesOptions.getSciNotesDisplay().highlightCurrentLine);
2154 sep.setHighlightedLineColor(SciNotesOptions.getSciNotesDisplay().currentLineColor);
2155 sep.setHighlightedContourColor(null);
2159 * Get SciNotes as a Tab.
2161 * @return SciNotes instance
2162 * @see org.scilab.modules.gui.tab.Tab#getAsSimpleTab()
2164 public SimpleTab getAsSimpleTab() {
2169 * Get SciNotes parent Window.
2171 * @return parent Window
2172 * @see org.scilab.modules.gui.tab.Tab#getParentWindow()
2174 public SwingScilabWindow getParentWindow() {
2175 if (parentWindow == null) {
2176 return super.getParentWindow();
2178 return parentWindow;
2182 * Get the UUID associated with the editor instance.
2184 * @return unique identifier
2186 public UUID getUUID() {
2191 * Add a status bar to SciNotes.
2193 * @param infoBarToAdd
2194 * the status bar to be added
2195 * @see org.scilab.modules.gui.uielement.UIElement#addInfoBar(org.scilab.modules.gui.textbox.TextBox)
2197 public void addInfoBar(TextBox infoBarToAdd) {
2198 setInfoBar(infoBarToAdd);
2202 * Add a menu bar to SciNotes.
2204 * @param menuBarToAdd
2205 * the menu bar to be added
2206 * @see org.scilab.modules.gui.uielement.UIElement#addMenuBar(org.scilab.modules.gui.menubar.MenuBar)
2208 public void addMenuBar(MenuBar menuBarToAdd) {
2209 setMenuBar(menuBarToAdd);
2213 * Add a tool bar to SciNotes.
2215 * @param toolBarToAdd
2216 * the tool bar to be added
2217 * @see org.scilab.modules.gui.uielement.UIElement#addToolBar(org.scilab.modules.gui.toolbar.ToolBar)
2219 public void addToolBar(ToolBar toolBarToAdd) {
2220 setToolBar(toolBarToAdd);
2224 * Get SciNotes main ScilabTabbedPane.
2226 * @return SciNotes main ScilabTabbedPane
2228 public ScilabTabbedPane getTabPane() {
2233 * Set SciNotes main ScilabTabbedPane.
2236 * SciNotes main ScilabTabbedPane
2238 public void setTabPane(ScilabTabbedPane tabPane) {
2239 this.tabPane = tabPane;
2243 * Load a file and add it at the end
2248 public void loadFile(File f) {
2253 * Load a file and add it at the index
2258 * the index where to put the file
2260 public void loadFile(File f, int index) {
2261 ScilabDocument styleDocument = null;
2262 ScilabEditorPane theTextPane;
2267 ScilabModalDialog.show(this, SciNotesMessages.NOTREADABLE, SciNotesMessages.SCINOTES_ERROR, IconType.ERROR_ICON);
2268 if (getTabPane().getTabCount() == 0) {
2274 getInfoBar().setText(SciNotesMessages.LOADING);
2277 theTextPane = addTab(f.getName(), index);
2279 theTextPane = addTab(f.getName());
2281 styleDocument = (ScilabDocument) theTextPane.getDocument();
2282 styleDocument.disableUndoManager();
2283 theTextPane.setLastModified(f.lastModified());
2286 styleDocument.setUpdater(false);
2287 boolean indentMode = styleDocument.getAutoIndent();
2288 styleDocument.setAutoIndent(false);
2290 ((ScilabEditorKit) editorKit).read(this, f, styleDocument, 0);
2291 } catch (BadLocationException e) {
2292 e.printStackTrace();
2294 styleDocument.setAutoIndent(indentMode);
2295 styleDocument.setUpdater(true);
2296 } catch (IOException ioex) {
2297 ioex.printStackTrace();
2300 theTextPane.setName(f.getAbsolutePath());
2301 getTabPane().setTitleAt(getTabPane().getSelectedIndex(), f.getName());
2302 setTitle(theTextPane.getTitle());
2303 styleDocument.setContentModified(false);
2304 styleDocument.enableUndoManager();
2306 if (styleDocument.getBinary()) {
2307 theTextPane.setBinary(true);
2310 if (!f.canWrite()) {
2311 getTextPane().setReadOnly(true);
2312 JOptionPane.showMessageDialog(SciNotes.this, SciNotesMessages.READONLY);
2315 getInfoBar().setText(theTextPane.getInfoBarText());
2317 EncodingAction.updateEncodingMenu((ScilabDocument) getTextPane().getDocument());
2320 ConfigManager.saveLastOpenedDirectory(f.getPath());
2321 ConfigSciNotesManager.saveToRecentOpenedFiles(f.getPath());
2322 ConfigSciNotesManager.saveToOpenFiles(f.getPath(), this, getTextPane());
2324 // Empty the undo Manager
2325 UndoManager undo = ((ScilabDocument) getTextPane().getDocument()).getUndoManager();
2326 undo.discardAllEdits();
2328 if (getTabPane().getTabCount() == 2) {
2329 ScilabEditorPane pane = getTextPane(0);
2330 if (pane != null && pane.getName() == null && !((ScilabDocument) pane.getDocument()).isContentModified()) {
2338 * Creates a file if it doesn't exist
2341 * the file to create
2343 public void createNewFile(File f) {
2344 ScilabEditorPane theTextPane = addEmptyTab();
2345 ScilabDocument styleDocument = null;
2346 int choice = JOptionPane.showConfirmDialog(SciNotes.this, String.format(SciNotesMessages.FILE_DOESNT_EXIST, f.getName()), SCINOTES,
2347 JOptionPane.YES_NO_OPTION);
2348 if (choice == 0) { // OK
2349 styleDocument = (ScilabDocument) theTextPane.getDocument();
2350 styleDocument.disableUndoManager();
2352 BufferedWriter bw = null;
2353 OutputStreamWriter osw = null;
2354 FileOutputStream fos = null;
2357 fos = new FileOutputStream(f);
2358 osw = new OutputStreamWriter(fos, styleDocument.getEncoding());
2359 bw = new BufferedWriter(osw);
2360 editorKit.write(bw, styleDocument, 0, styleDocument.getLength());
2362 } catch (IOException e) {
2363 System.err.println(e);
2364 } catch (BadLocationException e) {
2365 System.err.println(e);
2377 } catch (IOException e) {
2378 System.err.println(e);
2383 ConfigManager.saveLastOpenedDirectory(f.getPath());
2384 ConfigSciNotesManager.saveToRecentOpenedFiles(f.getPath());
2385 ConfigSciNotesManager.saveToOpenFiles(theTextPane.getName(), this, theTextPane);
2387 theTextPane.setName(f.getPath());
2388 getTabPane().setTitleAt(getTabPane().getSelectedIndex(), f.getName());
2389 setTitle(theTextPane.getTitle());
2390 RecentFileAction.updateRecentOpenedFilesMenu(this);
2392 styleDocument.setContentModified(false);
2393 styleDocument.enableUndoManager();
2394 theTextPane.setLastModified(f.lastModified());
2397 getInfoBar().setText("");
2405 public EditorKit getEditorKit() {
2411 * the focused editor
2413 public static void setEditor(SciNotes scinotes) {
2418 * @return the focused editor
2420 public static SciNotes getEditor() {
2430 public void setEditorKit(EditorKit editorKit) {
2431 this.editorKit = editorKit;
2435 * Close scinotes from scilab.
2437 * This method can be called outside the EDT thread.
2439 public static void closeSciNotesFromScilab() {
2441 SwingUtilities.invokeAndWait(new Runnable() {
2445 SciNotes[] arr = scinotesList.toArray(new SciNotes[0]);
2446 for (int i = 0; i < arr.length; i++) {
2447 arr[i].setProtectOpenFileList(true);
2448 ExitAction.doExit(arr[i]);
2450 scinotesList.clear();
2453 } catch (InterruptedException e) {
2454 Logger.getLogger(SciNotes.class.getName()).severe(e.toString());
2455 } catch (InvocationTargetException e) {
2456 Logger.getLogger(SciNotes.class.getName()).severe(e.toString());
2463 * @return the canonical file if possible
2465 public static File fileToCanonicalFile(File f) {
2468 return f.getCanonicalFile();
2469 } catch (IOException e) {
2475 * Set the shortcuts in the pane relatively to the file
2476 * keysConfiguration.xml
2481 * the SciNotes editor
2483 private static void setKeyStrokeAction(ScilabEditorPane sep, SciNotes ed) {
2484 ScilabEditorPane s = ed.getTextPane(0);
2485 if (s != null && s != sep) {
2486 sep.setInputMap(JComponent.WHEN_FOCUSED, s.getInputMap());
2490 Map<String, KeyStroke> map = getActionKeys();
2491 ClassLoader loader = ClassLoader.getSystemClassLoader();
2493 for (Map.Entry<String, KeyStroke> entry : map.entrySet()) {
2494 String actionName = entry.getKey();
2495 KeyStroke key = entry.getValue();
2496 String action = actionToName.get(actionName);
2498 if (!action.equals("SciNotesCompletionAction")) {
2500 if (action.lastIndexOf(DOT) != -1) {
2503 className = DEFAULTACTIONPATH + DOT + action;
2506 Class clazz = loader.loadClass(className);
2507 Constructor constructor = clazz.getConstructor(new Class[] { String.class, SciNotes.class });
2508 Object act = constructor.newInstance(new Object[] { "", ed });
2509 sep.getInputMap().put(key, act);
2510 } catch (ClassNotFoundException e) {
2511 System.err.println("No action: " + className);
2512 } catch (InstantiationException e) {
2513 System.err.println("Problem to instantiate in action: " + className);
2514 } catch (NoSuchMethodException e) {
2515 System.err.println("No valid constructor in action: " + className);
2516 } catch (IllegalAccessException e) {
2517 System.err.println("The constructor must be public: " + className);
2518 } catch (InvocationTargetException e) {
2519 System.err.println("The constructor in " + className + " threw an exception :");
2520 e.printStackTrace();
2523 sep.getInputMap().put(key, new SciNotesCompletionAction(sep, ed));
2528 // Add default common shortcuts
2529 sep.getInputMap().put(KeyStroke.getKeyStroke("shift DELETE"), sep.getInputMap().get(map.get("scinotes-cut")));
2530 sep.getInputMap().put(KeyStroke.getKeyStroke("CUT"), sep.getInputMap().get(map.get("scinotes-cut")));
2531 sep.getInputMap().put(ScilabKeyStroke.getKeyStroke("OSSCKEY INSERT"), sep.getInputMap().get(map.get("scinotes-copy")));
2532 sep.getInputMap().put(KeyStroke.getKeyStroke("COPY"), sep.getInputMap().get(map.get("scinotes-copy")));
2533 sep.getInputMap().put(KeyStroke.getKeyStroke("shift INSERT"), sep.getInputMap().get(map.get("scinotes-paste")));
2534 sep.getInputMap().put(KeyStroke.getKeyStroke("PASTE"), sep.getInputMap().get(map.get("scinotes-paste")));
2538 * Execute an action on file
2539 * @param fileName the name of the file
2540 * @param action the action
2542 public static void executeAction(String fileName, ActionOnDocument action) throws IOException {
2543 Charset charset = null;
2545 charset = ScilabEditorKit.tryToGuessEncoding(new File(fileName));
2546 } catch (CharacterCodingException e) {
2547 throw new IOException(SciNotesMessages.CANNOT_GUESS_ENCODING + ": " + fileName);
2549 FileInputStream fis = new FileInputStream(fileName);
2550 InputStreamReader isr = new InputStreamReader(fis, charset);
2551 BufferedReader reader = new BufferedReader(isr);
2552 ScilabDocument doc = new ScilabDocument();
2553 ScilabEditorKit kit = new ScilabEditorKit();
2555 kit.read(reader, doc, 0);
2556 } catch (BadLocationException e) {
2557 System.err.println(e);
2560 doc.addDocumentListener(doc);
2561 if (!doc.getBinary()) {
2562 action.actionOn(doc);
2566 if (doc.isContentModified()) {
2567 SaveFile.doSave(doc, new File(fileName), kit);
2572 * Execute an action on file
2573 * @param fileName the name of the file
2574 * @param actionsName the actions as an array
2576 public static boolean executeAction(String fileName, final String[] actionsName) throws IOException {
2577 final boolean[] hasAction = new boolean[] { false };
2578 ActionOnDocument action = new ActionOnDocument() {
2579 public void actionOn(ScilabDocument doc) throws IOException {
2580 for (String act : actionsName) {
2581 if (act.equalsIgnoreCase("indent")) {
2582 hasAction[0] = true;
2583 org.scilab.modules.scinotes.actions.IndentAction.getActionOnDocument().actionOn(doc);
2584 } else if (act.equalsIgnoreCase("trailing")) {
2585 hasAction[0] = true;
2586 org.scilab.modules.scinotes.actions.RemoveTrailingWhiteAction.getActionOnDocument().actionOn(doc);
2587 } else if (act.equalsIgnoreCase("quote")) {
2588 hasAction[0] = true;
2589 org.scilab.modules.scinotes.actions.DoubleQuoteStringAction.getActionOnDocument().actionOn(doc);
2595 executeAction(fileName, action);
2597 return hasAction[0];
2601 * Execute an action on file
2602 * @param fileName the name of the file
2603 * @param acts actions separated with , or ;
2605 public static void executeAction(String fileName, String acts) throws IOException {
2606 StringTokenizer toks = new StringTokenizer(acts, ",;");
2607 String[] actions = new String[toks.countTokens()];
2608 for (int i = 0; i < actions.length; i++) {
2609 actions[i] = toks.nextToken();
2611 executeAction(fileName, actions);
2615 * An interface to implement to execute an action on a document
2617 public static interface ActionOnDocument {
2619 public void actionOn(ScilabDocument doc) throws IOException;