KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > WorkbenchIntroManager


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 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.internal;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IExtension;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;
18 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
19 import org.eclipse.ui.IPerspectiveDescriptor;
20 import org.eclipse.ui.IViewPart;
21 import org.eclipse.ui.IViewReference;
22 import org.eclipse.ui.IWorkbenchPage;
23 import org.eclipse.ui.IWorkbenchWindow;
24 import org.eclipse.ui.PartInitException;
25 import org.eclipse.ui.internal.intro.IIntroConstants;
26 import org.eclipse.ui.internal.intro.IntroDescriptor;
27 import org.eclipse.ui.internal.intro.IntroMessages;
28 import org.eclipse.ui.intro.IIntroManager;
29 import org.eclipse.ui.intro.IIntroPart;
30 import org.eclipse.ui.intro.IntroContentDetector;
31
32 /**
33  * Workbench implementation of the IIntroManager interface.
34  *
35  * @since 3.0
36  */

37 public class WorkbenchIntroManager implements IIntroManager {
38     
39     private final Workbench workbench;
40
41     /**
42      * Create a new instance of the receiver.
43      *
44      * @param workbench the workbench instance
45      */

46     WorkbenchIntroManager(Workbench workbench) {
47         this.workbench = workbench;
48         workbench.getExtensionTracker().registerHandler(new IExtensionChangeHandler(){
49             
50             /* (non-Javadoc)
51              * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#addExtension(org.eclipse.core.runtime.dynamicHelpers.IExtensionTracker, org.eclipse.core.runtime.IExtension)
52              */

53             public void addExtension(IExtensionTracker tracker,IExtension extension) {
54                 //Do nothing
55
}
56             
57             /* (non-Javadoc)
58              * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#removeExtension(org.eclipse.core.runtime.IExtension, java.lang.Object[])
59              */

60             public void removeExtension(IExtension source, Object JavaDoc[] objects) {
61                 for (int i = 0; i < objects.length; i++) {
62                     if (objects[i] instanceof IIntroPart) {
63                         closeIntro((IIntroPart) objects[i]);
64                     }
65                 }
66                 
67             }}, null);
68         
69     }
70
71     /**
72      * The currently active introPart in this workspace, <code>null</code> if none.
73      */

74     private IIntroPart introPart;
75
76     /* (non-Javadoc)
77      * @see org.eclipse.ui.IWorkbench#closeIntro(org.eclipse.ui.intro.IIntroPart)
78      */

79     public boolean closeIntro(IIntroPart part) {
80         if (introPart == null || !introPart.equals(part)) {
81             return false;
82         }
83
84         IViewPart introView = getViewIntroAdapterPart();
85         if (introView != null) {
86             //assumption is that there is only ever one intro per workbench
87
//if we ever support one per window then this will need revisiting
88
IWorkbenchPage page = introView.getSite().getPage();
89             IViewReference reference = page
90                     .findViewReference(IIntroConstants.INTRO_VIEW_ID);
91             page.hideView(introView);
92             if (reference == null || reference.getPart(false) == null) {
93                 introPart = null;
94                 return true;
95             }
96             return false;
97         }
98         
99         // if there is no part then null our reference
100
introPart = null;
101         
102         return true;
103     }
104
105     /* (non-Javadoc)
106      * @see org.eclipse.ui.IWorkbench#showIntro(org.eclipse.ui.IWorkbenchWindow)
107      */

108     public IIntroPart showIntro(IWorkbenchWindow preferredWindow,
109             boolean standby) {
110         if (preferredWindow == null) {
111             preferredWindow = this.workbench.getActiveWorkbenchWindow();
112         }
113
114         if (preferredWindow == null) {
115             return null;
116         }
117
118         ViewIntroAdapterPart viewPart = getViewIntroAdapterPart();
119         if (viewPart == null) {
120             createIntro(preferredWindow);
121         } else {
122             try {
123                 IWorkbenchPage page = viewPart.getSite().getPage();
124                 IWorkbenchWindow window = page.getWorkbenchWindow();
125                 if (!window.equals(preferredWindow)) {
126                     window.getShell().setActive();
127                 }
128
129                 page.showView(IIntroConstants.INTRO_VIEW_ID);
130             } catch (PartInitException e) {
131                 WorkbenchPlugin
132                         .log(
133                                 "Could not open intro", new Status(IStatus.ERROR, WorkbenchPlugin.PI_WORKBENCH, IStatus.ERROR, "Could not open intro", e)); //$NON-NLS-1$ //$NON-NLS-2$
134
}
135         }
136         setIntroStandby(introPart, standby);
137         return introPart;
138     }
139
140     /**
141      * @param testWindow the window to test
142      * @return whether the intro exists in the given window
143      */

144     /*package*/boolean isIntroInWindow(IWorkbenchWindow testWindow) {
145         ViewIntroAdapterPart viewPart = getViewIntroAdapterPart();
146         if (viewPart == null) {
147             return false;
148         }
149
150         IWorkbenchWindow window = viewPart.getSite().getWorkbenchWindow();
151         if (window.equals(testWindow)) {
152             return true;
153         }
154         return false;
155     }
156
157     /**
158      * Create a new Intro area (a view, currently) in the provided window. If there is no intro
159      * descriptor for this workbench then no work is done.
160      *
161      * @param preferredWindow the window to create the intro in.
162      */

163     private void createIntro(IWorkbenchWindow preferredWindow) {
164         if (this.workbench.getIntroDescriptor() == null) {
165             return;
166         }
167
168         IWorkbenchPage workbenchPage = preferredWindow.getActivePage();
169         if (workbenchPage == null) {
170             return;
171         }
172         try {
173             workbenchPage.showView(IIntroConstants.INTRO_VIEW_ID);
174         } catch (PartInitException e) {
175             WorkbenchPlugin
176                     .log(
177                             IntroMessages.Intro_could_not_create_part, new Status(IStatus.ERROR, WorkbenchPlugin.PI_WORKBENCH, IStatus.ERROR, IntroMessages.Intro_could_not_create_part, e));
178         }
179     }
180
181     /* (non-Javadoc)
182      * @see org.eclipse.ui.IWorkbench#setIntroStandby(org.eclipse.ui.intro.IIntroPart, boolean)
183      */

184     public void setIntroStandby(IIntroPart part, boolean standby) {
185         if (introPart == null || !introPart.equals(part)) {
186             return;
187         }
188
189         ViewIntroAdapterPart viewIntroAdapterPart = getViewIntroAdapterPart();
190         if (viewIntroAdapterPart == null) {
191             return;
192         }
193
194         PartPane pane = ((PartSite) viewIntroAdapterPart.getSite()).getPane();
195         if (standby == !pane.isZoomed()) {
196             // the zoom state is already correct - just update the part's state.
197
viewIntroAdapterPart.setStandby(standby);
198             return;
199         }
200
201         viewIntroAdapterPart.getSite().getPage().toggleZoom(
202                 pane.getPartReference());
203     }
204
205     /*
206      * (non-Javadoc)
207      *
208      * @see org.eclipse.ui.IWorkbench#isIntroStandby(org.eclipse.ui.intro.IIntroPart)
209      */

210     public boolean isIntroStandby(IIntroPart part) {
211         if (introPart == null || !introPart.equals(part)) {
212             return false;
213         }
214
215         ViewIntroAdapterPart viewIntroAdapterPart = getViewIntroAdapterPart();
216         if (viewIntroAdapterPart == null) {
217             return false;
218         }
219
220         return !((PartSite) viewIntroAdapterPart.getSite()).getPane()
221                 .isZoomed();
222     }
223
224     /* (non-Javadoc)
225      * @see org.eclipse.ui.IWorkbench#findIntro()
226      */

227     public IIntroPart getIntro() {
228         return introPart;
229     }
230
231     /**
232      * @return the <code>ViewIntroAdapterPart</code> for this workbench, <code>null</code> if it
233      * cannot be found.
234      */

235     /*package*/ViewIntroAdapterPart getViewIntroAdapterPart() {
236         IWorkbenchWindow[] windows = this.workbench.getWorkbenchWindows();
237         for (int i = 0; i < windows.length; i++) {
238             IWorkbenchWindow window = windows[i];
239             WorkbenchPage page = (WorkbenchPage) window.getActivePage();
240             if (page == null) {
241                 continue;
242             }
243             IPerspectiveDescriptor[] perspDescs = page.getOpenPerspectives();
244             for (int j = 0; j < perspDescs.length; j++) {
245                 IPerspectiveDescriptor descriptor = perspDescs[j];
246                 IViewReference reference = page.findPerspective(descriptor)
247                         .findView(IIntroConstants.INTRO_VIEW_ID);
248                 if (reference != null) {
249                     IViewPart part = reference.getView(false);
250                     if (part != null && part instanceof ViewIntroAdapterPart) {
251                         return (ViewIntroAdapterPart) part;
252                     }
253                 }
254             }
255         }
256         return null;
257     }
258
259     /**
260      * @return a new IIntroPart. This has the side effect of setting the introPart field to the new
261      * value.
262      */

263     /*package*/IIntroPart createNewIntroPart() throws CoreException {
264         IntroDescriptor introDescriptor = workbench.getIntroDescriptor();
265         introPart = introDescriptor == null ? null
266                 : introDescriptor.createIntro();
267         if (introPart != null) {
268             workbench.getExtensionTracker().registerObject(
269                     introDescriptor.getConfigurationElement()
270                             .getDeclaringExtension(), introPart,
271                     IExtensionTracker.REF_WEAK);
272         }
273         return introPart;
274     }
275
276     /* (non-Javadoc)
277      * @see org.eclipse.ui.IWorkbench#hasIntro()
278      */

279     public boolean hasIntro() {
280         return workbench.getIntroDescriptor() != null;
281     }
282     
283     public boolean isNewContentAvailable() {
284         IntroDescriptor introDescriptor = workbench.getIntroDescriptor();
285         if (introDescriptor == null) {
286             return false;
287         }
288         try {
289             IntroContentDetector contentDetector = introDescriptor
290                     .getIntroContentDetector();
291             if (contentDetector != null) {
292                 return contentDetector.isNewContentAvailable();
293             }
294         } catch (CoreException ex) {
295             WorkbenchPlugin.log(new Status(IStatus.WARNING,
296                     WorkbenchPlugin.PI_WORKBENCH, IStatus.WARNING,
297                     "Could not load intro content detector", ex)); //$NON-NLS-1$
298
}
299         return false;
300     }
301 }
302
Popular Tags