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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
/*******************************************************************************
* Copyright (c) 2004, 2016 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.ui.part;
import org.eclipse.core.commands.common.EventManager;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExecutableExtension;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.util.SafeRunnable;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IPropertyListener;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.intro.IntroMessages;
import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
import org.eclipse.ui.internal.util.Util;
import org.eclipse.ui.intro.IIntroPart;
import org.eclipse.ui.intro.IIntroSite;
import org.eclipse.ui.plugin.AbstractUIPlugin;
/**
* Abstract base implementation of an intro part.
* <p>
* Subclasses must implement the following methods:
* </p>
* <ul>
* <li><code>createPartControl</code>- to create the intro part's controls</li>
* <li><code>setFocus</code>- to accept focus</li>
* <li><code>standbyStateChanged</code>- to change the standby mode</li>
* </ul>
* <p>
* Subclasses may extend or reimplement the following methods as required:
* </p>
* <ul>
* <li><code>setInitializationData</code>- extend to provide additional
* initialization when the intro extension is instantiated</li>
* <li><code>init(IIntroSite, IMemento)</code>- extend to provide additional
* initialization when intro is assigned its site</li>
* <li><code>dispose</code>- extend to provide additional cleanup</li>
* <li><code>getAdapter</code>- reimplement to make their intro adaptable</li>
* </ul>
*
* @since 3.0
*/
public abstract class IntroPart extends EventManager implements IIntroPart, IExecutableExtension {
private IConfigurationElement configElement;
private ImageDescriptor imageDescriptor;
private IIntroSite partSite;
private Image titleImage;
private String titleLabel;
/**
* Creates a new intro part.
*/
protected IntroPart() {
super();
}
@Override
public void addPropertyListener(IPropertyListener l) {
addListenerObject(l);
}
@Override
public abstract void createPartControl(Composite parent);
/**
* The <code>IntroPart</code> implementation of this <code>IIntroPart</code>
* method disposes the title image loaded by <code>setInitializationData</code>.
* Subclasses may extend.
*/
@Override
public void dispose() {
if (titleImage != null) {
JFaceResources.getResources().destroyImage(imageDescriptor);
titleImage = null;
}
// Clear out the property change listeners as we
// should not be notifying anyone after the part
// has been disposed.
clearListeners();
}
/**
* Fires a property changed event.
*
* @param propertyId the id of the property that changed
*/
protected void firePropertyChange(final int propertyId) {
for (Object listener : getListeners()) {
final IPropertyListener propertyListener = (IPropertyListener) listener;
SafeRunner.run(new SafeRunnable() {
@Override
public void run() {
propertyListener.propertyChanged(this, propertyId);
}
});
}
}
/**
* This implementation of the method declared by <code>IAdaptable</code> passes
* the request along to the platform's adapter manager; roughly
* <code>Platform.getAdapterManager().getAdapter(this, adapter)</code>.
* Subclasses may override this method (however, if they do so, they should
* invoke the method on their superclass to ensure that the Platform's adapter
* manager is consulted).
*/
@Override
public <T> T getAdapter(Class<T> adapter) {
return Platform.getAdapterManager().getAdapter(this, adapter);
}
/**
* Returns the configuration element for this part. The configuration element
* comes from the plug-in registry entry for the extension defining this part.
*
* @return the configuration element for this part
*/
protected IConfigurationElement getConfigurationElement() {
return configElement;
}
/**
* Returns the default title image.
*
* @return the default image
*/
protected Image getDefaultImage() {
return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_DEF_VIEW);
}
@Override
public final IIntroSite getIntroSite() {
return partSite;
}
@Override
public Image getTitleImage() {
if (titleImage != null) {
return titleImage;
}
return getDefaultImage();
}
@Override
public String getTitle() {
if (titleLabel != null) {
return titleLabel;
}
return getDefaultTitle();
}
/**
* Return the default title string.
*
* @return the default title string
*/
private String getDefaultTitle() {
return IntroMessages.Intro_default_title;
}
/**
* The base implementation of this {@link org.eclipse.ui.intro.IIntroPart}method
* ignores the memento and initializes the part in a fresh state. Subclasses may
* extend to perform any state restoration, but must call the super method.
*
* @param site the intro site
* @param memento the intro part state or <code>null</code> if there is no
* previous saved state
* @exception PartInitException if this part was not initialized successfully
*/
@Override
public void init(IIntroSite site, IMemento memento) throws PartInitException {
setSite(site);
}
/**
* Sets the part site.
* <p>
* Subclasses must invoke this method from
* {@link org.eclipse.ui.intro.IIntroPart#init(IIntroSite, IMemento)}.
* </p>
*
* @param site the intro part site
*/
protected void setSite(IIntroSite site) {
this.partSite = site;
}
@Override
public void removePropertyListener(IPropertyListener l) {
removeListenerObject(l);
}
/**
* The base implementation of this {@link org.eclipse.ui.intro.IIntroPart}
* method does nothing. Subclasses may override.
*
* @param memento a memento to receive the object state
*/
@Override
public void saveState(IMemento memento) {
// no-op
}
@Override
public abstract void setFocus();
/**
* The <code>IntroPart</code> implementation of this
* <code>IExecutableExtension</code> records the configuration element in and
* internal state variable (accessible via <code>getConfigElement</code>). It
* also loads the title image, if one is specified in the configuration element.
* Subclasses may extend.
*
* Should not be called by clients. It is called by the core plugin when
* creating this executable extension.
*/
@Override
public void setInitializationData(IConfigurationElement cfig, String propertyName, Object data) {
// Save config element.
configElement = cfig;
titleLabel = cfig.getAttribute(IWorkbenchRegistryConstants.ATT_LABEL);
// Icon.
String strIcon = cfig.getAttribute(IWorkbenchRegistryConstants.ATT_ICON);
if (strIcon == null) {
return;
}
imageDescriptor = AbstractUIPlugin.imageDescriptorFromPlugin(configElement.getContributor().getName(), strIcon);
if (imageDescriptor == null) {
return;
}
Image image = JFaceResources.getResources().createImageWithDefault(imageDescriptor);
titleImage = image;
}
/**
* Sets or clears the title image of this part.
*
* @param titleImage the title image, or <code>null</code> to clear
*/
protected void setTitleImage(Image titleImage) {
Assert.isTrue(titleImage == null || !titleImage.isDisposed());
// Do not send changes if they are the same
if (this.titleImage == titleImage) {
return;
}
this.titleImage = titleImage;
firePropertyChange(IIntroPart.PROP_TITLE);
}
/**
* Set the title string for this part.
*
* @param titleLabel the title string. Must not be <code>null</code>.
* @since 3.2
*/
protected void setTitle(String titleLabel) {
Assert.isNotNull(titleLabel);
if (Util.equals(this.titleLabel, titleLabel))
return;
this.titleLabel = titleLabel;
firePropertyChange(IIntroPart.PROP_TITLE);
}
}
|