]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/richtext/demo/EditApplication.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / richtext / demo / EditApplication.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.Frame;\r
16 import java.awt.datatransfer.Clipboard;\r
17 \r
18 import java.text.MessageFormat;\r
19 \r
20 import java.util.Vector;\r
21 \r
22 public abstract class EditApplication {\r
23 \r
24     private Clipboard fClipboard;\r
25     private int fDefaultFormat;\r
26     private Vector fWindows = new Vector();\r
27     private int fUntitledCount = 0;\r
28     \r
29     protected EditApplication(Clipboard clipboard, int defaultFormat) {\r
30         \r
31         fClipboard = clipboard;\r
32         fDefaultFormat = defaultFormat;\r
33     }\r
34     \r
35     /**\r
36      * New documents are named "Untitled 1", "Untitled 2", etc.  This\r
37      * method returns the appropriate name for the next new document.\r
38      * @return the next new document name\r
39      */\r
40     private String getNextNewName() {\r
41 \r
42         fUntitledCount += 1;\r
43         String pattern = ResourceUtils.getString(EditorResources.UNTITLED_MSG);\r
44         return MessageFormat.format(pattern,\r
45                                     new Object[]{new Integer(fUntitledCount)});\r
46     }\r
47     \r
48     public final Clipboard getClipboard() {\r
49         \r
50         return fClipboard;\r
51     }\r
52 \r
53     protected abstract DocumentWindow createDocumentWindow(TextDocument document);\r
54     \r
55     public abstract TextDocument openDocument(Frame dialogParent);\r
56     \r
57     public final TextDocument createNewDocument() {\r
58     \r
59         String name = getNextNewName();\r
60         int format = fDefaultFormat;\r
61         return TextDocument.createEmpty(name, format);\r
62     }\r
63     \r
64     public final void doNewWindow() {\r
65         \r
66         addDocument(TextDocument.createEmpty(getNextNewName(), fDefaultFormat));\r
67     }\r
68     \r
69     public final void addDocument(TextDocument document) {\r
70         \r
71         final DocumentWindow window = createDocumentWindow(document);\r
72 \r
73         window.setSize(500, 400);\r
74         window.show();\r
75         fWindows.addElement(window);\r
76     }\r
77     \r
78     /**\r
79      * Remove document from list of documents.  Quit application if list\r
80      * length falls to zero.\r
81      * @param window window of the document to remove\r
82      */\r
83     public final void removeDocumentWindow(DocumentWindow window) {\r
84 \r
85         fWindows.removeElement(window);\r
86         if (fWindows.isEmpty()) {\r
87             quit();\r
88         }\r
89     }\r
90 \r
91     /**\r
92      * Go through list of documents and attempt to close each document.\r
93      * If all documents close successfully, then exit.\r
94      */\r
95     public final void doExit() {\r
96 \r
97         // Clone fWindows since it can get modified while being traversed.\r
98         Vector windows = (Vector) fWindows.clone();\r
99 \r
100         int size = windows.size();\r
101         for (int i=0; i < size; i++) {\r
102             DocumentWindow window = (DocumentWindow) windows.elementAt(i);\r
103             if (!window.doClose()) {\r
104                 return;\r
105             }\r
106         }\r
107 \r
108         // quit will be called when last document removes itself\r
109     }\r
110 \r
111     /**\r
112      * Called when last document window closes.  Default implementation\r
113      * calls System.exit.\r
114      */\r
115     protected void quit() {\r
116 \r
117         System.exit(0);\r
118     }\r
119 }\r