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.event.ActionListener;
016: import java.awt.event.ActionEvent;
017:
018: /**
019: * This class creates a File menu and manages user interactions
020: * with the menu.
021: */
022: public abstract class FileMenuManager implements ActionListener {
023:
024: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
025: private EditApplication fApplication;
026: private DocumentWindow fDocumentWindow;
027: private Object fNew, fNewWindow, fOpen, fSave;
028: private Object fSaveAsStyled, fSaveAsText, fClose, fPrint, fExit;
029:
030: protected FileMenuManager(EditApplication application,
031: DocumentWindow document) {
032:
033: fApplication = application;
034: fDocumentWindow = document;
035: }
036:
037: protected final void createItems(boolean supportStyledFormat,
038: boolean supportPlainFormat) {
039:
040: if (!supportStyledFormat && !supportPlainFormat) {
041: throw new IllegalArgumentException(
042: "Must support at least one format.");
043: }
044:
045: fNew = addMenuItem(EditorResources.NEW);
046: fNewWindow = addMenuItem(EditorResources.NEW_WINDOW);
047:
048: addSeparator();
049:
050: fOpen = addMenuItem(EditorResources.OPEN);
051:
052: fSave = addMenuItem(EditorResources.SAVE);
053:
054: if (supportStyledFormat) {
055: if (supportPlainFormat) {
056: fSaveAsStyled = addMenuItem(EditorResources.SAVE_AS_STYLED);
057: fSaveAsText = addMenuItem(EditorResources.SAVE_AS_TEXT);
058: } else {
059: fSaveAsStyled = addMenuItem(EditorResources.SAVE_AS);
060: }
061: } else {
062: fSaveAsText = addMenuItem(EditorResources.SAVE_AS);
063: }
064:
065: addSeparator();
066: fClose = addMenuItem(EditorResources.CLOSE);
067: addSeparator();
068: fPrint = addMenuItem(EditorResources.PRINT);
069: addSeparator();
070: fExit = addMenuItem(EditorResources.EXIT);
071: }
072:
073: protected abstract Object addMenuItem(String key);
074:
075: protected abstract void addSeparator();
076:
077: public final void actionPerformed(ActionEvent event) {
078:
079: Object source = event.getSource();
080:
081: if (source == fNew) {
082: fDocumentWindow.doNew();
083: } else if (source == fNewWindow) {
084: fApplication.doNewWindow();
085: } else if (source == fOpen) {
086: fDocumentWindow.doOpen();
087: } else if (source == fClose) {
088: fDocumentWindow.doClose();
089: } else if (source == fSave) {
090: fDocumentWindow.doSave();
091: } else if (source == fSaveAsStyled) {
092: fDocumentWindow.doSaveAs(TextDocument.STYLED_TEXT);
093: } else if (source == fSaveAsText) {
094: fDocumentWindow.doSaveAs(TextDocument.PLAIN_TEXT);
095: } else if (source == fPrint) {
096: fDocumentWindow.doPrint();
097: } else if (source == fExit) {
098: fApplication.doExit();
099: } else {
100: throw new Error("Unknown event source: " + source);
101: }
102: }
103: }
|