001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 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.jdt.internal.ui.util;
011:
012: import org.eclipse.core.runtime.CoreException;
013:
014: import org.eclipse.swt.custom.StyledText;
015: import org.eclipse.swt.events.HelpEvent;
016: import org.eclipse.swt.events.HelpListener;
017:
018: import org.eclipse.help.HelpSystem;
019: import org.eclipse.help.IContext;
020: import org.eclipse.help.IContextProvider;
021:
022: import org.eclipse.jface.viewers.ISelection;
023: import org.eclipse.jface.viewers.IStructuredSelection;
024: import org.eclipse.jface.viewers.StructuredSelection;
025: import org.eclipse.jface.viewers.StructuredViewer;
026:
027: import org.eclipse.ui.IWorkbenchPart;
028:
029: import org.eclipse.jdt.core.IJavaElement;
030: import org.eclipse.jdt.core.JavaModelException;
031:
032: import org.eclipse.jdt.internal.ui.JavaPlugin;
033: import org.eclipse.jdt.internal.ui.actions.ActionUtil;
034: import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
035: import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
036:
037: public class JavaUIHelp {
038:
039: public static void setHelp(StructuredViewer viewer, String contextId) {
040: JavaUIHelpListener listener = new JavaUIHelpListener(viewer,
041: contextId);
042: viewer.getControl().addHelpListener(listener);
043: }
044:
045: public static void setHelp(JavaEditor editor, StyledText text,
046: String contextId) {
047: JavaUIHelpListener listener = new JavaUIHelpListener(editor,
048: contextId);
049: text.addHelpListener(listener);
050: }
051:
052: /**
053: * Creates and returns a help context provider for the given part.
054: *
055: * @param part the part for which to create the help context provider
056: * @param contextId the optional context ID used to retrieve static help
057: * @return the help context provider
058: */
059: public static IContextProvider getHelpContextProvider(
060: IWorkbenchPart part, String contextId) {
061: IStructuredSelection selection;
062: try {
063: selection = SelectionConverter.getStructuredSelection(part);
064: } catch (JavaModelException ex) {
065: JavaPlugin.log(ex);
066: selection = StructuredSelection.EMPTY;
067: }
068: Object[] elements = selection.toArray();
069: return new JavaUIHelpContextProvider(contextId, elements);
070: }
071:
072: private static class JavaUIHelpListener implements HelpListener {
073:
074: private StructuredViewer fViewer;
075: private String fContextId;
076: private JavaEditor fEditor;
077:
078: public JavaUIHelpListener(StructuredViewer viewer,
079: String contextId) {
080: fViewer = viewer;
081: fContextId = contextId;
082: }
083:
084: public JavaUIHelpListener(JavaEditor editor, String contextId) {
085: fContextId = contextId;
086: fEditor = editor;
087: }
088:
089: /*
090: * @see HelpListener#helpRequested(HelpEvent)
091: *
092: */
093: public void helpRequested(HelpEvent e) {
094: try {
095: Object[] selected = null;
096: if (fViewer != null) {
097: ISelection selection = fViewer.getSelection();
098: if (selection instanceof IStructuredSelection) {
099: selected = ((IStructuredSelection) selection)
100: .toArray();
101: }
102: } else if (fEditor != null) {
103: IJavaElement input = SelectionConverter
104: .getInput(fEditor);
105: if (ActionUtil.isOnBuildPath(input)) {
106: selected = SelectionConverter
107: .codeResolve(fEditor);
108: }
109: }
110: JavadocHelpContext.displayHelp(fContextId, selected);
111: } catch (CoreException x) {
112: JavaPlugin.log(x);
113: }
114: }
115: }
116:
117: private static class JavaUIHelpContextProvider implements
118: IContextProvider {
119: private String fId;
120: private Object[] fSelected;
121:
122: public JavaUIHelpContextProvider(String id, Object[] selected) {
123: fId = id;
124: fSelected = selected;
125: }
126:
127: public int getContextChangeMask() {
128: return SELECTION;
129: }
130:
131: public IContext getContext(Object target) {
132: IContext context = HelpSystem.getContext(fId);
133: if (fSelected != null && fSelected.length > 0) {
134: try {
135: context = new JavadocHelpContext(context, fSelected);
136: } catch (JavaModelException e) {
137: // since we are updating the UI with async exec it
138: // can happen that the element doesn't exist anymore
139: // but we are still showing it in the user interface
140: if (!e.isDoesNotExist())
141: JavaPlugin.log(e);
142: }
143: }
144: return context;
145: }
146:
147: public String getSearchExpression(Object target) {
148: return null;
149: }
150: }
151: }
|