001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.mail.gui.composer.util;
019:
020: import java.awt.Font;
021: import java.io.File;
022: import java.io.FileReader;
023: import java.io.FileWriter;
024: import java.io.IOException;
025:
026: import javax.swing.JOptionPane;
027:
028: import org.columba.core.desktop.ColumbaDesktop;
029: import org.columba.core.gui.frame.FrameManager;
030: import org.columba.core.gui.util.FontProperties;
031: import org.columba.core.util.TempFileStore;
032: import org.columba.mail.gui.composer.AbstractEditorController;
033: import org.columba.mail.util.MailResourceLoader;
034:
035: public class ExternalEditor {
036: String Cmd;
037:
038: public ExternalEditor() {
039: }
040:
041: // END public ExternalEditor()
042: public ExternalEditor(String EditorCommand) {
043: }
044:
045: private File writeToFile(
046: final AbstractEditorController editController) {
047:
048: File tmpFile = TempFileStore.createTempFileWithSuffix("txt");
049: FileWriter FO;
050:
051: try {
052: FO = new FileWriter(tmpFile);
053: } catch (java.io.IOException ex) {
054: JOptionPane.showMessageDialog(FrameManager.getInstance()
055: .getActiveFrame(),
056: "Error: Cannot write to temp file needed "
057: + "for external editor.");
058: return null;
059: }
060:
061: try {
062:
063: String M = editController.getViewText();
064: if (M != null)
065: FO.write(M);
066:
067: FO.close();
068: } catch (java.io.IOException ex) {
069: JOptionPane.showMessageDialog(FrameManager.getInstance()
070: .getActiveFrame(),
071: "Error: Cannot write to temp file needed "
072: + "for external editor:\n"
073: + ex.getMessage());
074: return null;
075: }
076:
077: return tmpFile;
078:
079: }
080:
081: private String readFromFile(File tmpFile) {
082: FileReader FI;
083: try {
084: FI = new FileReader(tmpFile);
085: } catch (java.io.FileNotFoundException ex) {
086: JOptionPane.showMessageDialog(FrameManager.getInstance()
087: .getActiveFrame(),
088: "Error: Cannot read from temp file used "
089: + "by external editor.");
090: return "";
091: }
092:
093: char[] buf = new char[1000];
094: int i;
095: String message = "";
096:
097: try {
098: while ((i = FI.read(buf)) >= 0)
099: message += new String(buf, 0, i);
100:
101: FI.close();
102: } catch (java.io.IOException ex) {
103: JOptionPane.showMessageDialog(null,
104: "Error: Cannot read from temp file used "
105: + "by external editor.");
106: return "";
107: }
108:
109: return message;
110: } // END public ExternalEditor(String EditorCommand)
111:
112: public boolean startExternalEditor(
113: final AbstractEditorController editController)
114: throws IOException {
115: /*
116: * *20030906, karlpeder* Method signature changed to take an
117: * AbstractEditorController (instead of an TextEditorView) as parameter
118: * since the view is no longer directly available
119: */
120:
121: // write text to file
122: File tmpFile = writeToFile(editController);
123:
124: // remember old font properties
125: final Font OldFont = editController.getViewFont();
126:
127: // create big size font to display in the composer textfield
128: Font font = FontProperties.getTextFont();
129: font = font.deriveFont(30);
130: editController.setViewFont(font);
131: editController.setViewText(MailResourceLoader.getString("menu",
132: "composer", "extern_editor_using_msg"));
133:
134: // execute application, enabling blocking
135: ColumbaDesktop.getInstance().openAndWait(tmpFile);
136:
137: // rafter the user saved the file and closed the
138: // external text editor, we read the new text from the file
139: final String message = readFromFile(tmpFile);
140:
141: // set old font properties
142: editController.setViewFont(OldFont);
143: // set new text
144: editController.setViewText(message);
145:
146: return true;
147: }
148:
149: // END public boolean startExternalEditor()
150: }
151:
152: // END public class ExternalEditor
|