56c01ad9e68a16682d22bbae86d359da746c169d
[phpeclipse.git] /
1 package net.sourceforge.phpeclipse.ui;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6
7 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
8 import net.sourceforge.phpeclipse.builder.ExternalEditorInput;
9 import net.sourceforge.phpeclipse.builder.FileStorage;
10
11 import org.eclipse.core.runtime.CoreException;
12 import org.eclipse.core.runtime.IProgressMonitor;
13 import org.eclipse.core.runtime.IStatus;
14 import org.eclipse.core.runtime.Status;
15 import org.eclipse.jface.text.IDocument;
16 import org.eclipse.ui.editors.text.StorageDocumentProvider;
17
18 /**
19  * @author ed
20  * @version 1.0, May 19, 2003
21  */
22 public class ExternalStorageDocumentProvider extends StorageDocumentProvider {
23
24         /*
25          * (non-Javadoc)
26          * 
27          * @see org.eclipse.ui.texteditor.AbstractDocumentProvider#doSaveDocument(org.eclipse.core.runtime.IProgressMonitor,
28          *      java.lang.Object, org.eclipse.jface.text.IDocument, boolean)
29          */
30         protected void doSaveDocument(IProgressMonitor monitor, Object element,
31                         IDocument document, boolean overwrite) throws CoreException {
32                 if (element instanceof ExternalEditorInput) {
33                         ExternalEditorInput external = (ExternalEditorInput) element;
34                         FileStorage storage = (FileStorage) external.getStorage();
35                         String encoding = getEncoding(element);
36                         if (encoding == null)
37                                 encoding = getDefaultEncoding();
38                         try {
39                                 InputStream stream = new ByteArrayInputStream(document.get()
40                                                 .getBytes(encoding));
41                                 try {
42                                         // inform about the upcoming content change
43                                         fireElementStateChanging(element);
44                                         storage.setContents(stream, overwrite, true, monitor);
45                                 } catch (RuntimeException e) {
46                                         // inform about failure
47                                         fireElementStateChangeFailed(element);
48                                         throw e;
49                                 }
50                         } catch (IOException e) {
51                                 IStatus s = new Status(IStatus.ERROR,
52                                                 PHPeclipsePlugin.PLUGIN_ID, IStatus.OK, e.getMessage(),
53                                                 e);
54                                 throw new CoreException(s);
55                         }
56
57                 } else {
58                         super.doSaveDocument(monitor, element, document, overwrite);
59                 }
60         }
61
62 }