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.Frame;
016: import java.awt.datatransfer.Clipboard;
017:
018: import java.text.MessageFormat;
019:
020: import java.util.Vector;
021:
022: public abstract class EditApplication {
023:
024: private Clipboard fClipboard;
025: private int fDefaultFormat;
026: private Vector fWindows = new Vector();
027: private int fUntitledCount = 0;
028:
029: protected EditApplication(Clipboard clipboard, int defaultFormat) {
030:
031: fClipboard = clipboard;
032: fDefaultFormat = defaultFormat;
033: }
034:
035: /**
036: * New documents are named "Untitled 1", "Untitled 2", etc. This
037: * method returns the appropriate name for the next new document.
038: * @return the next new document name
039: */
040: private String getNextNewName() {
041:
042: fUntitledCount += 1;
043: String pattern = ResourceUtils
044: .getString(EditorResources.UNTITLED_MSG);
045: return MessageFormat.format(pattern,
046: new Object[] { new Integer(fUntitledCount) });
047: }
048:
049: public final Clipboard getClipboard() {
050:
051: return fClipboard;
052: }
053:
054: protected abstract DocumentWindow createDocumentWindow(
055: TextDocument document);
056:
057: public abstract TextDocument openDocument(Frame dialogParent);
058:
059: public final TextDocument createNewDocument() {
060:
061: String name = getNextNewName();
062: int format = fDefaultFormat;
063: return TextDocument.createEmpty(name, format);
064: }
065:
066: public final void doNewWindow() {
067:
068: addDocument(TextDocument.createEmpty(getNextNewName(),
069: fDefaultFormat));
070: }
071:
072: public final void addDocument(TextDocument document) {
073:
074: final DocumentWindow window = createDocumentWindow(document);
075:
076: window.setSize(500, 400);
077: window.show();
078: fWindows.addElement(window);
079: }
080:
081: /**
082: * Remove document from list of documents. Quit application if list
083: * length falls to zero.
084: * @param window window of the document to remove
085: */
086: public final void removeDocumentWindow(DocumentWindow window) {
087:
088: fWindows.removeElement(window);
089: if (fWindows.isEmpty()) {
090: quit();
091: }
092: }
093:
094: /**
095: * Go through list of documents and attempt to close each document.
096: * If all documents close successfully, then exit.
097: */
098: public final void doExit() {
099:
100: // Clone fWindows since it can get modified while being traversed.
101: Vector windows = (Vector) fWindows.clone();
102:
103: int size = windows.size();
104: for (int i = 0; i < size; i++) {
105: DocumentWindow window = (DocumentWindow) windows
106: .elementAt(i);
107: if (!window.doClose()) {
108: return;
109: }
110: }
111:
112: // quit will be called when last document removes itself
113: }
114:
115: /**
116: * Called when last document window closes. Default implementation
117: * calls System.exit.
118: */
119: protected void quit() {
120:
121: System.exit(0);
122: }
123: }
|