e027f76d9c25be4e9b3b0af81453a7c1f252e703
[scilab.git] / scilab / modules / xpad / src / java / org / scilab / modules / xpad / actions / SaveAction.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2009 - DIGITEO - Bruno JOFRET
4  *
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at
9  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  *
11  */
12
13 package org.scilab.modules.xpad.actions;
14
15 import java.io.File;
16 import java.io.FileWriter;
17
18 import javax.swing.JFileChooser;
19 import javax.swing.JOptionPane;
20 import javax.swing.JTextPane;
21 import javax.swing.text.BadLocationException;
22 import javax.swing.text.Document;
23 import javax.swing.text.StyleConstants;
24
25 import org.scilab.modules.xpad.Xpad;
26
27 public class SaveAction extends DefaultAction {
28
29         public SaveAction(Xpad editor) {
30                 super("Save", editor);
31                 setMnemonic('S');
32         }
33
34         public void doAction() {
35                 JFileChooser _fileChooser = new JFileChooser();
36                 int retval = _fileChooser.showSaveDialog(getEditor());
37                 if (retval == JFileChooser.APPROVE_OPTION) {
38                         File f = _fileChooser.getSelectedFile();
39                         try {
40
41                                 String doc = getIndentedText(getEditor().getTextPane());
42
43                                 FileWriter writer = new FileWriter(f);
44                                 writer.write(doc);
45                                 writer.flush();
46                                 writer.close();
47
48                         } catch (Exception ioex) {
49                             JOptionPane.showMessageDialog(getEditor(), ioex);
50                         }
51                 }
52         }
53
54         public String getIndentedText(JTextPane ed) {
55                 
56                 int startOffset;
57                 int endOffset;
58                 double indent;
59                 String textLine = "";
60                 
61                 Document doc = ed.getStyledDocument();
62                 String text = "";
63                 String space = "";
64
65                 for (int i = 0; i < doc.getLength(); ) {
66
67                         startOffset = ed.getStyledDocument().getParagraphElement(i).getStartOffset();
68                         endOffset = ed.getStyledDocument().getParagraphElement(i).getEndOffset();
69                         String ind = ed.getStyledDocument().getParagraphElement(i).getAttributes().getAttribute(StyleConstants.LeftIndent).toString();
70                         indent = Double.parseDouble(ind);
71
72                         try {
73                                 textLine = ed.getStyledDocument().getText(startOffset, endOffset - startOffset);
74                         } catch (BadLocationException e) {
75                                 e.printStackTrace();
76                         }
77
78                         for (int j = 0; j < indent; j=j+10) {
79                                 if (indent == 0) {
80                                         space = "";
81                                 } else {
82                                         space += " ";
83                                 }
84                         }
85
86                         text += space + textLine;
87
88                         space = "";
89                         i = endOffset;
90                 }       
91                 return text;            
92         }
93
94 }