KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.runtime.Assert;
14 import org.eclipse.core.runtime.IConfigurationElement;
15 import org.eclipse.ui.IMemento;
16 import org.eclipse.ui.IPropertyListener;
17 import org.eclipse.ui.IViewPart;
18 import org.eclipse.ui.IViewSite;
19 import org.eclipse.ui.IWorkbenchPartConstants;
20 import org.eclipse.ui.IWorkbenchPartSite;
21 import org.eclipse.ui.PartInitException;
22 import org.eclipse.ui.internal.util.Util;
23
24 /**
25  * Abstract base implementation of all workbench views.
26  * <p>
27  * This class should be subclassed by clients wishing to define new views.
28  * The name of the subclass should be given as the <code>"class"</code>
29  * attribute in a <code>view</code> extension contributed to the workbench's
30  * view extension point (named <code>"org.eclipse.ui.views"</code>).
31  * For example, the plug-in's XML markup might contain:
32  * <pre>
33  * &LT;extension point="org.eclipse.ui.views"&GT;
34  * &LT;view id="com.example.myplugin.view"
35  * name="My View"
36  * class="com.example.myplugin.MyView"
37  * icon="images/eview.gif"
38  * /&GT;
39  * &LT;/extension&GT;
40  * </pre>
41  * where <code>com.example.myplugin.MyView</code> is the name of the
42  * <code>ViewPart</code> subclass.
43  * </p>
44  * <p>
45  * Subclasses must implement the following methods:
46  * <ul>
47  * <li><code>createPartControl</code> - to create the view's controls </li>
48  * <li><code>setFocus</code> - to accept focus</li>
49  * </ul>
50  * </p>
51  * <p>
52  * Subclasses may extend or reimplement the following methods as required:
53  * <ul>
54  * <li><code>setInitializationData</code> - extend to provide additional
55  * initialization when view extension is instantiated</li>
56  * <li><code>init(IWorkbenchPartSite)</code> - extend to provide additional
57  * initialization when view is assigned its site</li>
58  * <li><code>dispose</code> - extend to provide additional cleanup</li>
59  * <li><code>getAdapter</code> - reimplement to make their view adaptable</li>
60  * </ul>
61  * </p>
62  */

63 public abstract class ViewPart extends WorkbenchPart implements IViewPart {
64
65     /**
66      * Listens to PROP_TITLE property changes in this object until the first call to
67      * setContentDescription. Used for compatibility with old parts that call setTitle
68      * or overload getTitle instead of using setContentDescription.
69      */

70     private IPropertyListener compatibilityTitleListener = new IPropertyListener() {
71         /* (non-Javadoc)
72          * @see org.eclipse.ui.IPropertyListener#propertyChanged(java.lang.Object, int)
73          */

74         public void propertyChanged(Object JavaDoc source, int propId) {
75             if (propId == IWorkbenchPartConstants.PROP_TITLE) {
76                 setDefaultContentDescription();
77             }
78         }
79     };
80
81     /**
82      * Creates a new view.
83      */

84     protected ViewPart() {
85         super();
86
87         addPropertyListener(compatibilityTitleListener);
88     }
89
90     /* (non-Javadoc)
91      * Method declared on IViewPart.
92      */

93     public IViewSite getViewSite() {
94         return (IViewSite) getSite();
95     }
96
97     
98     /* (non-Javadoc)
99      * @see org.eclipse.ui.IViewPart#init(org.eclipse.ui.IViewSite)
100      */

101     public void init(IViewSite site) throws PartInitException {
102         setSite(site);
103
104         setDefaultContentDescription();
105     }
106
107     /*
108      * (non-Javadoc)
109      * @see org.eclipse.ui.IViewPart#init(org.eclipse.ui.IViewSite, org.eclipse.ui.IMemento)
110      */

111     public void init(IViewSite site, IMemento memento) throws PartInitException {
112         /*
113         * Initializes this view with the given view site. A memento is passed to
114         * the view which contains a snapshot of the views state from a previous
115         * session. Where possible, the view should try to recreate that state
116         * within the part controls.
117         * <p>
118         * This implementation will ignore the memento and initialize the view in
119         * a fresh state. Subclasses may override the implementation to perform any
120         * state restoration as needed.
121         */

122         init(site);
123     }
124
125   
126     /* (non-Javadoc)
127      * @see org.eclipse.ui.IViewPart#saveState(org.eclipse.ui.IMemento)
128      */

129     public void saveState(IMemento memento) {
130         // do nothing
131
}
132
133     /* (non-Javadoc)
134      * @see org.eclipse.ui.part.WorkbenchPart#setPartName(java.lang.String)
135      */

136     protected void setPartName(String JavaDoc partName) {
137         if (compatibilityTitleListener != null) {
138             removePropertyListener(compatibilityTitleListener);
139             compatibilityTitleListener = null;
140         }
141
142         super.setPartName(partName);
143     }
144
145     /* (non-Javadoc)
146      * @see org.eclipse.ui.part.WorkbenchPart#setContentDescription(java.lang.String)
147      */

148     protected void setContentDescription(String JavaDoc description) {
149         if (compatibilityTitleListener != null) {
150             removePropertyListener(compatibilityTitleListener);
151             compatibilityTitleListener = null;
152         }
153
154         super.setContentDescription(description);
155     }
156
157     /* (non-Javadoc)
158      * @see org.eclipse.core.runtime.IExecutableExtension#setInitializationData(org.eclipse.core.runtime.IConfigurationElement, java.lang.String, java.lang.Object)
159      */

160     public void setInitializationData(IConfigurationElement cfig,
161             String JavaDoc propertyName, Object JavaDoc data) {
162         super.setInitializationData(cfig, propertyName, data);
163
164         setDefaultContentDescription();
165     }
166
167     private void setDefaultContentDescription() {
168         if (compatibilityTitleListener == null) {
169             return;
170         }
171
172         String JavaDoc partName = getPartName();
173         String JavaDoc title = getTitle();
174
175         if (Util.equals(partName, title)) {
176             internalSetContentDescription(""); //$NON-NLS-1$
177
} else {
178             internalSetContentDescription(title);
179         }
180     }
181
182     /**
183      * Checks that the given site is valid for this type of part.
184      * The site for a view must be an <code>IViewSite</code>.
185      *
186      * @param site the site to check
187      * @since 3.1
188      */

189     protected final void checkSite(IWorkbenchPartSite site) {
190         super.checkSite(site);
191         Assert.isTrue(site instanceof IViewSite, "The site for a view must be an IViewSite"); //$NON-NLS-1$
192
}
193 }
194
Popular Tags