]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/richtext/demo/AwtDocumentWindow.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / richtext / demo / AwtDocumentWindow.java
1 /*\r
2  * (C) Copyright IBM Corp. 1998-2007.  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 com.ibm.richtext.awtui.TabRuler;\r
16 \r
17 import com.ibm.richtext.textpanel.TextPanel;\r
18 import com.ibm.richtext.textpanel.TextPanelListener;\r
19 import com.ibm.richtext.textpanel.TextPanelSettings;\r
20 \r
21 import com.ibm.richtext.awtui.AwtMenuBuilder;\r
22 \r
23 import com.ibm.richtext.print.PrintingUtils;\r
24 \r
25 import java.awt.BorderLayout;\r
26 import java.awt.Button;\r
27 import java.awt.Dialog;\r
28 import java.awt.FileDialog;\r
29 import java.awt.GridLayout;\r
30 import java.awt.Frame;\r
31 import java.awt.Menu;\r
32 import java.awt.MenuBar;\r
33 import java.awt.Label;\r
34 import java.awt.Panel;\r
35 \r
36 import java.awt.event.ActionEvent;\r
37 import java.awt.event.ActionListener;\r
38 import java.awt.event.WindowAdapter;\r
39 import java.awt.event.WindowEvent;\r
40 \r
41 import java.io.File;\r
42 \r
43 import java.text.MessageFormat;\r
44 \r
45 /**\r
46  * AwtDocumentWindow is a Frame containing a TextPanel, with a document\r
47  * for storing the text in the TextPanel.\r
48  */\r
49 public final class AwtDocumentWindow extends Frame implements DocumentWindow {\r
50 \r
51     /**\r
52      * For serialization\r
53      */\r
54     private static final long serialVersionUID = -8075495366541764458L;\r
55 \r
56     //static final String COPYRIGHT =\r
57     //            "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";\r
58 \r
59     private TextPanel fTextPanel;\r
60     private EditApplication fApplication;\r
61     private TextDocument fDocument;\r
62 \r
63     /**\r
64      * Create a new AwtDocumentWindow.\r
65      * @param application the application that owns this document\r
66      * @param clipboard the clipboard to use\r
67      * @param document the document to show in this AwtDocumentWindow\r
68      */\r
69     AwtDocumentWindow(EditApplication application,\r
70                    TextDocument document,\r
71                    TextPanelSettings textPanelSettings,\r
72                    boolean useTabRuler,\r
73                    TextPanelListener listener,\r
74                    boolean supportStyledText,\r
75                    boolean supportPlainText,\r
76                    int[] menus) {\r
77 \r
78         fApplication = application;\r
79         \r
80         fTextPanel = new TextPanel(textPanelSettings, null, application.getClipboard());\r
81         if (listener != null) {\r
82             fTextPanel.addListener(listener);\r
83         }\r
84         setDocument(document);\r
85 \r
86         addMenuBar(supportStyledText, supportPlainText, menus);\r
87 \r
88         setLayout(new BorderLayout());\r
89 \r
90         if (useTabRuler) {\r
91             TabRuler tabRuler = new TabRuler(14, 10, fTextPanel);\r
92             add(tabRuler, "North");\r
93         }\r
94         \r
95         add(fTextPanel, "Center");\r
96         pack();\r
97         addWindowListener(new WindowAdapter() {\r
98             public void windowClosing(WindowEvent e) {\r
99                 doClose();\r
100             }\r
101         });\r
102     }\r
103 \r
104     private void addMenuBar(boolean supportStyledText, \r
105                             boolean supportPlainText,\r
106                             int[] menus) {\r
107         \r
108         MenuBar menuBar = new MenuBar();\r
109         String menuTitle = ResourceUtils.getString(EditorResources.FILE);\r
110         Menu menu = new Menu(menuTitle);\r
111         new AwtFileMenuManager(menu, fApplication, this,\r
112                                 supportStyledText, supportPlainText);\r
113         menuBar.add(menu);\r
114 \r
115         AwtMenuBuilder.getInstance().createMenus(menuBar, fTextPanel, this, menus);\r
116         setMenuBar(menuBar);\r
117     }\r
118     \r
119     /**\r
120      * Return true if it is OK to set the document text and file to\r
121      * something different.\r
122      */\r
123     private boolean canChangeDocuments() {\r
124 \r
125         // If the text is modified, give the user a chance to\r
126         // save it.  Otherwise return true.\r
127 \r
128         if (fDocument.isModified()) {\r
129             byte save = askSave(this, getTitle());\r
130             if (save == YES) {\r
131                 return doSave();\r
132             }\r
133             else {\r
134                 return save == NO;\r
135             }\r
136         }\r
137         else {\r
138             return true;\r
139         }\r
140     }\r
141 \r
142     private void setDocument(TextDocument document) {\r
143 \r
144         fDocument = document;\r
145         fDocument.setTextPanel(fTextPanel);\r
146         setTitle(fDocument.getTitle());\r
147     }\r
148 \r
149     /**\r
150      * Set the document to empty text with no associated file.  If\r
151      * the document text is not saved, prompt the user to save the\r
152      * the text first.  If this operation is canceled, the document\r
153      * is unchanged.\r
154      */\r
155     public void doNew() {\r
156 \r
157         if (!canChangeDocuments()) {\r
158             return;\r
159         }\r
160         \r
161         setDocument(fApplication.createNewDocument());\r
162     }\r
163 \r
164     /**\r
165      * Prompt the user for a file from which to load a text document.\r
166      * If the current text is not saved, first prompt the user to\r
167      * save.  If either operation is canceled or fails, the document\r
168      * is unchanged.\r
169      */\r
170     public void doOpen() {\r
171 \r
172         if (!canChangeDocuments()) {\r
173             return;\r
174         }\r
175 \r
176         TextDocument document = fApplication.openDocument(this);\r
177 \r
178         if (document != null) {\r
179             setDocument(document);\r
180         }\r
181     }\r
182 \r
183     /**\r
184      * Prompt the user for a file in which to save the document text.\r
185      * If this operation is not canceled, save the text in the file.\r
186      * The file becomes this document's file.\r
187      */\r
188     public boolean doSaveAs(int format) {\r
189 \r
190         String title = ResourceUtils.getString(EditorResources.SAVE_TITLE);\r
191         File file = getFileFromDialog(fDocument.getFile(), title, this, FileDialog.SAVE);\r
192         \r
193         if (file == null) {\r
194             return false;\r
195         }\r
196         \r
197         fDocument.setFile(file);\r
198         setTitle(fDocument.getTitle());\r
199 \r
200         fDocument.setFormat(format);\r
201         \r
202         return fDocument.save();\r
203     }\r
204 \r
205     /**\r
206      * Save the text in this document.  If there is no file associated\r
207      * with the text, this is equivalent to <code>doSaveAs</code>.\r
208      * This method returns true if the document was successfully saved.\r
209      */\r
210     public boolean doSave() {\r
211 \r
212         if (fDocument.getFile() == null) {\r
213             return doSaveAs(fDocument.getFormat());\r
214         }\r
215         \r
216         return fDocument.save();\r
217     }\r
218 \r
219     /**\r
220      * Print the contents of this window.\r
221      */\r
222     public void doPrint() {\r
223 \r
224         PrintingUtils.userPrintText(fDocument.getText(),\r
225                                     fTextPanel.getDefaultValues(),\r
226                                     this,\r
227                                     this.getTitle());\r
228     }\r
229 \r
230     /**\r
231      * Attempt to close this window.  If the text has not been saved,\r
232      * give the user a chance to save the text before closing the\r
233      * window.  If the user cancels this operation, this method returns\r
234      * false and the window is not closed;  otherwise this method\r
235      * returns true and the window is closed.\r
236      */\r
237     public boolean doClose() {\r
238 \r
239         if (canChangeDocuments()) {\r
240             setVisible(false);\r
241             dispose();\r
242             fApplication.removeDocumentWindow(this);\r
243             return true;\r
244         }\r
245         else {\r
246             return false;\r
247         }\r
248     }\r
249 \r
250     /**\r
251      * Retrieve a file from a dialog.  If the user does not\r
252      * select a file in the dialog this method returns null.\r
253      * @param kind either FileDialog.LOAD or FileDialog.SAVE.\r
254      */\r
255     public static File getFileFromDialog(File origFile,\r
256                                          String dialogTitle, \r
257                                          Frame owner,\r
258                                          int kind) {\r
259         \r
260         FileDialog dialog = new FileDialog(owner, \r
261                                            dialogTitle,\r
262                                            kind);\r
263         if (origFile != null) {\r
264             dialog.setDirectory(origFile.getParent());\r
265             dialog.setFile(origFile.getName());\r
266         }\r
267         dialog.show();\r
268         String fileStr = dialog.getFile();\r
269         String dirStr = dialog.getDirectory();\r
270         \r
271         File file = null;\r
272         \r
273         if (fileStr != null) {\r
274             file = new File(dirStr, fileStr);\r
275         }\r
276         \r
277         return file;\r
278     }\r
279     \r
280     private static final byte YES = 0;\r
281     private static final byte NO = 1;\r
282     private static final byte CANCEL = 2;\r
283 \r
284     private static final class DialogListener implements ActionListener {\r
285 \r
286         Dialog fDialog;\r
287         Button fYes, fNo, fCancel;\r
288         byte fState;\r
289 \r
290         DialogListener(Dialog dialog,\r
291                        Button yes,\r
292                        Button no,\r
293                        Button cancel) {\r
294 \r
295             fDialog = dialog;\r
296             fYes = yes;\r
297             fNo = no;\r
298             fCancel = cancel;\r
299             fYes.addActionListener(this);\r
300             fNo.addActionListener(this);\r
301             fCancel.addActionListener(this);\r
302             fState = -1;\r
303         }\r
304 \r
305         public void actionPerformed(ActionEvent event) {\r
306 \r
307             Object source = event.getSource();\r
308             if (source == fYes) {\r
309                 fState = YES;\r
310             }\r
311             else if (source == fNo) {\r
312                 fState = NO;\r
313             }\r
314             else if (source == fCancel) {\r
315                 fState = CANCEL;\r
316             }\r
317             else {\r
318                 return;\r
319             }\r
320 \r
321             fDialog.dispose();\r
322         }\r
323 \r
324         byte getState() {\r
325 \r
326             return fState;\r
327         }\r
328     }\r
329 \r
330     /**\r
331      * Display a dialog that asks whether the user wants to\r
332      * save a document.  Possible reponses are Yes, No, and\r
333      * Cancel.  The returned value indicates which response\r
334      * was chosen.\r
335      */\r
336     private static byte askSave(Frame parent, String fileName) {\r
337 \r
338         Dialog dialog = new Dialog(parent, true);\r
339         dialog.setLayout(new GridLayout(0, 1));\r
340         \r
341         String pattern = ResourceUtils.getString(EditorResources.SAVE_MSG);\r
342         String text = MessageFormat.format(pattern, new Object[] {fileName});\r
343         dialog.add(new Label(text, Label.CENTER));\r
344         \r
345         Button yes = new Button(ResourceUtils.getString(EditorResources.YES));\r
346         Button no = new Button(ResourceUtils.getString(EditorResources.NO));\r
347         Button cancel = new Button(ResourceUtils.getString(EditorResources.CANCEL));\r
348 \r
349         Panel panel = new Panel();\r
350         panel.add(yes);\r
351         panel.add(no);\r
352         panel.add(cancel);\r
353         dialog.add(panel);\r
354 \r
355         DialogListener listener = new DialogListener(dialog, yes, no, cancel);\r
356 \r
357         dialog.setSize(220, 130);\r
358         dialog.show();\r
359 \r
360         return listener.getState();\r
361     }\r
362 }