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