Method: createDocument(Object)

1: /*
2: * #%~
3: * org.overture.ide.ui
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.ui.editor.core;
23:
24: import org.eclipse.core.resources.IFile;
25: import org.eclipse.core.runtime.Assert;
26: import org.eclipse.core.runtime.CoreException;
27: import org.eclipse.jface.text.IDocument;
28: import org.eclipse.jface.text.IDocumentExtension3;
29: import org.eclipse.jface.text.IDocumentPartitioner;
30: import org.eclipse.ui.IEditorInput;
31: import org.eclipse.ui.IStorageEditorInput;
32: import org.eclipse.ui.editors.text.FileDocumentProvider;
33: import org.eclipse.ui.part.FileEditorInput;
34: import org.overture.ide.core.resources.IVdmProject;
35: import org.overture.ide.core.resources.IVdmSourceUnit;
36: import org.overture.ide.core.utility.FileUtility;
37: import org.overture.ide.ui.VdmUIPlugin;
38: import org.overture.ide.ui.editor.partitioning.IVdmPartitions;
39: import org.overture.ide.ui.editor.partitioning.VdmDocumentPartitioner;
40: import org.overture.ide.ui.editor.partitioning.VdmPartitionScanner;
41:
42: public class VdmExternalDocumentProvider extends FileDocumentProvider
43: {
44:         
45:         protected boolean isExternalAssociated(IFile file)
46:         {
47:                 return IVdmProject.externalFileContentType.isAssociatedWith(file.getName());
48:         }
49:
50:         @Override
51:         protected IDocument createDocument(Object element) throws CoreException
52:         {
53:                 IDocument document = null;
54:
55:•                if (element instanceof FileEditorInput)
56:                 {
57:                         IFile file = ((FileEditorInput) element).getFile();
58:•                        if (isExternalAssociated(file))
59:                         {
60:                                 document = new VdmExternalDocument();
61:•                                if (setDocumentContent(document, (IEditorInput) element, getEncoding(element)))
62:                                 {
63:                                         setupDocument(element, document);
64:                                 }
65:                         }
66:                 }
67:
68:•                if (document == null)
69:                 {
70:                         document = super.createDocument(element);
71:                 }
72:
73:•                if (element instanceof FileEditorInput)
74:                 {
75:•                        if (document instanceof VdmDocument)
76:                         {
77:                                 IFile file = ((FileEditorInput) element).getFile();
78:
79:                                 IVdmProject vdmProject = (IVdmProject) file.getProject().getAdapter(IVdmProject.class);
80:
81:                                 Assert.isNotNull(vdmProject, "Project of file: "
82:                                                 + file.getName() + " is not VDM");
83:
84:•                                if (vdmProject != null)
85:                                 {
86:
87:                                         IVdmSourceUnit source = vdmProject.findSourceUnit(file);
88:•                                        if (source != null)
89:                                         {
90:                                                 ((VdmDocument) document).setSourceUnit(source);
91:                                         } else
92:                                         {
93:                                                 // throw new CoreException(new Status(IStatus.ERROR, IVdmUiConstants.PLUGIN_ID,
94:                                                 // "Error source file not found in build path: "+ file));
95:                                         }
96:                                 }
97:
98:                         }
99:                 }
100:
101:•                if (document instanceof IDocumentExtension3)
102:                 {
103:                         IDocumentExtension3 extension3 = (IDocumentExtension3) document;
104:                         IDocumentPartitioner partitioner = new VdmDocumentPartitioner(VdmUIPlugin.getDefault().getPartitionScanner(), VdmPartitionScanner.PARTITION_TYPES);
105:                         extension3.setDocumentPartitioner(IVdmPartitions.VDM_PARTITIONING, partitioner);
106:                         partitioner.connect(document);
107:                 }
108:
109:                 return document;
110:         }
111:
112:         @Override
113:         protected IDocument createEmptyDocument()
114:         {
115:                 return new VdmExternalDocument();
116:         }
117:
118:         @Override
119:         protected boolean setDocumentContent(IDocument document,
120:                         IEditorInput editorInput, String encoding) throws CoreException
121:         {
122:                 if (document instanceof VdmExternalDocument)
123:                 {
124:                         IFile file = null;
125:                         if (editorInput instanceof FileEditorInput)
126:                         {
127:                                 file = ((FileEditorInput) editorInput).getFile();
128:                         }
129:                         if (editorInput instanceof IStorageEditorInput)
130:                         {
131:                                 document.set(getExternalContent(file));
132:                                 return true;
133:                         }
134:                 }
135:                 return false;
136:         }
137:
138:         protected String getExternalContent(IFile file)
139:         {
140:                 return FileUtility.getContentExternalText(file);
141:         }
142: }