1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
/*******************************************************************************
* Copyright (c) 2012, 2013 Wind River Systems, Inc. and others. All rights reserved.
* This program and the accompanying materials are made available under the terms
* of the Eclipse Public License v1.0 which accompanies this distribution, and is
* available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Wind River Systems - initial API and implementation
*******************************************************************************/
package org.eclipse.tcf.te.ui.views.categories;
import org.eclipse.core.expressions.EvaluationContext;
import org.eclipse.core.expressions.EvaluationResult;
import org.eclipse.core.expressions.Expression;
import org.eclipse.core.expressions.ExpressionConverter;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.graphics.Image;
import org.eclipse.tcf.te.runtime.extensions.ExecutableExtension;
import org.eclipse.tcf.te.runtime.interfaces.IDisposable;
import org.eclipse.tcf.te.ui.views.Managers;
import org.eclipse.tcf.te.ui.views.activator.UIPlugin;
import org.eclipse.tcf.te.ui.views.interfaces.ICategory;
import org.eclipse.tcf.te.ui.views.interfaces.categories.ICategorizable;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IPersistableElement;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.IHandlerService;
import org.eclipse.ui.plugin.AbstractUIPlugin;
/**
* Default category implementation.
*/
public class Category extends ExecutableExtension implements ICategory, IDisposable, IPersistableElement {
// The category image / image descriptor
private ImageDescriptor descriptor = null;
private Image image = null;
// The sorting rank
private int rank = -1;
// The converted expression
private Expression expression;
/* (non-Javadoc)
* @see org.eclipse.tcf.te.runtime.extensions.ExecutableExtension#setInitializationData(org.eclipse.core.runtime.IConfigurationElement, java.lang.String, java.lang.Object)
*/
@Override
public void setInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException {
super.setInitializationData(config, propertyName, data);
// Read the icon attribute and create the image
String attrIcon = config.getAttribute("icon");//$NON-NLS-1$
if (attrIcon != null) {
descriptor = AbstractUIPlugin.imageDescriptorFromPlugin(config.getNamespaceIdentifier(), attrIcon);
if (descriptor != null) {
image = JFaceResources.getResources().createImageWithDefault(descriptor);
}
}
// Read the rank attribute
String attrRank = config.getAttribute("rank"); //$NON-NLS-1$
if (attrRank != null) {
try {
rank = Integer.valueOf(attrRank).intValue();
} catch (NumberFormatException e) { /* ignored on purpose */ }
}
// Read the "enablement" sub element of the extension
IConfigurationElement[] children = config.getChildren("enablement"); //$NON-NLS-1$
// Only one "enablement" element is expected
if (children != null && children.length > 0) {
expression = ExpressionConverter.getDefault().perform(children[0]);
}
}
/*
* (non-Javadoc)
* @see org.eclipse.core.runtime.PlatformObject#getAdapter(java.lang.Class)
*/
@Override
public Object getAdapter(Class adapter) {
if(adapter == IPersistableElement.class) {
return this;
}
return super.getAdapter(adapter);
}
/*
* (non-Javadoc)
* @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
*/
@Override
public void saveState(IMemento memento) {
memento.putString("id", this.getId()); //$NON-NLS-1$
}
/*
* (non-Javadoc)
* @see org.eclipse.ui.IPersistableElement#getFactoryId()
*/
@Override
public String getFactoryId() {
return "org.eclipse.tcf.te.ui.views.categoryFactory"; //$NON-NLS-1$
}
/* (non-Javadoc)
* @see org.eclipse.tcf.te.runtime.interfaces.IDisposable#dispose()
*/
@Override
public void dispose() {
if (descriptor != null) {
JFaceResources.getResources().destroyImage(descriptor);
descriptor = null;
}
image = null;
}
/* (non-Javadoc)
* @see org.eclipse.tcf.te.ui.views.interfaces.ICategory#getImage()
*/
@Override
public Image getImage() {
return image;
}
/* (non-Javadoc)
* @see org.eclipse.tcf.te.ui.views.interfaces.ICategory#getChildren()
*/
@Override
public Object[] getChildren() {
return null;
}
/* (non-Javadoc)
* @see org.eclipse.tcf.te.ui.views.interfaces.ICategory#getRank()
*/
@Override
public int getRank() {
return rank;
}
/* (non-Javadoc)
* @see org.eclipse.tcf.te.ui.views.interfaces.ICategory#belongsTo(java.lang.Object)
*/
@Override
public boolean belongsTo(Object element) {
ICategorizable categorizable = null;
if (element instanceof IAdaptable) {
categorizable = (ICategorizable)((IAdaptable)element).getAdapter(ICategorizable.class);
}
if (categorizable == null) {
categorizable = (ICategorizable)Platform.getAdapterManager().getAdapter(element, ICategorizable.class);
}
return categorizable != null ? Managers.getCategoryManager().belongsTo(getId(), categorizable.getId()) : false;
}
/* (non-Javadoc)
* @see org.eclipse.tcf.te.ui.views.interfaces.ICategory#isEnabled()
*/
@Override
public boolean isEnabled() {
// The category is enabled if no "enablement" expression is found
boolean enabled = true;
if (expression != null) {
// Get the handler service
IHandlerService handlerSvc = (IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class);
Assert.isNotNull(handlerSvc);
// Get the current action
IEvaluationContext currentState = handlerSvc.getCurrentState();
// Construct the evaluation context to pass to the expression
// The expressions default variable is the category itself.
IEvaluationContext ctx = new EvaluationContext(currentState, this);
try {
enabled = expression.evaluate(ctx).equals(EvaluationResult.TRUE);
} catch (CoreException e) {
IStatus status = new Status(IStatus.ERROR, UIPlugin.getUniqueIdentifier(), e.getLocalizedMessage(), e);
UIPlugin.getDefault().getLog().log(status);
}
}
return enabled;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuffer buffer = new StringBuffer(getLabel());
buffer.append(" ["); //$NON-NLS-1$
buffer.append(getId());
buffer.append("] {rank="); //$NON-NLS-1$
buffer.append(getRank());
buffer.append(", enabled="); //$NON-NLS-1$
buffer.append(isEnabled());
buffer.append("}"); //$NON-NLS-1$
return buffer.toString();
}
}
|