KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > part > IntroPart


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.part;
12
13 import org.eclipse.core.commands.common.EventManager;
14 import org.eclipse.core.runtime.Assert;
15 import org.eclipse.core.runtime.IConfigurationElement;
16 import org.eclipse.core.runtime.IExecutableExtension;
17 import org.eclipse.core.runtime.Platform;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.jface.resource.JFaceResources;
20 import org.eclipse.jface.util.SafeRunnable;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.ui.IMemento;
24 import org.eclipse.ui.IPropertyListener;
25 import org.eclipse.ui.ISharedImages;
26 import org.eclipse.ui.PartInitException;
27 import org.eclipse.ui.PlatformUI;
28 import org.eclipse.ui.internal.intro.IntroMessages;
29 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
30 import org.eclipse.ui.internal.util.Util;
31 import org.eclipse.ui.intro.IIntroPart;
32 import org.eclipse.ui.intro.IIntroSite;
33 import org.eclipse.ui.plugin.AbstractUIPlugin;
34
35 /**
36  * Abstract base implementation of an intro part.
37  * <p>
38  * Subclasses must implement the following methods:
39  * <ul>
40  * <li><code>createPartControl</code>- to create the intro part's controls
41  * </li>
42  * <li><code>setFocus</code>- to accept focus</li>
43  * <li><code>standbyStateChanged</code>- to change the standby mode</li>
44  * </ul>
45  * </p>
46  * <p>
47  * Subclasses may extend or reimplement the following methods as required:
48  * <ul>
49  * <li><code>setInitializationData</code>- extend to provide additional
50  * initialization when the intro extension is instantiated</li>
51  * <li><code>init(IIntroSite, IMemento)</code>- extend to provide additional
52  * initialization when intro is assigned its site</li>
53  * <li><code>dispose</code>- extend to provide additional cleanup</li>
54  * <li><code>getAdapter</code>- reimplement to make their intro adaptable
55  * </li>
56  * </ul>
57  * </p>
58  * @since 3.0
59  */

60 public abstract class IntroPart extends EventManager implements IIntroPart,
61         IExecutableExtension {
62
63     private IConfigurationElement configElement;
64
65     private ImageDescriptor imageDescriptor;
66
67     private IIntroSite partSite;
68
69     private Image titleImage;
70
71     private String JavaDoc titleLabel;
72
73     /**
74      * Creates a new intro part.
75      */

76     protected IntroPart() {
77         super();
78     }
79
80     /* (non-Javadoc)
81      * @see org.eclipse.ui.intro.IIntroPart#addPropertyListener(org.eclipse.ui.IPropertyListener)
82      */

83     public void addPropertyListener(IPropertyListener l) {
84         addListenerObject(l);
85     }
86
87     /*
88      * (non-Javadoc) Creates the SWT controls for this intro part. <p>
89      * Subclasses must implement this method. For a detailed description of the
90      * requirements see <code> IIntroPart </code></p>
91      *
92      * @param parent the parent control
93      *
94      * @see IIntroPart
95      */

96     public abstract void createPartControl(Composite parent);
97
98     /**
99      * The <code>IntroPart</code> implementation of this
100      * <code>IIntroPart</code> method disposes the title image loaded by
101      * <code>setInitializationData</code>. Subclasses may extend.
102      */

103     public void dispose() {
104         if (titleImage != null) {
105             JFaceResources.getResources().destroyImage(imageDescriptor);
106             titleImage = null;
107         }
108
109         // Clear out the property change listeners as we
110
// should not be notifying anyone after the part
111
// has been disposed.
112
clearListeners();
113     }
114
115     /**
116      * Fires a property changed event.
117      *
118      * @param propertyId
119      * the id of the property that changed
120      */

121     protected void firePropertyChange(final int propertyId) {
122         Object JavaDoc[] array = getListeners();
123         for (int nX = 0; nX < array.length; nX++) {
124             final IPropertyListener l = (IPropertyListener) array[nX];
125             Platform.run(new SafeRunnable() {
126
127                 public void run() {
128                     l.propertyChanged(this, propertyId);
129                 }
130             });
131         }
132     }
133
134     /**
135      * This implementation of the method declared by <code>IAdaptable</code>
136      * passes the request along to the platform's adapter manager; roughly
137      * <code>Platform.getAdapterManager().getAdapter(this, adapter)</code>.
138      * Subclasses may override this method (however, if they do so, they should
139      * invoke the method on their superclass to ensure that the Platform's
140      * adapter manager is consulted).
141      */

142     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
143         return Platform.getAdapterManager().getAdapter(this, adapter);
144     }
145
146     /**
147      * Returns the configuration element for this part. The configuration
148      * element comes from the plug-in registry entry for the extension defining
149      * this part.
150      *
151      * @return the configuration element for this part
152      */

153     protected IConfigurationElement getConfigurationElement() {
154         return configElement;
155     }
156
157     /**
158      * Returns the default title image.
159      *
160      * @return the default image
161      */

162     protected Image getDefaultImage() {
163         return PlatformUI.getWorkbench().getSharedImages().getImage(
164                 ISharedImages.IMG_DEF_VIEW);
165     }
166
167     /*
168      * (non-Javadoc)
169      *
170      * @see org.eclipse.ui.intro.IIntroPart#getIntroSite()
171      */

172     public final IIntroSite getIntroSite() {
173         return partSite;
174     }
175
176     /* (non-Javadoc)
177      * @see org.eclipse.ui.intro.IIntroPart#getTitleImage()
178      */

179     public Image getTitleImage() {
180         if (titleImage != null) {
181             return titleImage;
182         }
183         return getDefaultImage();
184     }
185     
186     /* (non-Javadoc)
187      * @see org.eclipse.ui.intro.IIntroPart#getTitle()
188      */

189     public String JavaDoc getTitle() {
190         if (titleLabel != null) {
191             return titleLabel;
192         }
193         return getDefaultTitle();
194     }
195
196     /**
197      * Return the default title string.
198      *
199      * @return the default title string
200      */

201     private String JavaDoc getDefaultTitle() {
202         return IntroMessages.Intro_default_title;
203     }
204
205     /**
206      * The base implementation of this {@link org.eclipse.ui.intro.IIntroPart}method ignores the
207      * memento and initializes the part in a fresh state. Subclasses may extend
208      * to perform any state restoration, but must call the super method.
209      *
210      * @param site
211      * the intro site
212      * @param memento
213      * the intro part state or <code>null</code> if there is no
214      * previous saved state
215      * @exception PartInitException
216      * if this part was not initialized successfully
217      */

218     public void init(IIntroSite site, IMemento memento)
219             throws PartInitException {
220         setSite(site);
221     }
222
223     /**
224      * Sets the part site.
225      * <p>
226      * Subclasses must invoke this method from {@link org.eclipse.ui.intro.IIntroPart#init(IIntroSite, IMemento)}.
227      * </p>
228      *
229      * @param site the intro part site
230      */

231     protected void setSite(IIntroSite site) {
232         this.partSite = site;
233     }
234
235     /* (non-Javadoc)
236      * @see org.eclipse.ui.intro.IIntroPart#removePropertyListener(org.eclipse.ui.IPropertyListener)
237      */

238     public void removePropertyListener(IPropertyListener l) {
239         removeListenerObject(l);
240     }
241
242     /**
243      * The base implementation of this {@link org.eclipse.ui.intro.IIntroPart} method does nothing.
244      * Subclasses may override.
245      *
246      * @param memento
247      * a memento to receive the object state
248      */

249     public void saveState(IMemento memento) {
250         //no-op
251
}
252
253     /*
254      * (non-Javadoc) Asks this part to take focus within the workbench.
255      * <p>
256      * Subclasses must implement this method. For a detailed description of the
257      * requirements see <code>IIntroPart</code>
258      * </p>
259      *
260      * @see IIntroPart
261      */

262     public abstract void setFocus();
263
264     /**
265      * The <code>IntroPart</code> implementation of this
266      * <code>IExecutableExtension</code> records the configuration element in
267      * and internal state variable (accessible via <code>getConfigElement</code>).
268      * It also loads the title image, if one is specified in the configuration
269      * element. Subclasses may extend.
270      *
271      * Should not be called by clients. It is called by the core plugin when
272      * creating this executable extension.
273      */

274     public void setInitializationData(IConfigurationElement cfig,
275             String JavaDoc propertyName, Object JavaDoc data) {
276
277         // Save config element.
278
configElement = cfig;
279
280         titleLabel = cfig.getAttribute(IWorkbenchRegistryConstants.ATT_LABEL);
281         
282         // Icon.
283
String JavaDoc strIcon = cfig.getAttribute(IWorkbenchRegistryConstants.ATT_ICON);
284         if (strIcon == null) {
285             return;
286         }
287
288         imageDescriptor = AbstractUIPlugin.imageDescriptorFromPlugin(
289                 configElement.getNamespace(), strIcon);
290
291         if (imageDescriptor == null) {
292             return;
293         }
294
295         Image image = JFaceResources.getResources().createImageWithDefault(imageDescriptor);
296         titleImage = image;
297     }
298
299     /**
300      * Sets or clears the title image of this part.
301      *
302      * @param titleImage
303      * the title image, or <code>null</code> to clear
304      */

305     protected void setTitleImage(Image titleImage) {
306         Assert.isTrue(titleImage == null || !titleImage.isDisposed());
307         //Do not send changes if they are the same
308
if (this.titleImage == titleImage) {
309             return;
310         }
311         this.titleImage = titleImage;
312         firePropertyChange(IIntroPart.PROP_TITLE);
313     }
314     
315     /**
316      * Set the title string for this part.
317      *
318      * @param titleLabel the title string. Must not be <code>null</code>.
319      * @since 3.2
320      */

321     protected void setTitle(String JavaDoc titleLabel) {
322         Assert.isNotNull(titleLabel);
323         if (Util.equals(this.titleLabel, titleLabel))
324             return;
325         this.titleLabel = titleLabel;
326         firePropertyChange(IIntroPart.PROP_TITLE);
327     }
328 }
329
Popular Tags