View Javadoc

1   /*
2    * (c) Copyright QNX Software Systems Ltd. 2002.
3    * All Rights Reserved.
4    */
5   package de.matthias_burbach.mosaique4eclipse.util;
6   
7   import java.io.ByteArrayInputStream;
8   import java.io.IOException;
9   import java.io.InputStream;
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       * (non-Javadoc)
25       * @see org.eclipse.ui.texteditor.AbstractDocumentProvider#doSaveDocument(
26       *          org.eclipse.core.runtime.IProgressMonitor,
27       *          java.lang.Object,
28       *          org.eclipse.jface.text.IDocument,
29       *          boolean)
30       */
31      /***
32       * {@inheritDoc}
33       */
34      protected void doSaveDocument(
35              final IProgressMonitor monitor,
36              final Object element,
37              final IDocument document,
38              final boolean overwrite) throws CoreException {
39          if (element instanceof ExternalEditorInput) {
40              ExternalEditorInput external = (ExternalEditorInput) element;
41              FileStorage storage = (FileStorage) external.getStorage();
42              String encoding = getEncoding(element);
43              if (encoding == null) {
44                  encoding = getDefaultEncoding();
45              }
46              try {
47                  InputStream stream = new ByteArrayInputStream(document.get()
48                          .getBytes(encoding));
49                  try {
50                      // inform about the upcoming content change
51                      fireElementStateChanging(element);
52                      storage.setContents(stream, overwrite, monitor);
53                  } catch (RuntimeException e) {
54                      // inform about failure
55                      fireElementStateChangeFailed(element);
56                      throw e;
57                  }
58              } catch (IOException e) {
59                  IStatus s = new Status(IStatus.ERROR, "Mosaique", IStatus.OK, e
60                          .getMessage(), e);
61                  throw new CoreException(s);
62              }
63  
64          } else {
65              super.doSaveDocument(monitor, element, document, overwrite);
66          }
67      }
68  }