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