From daa17424786eecf3328b351a354f5c60e899cd28 Mon Sep 17 00:00:00 2001 From: jannisCode Date: Fri, 18 Oct 2024 13:09:22 +0200 Subject: [PATCH] Filter menu in callHierarchy #1731 The filter button in the Call Hierarchy now has the option to expand a menu where one can switch between the filters https://github.com/eclipse-jdt/eclipse.jdt.ui/pull/1731 --- .../callhierarchy/CallHierarchyCore.java | 19 +++ .../callhierarchy/CallHierarchyViewPart.java | 44 +++++- .../callhierarchy/FilterDropDownAction.java | 145 ++++++++++++++++++ .../ui/callhierarchy/FiltersAction.java | 59 +++++++ .../callhierarchy/HistoryDropDownAction.java | 1 + 5 files changed, 267 insertions(+), 1 deletion(-) create mode 100644 org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/callhierarchy/FilterDropDownAction.java create mode 100644 org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/callhierarchy/FiltersAction.java diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/callhierarchy/CallHierarchyCore.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/callhierarchy/CallHierarchyCore.java index c18151cc809..4f2e220c943 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/callhierarchy/CallHierarchyCore.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/callhierarchy/CallHierarchyCore.java @@ -82,6 +82,15 @@ public boolean isShowAll() { public boolean isHideTestCode() { return Boolean.parseBoolean(JavaManipulation.getPreference(PREF_HIDE_TEST_CODE, null)); } + public String getActiveFilter() { + if(isShowAll()) { + return PREF_SHOW_ALL_CODE; + } else if(isHideTestCode()) { + return PREF_HIDE_TEST_CODE; + } else { + return PREF_SHOW_TEST_CODE_ONLY; + } + } public Collection getImplementingMethods(IMethod method) { if (isSearchUsingImplementorsEnabled()) { @@ -328,4 +337,14 @@ public static boolean isPossibleInputElement(Object element){ return true; } + + public String getCurrentSelection() { + if(isShowAll()) { + return PREF_SHOW_ALL_CODE; + } else if(isHideTestCode()) { + return PREF_HIDE_TEST_CODE; + } else { + return PREF_SHOW_TEST_CODE_ONLY; + } + } } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/callhierarchy/CallHierarchyViewPart.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/callhierarchy/CallHierarchyViewPart.java index 6847964887b..1023f450c4d 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/callhierarchy/CallHierarchyViewPart.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/callhierarchy/CallHierarchyViewPart.java @@ -98,6 +98,7 @@ import org.eclipse.jdt.core.search.IJavaSearchScope; import org.eclipse.jdt.internal.corext.callhierarchy.CallHierarchy; +import org.eclipse.jdt.internal.corext.callhierarchy.CallHierarchyCore; import org.eclipse.jdt.internal.corext.callhierarchy.CallLocation; import org.eclipse.jdt.internal.corext.callhierarchy.MethodWrapper; import org.eclipse.jdt.internal.corext.callhierarchy.RealCallers; @@ -227,6 +228,7 @@ protected void open(ISelection selection, boolean activate) { private SelectFieldModeAction[] fToggleFieldModeActions; private CallHierarchyFiltersActionGroup fFiltersActionGroup; private HistoryDropDownAction fHistoryDropDownAction; + private FilterDropDownAction fFilterDropDownAction; private RefreshElementAction fRefreshSingleElementAction; private RefreshViewAction fRefreshViewAction; private OpenLocationAction fOpenLocationAction; @@ -395,6 +397,40 @@ void setFieldMode(int mode) { } } + void setFilterMode(String mode) { + + if(mode == CallHierarchyCore.getDefault().getCurrentSelection()) { + return; + } + + + switch(mode) { + case CallHierarchyCore.PREF_SHOW_ALL_CODE: + CallHierarchy.getDefault().setShowAll(true); + CallHierarchy.getDefault().setHideTestCode(false); + CallHierarchy.getDefault().setShowTestCode(false); + + break; + + case CallHierarchyCore.PREF_HIDE_TEST_CODE: + CallHierarchy.getDefault().setShowAll(false); + CallHierarchy.getDefault().setHideTestCode(true); + CallHierarchy.getDefault().setShowTestCode(false); + break; + + case CallHierarchyCore.PREF_SHOW_TEST_CODE_ONLY: + CallHierarchy.getDefault().setShowAll(false); + CallHierarchy.getDefault().setHideTestCode(false); + CallHierarchy.getDefault().setShowTestCode(true); + break; + } + fFilterDropDownAction.setActiveFilterString(); + +// CallHierarchy.getDefault(). + updateView(); + refresh(); + } + /** * Fetches the search scope with the appropriate include mask. * @@ -926,6 +962,10 @@ private MethodWrapper[] getCallerRoots() { return fCallerRoots; } + void updateFilters() { + + } + /** * Adds the entry if new. Inserted at the beginning of the history entries list. * @param entry the entry to add @@ -1025,7 +1065,8 @@ private void fillActionBars() { } toolBar.add(fHistoryDropDownAction); toolBar.add(fPinViewAction); - toolBar.add(fFiltersAction); +// toolBar.add(fFiltersAction); + toolBar.add(fFilterDropDownAction); } private void makeActions() { @@ -1044,6 +1085,7 @@ private void makeActions() { fFiltersActionGroup = new CallHierarchyFiltersActionGroup(this, fCallHierarchyViewer); fHistoryDropDownAction = new HistoryDropDownAction(this); + fFilterDropDownAction = new FilterDropDownAction(this); fHistoryDropDownAction.setEnabled(false); fCancelSearchAction = new CancelSearchAction(this); setCancelEnabled(false); diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/callhierarchy/FilterDropDownAction.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/callhierarchy/FilterDropDownAction.java new file mode 100644 index 00000000000..b25f00bce44 --- /dev/null +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/callhierarchy/FilterDropDownAction.java @@ -0,0 +1,145 @@ +/******************************************************************************* + * Copyright (c) 2000, 2011 IBM Corporation and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Jesper Kamstrup Linnet (eclipse@kamstrup-linnet.dk) - initial API and implementation + * (report 36180: Callers/Callees view) + * Stephan Herrmann (stephan@cs.tu-berlin.de): + * - bug 75800: [call hierarchy] should allow searches for fields + *******************************************************************************/ +package org.eclipse.jdt.internal.ui.callhierarchy; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Menu; +import org.eclipse.swt.widgets.MenuItem; + +import org.eclipse.jface.action.Action; +import org.eclipse.jface.action.ActionContributionItem; +import org.eclipse.jface.action.IMenuCreator; +import org.eclipse.jface.window.Window; + +import org.eclipse.jdt.core.IMember; + +import org.eclipse.jdt.internal.corext.callhierarchy.CallHierarchy; +import org.eclipse.jdt.internal.corext.callhierarchy.CallHierarchyCore; + +import org.eclipse.jdt.internal.ui.JavaPluginImages; + + +class FilterDropDownAction extends Action implements IMenuCreator { + + private CallHierarchyViewPart fView; + private Menu fMenu; + private String activeFilterString; + + public FilterDropDownAction(CallHierarchyViewPart view) { + fView = view; + fMenu = null; + + + updateFilterString(); + setToolTipText(activeFilterString); + setImageDescriptor(JavaPluginImages.DESC_ELCL_FILTER); + + setText(CallHierarchyMessages.ShowFilterDialogAction_text); + + setMenuCreator(this); + } + + @Override + public Menu getMenu(Menu parent) { + return null; + } + + public void setActiveFilterString() { + updateFilterString(); + } + + private void updateFilterString() { + activeFilterString = getString(CallHierarchyCore.getDefault().getCurrentSelection()); + setToolTipText(activeFilterString); + } + + private String getString(String s) { + if(s == CallHierarchyCore.PREF_SHOW_ALL_CODE) { + return "Show All Code"; //$NON-NLS-1$ + } else if(s == CallHierarchyCore.PREF_HIDE_TEST_CODE) { + return "Hide Test Code"; //$NON-NLS-1$ + } else { + return "Test Code Only"; //$NON-NLS-1$ + } + } + @Override + public Menu getMenu(Control parent) { + if (fMenu != null) { + fMenu.dispose(); + } + fMenu= new Menu(parent); + IMember[][] elements= fView.getHistoryEntries(); + addEntries(fMenu); + return fMenu; + } + + @Override + public void dispose() { + fView = null; + + if (fMenu != null) { + fMenu.dispose(); + fMenu = null; + } + } + + protected void addActionsToMenu(Menu parent, Action action) { + ActionContributionItem item = new ActionContributionItem(action); + item.fill(parent, -1); + } + + private boolean addEntries(Menu menu) { + boolean checked = false; + + FiltersAction action = new FiltersAction(fView, CallHierarchyCore.PREF_SHOW_ALL_CODE); + addActionsToMenu(menu, action); + action.setChecked(CallHierarchy.getDefault().isShowAll()); + + FiltersAction actionTwo = new FiltersAction(fView, CallHierarchyCore.PREF_HIDE_TEST_CODE); + addActionsToMenu(menu, actionTwo); + actionTwo.setChecked(CallHierarchy.getDefault().isHideTestCode()); + + + FiltersAction actionThree = new FiltersAction(fView, CallHierarchyCore.PREF_SHOW_TEST_CODE_ONLY); + addActionsToMenu(menu, actionThree); + actionThree.setChecked(CallHierarchy.getDefault().isShowTestCode()); + + new MenuItem(menu, SWT.SEPARATOR); + addActionsToMenu(menu, new ShowCallHierarchyFilterDialogAction(fView, "Filters")); //$NON-NLS-1$ + + + + return checked; + + } + + @Override + public void run() { + openFiltersDialog(); + updateFilterString(); +// activeFilterString = CallHierarchyCore.getDefault().getActiveFilter(); +// setToolTipText(activeFilterString); + } + + private void openFiltersDialog() { + FiltersDialog dialog = new FiltersDialog(fView.getViewSite().getShell()); + if (Window.OK == dialog.open()) { + fView.refresh(); + } + } +} diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/callhierarchy/FiltersAction.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/callhierarchy/FiltersAction.java new file mode 100644 index 00000000000..620d42f2618 --- /dev/null +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/callhierarchy/FiltersAction.java @@ -0,0 +1,59 @@ +/******************************************************************************* + * Copyright (c) 2023 Vector Informatik GmbH and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Vector Informatik GmbH - initial API and implementation + *******************************************************************************/ + +package org.eclipse.jdt.internal.ui.callhierarchy; + +import org.eclipse.jface.action.Action; + +import org.eclipse.jdt.core.IMember; + +import org.eclipse.jdt.internal.corext.callhierarchy.CallHierarchyCore; + +/** + * + */ +public class FiltersAction extends Action { + + private CallHierarchyViewPart fView; + private IMember[] fMembers; + String typeString; + + public FiltersAction(CallHierarchyViewPart viewPart, String type) { + super("", AS_RADIO_BUTTON); //$NON-NLS-1$ + fView = viewPart; + typeString = type; + switch(type) { + case CallHierarchyCore.PREF_SHOW_ALL_CODE: + setText(CallHierarchyMessages.FiltersDialog_ShowAllCode); + + break; + case CallHierarchyCore.PREF_HIDE_TEST_CODE: + setText(CallHierarchyMessages.FiltersDialog_HideTestCode); + + break; + + case CallHierarchyCore.PREF_SHOW_TEST_CODE_ONLY: + setText(CallHierarchyMessages.FiltersDialog_TestCodeOnly); + + break; + } + + } + + @Override + public void run() { + fView.setFilterMode(typeString); + } + +} diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/callhierarchy/HistoryDropDownAction.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/callhierarchy/HistoryDropDownAction.java index 053f7fda131..ae9829917e6 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/callhierarchy/HistoryDropDownAction.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/callhierarchy/HistoryDropDownAction.java @@ -81,6 +81,7 @@ public Menu getMenu(Control parent) { } fMenu= new Menu(parent); IMember[][] elements= fView.getHistoryEntries(); + addEntries(fMenu, elements); new MenuItem(fMenu, SWT.SEPARATOR); addActionToMenu(fMenu, new HistoryListAction(fView));