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.demo;
014:
015: import java.awt.FileDialog;
016: import java.awt.Frame;
017: import java.awt.Toolkit;
018:
019: import java.io.File;
020: import java.io.StreamCorruptedException;
021: import com.ibm.richtext.textpanel.TextPanel;
022:
023: /**
024: * EditDemo is the main class for a simple, multiple-document
025: * styled text editor, built with the classes in the textpanel
026: * and textframe packages.
027: * <p>
028: * To run EditDemo, type:
029: * <blockquote><pre>
030: * java com.ibm.richtext.demo.EditDemo [file1] [file2] [...]
031: * </pre></blockquote>
032: * where the filenames are files saved with this demo.
033: */
034: public class EditDemo extends EditApplication {
035:
036: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
037:
038: public static synchronized void main(String[] args) {
039:
040: if (args.length > 0 && args[0].equals("-swing")) {
041: new com.ibm.richtext.swingdemo.SwingEditDemo(args, 1);
042: } else {
043: new EditDemo(args, 0);
044: }
045: }
046:
047: protected EditDemo(String[] args, int start) {
048:
049: super (Toolkit.getDefaultToolkit().getSystemClipboard(),
050: TextDocument.STYLED_TEXT);
051:
052: if (args.length == start) {
053: doNewWindow();
054: } else {
055: boolean openedADocument = false;
056: for (int i = start; i < args.length; i++) {
057:
058: File file = new File(args[i]);
059: TextDocument document = getDocumentFromFile(file);
060:
061: if (document != null) {
062: addDocument(document);
063: openedADocument = true;
064: }
065: }
066: if (!openedADocument) {
067: quit();
068: }
069: }
070: }
071:
072: public static TextDocument getDocumentFromFile(File file) {
073:
074: Exception exception = null;
075:
076: try {
077: return TextDocument.createFromFile(file,
078: TextDocument.STYLED_TEXT);
079: } catch (StreamCorruptedException e) {
080: try {
081: return TextDocument.createFromFile(file,
082: TextDocument.PLAIN_TEXT);
083: } catch (Exception e2) {
084: exception = e2;
085: }
086: } catch (Exception e) {
087: exception = e;
088: }
089:
090: System.err.println("Exception opening file.");
091: exception.printStackTrace();
092:
093: return null;
094: }
095:
096: protected DocumentWindow createDocumentWindow(TextDocument document) {
097:
098: return new AwtDocumentWindow(this , document, TextPanel
099: .getDefaultSettings(), true, null, true, true, null);
100: }
101:
102: public TextDocument openDocument(Frame dialogParent) {
103:
104: String title = ResourceUtils
105: .getString(EditorResources.OPEN_TITLE);
106: File file = AwtDocumentWindow.getFileFromDialog(null, title,
107: dialogParent, FileDialog.LOAD);
108: if (file != null) {
109: return getDocumentFromFile(file);
110: } else {
111: return null;
112: }
113: }
114: }
|