001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.part;
011:
012: import org.eclipse.core.runtime.Assert;
013: import org.eclipse.core.runtime.IConfigurationElement;
014: import org.eclipse.ui.IMemento;
015: import org.eclipse.ui.IPropertyListener;
016: import org.eclipse.ui.IViewPart;
017: import org.eclipse.ui.IViewSite;
018: import org.eclipse.ui.IWorkbenchPartConstants;
019: import org.eclipse.ui.IWorkbenchPartSite;
020: import org.eclipse.ui.PartInitException;
021: import org.eclipse.ui.internal.util.Util;
022:
023: /**
024: * Abstract base implementation of all workbench views.
025: * <p>
026: * This class should be subclassed by clients wishing to define new views.
027: * The name of the subclass should be given as the <code>"class"</code>
028: * attribute in a <code>view</code> extension contributed to the workbench's
029: * view extension point (named <code>"org.eclipse.ui.views"</code>).
030: * For example, the plug-in's XML markup might contain:
031: * <pre>
032: * <extension point="org.eclipse.ui.views">
033: * <view id="com.example.myplugin.view"
034: * name="My View"
035: * class="com.example.myplugin.MyView"
036: * icon="images/eview.gif"
037: * />
038: * </extension>
039: * </pre>
040: * where <code>com.example.myplugin.MyView</code> is the name of the
041: * <code>ViewPart</code> subclass.
042: * </p>
043: * <p>
044: * Subclasses must implement the following methods:
045: * <ul>
046: * <li><code>createPartControl</code> - to create the view's controls </li>
047: * <li><code>setFocus</code> - to accept focus</li>
048: * </ul>
049: * </p>
050: * <p>
051: * Subclasses may extend or reimplement the following methods as required:
052: * <ul>
053: * <li><code>setInitializationData</code> - extend to provide additional
054: * initialization when view extension is instantiated</li>
055: * <li><code>init(IWorkbenchPartSite)</code> - extend to provide additional
056: * initialization when view is assigned its site</li>
057: * <li><code>dispose</code> - extend to provide additional cleanup</li>
058: * <li><code>getAdapter</code> - reimplement to make their view adaptable</li>
059: * </ul>
060: * </p>
061: */
062: public abstract class ViewPart extends WorkbenchPart implements
063: IViewPart {
064:
065: /**
066: * Listens to PROP_TITLE property changes in this object until the first call to
067: * setContentDescription. Used for compatibility with old parts that call setTitle
068: * or overload getTitle instead of using setContentDescription.
069: */
070: private IPropertyListener compatibilityTitleListener = new IPropertyListener() {
071: /* (non-Javadoc)
072: * @see org.eclipse.ui.IPropertyListener#propertyChanged(java.lang.Object, int)
073: */
074: public void propertyChanged(Object source, int propId) {
075: if (propId == IWorkbenchPartConstants.PROP_TITLE) {
076: setDefaultContentDescription();
077: }
078: }
079: };
080:
081: /**
082: * Creates a new view.
083: */
084: protected ViewPart() {
085: super ();
086:
087: addPropertyListener(compatibilityTitleListener);
088: }
089:
090: /* (non-Javadoc)
091: * Method declared on IViewPart.
092: */
093: public IViewSite getViewSite() {
094: return (IViewSite) getSite();
095: }
096:
097: /* (non-Javadoc)
098: * @see org.eclipse.ui.IViewPart#init(org.eclipse.ui.IViewSite)
099: */
100: public void init(IViewSite site) throws PartInitException {
101: setSite(site);
102:
103: setDefaultContentDescription();
104: }
105:
106: /*
107: * (non-Javadoc)
108: * @see org.eclipse.ui.IViewPart#init(org.eclipse.ui.IViewSite, org.eclipse.ui.IMemento)
109: */
110: public void init(IViewSite site, IMemento memento)
111: 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: /* (non-Javadoc)
126: * @see org.eclipse.ui.IViewPart#saveState(org.eclipse.ui.IMemento)
127: */
128: public void saveState(IMemento memento) {
129: // do nothing
130: }
131:
132: /* (non-Javadoc)
133: * @see org.eclipse.ui.part.WorkbenchPart#setPartName(java.lang.String)
134: */
135: protected void setPartName(String partName) {
136: if (compatibilityTitleListener != null) {
137: removePropertyListener(compatibilityTitleListener);
138: compatibilityTitleListener = null;
139: }
140:
141: super .setPartName(partName);
142: }
143:
144: /* (non-Javadoc)
145: * @see org.eclipse.ui.part.WorkbenchPart#setContentDescription(java.lang.String)
146: */
147: protected void setContentDescription(String description) {
148: if (compatibilityTitleListener != null) {
149: removePropertyListener(compatibilityTitleListener);
150: compatibilityTitleListener = null;
151: }
152:
153: super .setContentDescription(description);
154: }
155:
156: /* (non-Javadoc)
157: * @see org.eclipse.core.runtime.IExecutableExtension#setInitializationData(org.eclipse.core.runtime.IConfigurationElement, java.lang.String, java.lang.Object)
158: */
159: public void setInitializationData(IConfigurationElement cfig,
160: String propertyName, Object data) {
161: super .setInitializationData(cfig, propertyName, data);
162:
163: setDefaultContentDescription();
164: }
165:
166: private void setDefaultContentDescription() {
167: if (compatibilityTitleListener == null) {
168: return;
169: }
170:
171: String partName = getPartName();
172: String title = getTitle();
173:
174: if (Util.equals(partName, title)) {
175: internalSetContentDescription(""); //$NON-NLS-1$
176: } else {
177: internalSetContentDescription(title);
178: }
179: }
180:
181: /**
182: * Checks that the given site is valid for this type of part.
183: * The site for a view must be an <code>IViewSite</code>.
184: *
185: * @param site the site to check
186: * @since 3.1
187: */
188: protected final void checkSite(IWorkbenchPartSite site) {
189: super .checkSite(site);
190: Assert.isTrue(site instanceof IViewSite,
191: "The site for a view must be an IViewSite"); //$NON-NLS-1$
192: }
193: }
|