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.demo;
\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
24 * A TextDocument handles the association between a file on disk
\r
27 public final class TextDocument {
\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
33 private String fTitle;
\r
34 private MConstText fText;
\r
36 private MTextPanel fTextPanel = null;
\r
37 private boolean isModified = false;
\r
38 private int fFormat = STYLED_TEXT;
\r
40 private TextDocument(String title,
\r
51 public static final int STYLED_TEXT = 0;
\r
52 public static final int PLAIN_TEXT = 1;
\r
55 * Return a new TextDocument with no associated file and
\r
58 public static TextDocument createEmpty(String title, int format) {
\r
60 return new TextDocument(title, new StyledText(), null, format);
\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
70 public static TextDocument createFromFile(File file, int format) throws Exception {
\r
72 if (format != STYLED_TEXT && format != PLAIN_TEXT) {
\r
73 throw new IllegalArgumentException("Invalid format");
\r
77 if (format == STYLED_TEXT) {
\r
78 text = readMText(file);
\r
81 text = readMTextFromTextFile(file);
\r
84 TextDocument document = new TextDocument(file.getName(),
\r
92 * Return true if this document's text differs from the contents
\r
95 public boolean isModified() {
\r
97 if (fTextPanel == null) {
\r
101 return fTextPanel.isModified();
\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
110 public void setTextPanel(MTextPanel textPanel) {
\r
112 if (fTextPanel != null) {
\r
113 fText = fTextPanel.getText();
\r
114 isModified = fTextPanel.isModified();
\r
117 fTextPanel = textPanel;
\r
119 if (fTextPanel != null) {
\r
120 fTextPanel.setText(fText);
\r
122 fTextPanel.setModified(isModified);
\r
123 fTextPanel.clearCommandLog();
\r
127 public File getFile() {
\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
136 public void setFile(File file) {
\r
139 fTitle = file.getName();
\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
147 public void setFormat(int format) {
\r
149 if (format != STYLED_TEXT && format != PLAIN_TEXT) {
\r
150 throw new IllegalArgumentException("Invalid format");
\r
156 * Return the format of this document.
\r
158 public int getFormat() {
\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
168 public boolean save() {
\r
170 if (fFile == null) {
\r
171 throw new RuntimeException("Can't save without a file.");
\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
184 * Return this document's styled text.
\r
186 public MConstText getText() {
\r
188 if (fTextPanel == null) {
\r
192 return fTextPanel.getText();
\r
197 * Return the title of this document.
\r
199 public String getTitle() {
\r
205 * Return the MText serialized in the given file.
\r
206 * In case of an error return null.
\r
208 private static MConstText readMText(File file) throws Exception {
\r
210 FileInputStream inStream = null;
\r
213 inStream = new FileInputStream(file);
\r
214 ObjectInputStream objStream = new ObjectInputStream(inStream);
\r
216 return (MConstText) objStream.readObject();
\r
219 if (inStream != null) {
\r
223 catch(IOException e) {
\r
224 System.out.print("");
\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
235 private static MConstText readMTextFromTextFile(File file) throws Exception {
\r
237 InputStreamReader in = null;
\r
240 in = new FileReader(file);
\r
242 MText text = new StyledText();
\r
244 char[] buf = new char[BUF_SIZE];
\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
257 catch(IOException e) {
\r
258 System.out.print("");
\r
265 * Attempt to save the given text in the given file.
\r
266 * @return true if the operation succeeded
\r
268 private static boolean writeMText(File file, MConstText text) {
\r
270 Throwable error = null;
\r
271 OutputStream outStream = null;
\r
274 outStream = new FileOutputStream(file);
\r
275 ObjectOutputStream objStream = new ObjectOutputStream(outStream);
\r
277 objStream.writeObject(text);
\r
279 catch(IOException e) {
\r
282 catch(ClassCastException e) {
\r
286 if (outStream != null) {
\r
290 catch(IOException e) {
\r
291 System.out.print("");
\r
297 if (error != null) {
\r
298 error.printStackTrace();
\r
307 * Write the given MConstText to the given file as plain text.
\r
309 private static boolean writePlainMText(File file, MConstText text) {
\r
311 Throwable error = null;
\r
312 OutputStreamWriter outStream = null;
\r
315 outStream = new FileWriter(file);
\r
316 char[] buf = new char[BUF_SIZE];
\r
317 int length = text.length();
\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
324 } while (start < length);
\r
326 catch(IOException e) {
\r
330 if (outStream != null) {
\r
334 catch(IOException e) {
\r
335 System.out.print("");
\r
341 if (error != null) {
\r
342 error.printStackTrace();
\r