2 * (C) Copyright IBM Corp. 1998-2007. 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 com.ibm.richtext.awtui.TabRuler;
\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
21 import com.ibm.richtext.awtui.AwtMenuBuilder;
\r
23 import com.ibm.richtext.print.PrintingUtils;
\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
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
41 import java.io.File;
\r
43 import java.text.MessageFormat;
\r
46 * AwtDocumentWindow is a Frame containing a TextPanel, with a document
\r
47 * for storing the text in the TextPanel.
\r
49 public final class AwtDocumentWindow extends Frame implements DocumentWindow {
\r
54 private static final long serialVersionUID = -8075495366541764458L;
\r
56 //static final String COPYRIGHT =
\r
57 // "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
\r
59 private TextPanel fTextPanel;
\r
60 private EditApplication fApplication;
\r
61 private TextDocument fDocument;
\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
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
78 fApplication = application;
\r
80 fTextPanel = new TextPanel(textPanelSettings, null, application.getClipboard());
\r
81 if (listener != null) {
\r
82 fTextPanel.addListener(listener);
\r
84 setDocument(document);
\r
86 addMenuBar(supportStyledText, supportPlainText, menus);
\r
88 setLayout(new BorderLayout());
\r
91 TabRuler tabRuler = new TabRuler(14, 10, fTextPanel);
\r
92 add(tabRuler, "North");
\r
95 add(fTextPanel, "Center");
\r
97 addWindowListener(new WindowAdapter() {
\r
98 public void windowClosing(WindowEvent e) {
\r
104 private void addMenuBar(boolean supportStyledText,
\r
105 boolean supportPlainText,
\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
115 AwtMenuBuilder.getInstance().createMenus(menuBar, fTextPanel, this, menus);
\r
116 setMenuBar(menuBar);
\r
120 * Return true if it is OK to set the document text and file to
\r
121 * something different.
\r
123 private boolean canChangeDocuments() {
\r
125 // If the text is modified, give the user a chance to
\r
126 // save it. Otherwise return true.
\r
128 if (fDocument.isModified()) {
\r
129 byte save = askSave(this, getTitle());
\r
142 private void setDocument(TextDocument document) {
\r
144 fDocument = document;
\r
145 fDocument.setTextPanel(fTextPanel);
\r
146 setTitle(fDocument.getTitle());
\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
155 public void doNew() {
\r
157 if (!canChangeDocuments()) {
\r
161 setDocument(fApplication.createNewDocument());
\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
170 public void doOpen() {
\r
172 if (!canChangeDocuments()) {
\r
176 TextDocument document = fApplication.openDocument(this);
\r
178 if (document != null) {
\r
179 setDocument(document);
\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
188 public boolean doSaveAs(int format) {
\r
190 String title = ResourceUtils.getString(EditorResources.SAVE_TITLE);
\r
191 File file = getFileFromDialog(fDocument.getFile(), title, this, FileDialog.SAVE);
\r
193 if (file == null) {
\r
197 fDocument.setFile(file);
\r
198 setTitle(fDocument.getTitle());
\r
200 fDocument.setFormat(format);
\r
202 return fDocument.save();
\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
210 public boolean doSave() {
\r
212 if (fDocument.getFile() == null) {
\r
213 return doSaveAs(fDocument.getFormat());
\r
216 return fDocument.save();
\r
220 * Print the contents of this window.
\r
222 public void doPrint() {
\r
224 PrintingUtils.userPrintText(fDocument.getText(),
\r
225 fTextPanel.getDefaultValues(),
\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
237 public boolean doClose() {
\r
239 if (canChangeDocuments()) {
\r
242 fApplication.removeDocumentWindow(this);
\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
255 public static File getFileFromDialog(File origFile,
\r
256 String dialogTitle,
\r
260 FileDialog dialog = new FileDialog(owner,
\r
263 if (origFile != null) {
\r
264 dialog.setDirectory(origFile.getParent());
\r
265 dialog.setFile(origFile.getName());
\r
268 String fileStr = dialog.getFile();
\r
269 String dirStr = dialog.getDirectory();
\r
273 if (fileStr != null) {
\r
274 file = new File(dirStr, fileStr);
\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
284 private static final class DialogListener implements ActionListener {
\r
287 Button fYes, fNo, fCancel;
\r
290 DialogListener(Dialog dialog,
\r
299 fYes.addActionListener(this);
\r
300 fNo.addActionListener(this);
\r
301 fCancel.addActionListener(this);
\r
305 public void actionPerformed(ActionEvent event) {
\r
307 Object source = event.getSource();
\r
308 if (source == fYes) {
\r
311 else if (source == fNo) {
\r
314 else if (source == fCancel) {
\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
336 private static byte askSave(Frame parent, String fileName) {
\r
338 Dialog dialog = new Dialog(parent, true);
\r
339 dialog.setLayout(new GridLayout(0, 1));
\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
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
349 Panel panel = new Panel();
\r
355 DialogListener listener = new DialogListener(dialog, yes, no, cancel);
\r
357 dialog.setSize(220, 130);
\r
360 return listener.getState();
\r