2 * (C) Copyright IBM Corp. 1998-2004. All Rights Reserved.
\r
4 * The program is provided "as is" without any warranty express or
\r
5 * implied, including the warranty of non-infringement and the implied
\r
6 * warranties of merchantibility and fitness for a particular purpose.
\r
7 * IBM will not be liable for any damages suffered by you as a result
\r
8 * of using the Program. In no event will IBM be liable for any
\r
9 * special, indirect or consequential damages or lost profits even if
\r
10 * IBM has been advised of the possibility of their occurrence. IBM
\r
11 * will not be liable for any third party claims against you.
\r
13 package com.ibm.richtext.textapps;
\r
15 import java.awt.FileDialog;
\r
16 import java.awt.Frame;
\r
19 import com.ibm.richtext.styledtext.MConstText;
\r
20 import com.ibm.richtext.styledtext.MText;
\r
22 public class FileUtils {
\r
24 static final String COPYRIGHT =
\r
25 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
\r
27 * Present the user with a file dialog, and replace
\r
28 * dest with the MText in the selected file, and return
\r
29 * the file objct. If any errors occur, return null and
\r
30 * do not modify dest.
\r
32 public static File userLoadMText(String title, MText dest, Frame owner) {
\r
34 FileDialog dialog = new FileDialog(owner, title, FileDialog.LOAD);
\r
36 String fileStr = dialog.getFile();
\r
37 String dirStr = dialog.getDirectory();
\r
39 if (fileStr != null) {
\r
40 File rval = new File(dirStr, fileStr);
\r
41 MConstText src = loadMText(rval);
\r
43 dest.replaceAll(src);
\r
52 * Return the MText serialized in the given file.
\r
53 * In case of an error return null.
\r
55 public static MConstText loadMText(File file) {
\r
60 FileInputStream inStream = new FileInputStream(file);
\r
61 ObjectInputStream objStream = new ObjectInputStream(inStream);
\r
63 MConstText text = (MConstText) objStream.readObject();
\r
67 catch(IOException e) {
\r
70 catch(ClassNotFoundException e) {
\r
73 catch(ClassCastException e) {
\r
77 error.printStackTrace();
\r
82 * Prompt the user for the file if file is null. Then save the
\r
83 * text in the file, if any.
\r
85 public static File userSaveMText(File file, String title, MConstText text, Frame owner) {
\r
89 FileDialog dialog = new FileDialog(owner, title, FileDialog.SAVE);
\r
91 String fileStr = dialog.getFile();
\r
92 String dirStr = dialog.getDirectory();
\r
94 if (fileStr != null) {
\r
95 file = new File(dirStr, fileStr);
\r
101 saveMText(file, text);
\r
107 public static void saveMText(File file, MConstText text) {
\r
112 OutputStream outStream = new FileOutputStream(file);
\r
113 ObjectOutputStream objStream = new ObjectOutputStream(outStream);
\r
115 objStream.writeObject(text);
\r
119 catch(IOException e) {
\r
122 catch(ClassCastException e) {
\r
126 error.printStackTrace();
\r