KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > composer > util > ExternalEditor


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.mail.gui.composer.util;
19
20 import java.awt.Font JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileReader JavaDoc;
23 import java.io.FileWriter JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 import javax.swing.JOptionPane JavaDoc;
27
28 import org.columba.core.desktop.ColumbaDesktop;
29 import org.columba.core.gui.frame.FrameManager;
30 import org.columba.core.gui.util.FontProperties;
31 import org.columba.core.util.TempFileStore;
32 import org.columba.mail.gui.composer.AbstractEditorController;
33 import org.columba.mail.util.MailResourceLoader;
34
35 public class ExternalEditor {
36     String JavaDoc Cmd;
37
38     public ExternalEditor() {
39     }
40
41     // END public ExternalEditor()
42
public ExternalEditor(String JavaDoc EditorCommand) {
43     }
44
45     private File JavaDoc writeToFile(final AbstractEditorController editController) {
46
47         File JavaDoc tmpFile = TempFileStore.createTempFileWithSuffix("txt");
48         FileWriter JavaDoc FO;
49
50         try {
51             FO = new FileWriter JavaDoc(tmpFile);
52         } catch (java.io.IOException JavaDoc ex) {
53             JOptionPane.showMessageDialog(FrameManager.getInstance()
54                     .getActiveFrame(),
55                     "Error: Cannot write to temp file needed "
56                             + "for external editor.");
57             return null;
58         }
59
60         try {
61
62             String JavaDoc M = editController.getViewText();
63             if (M != null)
64                 FO.write(M);
65
66             FO.close();
67         } catch (java.io.IOException JavaDoc ex) {
68             JOptionPane.showMessageDialog(FrameManager.getInstance()
69                     .getActiveFrame(),
70                     "Error: Cannot write to temp file needed "
71                             + "for external editor:\n" + ex.getMessage());
72             return null;
73         }
74
75         return tmpFile;
76
77     }
78
79     private String JavaDoc readFromFile(File JavaDoc tmpFile) {
80         FileReader JavaDoc FI;
81         try {
82             FI = new FileReader JavaDoc(tmpFile);
83         } catch (java.io.FileNotFoundException JavaDoc ex) {
84             JOptionPane.showMessageDialog(FrameManager.getInstance()
85                     .getActiveFrame(),
86                     "Error: Cannot read from temp file used "
87                             + "by external editor.");
88             return "";
89         }
90
91         char[] buf = new char[1000];
92         int i;
93         String JavaDoc message = "";
94
95         try {
96             while ((i = FI.read(buf)) >= 0)
97                 message += new String JavaDoc(buf, 0, i);
98
99             FI.close();
100         } catch (java.io.IOException JavaDoc ex) {
101             JOptionPane.showMessageDialog(null,
102                     "Error: Cannot read from temp file used "
103                             + "by external editor.");
104             return "";
105         }
106
107         return message;
108     } // END public ExternalEditor(String EditorCommand)
109

110     public boolean startExternalEditor(
111             final AbstractEditorController editController) throws IOException JavaDoc {
112         /*
113          * *20030906, karlpeder* Method signature changed to take an
114          * AbstractEditorController (instead of an TextEditorView) as parameter
115          * since the view is no longer directly available
116          */

117
118         // write text to file
119
File JavaDoc tmpFile = writeToFile(editController);
120
121         // remember old font properties
122
final Font JavaDoc OldFont = editController.getViewFont();
123
124         // create big size font to display in the composer textfield
125
Font JavaDoc font = FontProperties.getTextFont();
126         font = font.deriveFont(30);
127         editController.setViewFont(font);
128         editController.setViewText(MailResourceLoader.getString("menu",
129                 "composer", "extern_editor_using_msg"));
130
131         // execute application, enabling blocking
132
ColumbaDesktop.getInstance().openAndWait(tmpFile);
133
134         // rafter the user saved the file and closed the
135
// external text editor, we read the new text from the file
136
final String JavaDoc message = readFromFile(tmpFile);
137
138         // set old font properties
139
editController.setViewFont(OldFont);
140         // set new text
141
editController.setViewText(message);
142
143         return true;
144     }
145
146     // END public boolean startExternalEditor()
147
}
148
149 // END public class ExternalEditor
150
Popular Tags