1 | /******************************************************************************* |
---|---|
2 | * Copyright (c) 2000, 2015 IBM Corporation and others. |
3 | * |
4 | * This program and the accompanying materials |
5 | * are made available under the terms of the Eclipse Public License 2.0 |
6 | * which accompanies this distribution, and is available at |
7 | * https://www.eclipse.org/legal/epl-2.0/ |
8 | * |
9 | * SPDX-License-Identifier: EPL-2.0 |
10 | * |
11 | * Contributors: |
12 | * IBM Corporation - initial API and implementation |
13 | *******************************************************************************/ |
14 | |
15 | package org.eclipse.jdt.astview.views; |
16 | |
17 | import java.util.ArrayList; |
18 | |
19 | import org.eclipse.jface.viewers.ITreeContentProvider; |
20 | import org.eclipse.jface.viewers.Viewer; |
21 | |
22 | import org.eclipse.jdt.core.dom.IBinding; |
23 | import org.eclipse.jdt.core.dom.IMethodBinding; |
24 | import org.eclipse.jdt.core.dom.ITypeBinding; |
25 | |
26 | |
27 | public class TrayContentProvider implements ITreeContentProvider { |
28 | |
29 | public static final int DEFAULT_CHILDREN_COUNT= 7; |
30 | |
31 | protected static final String N_A= "N/A"; //$NON-NLS-1$ |
32 | protected static final Object[] EMPTY= new Object[0]; |
33 | |
34 | /* |
35 | * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object) |
36 | */ |
37 | @Override |
38 | public Object[] getChildren(Object parentElement) { |
39 | ArrayList<ExceptionAttribute> result= new ArrayList<>(); |
40 | if (parentElement instanceof ExceptionAttribute) |
41 | return EMPTY; |
42 | |
43 | addObjectComparisons(result, parentElement); |
44 | |
45 | if (parentElement instanceof Binding) { |
46 | Binding trayElement= (Binding) parentElement; |
47 | IBinding trayBinding= trayElement.getBinding(); |
48 | |
49 | addBindingComparisons(result, trayElement); |
50 | if (trayBinding instanceof ITypeBinding) |
51 | addTypeBindingComparions(result, trayElement); |
52 | if (trayBinding instanceof IMethodBinding) |
53 | addMethodBindingComparions(result, trayElement); |
54 | |
55 | } else { |
56 | } |
57 | |
58 | return result.toArray(); |
59 | } |
60 | |
61 | private void addObjectComparisons(ArrayList<ExceptionAttribute> result, Object trayElement) { |
62 | class IdentityProperty extends DynamicAttributeProperty { |
63 | public IdentityProperty(Object parent) { |
64 | super(parent, "* == this: "); |
65 | } |
66 | @Override |
67 | protected String executeQuery(Object viewerObject, Object trayObject) { |
68 | return Boolean.toString(viewerObject == trayObject); |
69 | } |
70 | } |
71 | result.add(new IdentityProperty(trayElement)); |
72 | |
73 | class EqualsProperty extends DynamicAttributeProperty { |
74 | public EqualsProperty(Object parent) { |
75 | super(parent, "*.equals(this): "); |
76 | } |
77 | @Override |
78 | protected String executeQuery(Object viewerObject, Object trayObject) { |
79 | if (viewerObject != null) |
80 | return Boolean.toString(viewerObject.equals(trayObject)); |
81 | else |
82 | return "* is null"; |
83 | } |
84 | } |
85 | result.add(new EqualsProperty(trayElement)); |
86 | } |
87 | |
88 | private void addBindingComparisons(ArrayList<ExceptionAttribute> result, Binding trayElement) { |
89 | class IsEqualToProperty extends DynamicBindingProperty { |
90 | public IsEqualToProperty(Binding parent) { |
91 | super(parent); |
92 | } |
93 | @Override |
94 | protected String getName() { |
95 | return "*.isEqualTo(this): "; //$NON-NLS-1$ |
96 | } |
97 | @Override |
98 | protected String executeQuery(IBinding viewerBinding, IBinding trayBinding) { |
99 | if (viewerBinding != null) |
100 | return Boolean.toString(viewerBinding.isEqualTo(trayBinding)); |
101 | else |
102 | return "* is null"; //$NON-NLS-1$ |
103 | } |
104 | } |
105 | result.add(new IsEqualToProperty(trayElement)); |
106 | |
107 | class KeysEqualProperty extends DynamicBindingProperty { |
108 | public KeysEqualProperty(Binding parent) { |
109 | super(parent); |
110 | } |
111 | @Override |
112 | protected String getName() { |
113 | return "*.getKey().equals(this.getKey()): "; //$NON-NLS-1$ |
114 | } |
115 | @Override |
116 | protected String executeQuery(IBinding viewerBinding, IBinding trayBinding) { |
117 | if (viewerBinding == null) |
118 | return "* is null"; //$NON-NLS-1$ |
119 | else if (viewerBinding.getKey() == null) |
120 | return "*.getKey() is null"; //$NON-NLS-1$ |
121 | else if (trayBinding.getKey() == null) |
122 | return "this.getKey() is null"; //$NON-NLS-1$ |
123 | else |
124 | return Boolean.toString(viewerBinding.getKey().equals(trayBinding.getKey())); |
125 | } |
126 | } |
127 | result.add(new KeysEqualProperty(trayElement)); |
128 | } |
129 | |
130 | private void addTypeBindingComparions(ArrayList<ExceptionAttribute> result, Binding trayElement) { |
131 | class IsSubTypeCompatibleProperty extends DynamicBindingProperty { |
132 | public IsSubTypeCompatibleProperty(Binding parent) { |
133 | super(parent); |
134 | } |
135 | @Override |
136 | protected String getName() { |
137 | return "*.isSubTypeCompatible(this): "; //$NON-NLS-1$ |
138 | } |
139 | @Override |
140 | protected String executeQuery(IBinding viewerBinding, IBinding trayBinding) { |
141 | if (viewerBinding instanceof ITypeBinding) { |
142 | ITypeBinding viewerTB= (ITypeBinding) viewerBinding; |
143 | ITypeBinding trayTB= (ITypeBinding) trayBinding; |
144 | return Boolean.toString(viewerTB.isSubTypeCompatible(trayTB)); |
145 | } else { |
146 | return "* not an ITypeBinding"; //$NON-NLS-1$ |
147 | } |
148 | } |
149 | } |
150 | result.add(new IsSubTypeCompatibleProperty(trayElement)); |
151 | |
152 | class IsCastCompatibleProperty extends DynamicBindingProperty { |
153 | public IsCastCompatibleProperty(Binding parent) { |
154 | super(parent); |
155 | } |
156 | @Override |
157 | protected String getName() { |
158 | return "*.isCastCompatible(this): "; //$NON-NLS-1$ |
159 | } |
160 | @Override |
161 | protected String executeQuery(IBinding viewerBinding, IBinding trayBinding) { |
162 | if (viewerBinding instanceof ITypeBinding) { |
163 | ITypeBinding viewerTB= (ITypeBinding) viewerBinding; |
164 | ITypeBinding trayTB= (ITypeBinding) trayBinding; |
165 | return Boolean.toString(viewerTB.isCastCompatible(trayTB)); |
166 | } else { |
167 | return "* not an ITypeBinding"; //$NON-NLS-1$ |
168 | } |
169 | } |
170 | } |
171 | result.add(new IsCastCompatibleProperty(trayElement)); |
172 | |
173 | class IsAssignmentCompatibleProperty extends DynamicBindingProperty { |
174 | public IsAssignmentCompatibleProperty(Binding parent) { |
175 | super(parent); |
176 | } |
177 | @Override |
178 | protected String getName() { |
179 | return "*.isAssignmentCompatible(this): "; //$NON-NLS-1$ |
180 | } |
181 | @Override |
182 | protected String executeQuery(IBinding viewerBinding, IBinding trayBinding) { |
183 | if (viewerBinding instanceof ITypeBinding) { |
184 | ITypeBinding viewerTB= (ITypeBinding) viewerBinding; |
185 | ITypeBinding trayTB= (ITypeBinding) trayBinding; |
186 | return Boolean.toString(viewerTB.isAssignmentCompatible(trayTB)); |
187 | } else { |
188 | return "* not an ITypeBinding"; //$NON-NLS-1$ |
189 | } |
190 | } |
191 | } |
192 | result.add(new IsAssignmentCompatibleProperty(trayElement)); |
193 | } |
194 | |
195 | private void addMethodBindingComparions(ArrayList<ExceptionAttribute> result, Binding trayElement) { |
196 | class OverridesProperty extends DynamicBindingProperty { |
197 | public OverridesProperty(Binding parent) { |
198 | super(parent); |
199 | } |
200 | @Override |
201 | protected String getName() { |
202 | return "*.overrides(this): "; //$NON-NLS-1$ |
203 | } |
204 | @Override |
205 | protected String executeQuery(IBinding viewerBinding, IBinding trayBinding) { |
206 | if (viewerBinding instanceof IMethodBinding) { |
207 | IMethodBinding viewerMB= (IMethodBinding) viewerBinding; |
208 | IMethodBinding trayMB= (IMethodBinding) trayBinding; |
209 | return Boolean.toString(viewerMB.overrides(trayMB)); |
210 | } else { |
211 | return "* not an IMethodBinding"; //$NON-NLS-1$ |
212 | } |
213 | } |
214 | } |
215 | result.add(new OverridesProperty(trayElement)); |
216 | |
217 | class IsSubsignatureProperty extends DynamicBindingProperty { |
218 | public IsSubsignatureProperty(Binding parent) { |
219 | super(parent); |
220 | } |
221 | @Override |
222 | protected String getName() { |
223 | return "*.isSubsignature(this): "; //$NON-NLS-1$ |
224 | } |
225 | @Override |
226 | protected String executeQuery(IBinding viewerBinding, IBinding trayBinding) { |
227 | if (viewerBinding instanceof IMethodBinding) { |
228 | IMethodBinding viewerMB= (IMethodBinding) viewerBinding; |
229 | IMethodBinding trayMB= (IMethodBinding) trayBinding; |
230 | return Boolean.toString(viewerMB.isSubsignature(trayMB)); |
231 | } else { |
232 | return "* not an IMethodBinding"; //$NON-NLS-1$ |
233 | } |
234 | } |
235 | } |
236 | result.add(new IsSubsignatureProperty(trayElement)); |
237 | } |
238 | |
239 | /* |
240 | * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object) |
241 | */ |
242 | @Override |
243 | public Object getParent(Object element) { |
244 | if (element instanceof ASTAttribute) { |
245 | return ((ASTAttribute) element).getParent(); |
246 | } else { |
247 | return null; |
248 | } |
249 | } |
250 | |
251 | /* |
252 | * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object) |
253 | */ |
254 | @Override |
255 | public boolean hasChildren(Object element) { |
256 | return !(element instanceof DynamicAttributeProperty) && !(element instanceof DynamicBindingProperty); |
257 | } |
258 | |
259 | /* |
260 | * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object) |
261 | */ |
262 | @Override |
263 | public Object[] getElements(Object inputElement) { |
264 | if (inputElement instanceof ArrayList) |
265 | return ((ArrayList<?>) inputElement).toArray(); |
266 | return EMPTY; |
267 | } |
268 | |
269 | /* |
270 | * @see org.eclipse.jface.viewers.IContentProvider#dispose() |
271 | */ |
272 | @Override |
273 | public void dispose() { |
274 | // do nothing |
275 | } |
276 | |
277 | /* |
278 | * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object) |
279 | */ |
280 | @Override |
281 | public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
282 | // do nothing |
283 | } |
284 | } |
285 |
Members