1 | /******************************************************************************* |
---|---|
2 | * Copyright (c) 2000, 2006 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 org.eclipse.jdt.core.dom.IBinding; |
18 | |
19 | import org.eclipse.swt.graphics.Image; |
20 | |
21 | |
22 | public abstract class DynamicAttributeProperty extends ExceptionAttribute { |
23 | |
24 | protected static final String N_A= "N/A"; //$NON-NLS-1$ |
25 | private final Object fParent; |
26 | private final String fName; |
27 | |
28 | private Object fViewerElement; |
29 | private String fLabel= "<unknown>"; |
30 | |
31 | public DynamicAttributeProperty(Object parentAttribute, String name) { |
32 | fParent= parentAttribute; |
33 | fName= name; |
34 | } |
35 | |
36 | @Override |
37 | public Object getParent() { |
38 | return fParent; |
39 | } |
40 | |
41 | @Override |
42 | public Object[] getChildren() { |
43 | return EMPTY; |
44 | } |
45 | |
46 | public void setViewerElement(Object viewerAttribute) { |
47 | if (fViewerElement == viewerAttribute) |
48 | return; |
49 | |
50 | fViewerElement= viewerAttribute; |
51 | fException= null; |
52 | Object trayObject= ASTView.unwrapAttribute(fParent); |
53 | StringBuilder buf= new StringBuilder(fName); |
54 | if (viewerAttribute != null) { |
55 | Object viewerObject= ASTView.unwrapAttribute(viewerAttribute); |
56 | try { |
57 | String queryResult= executeQuery(viewerObject, trayObject); |
58 | buf.append(queryResult); |
59 | } catch (RuntimeException e) { |
60 | fException= e; |
61 | buf.append(e.getClass().getName()); |
62 | buf.append(" for \""); //$NON-NLS-1$ |
63 | if (viewerObject == null) |
64 | buf.append("null"); //$NON-NLS-1$ |
65 | else |
66 | buf.append('"').append(objectToString(viewerObject)); |
67 | buf.append("\" and "); //$NON-NLS-1$ |
68 | buf.append(objectToString(trayObject)).append('"'); |
69 | } |
70 | } else { |
71 | buf.append(N_A); |
72 | } |
73 | fLabel= buf.toString(); |
74 | } |
75 | |
76 | private String objectToString(Object object) { |
77 | if (object instanceof IBinding) { |
78 | return ((IBinding) object).getKey(); |
79 | } else { |
80 | return String.valueOf(object); |
81 | } |
82 | } |
83 | |
84 | /** |
85 | * Executes this dynamic attribute property's query in a protected environment. |
86 | * A {@link RuntimeException} thrown by this method is made available via |
87 | * {@link #getException()}. |
88 | * |
89 | * @param viewerObject the object of the element selected in the AST viewer, or <code>null</code> iff none |
90 | * @param trayObject the object of the element selected in the comparison tray, or <code>null</code> iff none |
91 | * @return this property's result |
92 | */ |
93 | protected abstract String executeQuery(Object viewerObject, Object trayObject); |
94 | |
95 | @Override |
96 | public String getLabel() { |
97 | return fLabel; |
98 | } |
99 | |
100 | @Override |
101 | public Image getImage() { |
102 | return null; |
103 | } |
104 | } |
105 |
Members