001: /*******************************************************************************
002: * Copyright (c) 2004, 2007 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.internal;
011:
012: import org.eclipse.core.runtime.CoreException;
013: import org.eclipse.core.runtime.IStatus;
014: import org.eclipse.core.runtime.Status;
015: import org.eclipse.swt.custom.BusyIndicator;
016: import org.eclipse.swt.graphics.Image;
017: import org.eclipse.swt.widgets.Composite;
018: import org.eclipse.swt.widgets.Control;
019: import org.eclipse.ui.IMemento;
020: import org.eclipse.ui.IPropertyListener;
021: import org.eclipse.ui.IViewSite;
022: import org.eclipse.ui.IWorkbenchPartSite;
023: import org.eclipse.ui.PartInitException;
024: import org.eclipse.ui.internal.intro.IntroMessages;
025: import org.eclipse.ui.intro.IIntroPart;
026: import org.eclipse.ui.intro.IIntroSite;
027: import org.eclipse.ui.part.ViewPart;
028:
029: /**
030: * Simple view that will wrap an <code>IIntroPart</code>.
031: *
032: * @since 3.0
033: */
034: public final class ViewIntroAdapterPart extends ViewPart {
035:
036: private IIntroPart introPart;
037:
038: private IIntroSite introSite;
039:
040: private boolean handleZoomEvents = true;
041:
042: /**
043: * Adds a listener that toggles standby state if the view pane is zoomed.
044: */
045: private void addPaneListener() {
046: IWorkbenchPartSite site = getSite();
047: if (site instanceof PartSite) {
048: final WorkbenchPartReference ref = ((WorkbenchPartReference) ((PartSite) site)
049: .getPartReference());
050: ref.addInternalPropertyListener(new IPropertyListener() {
051: public void propertyChanged(Object source, int propId) {
052: if (handleZoomEvents) {
053: if (propId == WorkbenchPartReference.INTERNAL_PROPERTY_ZOOMED) {
054: setStandby(!ref.getPane().isZoomed());
055: }
056: }
057: }
058: });
059: }
060: }
061:
062: /**
063: * Forces the standby state of the intro part.
064: *
065: * @param standby update the standby state
066: */
067: public void setStandby(final boolean standby) {
068: final Control control = ((PartSite) getSite()).getPane()
069: .getControl();
070: BusyIndicator.showWhile(control.getDisplay(), new Runnable() {
071: public void run() {
072: try {
073: control.setRedraw(false);
074: introPart.standbyStateChanged(standby);
075: } finally {
076: control.setRedraw(true);
077: }
078:
079: setBarVisibility(standby);
080: }
081: });
082: }
083:
084: /**
085: * Toggles handling of zoom events.
086: *
087: * @param handle whether to handle zoom events
088: */
089: public void setHandleZoomEvents(boolean handle) {
090: handleZoomEvents = handle;
091: }
092:
093: /* (non-Javadoc)
094: * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
095: */
096: public void createPartControl(Composite parent) {
097: addPaneListener();
098: introPart.createPartControl(parent);
099: }
100:
101: /* (non-Javadoc)
102: * @see org.eclipse.ui.IWorkbenchPart#dispose()
103: */
104: public void dispose() {
105: setBarVisibility(true);
106: super .dispose();
107: getSite().getWorkbenchWindow().getWorkbench().getIntroManager()
108: .closeIntro(introPart);
109: introPart.dispose();
110: }
111:
112: /* (non-Javadoc)
113: * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
114: */
115: public Object getAdapter(Class adapter) {
116: return introPart.getAdapter(adapter);
117: }
118:
119: /* (non-Javadoc)
120: * @see org.eclipse.ui.IWorkbenchPart#getTitleImage()
121: */
122: public Image getTitleImage() {
123: return introPart.getTitleImage();
124: }
125:
126: /* (non-Javadoc)
127: * @see org.eclipse.ui.part.WorkbenchPart#getTitle()
128: */
129: public String getTitle() {
130: // this method is called eagerly before our init method is called (and
131: // therefore before our intropart is created). By default return
132: // the view title from the view declaration. We will fire a property
133: // change to set the title to the proper value in the init method.
134: return introPart == null ? super .getTitle() : introPart
135: .getTitle();
136: }
137:
138: /* (non-Javadoc)
139: * @see org.eclipse.ui.IViewPart#init(org.eclipse.ui.IViewSite, org.eclipse.ui.IMemento)
140: */
141: public void init(IViewSite site, IMemento memento)
142: throws PartInitException {
143: super .init(site);
144: Workbench workbench = (Workbench) site.getWorkbenchWindow()
145: .getWorkbench();
146: try {
147: introPart = workbench.getWorkbenchIntroManager()
148: .createNewIntroPart();
149: // reset the part name of this view to be that of the intro title
150: setPartName(introPart.getTitle());
151: introPart.addPropertyListener(new IPropertyListener() {
152: public void propertyChanged(Object source, int propId) {
153: firePropertyChange(propId);
154: }
155: });
156: introSite = new ViewIntroAdapterSite(site, workbench
157: .getIntroDescriptor());
158: introPart.init(introSite, memento);
159:
160: } catch (CoreException e) {
161: WorkbenchPlugin.log(
162: IntroMessages.Intro_could_not_create_proxy,
163: new Status(IStatus.ERROR,
164: WorkbenchPlugin.PI_WORKBENCH,
165: IStatus.ERROR,
166: IntroMessages.Intro_could_not_create_proxy,
167: e));
168: }
169: }
170:
171: /*
172: * (non-Javadoc)
173: *
174: * @see org.eclipse.ui.IWorkbenchPart#setFocus()
175: */
176: public void setFocus() {
177: introPart.setFocus();
178: }
179:
180: /* (non-Javadoc)
181: * @see org.eclipse.ui.IViewPart#saveState(org.eclipse.ui.IMemento)
182: */
183: public void saveState(IMemento memento) {
184: introPart.saveState(memento);
185: }
186:
187: /**
188: * Sets whether the CoolBar/PerspectiveBar should be visible.
189: *
190: * @param visible whether the CoolBar/PerspectiveBar should be visible
191: * @since 3.1
192: */
193: private void setBarVisibility(final boolean visible) {
194: WorkbenchWindow window = (WorkbenchWindow) getSite()
195: .getWorkbenchWindow();
196:
197: final boolean layout = (visible != window.getCoolBarVisible())
198: || (visible != window.getPerspectiveBarVisible()); // don't layout unless things have actually changed
199: if (visible) {
200: window.setCoolBarVisible(true);
201: window.setPerspectiveBarVisible(true);
202: } else {
203: window.setCoolBarVisible(false);
204: window.setPerspectiveBarVisible(false);
205: }
206:
207: if (layout) {
208: window.getShell().layout();
209: }
210: }
211: }
|