diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/callhierarchy/CHMessages.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/callhierarchy/CHMessages.java index afa51fa752d..c141cd06e57 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/callhierarchy/CHMessages.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/callhierarchy/CHMessages.java @@ -26,6 +26,8 @@ public class CHMessages extends NLS { public static String CHViewPart_HideMacros_tooltip; public static String CHViewPart_NextReference_label; public static String CHViewPart_NextReference_tooltip; + public static String CHViewPart_Open_label; + public static String CHViewPart_Open_tooltip; public static String CHViewPart_OpenReference_label; public static String CHViewPart_PreviousReference_label; public static String CHViewPart_PreviousReference_tooltip; @@ -37,6 +39,8 @@ public class CHMessages extends NLS { public static String CHViewPart_ShowCallers_tooltip; public static String CHViewPart_ShowFiles_label; public static String CHViewPart_ShowFiles_tooltip; + public static String CHViewPart_ShowReference_label; + public static String CHViewPart_ShowReference_tooltip; public static String CHViewPart_Title_callees; public static String CHViewPart_Title_callers; public static String CHViewPart_WorkspaceScope; diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/callhierarchy/CHMessages.properties b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/callhierarchy/CHMessages.properties index 71131086a4d..1a48a2dbbb8 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/callhierarchy/CHMessages.properties +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/callhierarchy/CHMessages.properties @@ -3,6 +3,8 @@ CHViewPart_ShowCallers_label=Show Callers CHViewPart_ShowCallers_tooltip=Show Callers CHViewPart_ShowCallees_label=Show Callees CHViewPart_ShowCallees_tooltip=Show Callees +CHViewPart_ShowReference_label=Show Reference +CHViewPart_ShowReference_tooltip=Show Reference CHViewPart_FilterVariables_label=Filter Variables CHViewPart_FilterVariables_tooltip=Hide Variables, Constents and Enumerators CHViewPart_HideMacros_label=Hide Macros @@ -19,6 +21,8 @@ CHViewPart_Refresh_tooltip=Refresh View Content CHViewPart_WorkspaceScope=workspace CHViewPart_Title_callers=Callers of {0} - in {1} CHViewPart_Title_callees=Calls made by {0} - in {1} +CHViewPart_Open_label=Open +CHViewPart_Open_tooltip=Open CHViewPart_OpenReference_label=Open Reference CHHistoryDropDownAction_ClearHistory_label=Clear History CHHistoryListAction_HistoryDialog_title=Call Hierarchy History diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/callhierarchy/CHNode.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/callhierarchy/CHNode.java index 5eba83b0408..b03a939db00 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/callhierarchy/CHNode.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/callhierarchy/CHNode.java @@ -15,6 +15,8 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import org.eclipse.core.runtime.IAdaptable; + import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.IMacro; import org.eclipse.cdt.core.model.ITranslationUnit; @@ -23,7 +25,7 @@ import org.eclipse.cdt.core.model.IVariableDeclaration; /** * Represents a node in the include browser */ -public class CHNode { +public class CHNode implements IAdaptable { private CHNode fParent; private ICElement fRepresentedDecl; private ITranslationUnit fFileOfReferences; @@ -138,4 +140,11 @@ public class CHNode { public ITranslationUnit getFileOfReferences() { return fFileOfReferences; } + + public Object getAdapter(Class adapter) { + if (adapter.isAssignableFrom(ICElement.class)) { + return getRepresentedDeclaration(); + } + return null; + } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/callhierarchy/CHViewPart.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/callhierarchy/CHViewPart.java index 31ed0e97d70..d0d9ab1c1a8 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/callhierarchy/CHViewPart.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/callhierarchy/CHViewPart.java @@ -33,7 +33,6 @@ import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.jface.viewers.Viewer; import org.eclipse.jface.viewers.ViewerComparator; import org.eclipse.jface.viewers.ViewerFilter; -import org.eclipse.search.ui.IContextMenuConstants; import org.eclipse.swt.SWT; import org.eclipse.swt.dnd.DND; import org.eclipse.swt.dnd.DropTarget; @@ -59,11 +58,16 @@ import org.eclipse.ui.part.ViewPart; import org.eclipse.cdt.core.model.CModelException; import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ITranslationUnit; +import org.eclipse.cdt.refactoring.actions.CRefactoringActionGroup; import org.eclipse.cdt.ui.CUIPlugin; +import org.eclipse.cdt.ui.actions.OpenViewActionGroup; import org.eclipse.cdt.internal.ui.CPluginImages; +import org.eclipse.cdt.internal.ui.IContextMenuConstants; +import org.eclipse.cdt.internal.ui.search.actions.SelectionSearchGroup; import org.eclipse.cdt.internal.ui.util.CoreUtility; import org.eclipse.cdt.internal.ui.util.Messages; +import org.eclipse.cdt.internal.ui.viewsupport.AdaptingSelectionProvider; import org.eclipse.cdt.internal.ui.viewsupport.EditorOpener; import org.eclipse.cdt.internal.ui.viewsupport.ExtendedTreeViewer; import org.eclipse.cdt.internal.ui.viewsupport.TreeNavigator; @@ -114,6 +118,13 @@ public class CHViewPart extends ViewPart { private Action fPreviousAction; private Action fRefreshAction; private Action fHistoryAction; + private Action fShowReference; + + // action groups + private OpenViewActionGroup fOpenViewActionGroup; + private SelectionSearchGroup fSelectionSearchGroup; + private CRefactoringActionGroup fRefactoringActionGroup; + private Action fOpenElement; public void setFocus() { @@ -161,16 +172,33 @@ public class CHViewPart extends ViewPart { createInfoPage(); createViewerPage(); + getSite().setSelectionProvider(new AdaptingSelectionProvider(ICElement.class, fTreeViewer)); + initDragAndDrop(); createActions(); createContextMenu(); - getSite().setSelectionProvider(fTreeViewer); setMessage(CHMessages.CHViewPart_emptyPageMessage); initializeActionStates(); } + public void dispose() { + if (fOpenViewActionGroup != null) { + fOpenViewActionGroup.dispose(); + fOpenViewActionGroup= null; + } + if (fSelectionSearchGroup != null) { + fSelectionSearchGroup.dispose(); + fSelectionSearchGroup= null; + } + if (fRefactoringActionGroup != null) { + fRefactoringActionGroup.dispose(); + fRefactoringActionGroup= null; + } + super.dispose(); + } + private void initializeActionStates() { boolean referencedBy= true; boolean filterVariables= false; @@ -218,7 +246,7 @@ public class CHViewPart extends ViewPart { Menu menu = manager.createContextMenu(fTreeViewer.getControl()); fTreeViewer.getControl().setMenu(menu); IWorkbenchPartSite site = getSite(); - site.registerContextMenu(CUIPlugin.ID_CALL_HIERARCHY, manager, fTreeViewer); + site.registerContextMenu(CUIPlugin.ID_CALL_HIERARCHY, manager, fTreeViewer); } private void createViewerPage() { @@ -236,9 +264,7 @@ public class CHViewPart extends ViewPart { fTreeViewer.setAutoExpandLevel(2); fTreeViewer.addOpenListener(new IOpenListener() { public void open(OpenEvent event) { - fNavigationDetail= 0; - fNavigationNode= selectionToNode(event.getSelection()); - onShowReference(); + onShowSelectedReference(event.getSelection()); } }); } @@ -263,6 +289,11 @@ public class CHViewPart extends ViewPart { } private void createActions() { + // action gruops + fOpenViewActionGroup= new OpenViewActionGroup(this); + fSelectionSearchGroup= new SelectionSearchGroup(getSite()); + fRefactoringActionGroup= new CRefactoringActionGroup(this); + WorkingSetFilterUI wsFilterUI= new WorkingSetFilterUI(this, fMemento, KEY_WORKING_SET_FILTER) { protected void onWorkingSetChange() { updateWorkingSetFilter(this); @@ -364,6 +395,19 @@ public class CHViewPart extends ViewPart { } }; + fShowReference= new Action(CHMessages.CHViewPart_ShowReference_label) { + public void run() { + onShowSelectedReference(fTreeViewer.getSelection()); + } + }; + fShowReference.setToolTipText(CHMessages.CHViewPart_ShowReference_tooltip); + fOpenElement= new Action(CHMessages.CHViewPart_Open_label) { + public void run() { + onOpenElement(fTreeViewer.getSelection()); + } + }; + fOpenElement.setToolTipText(CHMessages.CHViewPart_Open_tooltip); + fShowFilesInLabelsAction= new Action(CHMessages.CHViewPart_ShowFiles_label, IAction.AS_CHECK_BOX) { public void run() { onShowFilesInLabels(isChecked()); @@ -457,15 +501,44 @@ public class CHViewPart extends ViewPart { } } } - + else { + fNavigationNode= (CHNode) selectedItem.getData(); + if (!forward && fNavigationNode != null) { + fNavigationDetail= Math.max(0, fNavigationNode.getReferenceCount()-1); + } + else { + fNavigationDetail= 0; + } + } } + protected void onShowSelectedReference(ISelection selection) { + fNavigationDetail= 0; + fNavigationNode= selectionToNode(selection); + showReference(); + } + + protected void onOpenElement(ISelection selection) { + CHNode node= selectionToNode(selection); + if (node != null) { + ICElement elem= node.getRepresentedDeclaration(); + if (elem != null) { + IWorkbenchPage page= getSite().getPage(); + try { + EditorOpener.open(page, elem); + } catch (CModelException e) { + CUIPlugin.getDefault().log(e); + } + } + } + } + protected void onNextOrPrevious(boolean forward) { setNextNode(forward); if (fNavigationNode != null) { StructuredSelection sel= new StructuredSelection(fNavigationNode); fTreeViewer.setSelection(sel); - onShowReference(); + showReference(); } } @@ -564,42 +637,30 @@ public class CHViewPart extends ViewPart { } } - protected void onContextMenuAboutToShow(IMenuManager m) { -// final IStructuredSelection selection = (IStructuredSelection) fTreeViewer.getSelection(); -// final CHNode node= IBConversions.selectionToNode(selection); -// -// if (node != null) { -// // open reference -// if (node.getParent() != null && node.getDirectiveFile() != null) { -// m.add(new Action(CHMessages.CHViewPart_OpenReference_label) { -// public void run() { -// onShowReference(selection); -// } -// }); -// } + protected void onContextMenuAboutToShow(IMenuManager menu) { + CUIPlugin.createStandardGroups(menu); + + CHNode node= selectionToNode(fTreeViewer.getSelection()); + if (node != null) { + if (node.getReferenceCount() > 0) { + menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, fShowReference); + } + menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, fOpenElement); + } + + // action groups + ISelection selection = getSite().getSelectionProvider().getSelection(); + if (OpenViewActionGroup.canActionBeAdded(selection)){ + fOpenViewActionGroup.fillContextMenu(menu); + } - // support for opening the function/method -// final ITranslationUnit tu= node.getRepresentedTranslationUnit(); -// if (tu != null) { -// // open -// OpenFileAction ofa= new OpenFileAction(page); -// ofa.selectionChanged(selection); -// m.add(ofa); -// -// // open with -// // keep the menu shorter, no open with support -//// final IResource r= tu.getResource(); -//// if (r != null) { -//// IMenuManager submenu= new MenuManager(IBMessages.IBViewPart_OpenWithMenu_label); -//// submenu.add(new OpenWithMenu(page, r)); -//// m.add(submenu); -//// } -// } -// } - m.add(new Separator(IContextMenuConstants.GROUP_ADDITIONS)); + if (SelectionSearchGroup.canActionBeAdded(selection)){ + fSelectionSearchGroup.fillContextMenu(menu); + } + fRefactoringActionGroup.fillContextMenu(menu); } - - protected void onShowReference() { + + private void showReference() { if (fNavigationNode != null) { ITranslationUnit file= fNavigationNode.getFileOfReferences(); if (file != null) { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CContentOutlinePage.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CContentOutlinePage.java index c83d14bcf59..9a40618bdc2 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CContentOutlinePage.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CContentOutlinePage.java @@ -8,6 +8,7 @@ * Contributors: * IBM Corporation - initial API and implementation * QNX Software System + * Markus Schorn (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.internal.ui.editor; @@ -234,9 +235,7 @@ public class CContentOutlinePage extends Page implements IContentOutlinePage, IS ISelection selection= getSelection(); if (OpenViewActionGroup.canActionBeAdded(selection)){ - fOpenViewActionGroup.setContext(new ActionContext(selection)); fOpenViewActionGroup.fillContextMenu(menu); - fOpenViewActionGroup.setContext(null); } if (OpenIncludeAction.canActionBeAdded(selection)) { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/AdaptingSelectionProvider.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/AdaptingSelectionProvider.java new file mode 100644 index 00000000000..9eb2e6e5e48 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/AdaptingSelectionProvider.java @@ -0,0 +1,100 @@ +/******************************************************************************* + * Copyright (c) 2006 Wind River Systems, Inc. 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Markus Schorn - initial API and implementation + *******************************************************************************/ + +package org.eclipse.cdt.internal.ui.viewsupport; + +import java.util.ArrayList; +import java.util.Iterator; + +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.core.runtime.ListenerList; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.ISelectionChangedListener; +import org.eclipse.jface.viewers.ISelectionProvider; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.SelectionChangedEvent; +import org.eclipse.jface.viewers.StructuredSelection; + +/** + * A selection provider that adapts the elements of structured selections + * to a requested type. + * @author markus.schorn@windriver.com + */ +public class AdaptingSelectionProvider implements ISelectionProvider, ISelectionChangedListener { + + private Class fTargetType; + private ListenerList fListenerList; + private ISelectionProvider fProvider; + + public AdaptingSelectionProvider(Class targetType, ISelectionProvider provider) { + fProvider= provider; + fTargetType= targetType; + fListenerList= new ListenerList(); + } + + private ISelection convertSelection(ISelection selection) { + if (selection != null) { + if (selection instanceof IStructuredSelection) { + IStructuredSelection ss= (IStructuredSelection) selection; + ArrayList adapted= new ArrayList(); + for (Iterator iter = ss.iterator(); iter.hasNext(); ) { + Object elem= adaptElem(iter.next()); + if (elem != null) { + adapted.add(elem); + } + } + return new StructuredSelection(adapted); + } + } + return selection; + } + + private Object adaptElem(Object elem) { + if (fTargetType.isInstance(elem)) { + return elem; + } + if (elem instanceof IAdaptable) { + return ((IAdaptable) elem).getAdapter(fTargetType); + } + return null; + } + + public void addSelectionChangedListener(ISelectionChangedListener listener) { + if (fListenerList.isEmpty()) { + fProvider.addSelectionChangedListener(this); + } + fListenerList.add(listener); + } + + public ISelection getSelection() { + return convertSelection(fProvider.getSelection()); + } + + public void removeSelectionChangedListener(ISelectionChangedListener listener) { + fListenerList.remove(listener); + if (fListenerList.isEmpty()) { + fProvider.removeSelectionChangedListener(this); + } + } + + public void setSelection(ISelection selection) { + throw new UnsupportedOperationException(); + } + + public void selectionChanged(SelectionChangedEvent event) { + SelectionChangedEvent event2= new SelectionChangedEvent(this, convertSelection(event.getSelection())); + Object[] listeners= fListenerList.getListeners(); + for (int i = 0; i < listeners.length; i++) { + ISelectionChangedListener l= (ISelectionChangedListener) listeners[i]; + l.selectionChanged(event2); + } + } +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/OpenViewActionGroup.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/OpenViewActionGroup.java index 84e9bc66cae..f8087aff30e 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/OpenViewActionGroup.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/OpenViewActionGroup.java @@ -21,7 +21,6 @@ import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.ui.IActionBars; import org.eclipse.ui.IViewPart; import org.eclipse.ui.IWorkbenchSite; -import org.eclipse.ui.actions.ActionContext; import org.eclipse.ui.actions.ActionFactory; import org.eclipse.ui.actions.ActionGroup; import org.eclipse.ui.dialogs.PropertyDialogAction; @@ -195,11 +194,11 @@ public class OpenViewActionGroup extends ActionGroup { } private IStructuredSelection getStructuredSelection() { - ActionContext context= getContext(); - if (context != null) { - ISelection selection= getContext().getSelection(); - if (selection instanceof IStructuredSelection) + if (fSite != null) { + ISelection selection= fSite.getSelectionProvider().getSelection(); + if (selection instanceof IStructuredSelection) { return (IStructuredSelection)selection; + } } return null; }