shortcuts for xpad
[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.awt.event.ActionEvent;
16 import java.awt.event.KeyEvent;
17 import java.io.File;
18 import java.io.FileWriter;
19
20 import javax.swing.JFileChooser;
21 import javax.swing.JOptionPane;
22 import javax.swing.JTextPane;
23 import javax.swing.KeyStroke;
24 import javax.swing.text.BadLocationException;
25 import javax.swing.text.Document;
26 import javax.swing.text.StyleConstants;
27
28 import org.scilab.modules.xpad.Xpad;
29
30 public class SaveAction extends DefaultAction {
31
32         public SaveAction(Xpad editor) {
33                 super("Save", editor);
34                 //setMnemonic('S');
35                 setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
36         }
37
38         public void doAction() {
39                 JFileChooser _fileChooser = new JFileChooser();
40                 int retval = _fileChooser.showSaveDialog(getEditor());
41                 if (retval == JFileChooser.APPROVE_OPTION) {
42                         File f = _fileChooser.getSelectedFile();
43                         try {
44
45                                 String doc = getIndentedText(getEditor().getTextPane());
46
47                                 FileWriter writer = new FileWriter(f);
48                                 writer.write(doc);
49                                 writer.flush();
50                                 writer.close();
51
52                         } catch (Exception ioex) {
53                             JOptionPane.showMessageDialog(getEditor(), ioex);
54                         }
55                 }
56         }
57
58         public String getIndentedText(JTextPane ed) {
59                 
60                 int startOffset;
61                 int endOffset;
62                 double indent;
63                 String textLine = "";
64                 
65                 Document doc = ed.getStyledDocument();
66                 String text = "";
67                 String space = "";
68
69                 for (int i = 0; i < doc.getLength(); ) {
70
71                         startOffset = ed.getStyledDocument().getParagraphElement(i).getStartOffset();
72                         endOffset = ed.getStyledDocument().getParagraphElement(i).getEndOffset();
73                         String ind = ed.getStyledDocument().getParagraphElement(i).getAttributes().getAttribute(StyleConstants.LeftIndent).toString();
74                         indent = Double.parseDouble(ind);
75
76                         try {
77                                 textLine = ed.getStyledDocument().getText(startOffset, endOffset - startOffset);
78                         } catch (BadLocationException e) {
79                                 e.printStackTrace();
80                         }
81
82                         for (int j = 0; j < indent; j=j+10) {
83                                 if (indent == 0) {
84                                         space = "";
85                                 } else {
86                                         space += " ";
87                                 }
88                         }
89
90                         text += space + textLine;
91
92                         space = "";
93                         i = endOffset;
94                 }       
95                 return text;            
96         }
97
98 }