KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jawe > actions > TextSaveAs


1 package org.enhydra.jawe.actions;
2
3 import javax.swing.*;
4 import org.enhydra.jawe.*;
5 import org.enhydra.jawe.xml.*;
6 import java.awt.event.*;
7 import java.io.*;
8 import javax.swing.*;
9 import javax.swing.text.*;
10 import java.awt.*;
11
12 /**
13  * Class that implements SaveAs action in TextView.
14  *
15  * @author Zoran Milakovic
16  * @version 1.0
17  */

18 public class TextSaveAs extends AbstractAction {
19
20    private JTextComponent textComponent = null;
21
22    public TextSaveAs(JTextComponent textComponent) {
23       this.textComponent = textComponent;
24       putValue(Action.NAME,Utils.getUnqualifiedClassName(getClass()));
25    }
26
27    public void actionPerformed(ActionEvent e) {
28       String JavaDoc fileName = XMLUtil.dialog(null, XMLUtil.getLanguageDependentString("SaveAsLabel"), 1, 3, "new_file.txt");
29       if( fileName != null ) {
30          if( !this.saveFile(fileName) )
31             XMLUtil.message(XMLUtil.getLanguageDependentString("ErrorErrorWhileSaveFile"), XMLUtil.ERROR_MESSAGE);
32       }
33    }
34
35    private boolean saveFile(String JavaDoc fileName) {
36       boolean success = true;
37       try {
38          File saveAsFile = new File( fileName );
39          FileOutputStream fos = new FileOutputStream( saveAsFile );
40          PrintStream printStream = new PrintStream(fos, true, JaWEConfig.getInstance().getEncoding());
41          String JavaDoc text = new String JavaDoc( textComponent.getText() );
42          printStream.print(text);
43          fos.close();
44          printStream.close();
45       }catch(Exception JavaDoc e) {
46          e.printStackTrace();
47          success = false;
48       }
49       return success;
50    }
51
52 }
53
Popular Tags