001: /*
002: * (C) Copyright IBM Corp. 1998-2004. All Rights Reserved.
003: *
004: * The program is provided "as is" without any warranty express or
005: * implied, including the warranty of non-infringement and the implied
006: * warranties of merchantibility and fitness for a particular purpose.
007: * IBM will not be liable for any damages suffered by you as a result
008: * of using the Program. In no event will IBM be liable for any
009: * special, indirect or consequential damages or lost profits even if
010: * IBM has been advised of the possibility of their occurrence. IBM
011: * will not be liable for any third party claims against you.
012: */
013: package com.ibm.richtext.textapps;
014:
015: import java.awt.FileDialog;
016: import java.awt.Frame;
017: import java.io.*;
018:
019: import com.ibm.richtext.styledtext.MConstText;
020: import com.ibm.richtext.styledtext.MText;
021:
022: public class FileUtils {
023:
024: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
025:
026: /**
027: * Present the user with a file dialog, and replace
028: * dest with the MText in the selected file, and return
029: * the file objct. If any errors occur, return null and
030: * do not modify dest.
031: */
032: public static File userLoadMText(String title, MText dest,
033: Frame owner) {
034:
035: FileDialog dialog = new FileDialog(owner, title,
036: FileDialog.LOAD);
037: dialog.show();
038: String fileStr = dialog.getFile();
039: String dirStr = dialog.getDirectory();
040:
041: if (fileStr != null) {
042: File rval = new File(dirStr, fileStr);
043: MConstText src = loadMText(rval);
044: if (src != null) {
045: dest.replaceAll(src);
046: return rval;
047: }
048: }
049:
050: return null;
051: }
052:
053: /**
054: * Return the MText serialized in the given file.
055: * In case of an error return null.
056: */
057: public static MConstText loadMText(File file) {
058:
059: Throwable error;
060:
061: try {
062: FileInputStream inStream = new FileInputStream(file);
063: ObjectInputStream objStream = new ObjectInputStream(
064: inStream);
065:
066: MConstText text = (MConstText) objStream.readObject();
067: inStream.close();
068: return text;
069: } catch (IOException e) {
070: error = e;
071: } catch (ClassNotFoundException e) {
072: error = e;
073: } catch (ClassCastException e) {
074: error = e;
075: }
076:
077: error.printStackTrace();
078: return null;
079: }
080:
081: /**
082: * Prompt the user for the file if file is null. Then save the
083: * text in the file, if any.
084: */
085: public static File userSaveMText(File file, String title,
086: MConstText text, Frame owner) {
087:
088: if (file == null) {
089:
090: FileDialog dialog = new FileDialog(owner, title,
091: FileDialog.SAVE);
092: dialog.show();
093: String fileStr = dialog.getFile();
094: String dirStr = dialog.getDirectory();
095:
096: if (fileStr != null) {
097: file = new File(dirStr, fileStr);
098: }
099: }
100:
101: if (file != null) {
102:
103: saveMText(file, text);
104: }
105:
106: return file;
107: }
108:
109: public static void saveMText(File file, MConstText text) {
110:
111: Throwable error;
112:
113: try {
114: OutputStream outStream = new FileOutputStream(file);
115: ObjectOutputStream objStream = new ObjectOutputStream(
116: outStream);
117:
118: objStream.writeObject(text);
119: outStream.close();
120: return;
121: } catch (IOException e) {
122: error = e;
123: } catch (ClassCastException e) {
124: error = e;
125: }
126:
127: error.printStackTrace();
128: }
129: }
|