1 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 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 contextId) { 46 JavaUIHelpListener listener= new JavaUIHelpListener(editor, contextId); 47 text.addHelpListener(listener); 48 } 49 50 57 public static IContextProvider getHelpContextProvider(IWorkbenchPart part, String 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 [] 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 fContextId; 73 private JavaEditor fEditor; 74 75 public JavaUIHelpListener(StructuredViewer viewer, String contextId) { 76 fViewer= viewer; 77 fContextId= contextId; 78 } 79 80 public JavaUIHelpListener(JavaEditor editor, String contextId) { 81 fContextId= contextId; 82 fEditor= editor; 83 } 84 85 89 public void helpRequested(HelpEvent e) { 90 try { 91 Object [] 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 fId; 112 private Object [] fSelected; 113 public JavaUIHelpContextProvider(String id, Object [] selected) { 114 fId= id; 115 fSelected= selected; 116 } 117 public int getContextChangeMask() { 118 return SELECTION; 119 } 120 public IContext getContext(Object 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 if (!e.isDoesNotExist()) 130 JavaPlugin.log(e); 131 } 132 } 133 return context; 134 } 135 public String getSearchExpression(Object target) { 136 return null; 137 } 138 } 139 } 140 | Popular Tags |