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
15 import java.awt.Frame;
\r
16 import java.awt.datatransfer.Clipboard;
\r
18 import java.text.MessageFormat;
\r
20 import java.util.Vector;
\r
22 public abstract class EditApplication {
\r
24 private Clipboard fClipboard;
\r
25 private int fDefaultFormat;
\r
26 private Vector fWindows = new Vector();
\r
27 private int fUntitledCount = 0;
\r
29 protected EditApplication(Clipboard clipboard, int defaultFormat) {
\r
31 fClipboard = clipboard;
\r
32 fDefaultFormat = defaultFormat;
\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
40 private String getNextNewName() {
\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
48 public final Clipboard getClipboard() {
\r
53 protected abstract DocumentWindow createDocumentWindow(TextDocument document);
\r
55 public abstract TextDocument openDocument(Frame dialogParent);
\r
57 public final TextDocument createNewDocument() {
\r
59 String name = getNextNewName();
\r
60 int format = fDefaultFormat;
\r
61 return TextDocument.createEmpty(name, format);
\r
64 public final void doNewWindow() {
\r
66 addDocument(TextDocument.createEmpty(getNextNewName(), fDefaultFormat));
\r
69 public final void addDocument(TextDocument document) {
\r
71 final DocumentWindow window = createDocumentWindow(document);
\r
73 window.setSize(500, 400);
\r
75 fWindows.addElement(window);
\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
83 public final void removeDocumentWindow(DocumentWindow window) {
\r
85 fWindows.removeElement(window);
\r
86 if (fWindows.isEmpty()) {
\r
92 * Go through list of documents and attempt to close each document.
\r
93 * If all documents close successfully, then exit.
\r
95 public final void doExit() {
\r
97 // Clone fWindows since it can get modified while being traversed.
\r
98 Vector windows = (Vector) fWindows.clone();
\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
108 // quit will be called when last document removes itself
\r
112 * Called when last document window closes. Default implementation
\r
113 * calls System.exit.
\r
115 protected void quit() {
\r