Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show recently used "File > New" wizards #1922

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
package org.eclipse.ui.actions;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IAction;
Expand All @@ -28,6 +32,7 @@
import org.eclipse.ui.activities.WorkbenchActivityHelper;
import org.eclipse.ui.internal.WorkbenchPlugin;
import org.eclipse.ui.internal.actions.NewWizardShortcutAction;
import org.eclipse.ui.internal.dialogs.RecentNewWizardSelection;
import org.eclipse.ui.internal.dialogs.WorkbenchWizardElement;
import org.eclipse.ui.internal.registry.WizardsRegistryReader;
import org.eclipse.ui.wizards.IWizardCategory;
Expand Down Expand Up @@ -186,9 +191,28 @@ protected void addItems(List<IContributionItem> list) {
list.add(new ActionContributionItem(newExampleAction));
list.add(new Separator());
}
// To add shortcuts from OTHER... wizard regardless of perspective
Collection<IContributionItem> actions = new LinkedList<>();
if (!RecentNewWizardSelection.getInstance().getMenuIds().isEmpty()) {
for (String selectedItem : RecentNewWizardSelection.getInstance()
.getMenuIds()) {
IAction action = getAction(selectedItem);
actions.add(new ActionContributionItem(action));
}
subMenuShortcutCheck(list, actions);
}
list.add(new ActionContributionItem(getShowDialogAction()));
}

private void subMenuShortcutCheck(List<IContributionItem> list, Collection<IContributionItem> otherItems) {
Set<IContributionItem> existingShortcutsInPerspective = new HashSet<>(list);
for (IContributionItem item : otherItems) {
if (!existingShortcutsInPerspective.contains(item)) {
list.add(item);
existingShortcutsInPerspective.add(item);
}
}
}
private boolean isNewProjectWizardAction(IAction action) {
if (action instanceof NewWizardShortcutAction) {
IWizardDescriptor wizardDescriptor= ((NewWizardShortcutAction) action).getWizardDescriptor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,12 @@ public void dispose() {
}
}

/*
/**
* Returns the action for the given wizard id, or null if not found.
*
* @since 3.134
*/
private IAction getAction(String id) {
public IAction getAction(String id) {
// Keep a cache, rather than creating a new action each time,
// so that image caching in ActionContributionItem works.
IAction action = actions.get(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
import org.eclipse.ui.IWorkingSetManager;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.decorators.DecoratorManager;
import org.eclipse.ui.internal.dialogs.NewWizard;
import org.eclipse.ui.internal.dialogs.NewWizard.RecentNewWizardsPreferenceManager;
import org.eclipse.ui.internal.dialogs.WorkbenchPreferenceManager;
import org.eclipse.ui.internal.help.CommandHelpServiceImpl;
import org.eclipse.ui.internal.help.HelpServiceImpl;
Expand Down Expand Up @@ -202,6 +204,8 @@ public class WorkbenchPlugin extends AbstractUIPlugin {

private ICommandHelpService commandHelpService;

private NewWizard.RecentNewWizardsPreferenceManager recentNewWizardsPreferenceManager;

/**
* Create an instance of the WorkbenchPlugin. The workbench plugin is
* effectively the "application" for the workbench UI. The entire UI operates as
Expand Down Expand Up @@ -766,6 +770,10 @@ public void start(BundleContext context) throws Exception {
// to be loaded.s
if (uiBundle != null)
uiBundle.start(Bundle.START_TRANSIENT);

recentNewWizardsPreferenceManager = new RecentNewWizardsPreferenceManager();
recentNewWizardsPreferenceManager.populate();

} catch (BundleException e) {
WorkbenchPlugin.log("Unable to load UI activator", e); //$NON-NLS-1$
}
Expand Down Expand Up @@ -1048,6 +1056,8 @@ public void stop(BundleContext context) throws Exception {
testableTracker.close();
testableTracker = null;
}
// Store recently used new page shortcuts to preferences
recentNewWizardsPreferenceManager.shutdown();
super.stop(context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
*******************************************************************************/
package org.eclipse.ui.internal.dialogs;

import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.IWizard;
import org.eclipse.jface.wizard.Wizard;
Expand Down Expand Up @@ -169,4 +173,52 @@ public boolean canFinish() {
return super.canFinish();
}

/**
* A <code>RecentNewWizardsPreferenceManager</code> is used to store and fetch
* the most recent new wizards used or created from the "Other" shortcut, which
* is part of the menu manager with New Wizard actions, from preferences.
*/
public static class RecentNewWizardsPreferenceManager {

private static final String PLUGIN_ID = "org.eclipse.ui.workbench"; //$NON-NLS-1$
private static final String RECENT_NEW_MENU_ITEMS = "recentNewMenuItems"; //$NON-NLS-1$
private static final String SEPARATOR = ","; //$NON-NLS-1$

public List<String> getMenuShortcutsFromPreferences() {
IEclipsePreferences pref = InstanceScope.INSTANCE.getNode(PLUGIN_ID);
String preferences = pref.get(RECENT_NEW_MENU_ITEMS, null);
if (preferences != null && !preferences.trim().isEmpty()) {
return List.of(preferences.split(SEPARATOR));
}
return List.of();
}

public void setMenuShortcutsToPreferences(Set<String> items) {
IEclipsePreferences pref = InstanceScope.INSTANCE.getNode(PLUGIN_ID);
pref.put(RECENT_NEW_MENU_ITEMS, String.join(SEPARATOR, items));
try {
pref.flush();
} catch (org.osgi.service.prefs.BackingStoreException e) {
WorkbenchPlugin.log(e);
}
}

/**
* populates recently used new wizards into
* <code>RecentNewWizardSelection</code> from preferences.
*/
public void populate() {
getMenuShortcutsFromPreferences().stream()
.forEach(newPage -> RecentNewWizardSelection.getInstance().addItem(newPage));

}

/**
* stores recently used new wizards to preferences.
*/
public void shutdown() {
Set<String> menuIds = RecentNewWizardSelection.getInstance().getMenuIds();
setMenuShortcutsToPreferences(menuIds);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -689,4 +689,11 @@ public IWorkbenchWizard createWizard() throws CoreException {

updateDescription(selectedObject);
}

/**
* @return selected element or {@code null}
*/
public IWizardDescriptor getSelectedElement() {
return selectedElement;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ public void createControl(Composite parent) {
* they will persist into the next invocation of this wizard page
*/
protected void saveWidgetValues() {
if (newResourcePage.getSelectedElement() != null) {
RecentNewWizardSelection.getInstance().addItem(newResourcePage.getSelectedElement().getId());
}
newResourcePage.saveWidgetValues();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*******************************************************************************
* Copyright (c) 2023 ETAS GmbH and others, all rights reserved.
*
* 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:
* ETAS GmbH - initial API and implementation
*******************************************************************************/

package org.eclipse.ui.internal.dialogs;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

/**
* A <code>RecentNewWizardSelection</code> is used to manage the five most
* recent new wizards used or created from the "Other" shortcut, which is part
* of the menu manager with New Wizard actions. The recently used new wizards
* will appear before the "Other" shortcut.
*/
public class RecentNewWizardSelection {
private Set<String> menuIds = new LinkedHashSet<>();
private static RecentNewWizardSelection instance;
private static final int MAX_MENU_SIZE = 5;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you intended to have 5 recent items: currently only four are kept.

Copy link
Contributor Author

@lathapatil lathapatil Oct 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you intended to have 5 recent items: currently only four are kept.

Could you please share the scenario you tested .
It do not add the shortcuts if it is already present as part of fixed shortcuts (highlighted in the plugin perspective image) of current perspective . The last used shortcuts change if you switch the perspective .

Java perspective

image

When I switch to plugin perspective ..

image


public static RecentNewWizardSelection getInstance() {
synchronized (RecentNewWizardSelection.class) {
if (instance == null) {
instance = new RecentNewWizardSelection();
}
return instance;
}
}

/**
* Adds the new wizard menu shortcut ID to the set and removes the oldest one if
* the number of recently used new wizard menu shortcuts exceeds MAX_MENU_SIZE.
*
* @param shortcut the new wizard menu shortcut ID
*/
public void addItem(String shortcut) {
menuIds.add(shortcut);
if (menuIds.size() > MAX_MENU_SIZE) {
Iterator<String> iterator = menuIds.iterator();
iterator.next();
iterator.remove();
}
}

/**
* Returns the set of recently used new wizard menu shortcut IDs.
*
* @return the set of recently used new wizard menu shortcut IDs
*/

public Set<String> getMenuIds() {
return Collections.unmodifiableSet(menuIds);
}

}
2 changes: 1 addition & 1 deletion bundles/org.eclipse.ui.workbench/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.ui.workbench; singleton:=true
Bundle-Version: 3.133.100.qualifier
Bundle-Version: 3.134.0.qualifier
Bundle-Activator: org.eclipse.ui.internal.WorkbenchPlugin
Bundle-ActivationPolicy: lazy
Bundle-Vendor: %providerName
Expand Down
Loading