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.IExtension;
014: import org.eclipse.core.runtime.IStatus;
015: import org.eclipse.core.runtime.Status;
016: import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;
017: import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
018: import org.eclipse.ui.IPerspectiveDescriptor;
019: import org.eclipse.ui.IViewPart;
020: import org.eclipse.ui.IViewReference;
021: import org.eclipse.ui.IWorkbenchPage;
022: import org.eclipse.ui.IWorkbenchWindow;
023: import org.eclipse.ui.PartInitException;
024: import org.eclipse.ui.internal.intro.IIntroConstants;
025: import org.eclipse.ui.internal.intro.IntroDescriptor;
026: import org.eclipse.ui.internal.intro.IntroMessages;
027: import org.eclipse.ui.intro.IIntroManager;
028: import org.eclipse.ui.intro.IIntroPart;
029: import org.eclipse.ui.intro.IntroContentDetector;
030:
031: /**
032: * Workbench implementation of the IIntroManager interface.
033: *
034: * @since 3.0
035: */
036: public class WorkbenchIntroManager implements IIntroManager {
037:
038: private final Workbench workbench;
039:
040: /**
041: * Create a new instance of the receiver.
042: *
043: * @param workbench the workbench instance
044: */
045: WorkbenchIntroManager(Workbench workbench) {
046: this .workbench = workbench;
047: workbench.getExtensionTracker().registerHandler(
048: new IExtensionChangeHandler() {
049:
050: /* (non-Javadoc)
051: * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#addExtension(org.eclipse.core.runtime.dynamicHelpers.IExtensionTracker, org.eclipse.core.runtime.IExtension)
052: */
053: public void addExtension(IExtensionTracker tracker,
054: IExtension extension) {
055: //Do nothing
056: }
057:
058: /* (non-Javadoc)
059: * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#removeExtension(org.eclipse.core.runtime.IExtension, java.lang.Object[])
060: */
061: public void removeExtension(IExtension source,
062: Object[] objects) {
063: for (int i = 0; i < objects.length; i++) {
064: if (objects[i] instanceof IIntroPart) {
065: closeIntro((IIntroPart) objects[i]);
066: }
067: }
068:
069: }
070: }, null);
071:
072: }
073:
074: /**
075: * The currently active introPart in this workspace, <code>null</code> if none.
076: */
077: private IIntroPart introPart;
078:
079: /* (non-Javadoc)
080: * @see org.eclipse.ui.IWorkbench#closeIntro(org.eclipse.ui.intro.IIntroPart)
081: */
082: public boolean closeIntro(IIntroPart part) {
083: if (introPart == null || !introPart.equals(part)) {
084: return false;
085: }
086:
087: IViewPart introView = getViewIntroAdapterPart();
088: if (introView != null) {
089: //assumption is that there is only ever one intro per workbench
090: //if we ever support one per window then this will need revisiting
091: IWorkbenchPage page = introView.getSite().getPage();
092: IViewReference reference = page
093: .findViewReference(IIntroConstants.INTRO_VIEW_ID);
094: page.hideView(introView);
095: if (reference == null || reference.getPart(false) == null) {
096: introPart = null;
097: return true;
098: }
099: return false;
100: }
101:
102: // if there is no part then null our reference
103: introPart = null;
104:
105: return true;
106: }
107:
108: /* (non-Javadoc)
109: * @see org.eclipse.ui.IWorkbench#showIntro(org.eclipse.ui.IWorkbenchWindow)
110: */
111: public IIntroPart showIntro(IWorkbenchWindow preferredWindow,
112: boolean standby) {
113: if (preferredWindow == null) {
114: preferredWindow = this .workbench.getActiveWorkbenchWindow();
115: }
116:
117: if (preferredWindow == null) {
118: return null;
119: }
120:
121: ViewIntroAdapterPart viewPart = getViewIntroAdapterPart();
122: if (viewPart == null) {
123: createIntro(preferredWindow);
124: } else {
125: try {
126: IWorkbenchPage page = viewPart.getSite().getPage();
127: IWorkbenchWindow window = page.getWorkbenchWindow();
128: if (!window.equals(preferredWindow)) {
129: window.getShell().setActive();
130: }
131:
132: page.showView(IIntroConstants.INTRO_VIEW_ID);
133: } catch (PartInitException e) {
134: WorkbenchPlugin
135: .log(
136: "Could not open intro", new Status(IStatus.ERROR, WorkbenchPlugin.PI_WORKBENCH, IStatus.ERROR, "Could not open intro", e)); //$NON-NLS-1$ //$NON-NLS-2$
137: }
138: }
139: setIntroStandby(introPart, standby);
140: return introPart;
141: }
142:
143: /**
144: * @param testWindow the window to test
145: * @return whether the intro exists in the given window
146: */
147: /*package*/boolean isIntroInWindow(IWorkbenchWindow testWindow) {
148: ViewIntroAdapterPart viewPart = getViewIntroAdapterPart();
149: if (viewPart == null) {
150: return false;
151: }
152:
153: IWorkbenchWindow window = viewPart.getSite()
154: .getWorkbenchWindow();
155: if (window.equals(testWindow)) {
156: return true;
157: }
158: return false;
159: }
160:
161: /**
162: * Create a new Intro area (a view, currently) in the provided window. If there is no intro
163: * descriptor for this workbench then no work is done.
164: *
165: * @param preferredWindow the window to create the intro in.
166: */
167: private void createIntro(IWorkbenchWindow preferredWindow) {
168: if (this .workbench.getIntroDescriptor() == null) {
169: return;
170: }
171:
172: IWorkbenchPage workbenchPage = preferredWindow.getActivePage();
173: if (workbenchPage == null) {
174: return;
175: }
176: try {
177: workbenchPage.showView(IIntroConstants.INTRO_VIEW_ID);
178: } catch (PartInitException e) {
179: WorkbenchPlugin.log(
180: IntroMessages.Intro_could_not_create_part,
181: new Status(IStatus.ERROR,
182: WorkbenchPlugin.PI_WORKBENCH,
183: IStatus.ERROR,
184: IntroMessages.Intro_could_not_create_part,
185: e));
186: }
187: }
188:
189: /* (non-Javadoc)
190: * @see org.eclipse.ui.IWorkbench#setIntroStandby(org.eclipse.ui.intro.IIntroPart, boolean)
191: */
192: public void setIntroStandby(IIntroPart part, boolean standby) {
193: if (introPart == null || !introPart.equals(part)) {
194: return;
195: }
196:
197: ViewIntroAdapterPart viewIntroAdapterPart = getViewIntroAdapterPart();
198: if (viewIntroAdapterPart == null) {
199: return;
200: }
201:
202: PartPane pane = ((PartSite) viewIntroAdapterPart.getSite())
203: .getPane();
204: if (standby == !pane.isZoomed()) {
205: // the zoom state is already correct - just update the part's state.
206: viewIntroAdapterPart.setStandby(standby);
207: return;
208: }
209:
210: viewIntroAdapterPart.getSite().getPage().toggleZoom(
211: pane.getPartReference());
212: }
213:
214: /*
215: * (non-Javadoc)
216: *
217: * @see org.eclipse.ui.IWorkbench#isIntroStandby(org.eclipse.ui.intro.IIntroPart)
218: */
219: public boolean isIntroStandby(IIntroPart part) {
220: if (introPart == null || !introPart.equals(part)) {
221: return false;
222: }
223:
224: ViewIntroAdapterPart viewIntroAdapterPart = getViewIntroAdapterPart();
225: if (viewIntroAdapterPart == null) {
226: return false;
227: }
228:
229: return !((PartSite) viewIntroAdapterPart.getSite()).getPane()
230: .isZoomed();
231: }
232:
233: /* (non-Javadoc)
234: * @see org.eclipse.ui.IWorkbench#findIntro()
235: */
236: public IIntroPart getIntro() {
237: return introPart;
238: }
239:
240: /**
241: * @return the <code>ViewIntroAdapterPart</code> for this workbench, <code>null</code> if it
242: * cannot be found.
243: */
244: /*package*/ViewIntroAdapterPart getViewIntroAdapterPart() {
245: IWorkbenchWindow[] windows = this .workbench
246: .getWorkbenchWindows();
247: for (int i = 0; i < windows.length; i++) {
248: IWorkbenchWindow window = windows[i];
249: WorkbenchPage page = (WorkbenchPage) window.getActivePage();
250: if (page == null) {
251: continue;
252: }
253: IPerspectiveDescriptor[] perspDescs = page
254: .getOpenPerspectives();
255: for (int j = 0; j < perspDescs.length; j++) {
256: IPerspectiveDescriptor descriptor = perspDescs[j];
257: IViewReference reference = page.findPerspective(
258: descriptor).findView(
259: IIntroConstants.INTRO_VIEW_ID);
260: if (reference != null) {
261: IViewPart part = reference.getView(false);
262: if (part != null
263: && part instanceof ViewIntroAdapterPart) {
264: return (ViewIntroAdapterPart) part;
265: }
266: }
267: }
268: }
269: return null;
270: }
271:
272: /**
273: * @return a new IIntroPart. This has the side effect of setting the introPart field to the new
274: * value.
275: */
276: /*package*/IIntroPart createNewIntroPart() throws CoreException {
277: IntroDescriptor introDescriptor = workbench
278: .getIntroDescriptor();
279: introPart = introDescriptor == null ? null : introDescriptor
280: .createIntro();
281: if (introPart != null) {
282: workbench.getExtensionTracker().registerObject(
283: introDescriptor.getConfigurationElement()
284: .getDeclaringExtension(), introPart,
285: IExtensionTracker.REF_WEAK);
286: }
287: return introPart;
288: }
289:
290: /* (non-Javadoc)
291: * @see org.eclipse.ui.IWorkbench#hasIntro()
292: */
293: public boolean hasIntro() {
294: return workbench.getIntroDescriptor() != null;
295: }
296:
297: public boolean isNewContentAvailable() {
298: IntroDescriptor introDescriptor = workbench
299: .getIntroDescriptor();
300: if (introDescriptor == null) {
301: return false;
302: }
303: try {
304: IntroContentDetector contentDetector = introDescriptor
305: .getIntroContentDetector();
306: if (contentDetector != null) {
307: return contentDetector.isNewContentAvailable();
308: }
309: } catch (CoreException ex) {
310: WorkbenchPlugin.log(new Status(IStatus.WARNING,
311: WorkbenchPlugin.PI_WORKBENCH, IStatus.WARNING,
312: "Could not load intro content detector", ex)); //$NON-NLS-1$
313: }
314: return false;
315: }
316: }
|