From 6f2a059c1ad3fd7d2e81d482f56e892479cce8f4 Mon Sep 17 00:00:00 2001 From: Anton Leherbauer Date: Wed, 25 Apr 2007 12:01:47 +0000 Subject: [PATCH] Add support for dynamic help context provider --- .../cdt/internal/ui/editor/CEditor.java | 4 + .../internal/ui/text/c/hover/CDocHover.java | 11 ++- .../internal/ui/util/CHelpDisplayContext.java | 12 +-- .../eclipse/cdt/internal/ui/util/CUIHelp.java | 76 ++++++++++++++++++- 4 files changed, 94 insertions(+), 9 deletions(-) diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java index 38c76a65cbd..1e6b2e59adc 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java @@ -28,6 +28,7 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.ListenerList; import org.eclipse.core.runtime.content.IContentType; +import org.eclipse.help.IContextProvider; import org.eclipse.jface.action.GroupMarker; import org.eclipse.jface.action.IAction; import org.eclipse.jface.action.IMenuManager; @@ -1262,6 +1263,9 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IR return adapter; } } + if (IContextProvider.class.equals(required)) { + return new CUIHelp.CUIHelpContextProvider(this); + } return super.getAdapter(required); } /** diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/c/hover/CDocHover.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/c/hover/CDocHover.java index f5d36c93512..325535f58a3 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/c/hover/CDocHover.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/c/hover/CDocHover.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2006 QNX Software Systems and others. + * Copyright (c) 2000, 2007 QNX Software Systems and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -7,6 +7,7 @@ * * Contributors: * QNX Software Systems - Initial API and implementation + * Anton Leherbauer (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.internal.ui.text.c.hover; @@ -18,6 +19,7 @@ import org.eclipse.cdt.internal.ui.text.CWordFinder; import org.eclipse.cdt.internal.ui.text.HTMLPrinter; import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.IFunctionSummary; +import org.eclipse.cdt.ui.IFunctionSummary.IFunctionPrototypeSummary; import org.eclipse.cdt.ui.text.ICHelpInvocationContext; import org.eclipse.core.resources.IProject; import org.eclipse.jface.text.IRegion; @@ -72,8 +74,11 @@ public class CDocHover extends AbstractCEditorTextHover { if (fs != null) { buffer.append(CEditorMessages.getString("DefaultCEditorTextHover.html.name")); //$NON-NLS-1$ buffer.append(HTMLPrinter.convertToHTMLContent(fs.getName())); - buffer.append(CEditorMessages.getString("DefaultCEditorTextHover.html.prototype")); //$NON-NLS-1$ - buffer.append(HTMLPrinter.convertToHTMLContent(fs.getPrototype().getPrototypeString(false))); + final IFunctionPrototypeSummary prototype = fs.getPrototype(); + if (prototype != null) { + buffer.append(CEditorMessages.getString("DefaultCEditorTextHover.html.prototype")); //$NON-NLS-1$ + buffer.append(HTMLPrinter.convertToHTMLContent(prototype.getPrototypeString(false))); + } if(fs.getDescription() != null) { buffer.append(CEditorMessages.getString("DefaultCEditorTextHover.html.description")); //$NON-NLS-1$ //Don't convert this description since it could already be formatted diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/CHelpDisplayContext.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/CHelpDisplayContext.java index b1d0584b477..65a5c3ac99e 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/CHelpDisplayContext.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/CHelpDisplayContext.java @@ -1,5 +1,5 @@ /********************************************************************** - * Copyright (c) 2004, 2005 Intel Corporation and others. + * Copyright (c) 2004, 2007 Intel Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -7,6 +7,7 @@ * * Contributors: * Intel Corporation - Initial API and implementation + * Anton Leherbauer (Wind River Systems) **********************************************************************/ package org.eclipse.cdt.internal.ui.util; @@ -24,6 +25,7 @@ import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.ITextSelection; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.texteditor.ITextEditor; import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.cdt.ui.CUIPlugin; @@ -31,7 +33,6 @@ import org.eclipse.cdt.ui.ICHelpResourceDescriptor; import org.eclipse.cdt.ui.text.ICHelpInvocationContext; import org.eclipse.cdt.internal.ui.CHelpProviderManager; -import org.eclipse.cdt.internal.ui.editor.CEditor; import org.eclipse.cdt.internal.ui.text.CWordFinder; /** @@ -39,10 +40,11 @@ import org.eclipse.cdt.internal.ui.text.CWordFinder; * @since 2.1 */ public class CHelpDisplayContext implements IContext { + private IHelpResource[] fHelpResources; private String fText; - public static void displayHelp(String contextId, CEditor editor) throws CoreException { + public static void displayHelp(String contextId, ITextEditor editor) throws CoreException { String selected = getSelectedString(editor); IContext context= HelpSystem.getContext(contextId); if (context != null) { @@ -53,7 +55,7 @@ public class CHelpDisplayContext implements IContext { } } - private static String getSelectedString(CEditor editor){ + private static String getSelectedString(ITextEditor editor){ String expression = null; try{ ITextSelection selection = (ITextSelection)editor.getSite().getSelectionProvider().getSelection(); @@ -66,7 +68,7 @@ public class CHelpDisplayContext implements IContext { return expression; } - public CHelpDisplayContext(IContext context, final CEditor editor , String selected) throws CoreException { + public CHelpDisplayContext(IContext context, final ITextEditor editor , String selected) throws CoreException { List helpResources= new ArrayList(); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/CUIHelp.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/CUIHelp.java index 8742056754b..472cd0550a5 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/CUIHelp.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/CUIHelp.java @@ -1,5 +1,5 @@ /********************************************************************** - * Copyright (c) 2004, 2005 Intel Corporation and others. + * Copyright (c) 2004, 2007 Intel Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -7,16 +7,27 @@ * * Contributors: * Intel Corporation - Initial API and implementation + * Anton Leherbauer (Wind River Systems) **********************************************************************/ package org.eclipse.cdt.internal.ui.util; +import org.eclipse.cdt.internal.ui.ICHelpContextIds; import org.eclipse.cdt.internal.ui.editor.CEditor; +import org.eclipse.cdt.internal.ui.text.CWordFinder; + import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.core.runtime.CoreException; +import org.eclipse.help.HelpSystem; +import org.eclipse.help.IContext; +import org.eclipse.help.IContextProvider; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IRegion; +import org.eclipse.jface.text.ITextSelection; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.events.HelpEvent; import org.eclipse.swt.events.HelpListener; +import org.eclipse.ui.texteditor.ITextEditor; /** * @@ -52,4 +63,67 @@ public class CUIHelp { } } + /** + * A dynamic help context provider. + * + * @since 4.0 + */ + public static final class CUIHelpContextProvider implements IContextProvider { + + private final ITextEditor fEditor; + + /** + * Creates a context provider for the given text editor. + * @param editor + */ + public CUIHelpContextProvider(ITextEditor editor) { + fEditor= editor; + } + + /* + * @see org.eclipse.help.IContextProvider#getContext(java.lang.Object) + */ + public IContext getContext(Object target) { + String selected = getSelectedString(fEditor); + IContext context= HelpSystem.getContext(ICHelpContextIds.CEDITOR_VIEW); + if (context != null) { + if (selected != null && selected.length() > 0) { + try { + context= new CHelpDisplayContext(context, fEditor, selected); + } catch (CoreException exc) { + } + } + } + return context; + } + + /* + * @see org.eclipse.help.IContextProvider#getContextChangeMask() + */ + public int getContextChangeMask() { + return SELECTION; + } + + /* + * @see org.eclipse.help.IContextProvider#getSearchExpression(java.lang.Object) + */ + public String getSearchExpression(Object target) { + return getSelectedString(fEditor); + } + + private static String getSelectedString(ITextEditor editor){ + String expression = null; + try{ + ITextSelection selection = (ITextSelection)editor.getSite().getSelectionProvider().getSelection(); + IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput()); + IRegion region = CWordFinder.findWord(document, selection.getOffset()); + expression = document.get(region.getOffset(), region.getLength()); + } + catch(Exception e){ + } + return expression; + } + + } + }