KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > util > JavaUIHelp


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.jdt.internal.ui.util;
12
13 import org.eclipse.core.runtime.CoreException;
14
15 import org.eclipse.swt.custom.StyledText;
16 import org.eclipse.swt.events.HelpEvent;
17 import org.eclipse.swt.events.HelpListener;
18
19 import org.eclipse.help.HelpSystem;
20 import org.eclipse.help.IContext;
21 import org.eclipse.help.IContextProvider;
22
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.jface.viewers.StructuredSelection;
26 import org.eclipse.jface.viewers.StructuredViewer;
27
28 import org.eclipse.ui.IWorkbenchPart;
29
30 import org.eclipse.jdt.core.IJavaElement;
31 import org.eclipse.jdt.core.JavaModelException;
32
33 import org.eclipse.jdt.internal.ui.JavaPlugin;
34 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
35 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
36 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
37
38 public class JavaUIHelp {
39
40     public static void setHelp(StructuredViewer viewer, String JavaDoc contextId) {
41         JavaUIHelpListener listener= new JavaUIHelpListener(viewer, contextId);
42         viewer.getControl().addHelpListener(listener);
43     }
44
45     public static void setHelp(JavaEditor editor, StyledText text, String JavaDoc contextId) {
46         JavaUIHelpListener listener= new JavaUIHelpListener(editor, contextId);
47         text.addHelpListener(listener);
48     }
49     
50     /**
51      * Creates and returns a help context provider for the given part.
52      *
53      * @param part the part for which to create the help context provider
54      * @param contextId the optional context ID used to retrieve static help
55      * @return the help context provider
56      */

57     public static IContextProvider getHelpContextProvider(IWorkbenchPart part, String JavaDoc contextId) {
58         IStructuredSelection selection;
59         try {
60             selection= SelectionConverter.getStructuredSelection(part);
61         } catch (JavaModelException ex) {
62             JavaPlugin.log(ex);
63             selection= StructuredSelection.EMPTY;
64         }
65         Object JavaDoc[] elements= selection.toArray();
66         return new JavaUIHelpContextProvider(contextId, elements);
67     }
68
69     private static class JavaUIHelpListener implements HelpListener {
70
71         private StructuredViewer fViewer;
72         private String JavaDoc fContextId;
73         private JavaEditor fEditor;
74
75         public JavaUIHelpListener(StructuredViewer viewer, String JavaDoc contextId) {
76             fViewer= viewer;
77             fContextId= contextId;
78         }
79
80         public JavaUIHelpListener(JavaEditor editor, String JavaDoc contextId) {
81             fContextId= contextId;
82             fEditor= editor;
83         }
84
85         /*
86          * @see HelpListener#helpRequested(HelpEvent)
87          *
88          */

89         public void helpRequested(HelpEvent e) {
90             try {
91                 Object JavaDoc[] selected= null;
92                 if (fViewer != null) {
93                     ISelection selection= fViewer.getSelection();
94                     if (selection instanceof IStructuredSelection) {
95                         selected= ((IStructuredSelection)selection).toArray();
96                     }
97                 } else if (fEditor != null) {
98                     IJavaElement input= SelectionConverter.getInput(fEditor);
99                     if (ActionUtil.isOnBuildPath(input)) {
100                         selected= SelectionConverter.codeResolve(fEditor);
101                     }
102                 }
103                 JavadocHelpContext.displayHelp(fContextId, selected);
104             } catch (CoreException x) {
105                 JavaPlugin.log(x);
106             }
107         }
108     }
109
110     private static class JavaUIHelpContextProvider implements IContextProvider {
111         private String JavaDoc fId;
112         private Object JavaDoc[] fSelected;
113         public JavaUIHelpContextProvider(String JavaDoc id, Object JavaDoc[] selected) {
114             fId= id;
115             fSelected= selected;
116         }
117         public int getContextChangeMask() {
118             return SELECTION;
119         }
120         public IContext getContext(Object JavaDoc target) {
121             IContext context= HelpSystem.getContext(fId);
122             if (fSelected != null && fSelected.length > 0) {
123                 try {
124                     context= new JavadocHelpContext(context, fSelected);
125                 } catch (JavaModelException e) {
126                     // since we are updating the UI with async exec it
127
// can happen that the element doesn't exist anymore
128
// but we are still showing it in the user interface
129
if (!e.isDoesNotExist())
130                         JavaPlugin.log(e);
131                 }
132             }
133             return context;
134         }
135         public String JavaDoc getSearchExpression(Object JavaDoc target) {
136             return null;
137         }
138     }
139 }
140
Popular Tags