]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/richtext/demo/EditDemo.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / richtext / demo / EditDemo.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.awt.FileDialog;\r
16 import java.awt.Frame;\r
17 import java.awt.Toolkit;\r
18 \r
19 import java.io.File;\r
20 import java.io.StreamCorruptedException;\r
21 import com.ibm.richtext.textpanel.TextPanel;\r
22 \r
23 /**\r
24  * EditDemo is the main class for a simple, multiple-document\r
25  * styled text editor, built with the classes in the textpanel\r
26  * and textframe packages.\r
27  * <p>\r
28  * To run EditDemo, type:\r
29  * <blockquote><pre>\r
30  * java com.ibm.richtext.demo.EditDemo [file1] [file2] [...]\r
31  * </pre></blockquote>\r
32  * where the filenames are files saved with this demo.\r
33  */\r
34 public class EditDemo extends EditApplication {\r
35 \r
36     static final String COPYRIGHT =\r
37                 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";\r
38     \r
39     public static synchronized void main(String[] args) {\r
40 \r
41         if (args.length > 0 && args[0].equals("-swing")) {\r
42             new com.ibm.richtext.swingdemo.SwingEditDemo(args,1);\r
43         }\r
44         else {\r
45             new EditDemo(args, 0);\r
46         }\r
47     }\r
48 \r
49     protected EditDemo(String[] args, int start) {\r
50 \r
51         super(Toolkit.getDefaultToolkit().getSystemClipboard(), \r
52               TextDocument.STYLED_TEXT);\r
53 \r
54         if (args.length == start) {\r
55             doNewWindow();\r
56         }\r
57         else {\r
58             boolean openedADocument = false;\r
59             for (int i=start; i < args.length; i++) {\r
60 \r
61                 File file = new File(args[i]);\r
62                 TextDocument document = getDocumentFromFile(file);\r
63                 \r
64                 if (document != null) {\r
65                     addDocument(document);\r
66                     openedADocument = true;\r
67                 }\r
68             }\r
69             if (!openedADocument) {\r
70                 quit();\r
71             }\r
72         }\r
73     }\r
74     \r
75     public static TextDocument getDocumentFromFile(File file) {\r
76     \r
77         Exception exception = null;\r
78         \r
79         try {\r
80             return TextDocument.createFromFile(file, TextDocument.STYLED_TEXT);\r
81         }\r
82         catch(StreamCorruptedException e) {\r
83             try {\r
84                 return TextDocument.createFromFile(file, TextDocument.PLAIN_TEXT);\r
85             }\r
86             catch(Exception e2) {\r
87                 exception = e2;\r
88             }\r
89         }\r
90         catch(Exception e) {\r
91             exception = e;\r
92         }\r
93         \r
94         System.err.println("Exception opening file.");\r
95         exception.printStackTrace();\r
96         \r
97         return null;\r
98     }\r
99     \r
100     protected DocumentWindow createDocumentWindow(TextDocument document) {\r
101         \r
102         return new AwtDocumentWindow(this, \r
103                                      document,\r
104                                      TextPanel.getDefaultSettings(),\r
105                                      true,\r
106                                      null,\r
107                                      true,\r
108                                      true,\r
109                                      null);\r
110     }\r
111     \r
112     public TextDocument openDocument(Frame dialogParent) {\r
113     \r
114         String title = ResourceUtils.getString(EditorResources.OPEN_TITLE);\r
115         File file = AwtDocumentWindow.getFileFromDialog(null,\r
116                                                      title,\r
117                                                      dialogParent,\r
118                                                      FileDialog.LOAD);\r
119         if (file != null) {\r
120             return getDocumentFromFile(file);\r
121         }\r
122         else {\r
123             return null;\r
124         }\r
125     }\r
126 }