KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > ui > internal > views > HelpView


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.help.ui.internal.views;
12
13 import org.eclipse.help.*;
14 import org.eclipse.help.ui.internal.*;
15 import org.eclipse.jface.dialogs.IPageChangeProvider;
16 import org.eclipse.jface.dialogs.IPageChangedListener;
17 import org.eclipse.jface.dialogs.PageChangedEvent;
18 import org.eclipse.jface.viewers.*;
19 import org.eclipse.swt.widgets.*;
20 import org.eclipse.ui.*;
21 import org.eclipse.ui.forms.HyperlinkGroup;
22 import org.eclipse.ui.forms.widgets.FormToolkit;
23 import org.eclipse.ui.part.ViewPart;
24
25 public class HelpView extends ViewPart implements IPartListener2,
26         ISelectionChangedListener, IPageChangedListener {
27     private FormToolkit toolkit;
28     
29     private String JavaDoc firstPageId;
30
31     private ReusableHelpPart reusableHelpPart;
32
33     //private Hashtable pageRecs;
34

35     private IWorkbenchPart monitoredPart;
36
37     private boolean visible;
38
39     /**
40      *
41      */

42     public HelpView() {
43     }
44
45     /*
46      * (non-Javadoc)
47      *
48      * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
49      */

50     public void createPartControl(Composite parent) {
51         toolkit = new FormToolkit(parent.getDisplay());
52         toolkit.getHyperlinkGroup().setHyperlinkUnderlineMode(
53                 HyperlinkGroup.UNDERLINE_HOVER);
54         // toolkit.setBackground(toolkit.getColors().createNoContentBackground());
55
toolkit.getColors().initializeSectionToolBarColors();
56         reusableHelpPart.createControl(parent, toolkit);
57         reusableHelpPart.setDefaultContextHelpText(Messages.HelpView_defaultText);
58         reusableHelpPart.showPage(getFirstPage());
59         IWorkbenchWindow window = PlatformUI.getWorkbench()
60                 .getActiveWorkbenchWindow();
61         if (window == null)
62             return;
63         IWorkbenchPage page = window.getActivePage();
64         if (page == null)
65             return;
66         IWorkbenchPartReference aref = page.getActivePartReference();
67         if (aref != null)
68             handlePartActivation(aref);
69     }
70
71     public void dispose() {
72         IWorkbenchWindow window = PlatformUI.getWorkbench()
73                 .getActiveWorkbenchWindow();
74         IPartService service = window.getPartService();
75         if (monitoredPart != null) {
76             uninstallSelectionListener(monitoredPart);
77             uninstallPageListener(monitoredPart);
78         }
79         service.removePartListener(this);
80         if (reusableHelpPart != null) {
81             reusableHelpPart.dispose();
82             reusableHelpPart = null;
83         }
84         if (toolkit != null) {
85             toolkit.dispose();
86             toolkit = null;
87         }
88         super.dispose();
89     }
90
91     public void init(IViewSite site, IMemento memento) throws PartInitException {
92         if (memento!=null)
93             this.firstPageId = memento.getString("pageId"); //$NON-NLS-1$
94
init(site);
95         reusableHelpPart = new ReusableHelpPart(site.getWorkbenchWindow(),
96                 getHelpPartStyle());
97         IActionBars actionBars = site.getActionBars();
98         reusableHelpPart.init(actionBars, actionBars.getToolBarManager(),
99                 actionBars.getStatusLineManager(), memento);
100         IWorkbenchWindow window = PlatformUI.getWorkbench()
101                 .getActiveWorkbenchWindow();
102         IPartService service = window.getPartService();
103         service.addPartListener(this);
104     }
105     
106     public void saveState(IMemento memento) {
107         if (reusableHelpPart!=null && memento!=null) {
108             String JavaDoc pageId = reusableHelpPart.getCurrentPageId();
109             if (pageId!=null)
110                 memento.putString("pageId", pageId); //$NON-NLS-1$
111
reusableHelpPart.saveState(memento);
112         }
113     }
114
115     private void handlePartActivation(IWorkbenchPartReference ref) {
116         if (reusableHelpPart == null)
117             return;
118         if (!visible || !reusableHelpPart.isMonitoringContextHelp())
119             return;
120         if (isThisPart(ref))
121             return;
122         IWorkbenchPart part = ref.getPart(false);
123         Display display = part.getSite().getShell().getDisplay();
124         Control c = display.getFocusControl();
125         if (c != null && c.isVisible() && !c.isDisposed()) {
126             IContextProvider provider = (IContextProvider) part
127                     .getAdapter(IContextProvider.class);
128             if (provider != null) {
129                 reusableHelpPart.update(provider, null, part, c);
130                 if ((provider.getContextChangeMask() & IContextProvider.SELECTION) != 0) {
131                     // context help changes with selections
132
installSelectionListener(part);
133                 }
134             } else
135                 reusableHelpPart.update(part, c);
136             if (part instanceof IPageChangeProvider)
137                 installPageListener(part);
138         }
139     }
140
141     private void installPageListener(IWorkbenchPart part) {
142         if (part instanceof IPageChangeProvider)
143             ((IPageChangeProvider)part).addPageChangedListener(this);
144         monitoredPart = part;
145     }
146     
147     private void uninstallPageListener(IWorkbenchPart part) {
148         if (part instanceof IPageChangeProvider)
149             ((IPageChangeProvider)part).removePageChangedListener(this);
150         monitoredPart = null;
151     }
152
153     private void installSelectionListener(IWorkbenchPart part) {
154         ISelectionProvider provider = part.getSite().getSelectionProvider();
155         if (provider instanceof IPostSelectionProvider)
156             ((IPostSelectionProvider) provider)
157                 .addPostSelectionChangedListener(this);
158         else
159             provider.addSelectionChangedListener(this);
160         monitoredPart = part;
161     }
162
163     private void uninstallSelectionListener(IWorkbenchPart part) {
164         ISelectionProvider provider = part.getSite().getSelectionProvider();
165         if (provider instanceof IPostSelectionProvider)
166             ((IPostSelectionProvider) provider)
167                     .removePostSelectionChangedListener(this);
168         else if (provider != null)
169             provider.removeSelectionChangedListener(this);
170         monitoredPart = null;
171     }
172
173     private boolean isThisPart(IWorkbenchPartReference ref) {
174         IWorkbenchPart part = ref.getPart(false);
175         return part != null && part.equals(this);
176     }
177
178     private void updateActivePart() {
179         if (reusableHelpPart == null)
180             return;
181         if (!reusableHelpPart.isMonitoringContextHelp())
182             return;
183         if (monitoredPart == null)
184             return;
185         Control c = monitoredPart.getSite().getShell().getDisplay()
186                 .getFocusControl();
187         if (c != null && c.isDisposed() == false && visible) {
188             IContextProvider provider = (IContextProvider) monitoredPart
189             .getAdapter(IContextProvider.class);
190             if (provider != null)
191                 reusableHelpPart.update(provider, null, monitoredPart, c);
192             else
193                 reusableHelpPart.update(monitoredPart, c);
194         }
195     }
196
197     private void handlePartDeactivation(IWorkbenchPartReference ref) {
198         IWorkbenchPart part = ref.getPart(false);
199         if (monitoredPart != null && part != null && part.equals(monitoredPart)) {
200             uninstallSelectionListener(part);
201             uninstallPageListener(part);
202         }
203     }
204
205     /*
206      * (non-Javadoc)
207      *
208      * @see org.eclipse.ui.IPartListener2#partActivated(org.eclipse.ui.IWorkbenchPartReference)
209      */

210     public void partActivated(final IWorkbenchPartReference partRef) {
211         if (isThisPart(partRef)) {
212             visible = true;
213             hook(true);
214             selectionChanged(null);
215         } else {
216             handlePartActivation(partRef);
217         }
218     }
219
220     /*
221      * (non-Javadoc)
222      *
223      * @see org.eclipse.ui.IPartListener2#partBroughtToTop(org.eclipse.ui.IWorkbenchPartReference)
224      */

225     public void partBroughtToTop(IWorkbenchPartReference partRef) {
226         if (isThisPart(partRef)) {
227             visible = true;
228             hook(true);
229             selectionChanged(null);
230         }
231     }
232
233     /*
234      * (non-Javadoc)
235      *
236      * @see org.eclipse.ui.IPartListener2#partClosed(org.eclipse.ui.IWorkbenchPartReference)
237      */

238     public void partClosed(IWorkbenchPartReference partRef) {
239         handlePartDeactivation(partRef);
240     }
241
242     /*
243      * (non-Javadoc)
244      *
245      * @see org.eclipse.ui.IPartListener2#partDeactivated(org.eclipse.ui.IWorkbenchPartReference)
246      */

247     public void partDeactivated(IWorkbenchPartReference partRef) {
248         handlePartDeactivation(partRef);
249     }
250
251     /*
252      * (non-Javadoc)
253      *
254      * @see org.eclipse.ui.IPartListener2#partHidden(org.eclipse.ui.IWorkbenchPartReference)
255      */

256     public void partHidden(IWorkbenchPartReference partRef) {
257         if (isThisPart(partRef)) {
258             visible = false;
259             hook(false);
260         }
261     }
262
263     /*
264      * (non-Javadoc)
265      *
266      * @see org.eclipse.ui.IPartListener2#partInputChanged(org.eclipse.ui.IWorkbenchPartReference)
267      */

268     public void partInputChanged(IWorkbenchPartReference partRef) {
269     }
270
271     /*
272      * (non-Javadoc)
273      *
274      * @see org.eclipse.ui.IPartListener2#partOpened(org.eclipse.ui.IWorkbenchPartReference)
275      */

276     public void partOpened(IWorkbenchPartReference partRef) {
277         if (isThisPart(partRef)) {
278             visible = true;
279             hook(true);
280             selectionChanged(null);
281         }
282     }
283
284     /*
285      * (non-Javadoc)
286      *
287      * @see org.eclipse.ui.IPartListener2#partVisible(org.eclipse.ui.IWorkbenchPartReference)
288      */

289     public void partVisible(IWorkbenchPartReference partRef) {
290         if (isThisPart(partRef)) {
291             visible = true;
292             hook(true);
293             selectionChanged(null);
294         }
295     }
296
297     private void hook(boolean doHook) {
298         if (doHook) {
299             IWorkbenchWindow window = PlatformUI.getWorkbench()
300                     .getActiveWorkbenchWindow();
301             IPartService service = window.getPartService();
302             IWorkbenchPartReference aref = service.getActivePartReference();
303             if (aref != null)
304                 handlePartActivation(aref);
305         } else {
306             if (monitoredPart != null) {
307                 uninstallSelectionListener(monitoredPart);
308                 uninstallPageListener(monitoredPart);
309             }
310         }
311     }
312
313     /*
314      * (non-Javadoc)
315      *
316      * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
317      */

318     public void selectionChanged(SelectionChangedEvent event) {
319         if (!visible)
320             return;
321         getSite().getShell().getDisplay().asyncExec(new Runnable JavaDoc() {
322             public void run() {
323                 updateActivePart();
324             }
325         });
326     }
327
328     /*
329      * (non-Javadoc)
330      *
331      * @see org.eclipse.help.ui.internal.views.BaseHelpView#getFirstPage()
332      */

333     protected String JavaDoc getFirstPage() {
334         if (firstPageId!=null)
335             return firstPageId;
336         return IHelpUIConstants.HV_CONTEXT_HELP_PAGE;
337     }
338
339     public void displayContext(IContext context, IWorkbenchPart part,
340             Control control) {
341         if (reusableHelpPart != null) {
342             /*
343              * If the context help has no description text and exactly one
344              * topic, go straight to the topic and skip context help.
345              */

346             IHelpResource[] topics = context.getRelatedTopics();
347             if (context.getText() != null || topics.length != 1) {
348                 // Ensure that context help is currently showing
349
reusableHelpPart.showPage(IHelpUIConstants.HV_CONTEXT_HELP_PAGE);
350                 // check if there is a dynamic version
351
IContextProvider provider = null;
352                 if (part!=null)
353                         provider = (IContextProvider) part
354                         .getAdapter(IContextProvider.class);
355                 if (provider != null)
356                     reusableHelpPart.update(provider, context, part, control);
357                 else
358                     reusableHelpPart.update(context, part, control);
359             }
360             else {
361                 reusableHelpPart.showURL(topics[0].getHref());
362             }
363         }
364     }
365
366     /*
367      * (non-Javadoc)
368      *
369      * @see org.eclipse.help.ui.internal.views.BaseHelpView#getHelpPartStyle()
370      */

371     protected int getHelpPartStyle() {
372         return ReusableHelpPart.getDefaultStyle();
373     }
374
375     public void setFocus() {
376         if (reusableHelpPart != null)
377             reusableHelpPart.setFocus();
378     }
379
380     public void startSearch(String JavaDoc phrase) {
381         if (reusableHelpPart != null)
382             reusableHelpPart.startSearch(phrase);
383     }
384     public void showDynamicHelp(IWorkbenchPart part, Control c) {
385         if (reusableHelpPart != null)
386             reusableHelpPart.showDynamicHelp(part, c);
387     }
388
389     public void pageChanged(PageChangedEvent event) {
390         if (!visible)
391             return;
392         updateActivePart();
393     }
394 }
395
Popular Tags