1
2
3
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
25
26
27
28
29
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
51 fireElementStateChanging(element);
52 storage.setContents(stream, overwrite, monitor);
53 } catch (RuntimeException e) {
54
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 }