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.editors.text.FileDocumentProvider;
31: import org.eclipse.ui.part.FileEditorInput;
32: import org.overture.ide.core.resources.IVdmProject;
33: import org.overture.ide.core.resources.IVdmSourceUnit;
34: import org.overture.ide.ui.VdmUIPlugin;
35: import org.overture.ide.ui.editor.partitioning.IVdmPartitions;
36: import org.overture.ide.ui.editor.partitioning.VdmDocumentPartitioner;
37: import org.overture.ide.ui.editor.partitioning.VdmPartitionScanner;
38:
39: public class VdmDocumentProvider extends FileDocumentProvider
40: {
41:
42:         @Override
43:         protected IDocument createDocument(Object element) throws CoreException
44:         {
45:                 IDocument document = super.createDocument(element);
46:
47:•                if (element instanceof FileEditorInput)
48:                 {
49:•                        if (document instanceof VdmDocument)
50:                         {
51:                                 IFile file = ((FileEditorInput) element).getFile();
52:
53:                                 IVdmProject vdmProject = (IVdmProject) file.getProject().getAdapter(IVdmProject.class);
54:
55:                                 Assert.isNotNull(vdmProject, "Project of file: "
56:                                                 + file.getName() + " is not VDM");
57:
58:•                                if (vdmProject != null)
59:                                 {
60:
61:                                         IVdmSourceUnit source = vdmProject.findSourceUnit(file);
62:•                                        if (source != null)
63:                                         {
64:                                                 ((VdmDocument) document).setSourceUnit(source);
65:                                         }else{
66:                                                 //throw new CoreException(new Status(IStatus.ERROR, IVdmUiConstants.PLUGIN_ID, "Error source file not found in build path: "+ file));
67:                                         }
68:                                 }
69:
70:                         }
71:                 }
72:
73:•                if (document instanceof IDocumentExtension3)
74:                 {
75:                         IDocumentExtension3 extension3 = (IDocumentExtension3) document;
76:                         IDocumentPartitioner partitioner = new VdmDocumentPartitioner(VdmUIPlugin.getDefault().getPartitionScanner(), VdmPartitionScanner.PARTITION_TYPES);                        
77:                         partitioner.connect(document);
78:                         extension3.setDocumentPartitioner(IVdmPartitions.VDM_PARTITIONING, partitioner);
79:                 }
80:
81:                 return document;
82:         }
83:         
84:         @Override
85:         protected IDocument createEmptyDocument()
86:         {
87:                 return new VdmDocument();
88:         }
89: }