]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/richtext/demo/TextDocument.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / richtext / demo / TextDocument.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.demo;\r
14 \r
15 import java.io.*;\r
16 \r
17 import com.ibm.richtext.styledtext.MConstText;\r
18 import com.ibm.richtext.styledtext.MText;\r
19 import com.ibm.richtext.styledtext.StyledText;\r
20 import com.ibm.richtext.textpanel.MTextPanel;\r
21 import com.ibm.richtext.textlayout.attributes.AttributeMap;\r
22 \r
23 /**\r
24  * A TextDocument handles the association between a file on disk\r
25  * and a TextPanel.\r
26  */\r
27 public final class TextDocument {\r
28 \r
29     static final String COPYRIGHT =\r
30                 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";\r
31     private static final int BUF_SIZE = 1024;\r
32     \r
33     private String fTitle;\r
34     private MConstText fText;\r
35     private File fFile;\r
36     private MTextPanel fTextPanel = null;\r
37     private boolean isModified = false;\r
38     private int fFormat = STYLED_TEXT;\r
39     \r
40     private TextDocument(String title,\r
41                          MConstText text,\r
42                          File file,\r
43                          int format) {\r
44         \r
45         fTitle = title;\r
46         fText = text;\r
47         fFile = file;\r
48         setFormat(format);\r
49     }\r
50     \r
51     public static final int STYLED_TEXT = 0;\r
52     public static final int PLAIN_TEXT = 1;\r
53     \r
54     /** \r
55      * Return a new TextDocument with no associated file and\r
56      * empty text.\r
57      */\r
58     public static TextDocument createEmpty(String title, int format) {\r
59         \r
60         return new TextDocument(title, new StyledText(), null, format);\r
61     }\r
62     \r
63     /**\r
64      * Return a TextDocument created from the contents of the given\r
65      * file.  This method may throw an exception if the file cannot\r
66      * be read.  In particular, if the format is given as STYLED_TEXT\r
67      * but the file does not contain a serialized MConstText, \r
68      * this method will throw StreamCorruptedException.\r
69      */\r
70     public static TextDocument createFromFile(File file, int format) throws Exception {\r
71         \r
72         if (format != STYLED_TEXT && format != PLAIN_TEXT) {\r
73             throw new IllegalArgumentException("Invalid format");\r
74         }\r
75         \r
76         MConstText text;\r
77         if (format == STYLED_TEXT) {\r
78             text = readMText(file);\r
79         }\r
80         else {\r
81             text = readMTextFromTextFile(file);\r
82         }\r
83         \r
84         TextDocument document = new TextDocument(file.getName(), \r
85                                                  text,\r
86                                                  file,\r
87                                                  format);\r
88         return document;\r
89     }\r
90     \r
91     /**\r
92      * Return true if this document's text differs from the contents\r
93      * of its file.\r
94      */\r
95     public boolean isModified() {\r
96         \r
97         if (fTextPanel == null) {\r
98             return isModified;\r
99         }\r
100         else {\r
101             return fTextPanel.isModified();\r
102         }\r
103     }\r
104     \r
105     /**\r
106      * Set the MTextPanel that will be used to edit the document's\r
107      * text.  The document's text becomes the contents of the\r
108      * MTextPanel.\r
109      */\r
110     public void setTextPanel(MTextPanel textPanel) {\r
111 \r
112         if (fTextPanel != null) {\r
113             fText = fTextPanel.getText();\r
114             isModified = fTextPanel.isModified();\r
115         }\r
116         \r
117         fTextPanel = textPanel;\r
118         \r
119         if (fTextPanel != null) {\r
120             fTextPanel.setText(fText);\r
121             fText = null;\r
122             fTextPanel.setModified(isModified);\r
123             fTextPanel.clearCommandLog();\r
124         }\r
125     }\r
126     \r
127     public File getFile() {\r
128     \r
129         return fFile;\r
130     }\r
131     \r
132     /**\r
133      * Set this document's file.  The document's title will\r
134      * change to the file name.  The file cannot be null.\r
135      */\r
136     public void setFile(File file) {\r
137         \r
138         fFile = file;\r
139         fTitle = file.getName();\r
140     }\r
141     \r
142     /**\r
143      * Set the format of this document.  The format determines\r
144      * whether the document will be written to files as styled\r
145      * text or plain characters.\r
146      */\r
147     public void setFormat(int format) {\r
148         \r
149         if (format != STYLED_TEXT && format != PLAIN_TEXT) {\r
150             throw new IllegalArgumentException("Invalid format");\r
151         }\r
152         fFormat = format;\r
153     }\r
154     \r
155     /**\r
156      * Return the format of this document.\r
157      */\r
158     public int getFormat() {\r
159         \r
160         return fFormat;\r
161     }\r
162     \r
163     /**\r
164      * Write the document's text to its file.  If the document does\r
165      * not have an associated file then this method is equivalent to\r
166      * saveAs.  This method returns true if the save operation succeeds.\r
167      */\r
168     public boolean save() {\r
169 \r
170         if (fFile == null) {\r
171             throw new RuntimeException("Can't save without a file.");\r
172         }\r
173                 \r
174         MConstText text = getText();\r
175         boolean success = fFormat==STYLED_TEXT? writeMText(fFile, text) :\r
176                                                 writePlainMText(fFile, text);\r
177         if (success && fTextPanel != null) {\r
178             fTextPanel.setModified(false);\r
179         }\r
180         return success;\r
181     }\r
182     \r
183     /** \r
184      * Return this document's styled text.\r
185      */\r
186     public MConstText getText() {\r
187         \r
188         if (fTextPanel == null) {\r
189             return fText;\r
190         }\r
191         else {\r
192             return fTextPanel.getText();\r
193         }\r
194     }\r
195     \r
196     /**\r
197      * Return the title of this document.\r
198      */\r
199     public String getTitle() {\r
200         \r
201         return fTitle;\r
202     }\r
203     \r
204     /**\r
205      * Return the MText serialized in the given file.\r
206      * In case of an error return null.\r
207      */\r
208     private static MConstText readMText(File file) throws Exception {\r
209 \r
210         FileInputStream inStream = null;\r
211         \r
212         try {\r
213             inStream = new FileInputStream(file);\r
214             ObjectInputStream objStream = new ObjectInputStream(inStream);\r
215 \r
216             return (MConstText) objStream.readObject();\r
217         }\r
218         finally {\r
219             if (inStream != null) {\r
220                 try {\r
221                     inStream.close();\r
222                 }\r
223                 catch(IOException e) {\r
224                     System.out.print("");\r
225                 }\r
226             }\r
227         }\r
228     }\r
229     \r
230     /**\r
231      * Read the given file as a plain text file, and return its\r
232      * contents as an MConstText.  The character and paragraph styles in \r
233      * the returned text will be EMPTY_ATTRIBUTE_MAP.\r
234      */\r
235     private static MConstText readMTextFromTextFile(File file) throws Exception {\r
236         \r
237         InputStreamReader in = null;\r
238         \r
239         try {\r
240             in = new FileReader(file);\r
241             \r
242             MText text = new StyledText();\r
243             \r
244             char[] buf = new char[BUF_SIZE];\r
245             int read;\r
246             while ((read=in.read(buf, 0, buf.length)) != -1) {\r
247                 int len = text.length();\r
248                 text.replace(len, len, buf, 0, read, AttributeMap.EMPTY_ATTRIBUTE_MAP);\r
249             }\r
250             return text;\r
251         }\r
252         finally {\r
253             if (in != null) {\r
254                 try {\r
255                     in.close();\r
256                 }\r
257                 catch(IOException e) {\r
258                     System.out.print("");\r
259                 }\r
260             }\r
261         }\r
262     }\r
263 \r
264     /**\r
265      * Attempt to save the given text in the given file.\r
266      * @return true if the operation succeeded\r
267      */\r
268     private static boolean writeMText(File file, MConstText text) {\r
269 \r
270         Throwable error = null;\r
271         OutputStream outStream = null;\r
272         \r
273         try {\r
274             outStream = new FileOutputStream(file);\r
275             ObjectOutputStream objStream = new ObjectOutputStream(outStream);\r
276 \r
277             objStream.writeObject(text);\r
278         }\r
279         catch(IOException e) {\r
280             error = e;\r
281         }\r
282         catch(ClassCastException e) {\r
283             error = e;\r
284         }\r
285         finally {\r
286             if (outStream != null) {\r
287                 try {\r
288                     outStream.close();\r
289                 }\r
290                 catch(IOException e) {\r
291                     System.out.print("");\r
292                 }\r
293             }\r
294         }\r
295 \r
296 \r
297         if (error != null) {\r
298             error.printStackTrace();\r
299             return false;\r
300         }\r
301         else {\r
302             return true;\r
303         }\r
304     }\r
305 \r
306     /**\r
307      * Write the given MConstText to the given file as plain text.\r
308      */\r
309     private static boolean writePlainMText(File file, MConstText text) {\r
310 \r
311         Throwable error = null;\r
312         OutputStreamWriter outStream = null;\r
313         \r
314         try {\r
315             outStream = new FileWriter(file);\r
316             char[] buf = new char[BUF_SIZE];\r
317             int length = text.length();\r
318             int start = 0;\r
319             do {\r
320                 int count = Math.min(length-start, buf.length);\r
321                 text.extractChars(start, start+count, buf, 0);\r
322                 outStream.write(buf, 0, count);\r
323                 start += count;\r
324             } while (start < length);\r
325         }\r
326         catch(IOException e) {\r
327             error = e;\r
328         }\r
329         finally {\r
330             if (outStream != null) {\r
331                 try {\r
332                     outStream.close();\r
333                 }\r
334                 catch(IOException e) {\r
335                     System.out.print("");\r
336                 }\r
337             }\r
338         }\r
339 \r
340 \r
341         if (error != null) {\r
342             error.printStackTrace();\r
343             return false;\r
344         }\r
345         else {\r
346             return true;\r
347         }\r
348     }\r
349 }