]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/richtext/textapps/FileUtils.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / richtext / textapps / FileUtils.java
1 /*\r
2  * (C) Copyright IBM Corp. 1998-2004.  All Rights Reserved.\r
3  *\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
12  */\r
13 package com.ibm.richtext.textapps;\r
14 \r
15 import java.awt.FileDialog;\r
16 import java.awt.Frame;\r
17 import java.io.*;\r
18 \r
19 import com.ibm.richtext.styledtext.MConstText;\r
20 import com.ibm.richtext.styledtext.MText;\r
21 \r
22 public class FileUtils {\r
23 \r
24     static final String COPYRIGHT =\r
25                 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";\r
26     /**\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
31      */\r
32     public static File userLoadMText(String title, MText dest, Frame owner) {\r
33 \r
34         FileDialog dialog = new FileDialog(owner, title, FileDialog.LOAD);\r
35         dialog.show();\r
36         String fileStr = dialog.getFile();\r
37         String dirStr = dialog.getDirectory();\r
38 \r
39         if (fileStr != null) {\r
40             File rval = new File(dirStr, fileStr);\r
41             MConstText src = loadMText(rval);\r
42             if (src != null) {\r
43                 dest.replaceAll(src);\r
44                 return rval;\r
45             }\r
46         }\r
47 \r
48         return null;\r
49     }\r
50 \r
51     /**\r
52      * Return the MText serialized in the given file.\r
53      * In case of an error return null.\r
54      */\r
55     public static MConstText loadMText(File file) {\r
56 \r
57         Throwable error;\r
58 \r
59         try {\r
60             FileInputStream inStream = new FileInputStream(file);\r
61             ObjectInputStream objStream = new ObjectInputStream(inStream);\r
62 \r
63             MConstText text = (MConstText) objStream.readObject();\r
64             inStream.close();\r
65             return text;\r
66         }\r
67         catch(IOException e) {\r
68             error = e;\r
69         }\r
70         catch(ClassNotFoundException e) {\r
71             error = e;\r
72         }\r
73         catch(ClassCastException e) {\r
74             error = e;\r
75         }\r
76 \r
77         error.printStackTrace();\r
78         return null;\r
79     }\r
80 \r
81     /**\r
82      * Prompt the user for the file if file is null.  Then save the\r
83      * text in the file, if any.\r
84      */\r
85     public static File userSaveMText(File file, String title, MConstText text, Frame owner) {\r
86 \r
87         if (file == null) {\r
88 \r
89             FileDialog dialog = new FileDialog(owner, title, FileDialog.SAVE);\r
90             dialog.show();\r
91             String fileStr = dialog.getFile();\r
92             String dirStr = dialog.getDirectory();\r
93 \r
94             if (fileStr != null) {\r
95                 file = new File(dirStr, fileStr);\r
96             }\r
97         }\r
98 \r
99         if (file != null) {\r
100 \r
101             saveMText(file, text);\r
102         }\r
103 \r
104         return file;\r
105     }\r
106 \r
107     public static void saveMText(File file, MConstText text) {\r
108 \r
109         Throwable error;\r
110 \r
111         try {\r
112             OutputStream outStream = new FileOutputStream(file);\r
113             ObjectOutputStream objStream = new ObjectOutputStream(outStream);\r
114 \r
115             objStream.writeObject(text);\r
116             outStream.close();\r
117             return;\r
118         }\r
119         catch(IOException e) {\r
120             error = e;\r
121         }\r
122         catch(ClassCastException e) {\r
123             error = e;\r
124         }\r
125 \r
126         error.printStackTrace();\r
127     }\r
128 }