2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009 - DIGITEO - Allan CORNET
4 * Copyright (C) 2010 - 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-en.txt
14 package org.scilab.modules.scinotes.utils;
16 import java.io.BufferedWriter;
18 import java.io.FileNotFoundException;
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21 import java.io.OutputStreamWriter;
22 import java.io.UnsupportedEncodingException;
24 import javax.swing.JEditorPane;
25 import javax.swing.text.BadLocationException;
26 import javax.swing.text.EditorKit;
28 import org.scilab.modules.gui.messagebox.ScilabModalDialog;
29 import org.scilab.modules.gui.messagebox.ScilabModalDialog.AnswerOption;
30 import org.scilab.modules.gui.messagebox.ScilabModalDialog.ButtonType;
31 import org.scilab.modules.gui.messagebox.ScilabModalDialog.IconType;
32 import org.scilab.modules.scinotes.ScilabDocument;
33 import org.scilab.modules.scinotes.ScilabEditorPane;
34 import org.scilab.modules.scinotes.SciNotesOptions;
37 * Save File utility class
38 * @author Allan CORNET
39 * @author Calixte DENIZET
41 public final class SaveFile {
43 private static final String LINE_SEPARATOR = "line.separator";
48 private SaveFile() { }
51 * save text in JEditorPane
52 * @param textPane JEditorPane
54 * @param editorKit EditorKit
55 * @return true if saved
57 public static boolean doSave(ScilabEditorPane textPane, int index, File fOut, EditorKit editorKit) {
58 return doSave(textPane, index, fOut, editorKit, true, false);
62 * save text in JEditorPane
63 * @param textPane JEditorPane
65 * @param editorKit EditorKit
66 * @return true if saved
68 public static boolean doSave(ScilabEditorPane textPane, int index, File fOut, EditorKit editorKit, boolean addEOL, boolean silent) {
69 ScilabDocument styledDocument = (ScilabDocument) textPane.getDocument();
71 if (!styledDocument.getEncoding().equalsIgnoreCase(SciNotesOptions.getSciNotesPreferences().encoding)) {
73 String msg = String.format(SciNotesMessages.DIFFERENT_ENCODINGS, styledDocument.getEncoding(), SciNotesOptions.getSciNotesPreferences().encoding);
74 if (ScilabModalDialog.show(textPane.getEditor(), msg, SciNotesMessages.DIFFERENT_ENCODINGS_TITLE, IconType.QUESTION_ICON, ButtonType.YES_NO) == AnswerOption.NO_OPTION) {
85 } catch (IOException e) {
86 System.err.println(e);
89 if (!fOut.canWrite()) {
91 ScilabModalDialog.show(textPane.getEditor(), SciNotesMessages.NOTWRITABLE, SciNotesMessages.SCINOTES_ERROR, IconType.ERROR_ICON);
97 String defaultEol = System.getProperty(LINE_SEPARATOR);
99 // set eol used to save file
100 if (styledDocument.getEOL().compareTo(defaultEol) != 0) {
101 System.setProperty(LINE_SEPARATOR, styledDocument.getEOL());
104 styledDocument.addEOL();
107 boolean bReturn = false;
109 BufferedWriter bw = null;
110 OutputStreamWriter osw = null;
111 FileOutputStream fos = null;
114 fos = new FileOutputStream(fOut);
115 osw = new OutputStreamWriter(fos, ConfigSciNotesManager.getDefaultEncoding());
116 bw = new BufferedWriter(osw);
117 editorKit.write(bw, styledDocument, 0, styledDocument.getLength());
120 } catch (IOException e) {
121 System.err.println(e);
123 } catch (BadLocationException e) {
124 System.err.println(e);
137 } catch (IOException e) {
138 System.err.println(e);
142 // restore default eol
143 System.setProperty(LINE_SEPARATOR, defaultEol);
145 textPane.getEditor().reload(index);