001: /*
002: * (C) Copyright IBM Corp. 1998-2005. 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.swingdemo;
014:
015: import com.ibm.richtext.textpanel.JTextPanel;
016: import com.ibm.richtext.textpanel.TextPanelListener;
017: import com.ibm.richtext.textpanel.TextPanelSettings;
018:
019: import com.ibm.richtext.swingui.JTabRuler;
020: import com.ibm.richtext.swingui.SwingMenuBuilder;
021:
022: import com.ibm.richtext.print.PrintingUtils;
023:
024: import com.ibm.richtext.demo.AwtDocumentWindow;
025: import com.ibm.richtext.demo.DocumentWindow;
026: import com.ibm.richtext.demo.EditApplication;
027: import com.ibm.richtext.demo.EditorResources;
028: import com.ibm.richtext.demo.ResourceUtils;
029: import com.ibm.richtext.demo.TextDocument;
030:
031: import java.awt.BorderLayout;
032: import java.awt.FileDialog;
033:
034: import javax.swing.JFrame;
035: import javax.swing.JMenu;
036: import javax.swing.JMenuBar;
037: import javax.swing.JOptionPane;
038: import javax.swing.WindowConstants;
039:
040: import java.awt.event.WindowAdapter;
041: import java.awt.event.WindowEvent;
042:
043: import java.io.File;
044:
045: import java.text.MessageFormat;
046:
047: /**
048: * AwtDocumentWindow is a Frame containing a TextPanel, with a document
049: * for storing the text in the TextPanel.
050: */
051: final class SwingDocumentWindow extends JFrame implements
052: DocumentWindow {
053:
054: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
055:
056: private JTextPanel fTextPanel;
057: private EditApplication fApplication;
058: private TextDocument fDocument;
059:
060: /**
061: * Create a new AwtDocumentWindow.
062: * @param application the application that owns this document
063: * @param document the document to show in this AwtDocumentWindow
064: */
065: SwingDocumentWindow(EditApplication application,
066: TextDocument document, TextPanelSettings textPanelSettings,
067: boolean useTabRuler, TextPanelListener listener,
068: boolean supportStyledText, boolean supportPlainText,
069: int[] menus) {
070:
071: fApplication = application;
072: setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
073:
074: fTextPanel = new JTextPanel(textPanelSettings, null,
075: application.getClipboard());
076: if (listener != null) {
077: fTextPanel.addListener(listener);
078: }
079: setDocument(document);
080:
081: addMenuBar(supportStyledText, supportPlainText, menus);
082:
083: getContentPane().setLayout(new BorderLayout());
084:
085: if (useTabRuler) {
086: JTabRuler tabRuler = new JTabRuler(14, 10, fTextPanel);
087: getContentPane().add(tabRuler, "North");
088: }
089:
090: getContentPane().add(fTextPanel, "Center");
091:
092: addWindowListener(new WindowAdapter() {
093: public void windowClosing(WindowEvent e) {
094: doClose();
095: }
096: });
097: }
098:
099: private void addMenuBar(boolean supportStyledText,
100: boolean supportPlainText, int[] menus) {
101:
102: JMenuBar menuBar = new JMenuBar();
103: String menuTitle = ResourceUtils
104: .getString(EditorResources.FILE);
105: JMenu menu = new JMenu(menuTitle);
106: new SwingFileMenuManager(menu, fApplication, this ,
107: supportStyledText, supportPlainText);
108: menuBar.add(menu);
109:
110: SwingMenuBuilder.getInstance().createMenus(menuBar, fTextPanel,
111: this , menus);
112: setJMenuBar(menuBar);
113: }
114:
115: /**
116: * Return true if it is OK to set the document text and file to
117: * something different.
118: */
119: private boolean canChangeDocuments() {
120:
121: // If the text is modified, give the user a chance to
122: // save it. Otherwise return true.
123:
124: if (fDocument.isModified()) {
125: int save = askSave();
126: if (save == JOptionPane.YES_OPTION) {
127: return doSave();
128: } else {
129: return save == JOptionPane.NO_OPTION;
130: }
131: } else {
132: return true;
133: }
134: }
135:
136: private void setDocument(TextDocument document) {
137:
138: fDocument = document;
139: fDocument.setTextPanel(fTextPanel);
140: setTitle(fDocument.getTitle());
141: }
142:
143: /**
144: * Set the document to empty text with no associated file. If
145: * the document text is not saved, prompt the user to save the
146: * the text first. If this operation is canceled, the document
147: * is unchanged.
148: */
149: public void doNew() {
150:
151: if (!canChangeDocuments()) {
152: return;
153: }
154:
155: setDocument(fApplication.createNewDocument());
156: }
157:
158: /**
159: * Prompt the user for a file from which to load a text document.
160: * If the current text is not saved, first prompt the user to
161: * save. If either operation is canceled or fails, the document
162: * is unchanged.
163: */
164: public void doOpen() {
165:
166: if (!canChangeDocuments()) {
167: return;
168: }
169:
170: TextDocument document = fApplication.openDocument(this );
171:
172: if (document != null) {
173: setDocument(document);
174: }
175: }
176:
177: /**
178: * Prompt the user for a file in which to save the document text.
179: * If this operation is not canceled, save the text in the file.
180: * The file becomes this document's file.
181: */
182: public boolean doSaveAs(int format) {
183:
184: String title = ResourceUtils
185: .getString(EditorResources.SAVE_TITLE);
186: File file = AwtDocumentWindow.getFileFromDialog(fDocument
187: .getFile(), title, this , FileDialog.SAVE);
188:
189: if (file == null) {
190: return false;
191: }
192:
193: fDocument.setFile(file);
194: setTitle(fDocument.getTitle());
195:
196: fDocument.setFormat(format);
197:
198: return fDocument.save();
199: }
200:
201: /**
202: * Save the text in this document. If there is no file associated
203: * with the text, this is equivalent to <code>doSaveAs</code>.
204: * This method returns true if the document was successfully saved.
205: */
206: public boolean doSave() {
207:
208: if (fDocument.getFile() == null) {
209: return doSaveAs(fDocument.getFormat());
210: }
211:
212: return fDocument.save();
213: }
214:
215: /**
216: * Print the contents of this window.
217: */
218: public void doPrint() {
219:
220: PrintingUtils.userPrintText(fDocument.getText(), fTextPanel
221: .getDefaultValues(), this , this .getTitle());
222: }
223:
224: /**
225: * Attempt to close this window. If the text has not been saved,
226: * give the user a chance to save the text before closing the
227: * window. If the user cancels this operation, this method returns
228: * false and the window is not closed; otherwise this method
229: * returns true and the window is closed.
230: */
231: public boolean doClose() {
232:
233: if (canChangeDocuments()) {
234: setVisible(false);
235: dispose();
236: fApplication.removeDocumentWindow(this );
237: return true;
238: } else {
239: return false;
240: }
241: }
242:
243: /**
244: * Display a dialog that asks whether the user wants to
245: * save a document. The returned value will be YES_OPTION,
246: * NO_OPTION, or CANCEL_OPTION from JOptionPane.
247: */
248: private int askSave() {
249:
250: String pattern = ResourceUtils
251: .getString(EditorResources.SAVE_MSG);
252: String message = MessageFormat.format(pattern,
253: new Object[] { getTitle() });
254:
255: String yes = ResourceUtils.getString(EditorResources.YES);
256: String no = ResourceUtils.getString(EditorResources.NO);
257: String cancel = ResourceUtils.getString(EditorResources.CANCEL);
258:
259: return JOptionPane.showOptionDialog(this , message, "",
260: JOptionPane.YES_NO_CANCEL_OPTION,
261: JOptionPane.QUESTION_MESSAGE, null, new Object[] { yes,
262: no, cancel }, yes);
263: }
264: }
|