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 com.ibm.richtext.awtui.TabRuler;
016:
017: import com.ibm.richtext.textpanel.TextPanel;
018: import com.ibm.richtext.textpanel.TextPanelListener;
019: import com.ibm.richtext.textpanel.TextPanelSettings;
020:
021: import com.ibm.richtext.awtui.AwtMenuBuilder;
022:
023: import com.ibm.richtext.print.PrintingUtils;
024:
025: import java.awt.BorderLayout;
026: import java.awt.Button;
027: import java.awt.Dialog;
028: import java.awt.FileDialog;
029: import java.awt.GridLayout;
030: import java.awt.Frame;
031: import java.awt.Menu;
032: import java.awt.MenuBar;
033: import java.awt.Label;
034: import java.awt.Panel;
035:
036: import java.awt.event.ActionEvent;
037: import java.awt.event.ActionListener;
038: import java.awt.event.WindowAdapter;
039: import java.awt.event.WindowEvent;
040:
041: import java.io.File;
042:
043: import java.text.MessageFormat;
044:
045: /**
046: * AwtDocumentWindow is a Frame containing a TextPanel, with a document
047: * for storing the text in the TextPanel.
048: */
049: public final class AwtDocumentWindow extends Frame implements
050: DocumentWindow {
051:
052: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
053:
054: private TextPanel fTextPanel;
055: private EditApplication fApplication;
056: private TextDocument fDocument;
057:
058: /**
059: * Create a new AwtDocumentWindow.
060: * @param application the application that owns this document
061: * @param clipboard the clipboard to use
062: * @param document the document to show in this AwtDocumentWindow
063: */
064: AwtDocumentWindow(EditApplication application,
065: TextDocument document, TextPanelSettings textPanelSettings,
066: boolean useTabRuler, TextPanelListener listener,
067: boolean supportStyledText, boolean supportPlainText,
068: int[] menus) {
069:
070: fApplication = application;
071:
072: fTextPanel = new TextPanel(textPanelSettings, null, application
073: .getClipboard());
074: if (listener != null) {
075: fTextPanel.addListener(listener);
076: }
077: setDocument(document);
078:
079: addMenuBar(supportStyledText, supportPlainText, menus);
080:
081: setLayout(new BorderLayout());
082:
083: if (useTabRuler) {
084: TabRuler tabRuler = new TabRuler(14, 10, fTextPanel);
085: add(tabRuler, "North");
086: }
087:
088: add(fTextPanel, "Center");
089: pack();
090: addWindowListener(new WindowAdapter() {
091: public void windowClosing(WindowEvent e) {
092: doClose();
093: }
094: });
095: }
096:
097: private void addMenuBar(boolean supportStyledText,
098: boolean supportPlainText, int[] menus) {
099:
100: MenuBar menuBar = new MenuBar();
101: String menuTitle = ResourceUtils
102: .getString(EditorResources.FILE);
103: Menu menu = new Menu(menuTitle);
104: new AwtFileMenuManager(menu, fApplication, this ,
105: supportStyledText, supportPlainText);
106: menuBar.add(menu);
107:
108: AwtMenuBuilder.getInstance().createMenus(menuBar, fTextPanel,
109: this , menus);
110: setMenuBar(menuBar);
111: }
112:
113: /**
114: * Return true if it is OK to set the document text and file to
115: * something different.
116: */
117: private boolean canChangeDocuments() {
118:
119: // If the text is modified, give the user a chance to
120: // save it. Otherwise return true.
121:
122: if (fDocument.isModified()) {
123: byte save = askSave(this , getTitle());
124: if (save == YES) {
125: return doSave();
126: } else {
127: return save == NO;
128: }
129: } else {
130: return true;
131: }
132: }
133:
134: private void setDocument(TextDocument document) {
135:
136: fDocument = document;
137: fDocument.setTextPanel(fTextPanel);
138: setTitle(fDocument.getTitle());
139: }
140:
141: /**
142: * Set the document to empty text with no associated file. If
143: * the document text is not saved, prompt the user to save the
144: * the text first. If this operation is canceled, the document
145: * is unchanged.
146: */
147: public void doNew() {
148:
149: if (!canChangeDocuments()) {
150: return;
151: }
152:
153: setDocument(fApplication.createNewDocument());
154: }
155:
156: /**
157: * Prompt the user for a file from which to load a text document.
158: * If the current text is not saved, first prompt the user to
159: * save. If either operation is canceled or fails, the document
160: * is unchanged.
161: */
162: public void doOpen() {
163:
164: if (!canChangeDocuments()) {
165: return;
166: }
167:
168: TextDocument document = fApplication.openDocument(this );
169:
170: if (document != null) {
171: setDocument(document);
172: }
173: }
174:
175: /**
176: * Prompt the user for a file in which to save the document text.
177: * If this operation is not canceled, save the text in the file.
178: * The file becomes this document's file.
179: */
180: public boolean doSaveAs(int format) {
181:
182: String title = ResourceUtils
183: .getString(EditorResources.SAVE_TITLE);
184: File file = getFileFromDialog(fDocument.getFile(), title, this ,
185: FileDialog.SAVE);
186:
187: if (file == null) {
188: return false;
189: }
190:
191: fDocument.setFile(file);
192: setTitle(fDocument.getTitle());
193:
194: fDocument.setFormat(format);
195:
196: return fDocument.save();
197: }
198:
199: /**
200: * Save the text in this document. If there is no file associated
201: * with the text, this is equivalent to <code>doSaveAs</code>.
202: * This method returns true if the document was successfully saved.
203: */
204: public boolean doSave() {
205:
206: if (fDocument.getFile() == null) {
207: return doSaveAs(fDocument.getFormat());
208: }
209:
210: return fDocument.save();
211: }
212:
213: /**
214: * Print the contents of this window.
215: */
216: public void doPrint() {
217:
218: PrintingUtils.userPrintText(fDocument.getText(), fTextPanel
219: .getDefaultValues(), this , this .getTitle());
220: }
221:
222: /**
223: * Attempt to close this window. If the text has not been saved,
224: * give the user a chance to save the text before closing the
225: * window. If the user cancels this operation, this method returns
226: * false and the window is not closed; otherwise this method
227: * returns true and the window is closed.
228: */
229: public boolean doClose() {
230:
231: if (canChangeDocuments()) {
232: setVisible(false);
233: dispose();
234: fApplication.removeDocumentWindow(this );
235: return true;
236: } else {
237: return false;
238: }
239: }
240:
241: /**
242: * Retrieve a file from a dialog. If the user does not
243: * select a file in the dialog this method returns null.
244: * @param kind either FileDialog.LOAD or FileDialog.SAVE.
245: */
246: public static File getFileFromDialog(File origFile,
247: String dialogTitle, Frame owner, int kind) {
248:
249: FileDialog dialog = new FileDialog(owner, dialogTitle, kind);
250: if (origFile != null) {
251: dialog.setDirectory(origFile.getParent());
252: dialog.setFile(origFile.getName());
253: }
254: dialog.show();
255: String fileStr = dialog.getFile();
256: String dirStr = dialog.getDirectory();
257:
258: File file = null;
259:
260: if (fileStr != null) {
261: file = new File(dirStr, fileStr);
262: }
263:
264: return file;
265: }
266:
267: private static final byte YES = 0;
268: private static final byte NO = 1;
269: private static final byte CANCEL = 2;
270:
271: private static final class DialogListener implements ActionListener {
272:
273: Dialog fDialog;
274: Button fYes, fNo, fCancel;
275: byte fState;
276:
277: DialogListener(Dialog dialog, Button yes, Button no,
278: Button cancel) {
279:
280: fDialog = dialog;
281: fYes = yes;
282: fNo = no;
283: fCancel = cancel;
284: fYes.addActionListener(this );
285: fNo.addActionListener(this );
286: fCancel.addActionListener(this );
287: fState = -1;
288: }
289:
290: public void actionPerformed(ActionEvent event) {
291:
292: Object source = event.getSource();
293: if (source == fYes) {
294: fState = YES;
295: } else if (source == fNo) {
296: fState = NO;
297: } else if (source == fCancel) {
298: fState = CANCEL;
299: } else {
300: return;
301: }
302:
303: fDialog.dispose();
304: }
305:
306: byte getState() {
307:
308: return fState;
309: }
310: }
311:
312: /**
313: * Display a dialog that asks whether the user wants to
314: * save a document. Possible reponses are Yes, No, and
315: * Cancel. The returned value indicates which response
316: * was chosen.
317: */
318: private static byte askSave(Frame parent, String fileName) {
319:
320: Dialog dialog = new Dialog(parent, true);
321: dialog.setLayout(new GridLayout(0, 1));
322:
323: String pattern = ResourceUtils
324: .getString(EditorResources.SAVE_MSG);
325: String text = MessageFormat.format(pattern,
326: new Object[] { fileName });
327: dialog.add(new Label(text, Label.CENTER));
328:
329: Button yes = new Button(ResourceUtils
330: .getString(EditorResources.YES));
331: Button no = new Button(ResourceUtils
332: .getString(EditorResources.NO));
333: Button cancel = new Button(ResourceUtils
334: .getString(EditorResources.CANCEL));
335:
336: Panel panel = new Panel();
337: panel.add(yes);
338: panel.add(no);
339: panel.add(cancel);
340: dialog.add(panel);
341:
342: DialogListener listener = new DialogListener(dialog, yes, no,
343: cancel);
344:
345: dialog.setSize(220, 130);
346: dialog.show();
347:
348: return listener.getState();
349: }
350: }
|