1 | package com.framsticks.framclipse.editors.configuration; |
---|
2 | |
---|
3 | import org.eclipse.core.filebuffers.IDocumentSetupParticipant; |
---|
4 | import org.eclipse.jface.text.IDocument; |
---|
5 | import org.eclipse.jface.text.IDocumentExtension3; |
---|
6 | import org.eclipse.jface.text.IDocumentPartitioner; |
---|
7 | import org.eclipse.jface.text.rules.FastPartitioner; |
---|
8 | import org.jdom.Attribute; |
---|
9 | import org.jdom.Document; |
---|
10 | import org.jdom.Element; |
---|
11 | import org.jdom.xpath.XPath; |
---|
12 | |
---|
13 | import com.framsticks.framclipse.Framclipse; |
---|
14 | import com.framsticks.framclipse.editors.EditorType; |
---|
15 | |
---|
16 | |
---|
17 | public class FramclipseDocumentSetupParticipant implements IDocumentSetupParticipant { |
---|
18 | |
---|
19 | private final EditorType editorType; |
---|
20 | |
---|
21 | public FramclipseDocumentSetupParticipant(EditorType editorType) { |
---|
22 | this.editorType = editorType; |
---|
23 | } |
---|
24 | |
---|
25 | /* |
---|
26 | * @see org.eclipse.core.filebuffers.IDocumentSetupParticipant#setup(org.eclipse.jface.text.IDocument) |
---|
27 | */ |
---|
28 | public void setup(IDocument document) { |
---|
29 | if (document instanceof IDocumentExtension3) { |
---|
30 | IDocumentExtension3 extension3 = (IDocumentExtension3) document; |
---|
31 | FramscriptPartitionScanner scanner = Framclipse.getDefault() |
---|
32 | .getFramscriptPartitionScanner(getCodeFieldName()); |
---|
33 | IDocumentPartitioner partitioner = new FastPartitioner(scanner, |
---|
34 | FramscriptPartitionScanner.PARTITION_TYPES); |
---|
35 | extension3.setDocumentPartitioner(Framclipse.FRAMSCRIPT_PARTITIONING, partitioner); |
---|
36 | partitioner.connect(document); |
---|
37 | } |
---|
38 | } |
---|
39 | |
---|
40 | private String getCodeFieldName() { |
---|
41 | Document context = Framclipse.getDefault().getFramscriptContext(); |
---|
42 | try { |
---|
43 | String query = "//file[@pattern='*." + editorType.getExtension() + "']/code"; |
---|
44 | XPath xpath = XPath.newInstance(query); |
---|
45 | Element code = (Element) xpath.selectSingleNode(context); |
---|
46 | Attribute codeFieldNameAttribute = code.getAttribute("member"); |
---|
47 | return codeFieldNameAttribute.getValue(); |
---|
48 | } catch (Exception e) { |
---|
49 | e.printStackTrace(); |
---|
50 | } |
---|
51 | return ""; |
---|
52 | } |
---|
53 | } |
---|