1 | package com.framsticks.framclipse.editors.outline; |
---|
2 | |
---|
3 | import java.util.List; |
---|
4 | |
---|
5 | import org.eclipse.jface.text.BadPositionCategoryException; |
---|
6 | import org.eclipse.jface.text.DefaultPositionUpdater; |
---|
7 | import org.eclipse.jface.text.IDocument; |
---|
8 | import org.eclipse.jface.text.IPositionUpdater; |
---|
9 | import org.eclipse.jface.viewers.ITreeContentProvider; |
---|
10 | import org.eclipse.jface.viewers.Viewer; |
---|
11 | |
---|
12 | import com.framsticks.framclipse.editors.FramclipseEditor; |
---|
13 | import com.framsticks.framclipse.internal.parser.ASTFunction; |
---|
14 | import com.framsticks.framclipse.internal.parser.ASTGlobalDecl; |
---|
15 | import com.framsticks.framclipse.internal.parser.ASTIdentList; |
---|
16 | import com.framsticks.framclipse.internal.parser.Node; |
---|
17 | |
---|
18 | |
---|
19 | public class FramclipseEditorOutlineContentProvider implements ITreeContentProvider |
---|
20 | { |
---|
21 | private Node root; |
---|
22 | private FramclipseEditor editor; |
---|
23 | |
---|
24 | protected final static String FRAMCLIPSE_ELEM_POSITIONS = "__framclipse_elem_positions"; |
---|
25 | protected IPositionUpdater positionUpdater = new DefaultPositionUpdater(FRAMCLIPSE_ELEM_POSITIONS); |
---|
26 | |
---|
27 | public FramclipseEditorOutlineContentProvider(FramclipseEditor framsEditor) |
---|
28 | { |
---|
29 | this.editor = framsEditor; |
---|
30 | } |
---|
31 | |
---|
32 | public Object[] getChildren(Object parentElement) { |
---|
33 | |
---|
34 | if(parentElement instanceof Node) |
---|
35 | { |
---|
36 | Node node = (Node)parentElement; |
---|
37 | |
---|
38 | if(node instanceof ASTFunction) |
---|
39 | { |
---|
40 | if(node.jjtGetNumChildren() > 1) |
---|
41 | { |
---|
42 | ASTIdentList params = (ASTIdentList) node.jjtGetChild(1); |
---|
43 | List<String> names = params.getIdents(); |
---|
44 | |
---|
45 | Node[] children = new Node[names.size()]; |
---|
46 | |
---|
47 | for(int l = 0; l < names.size(); ++l) |
---|
48 | children[l] = new IdentifierNode(names.get(l), "Parameter", node); |
---|
49 | |
---|
50 | return children; |
---|
51 | } |
---|
52 | return new Object[0]; |
---|
53 | } |
---|
54 | if(node instanceof ASTGlobalDecl) |
---|
55 | { |
---|
56 | if(node.jjtGetNumChildren() > 0) |
---|
57 | { |
---|
58 | ASTIdentList params = (ASTIdentList) node.jjtGetChild(0); |
---|
59 | List<String> names = params.getIdents(); |
---|
60 | |
---|
61 | Node[] children = new Node[names.size()]; |
---|
62 | |
---|
63 | for(int l = 0; l < names.size(); ++l) |
---|
64 | children[l] = new IdentifierNode(names.get(l), "Variable", node); |
---|
65 | |
---|
66 | return children; |
---|
67 | } |
---|
68 | return new Object[0]; |
---|
69 | } |
---|
70 | |
---|
71 | if(node.jjtGetNumChildren() > 0) |
---|
72 | { |
---|
73 | Node[] children = new Node[node.jjtGetNumChildren()]; |
---|
74 | |
---|
75 | for(int k = 0; k < node.jjtGetNumChildren(); ++k) |
---|
76 | children[k] = node.jjtGetChild(k); |
---|
77 | |
---|
78 | return children; |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | return new Object[0]; |
---|
84 | } |
---|
85 | |
---|
86 | public Object getParent(Object element) { |
---|
87 | |
---|
88 | if(element instanceof Node) |
---|
89 | { |
---|
90 | return ((Node)element).jjtGetParent(); |
---|
91 | } |
---|
92 | return null; |
---|
93 | } |
---|
94 | |
---|
95 | public boolean hasChildren(Object element) { |
---|
96 | |
---|
97 | if(element instanceof Node) |
---|
98 | { |
---|
99 | if(element instanceof ASTFunction) |
---|
100 | { |
---|
101 | return ((Node)element).jjtGetNumChildren() > 1; |
---|
102 | } |
---|
103 | |
---|
104 | return ((Node)element).jjtGetNumChildren() > 0; |
---|
105 | } |
---|
106 | |
---|
107 | return false; |
---|
108 | } |
---|
109 | |
---|
110 | public Object[] getElements(Object inputElement) { |
---|
111 | if (root == null) |
---|
112 | return new Object[0]; |
---|
113 | if(root.jjtGetNumChildren() > 0) |
---|
114 | { |
---|
115 | Node[] children = new Node[root.jjtGetNumChildren()]; |
---|
116 | |
---|
117 | for(int k = 0; k < root.jjtGetNumChildren(); ++k) |
---|
118 | children[k] = root.jjtGetChild(k); |
---|
119 | |
---|
120 | return children; |
---|
121 | } |
---|
122 | |
---|
123 | return new Object[0]; |
---|
124 | } |
---|
125 | |
---|
126 | public void dispose() { |
---|
127 | // TODO Auto-generated method stub |
---|
128 | |
---|
129 | } |
---|
130 | |
---|
131 | public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
---|
132 | if (oldInput != null) |
---|
133 | { |
---|
134 | IDocument document = editor.getDocumentProvider().getDocument(oldInput); |
---|
135 | if (document != null) |
---|
136 | { |
---|
137 | try |
---|
138 | { |
---|
139 | document.removePositionCategory(FRAMCLIPSE_ELEM_POSITIONS); |
---|
140 | } |
---|
141 | catch (BadPositionCategoryException x) |
---|
142 | { |
---|
143 | } |
---|
144 | document.removePositionUpdater(positionUpdater); |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | root = (Node) newInput; |
---|
149 | |
---|
150 | if (newInput != null) |
---|
151 | { |
---|
152 | IDocument document = editor.getDocumentProvider().getDocument(newInput); |
---|
153 | if (document != null) |
---|
154 | { |
---|
155 | document.addPositionCategory(FRAMCLIPSE_ELEM_POSITIONS); |
---|
156 | document.addPositionUpdater(positionUpdater); |
---|
157 | |
---|
158 | } |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | } |
---|