Skip to content

Commit 3683ab5

Browse files
committed
Show recently used File > New wizards
Changes requested in the PR #1837 are addressed here Review points addressed
1 parent 3b49502 commit 3683ab5

File tree

8 files changed

+168
-3
lines changed

8 files changed

+168
-3
lines changed

bundles/org.eclipse.ui.ide/extensions/org/eclipse/ui/actions/NewWizardMenu.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515
package org.eclipse.ui.actions;
1616

1717
import java.util.ArrayList;
18+
import java.util.Collection;
19+
import java.util.HashSet;
1820
import java.util.Iterator;
21+
import java.util.LinkedList;
1922
import java.util.List;
23+
import java.util.Set;
2024

2125
import org.eclipse.jface.action.ActionContributionItem;
2226
import org.eclipse.jface.action.IAction;
@@ -28,6 +32,7 @@
2832
import org.eclipse.ui.activities.WorkbenchActivityHelper;
2933
import org.eclipse.ui.internal.WorkbenchPlugin;
3034
import org.eclipse.ui.internal.actions.NewWizardShortcutAction;
35+
import org.eclipse.ui.internal.dialogs.RecentNewWizardSelection;
3136
import org.eclipse.ui.internal.dialogs.WorkbenchWizardElement;
3237
import org.eclipse.ui.internal.registry.WizardsRegistryReader;
3338
import org.eclipse.ui.wizards.IWizardCategory;
@@ -186,9 +191,28 @@ protected void addItems(List<IContributionItem> list) {
186191
list.add(new ActionContributionItem(newExampleAction));
187192
list.add(new Separator());
188193
}
194+
// To add shortcuts from OTHER... wizard regardless of perspective
195+
Collection<IContributionItem> actions = new LinkedList<>();
196+
if (!RecentNewWizardSelection.getInstance().getMenuIds().isEmpty()) {
197+
for (String selectedItem : RecentNewWizardSelection.getInstance()
198+
.getMenuIds()) {
199+
IAction action = getAction(selectedItem);
200+
actions.add(new ActionContributionItem(action));
201+
}
202+
subMenuShortcutCheck(list, actions);
203+
}
189204
list.add(new ActionContributionItem(getShowDialogAction()));
190205
}
191206

207+
private void subMenuShortcutCheck(List<IContributionItem> list, Collection<IContributionItem> otherItems) {
208+
Set<IContributionItem> existingShortcutsInPerspective = new HashSet<>(list);
209+
for (IContributionItem item : otherItems) {
210+
if (!existingShortcutsInPerspective.contains(item)) {
211+
list.add(item);
212+
existingShortcutsInPerspective.add(item);
213+
}
214+
}
215+
}
192216
private boolean isNewProjectWizardAction(IAction action) {
193217
if (action instanceof NewWizardShortcutAction) {
194218
IWizardDescriptor wizardDescriptor= ((NewWizardShortcutAction) action).getWizardDescriptor();

bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/actions/BaseNewWizardMenu.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,12 @@ public void dispose() {
163163
}
164164
}
165165

166-
/*
166+
/**
167167
* Returns the action for the given wizard id, or null if not found.
168+
*
169+
* @since 3.134
168170
*/
169-
private IAction getAction(String id) {
171+
public IAction getAction(String id) {
170172
// Keep a cache, rather than creating a new action each time,
171173
// so that image caching in ActionContributionItem works.
172174
IAction action = actions.get(id);

bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/WorkbenchPlugin.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
import org.eclipse.ui.IWorkingSetManager;
5454
import org.eclipse.ui.PlatformUI;
5555
import org.eclipse.ui.internal.decorators.DecoratorManager;
56+
import org.eclipse.ui.internal.dialogs.NewWizard;
57+
import org.eclipse.ui.internal.dialogs.NewWizard.RecentNewWizardsPreferenceManager;
5658
import org.eclipse.ui.internal.dialogs.WorkbenchPreferenceManager;
5759
import org.eclipse.ui.internal.help.CommandHelpServiceImpl;
5860
import org.eclipse.ui.internal.help.HelpServiceImpl;
@@ -202,6 +204,8 @@ public class WorkbenchPlugin extends AbstractUIPlugin {
202204

203205
private ICommandHelpService commandHelpService;
204206

207+
private NewWizard.RecentNewWizardsPreferenceManager recentNewWizardsPreferenceManager;
208+
205209
/**
206210
* Create an instance of the WorkbenchPlugin. The workbench plugin is
207211
* effectively the "application" for the workbench UI. The entire UI operates as
@@ -766,6 +770,10 @@ public void start(BundleContext context) throws Exception {
766770
// to be loaded.s
767771
if (uiBundle != null)
768772
uiBundle.start(Bundle.START_TRANSIENT);
773+
774+
recentNewWizardsPreferenceManager = new RecentNewWizardsPreferenceManager();
775+
recentNewWizardsPreferenceManager.populate();
776+
769777
} catch (BundleException e) {
770778
WorkbenchPlugin.log("Unable to load UI activator", e); //$NON-NLS-1$
771779
}
@@ -1048,6 +1056,8 @@ public void stop(BundleContext context) throws Exception {
10481056
testableTracker.close();
10491057
testableTracker = null;
10501058
}
1059+
// Store recently used new page shortcuts to preferences
1060+
recentNewWizardsPreferenceManager.shutdown();
10511061
super.stop(context);
10521062
}
10531063

bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/dialogs/NewWizard.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
*******************************************************************************/
1414
package org.eclipse.ui.internal.dialogs;
1515

16+
import java.util.List;
17+
import java.util.Set;
1618
import java.util.StringTokenizer;
19+
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
20+
import org.eclipse.core.runtime.preferences.InstanceScope;
1721
import org.eclipse.jface.viewers.IStructuredSelection;
1822
import org.eclipse.jface.wizard.IWizard;
1923
import org.eclipse.jface.wizard.Wizard;
@@ -169,4 +173,52 @@ public boolean canFinish() {
169173
return super.canFinish();
170174
}
171175

176+
/**
177+
* A <code>RecentNewWizardsPreferenceManager</code> is used to store and fetch
178+
* the most recent new wizards used or created from the "Other" shortcut, which
179+
* is part of the menu manager with New Wizard actions, from preferences.
180+
*/
181+
public static class RecentNewWizardsPreferenceManager {
182+
183+
private static final String PLUGIN_ID = "org.eclipse.ui.workbench"; //$NON-NLS-1$
184+
private static final String RECENT_NEW_MENU_ITEMS = "recentNewMenuItems"; //$NON-NLS-1$
185+
private static final String SEPARATOR = ","; //$NON-NLS-1$
186+
187+
public List<String> getMenuShortcutsFromPreferences() {
188+
IEclipsePreferences pref = InstanceScope.INSTANCE.getNode(PLUGIN_ID);
189+
String preferences = pref.get(RECENT_NEW_MENU_ITEMS, null);
190+
if (preferences != null && !preferences.trim().isEmpty()) {
191+
return List.of(preferences.split(SEPARATOR));
192+
}
193+
return List.of();
194+
}
195+
196+
public void setMenuShortcutsToPreferences(Set<String> items) {
197+
IEclipsePreferences pref = InstanceScope.INSTANCE.getNode(PLUGIN_ID);
198+
pref.put(RECENT_NEW_MENU_ITEMS, String.join(SEPARATOR, items));
199+
try {
200+
pref.flush();
201+
} catch (org.osgi.service.prefs.BackingStoreException e) {
202+
WorkbenchPlugin.log(e);
203+
}
204+
}
205+
206+
/**
207+
* populates recently used new wizards into
208+
* <code>RecentNewWizardSelection</code> from preferences.
209+
*/
210+
public void populate() {
211+
getMenuShortcutsFromPreferences().stream()
212+
.forEach(newPage -> RecentNewWizardSelection.getInstance().addItem(newPage));
213+
214+
}
215+
216+
/**
217+
* stores recently used new wizards to preferences.
218+
*/
219+
public void shutdown() {
220+
Set<String> menuIds = RecentNewWizardSelection.getInstance().getMenuIds();
221+
setMenuShortcutsToPreferences(menuIds);
222+
}
223+
}
172224
}

bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/dialogs/NewWizardNewPage.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,4 +689,11 @@ public IWorkbenchWizard createWizard() throws CoreException {
689689

690690
updateDescription(selectedObject);
691691
}
692+
693+
/**
694+
* @return selected element or {@code null}
695+
*/
696+
public IWizardDescriptor getSelectedElement() {
697+
return selectedElement;
698+
}
692699
}

bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/dialogs/NewWizardSelectionPage.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ public void createControl(Composite parent) {
9494
* they will persist into the next invocation of this wizard page
9595
*/
9696
protected void saveWidgetValues() {
97+
if (newResourcePage.getSelectedElement() != null) {
98+
RecentNewWizardSelection.getInstance().addItem(newResourcePage.getSelectedElement().getId());
99+
}
97100
newResourcePage.saveWidgetValues();
98101
}
99102

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2023 ETAS GmbH and others, all rights reserved.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* ETAS GmbH - initial API and implementation
13+
*******************************************************************************/
14+
15+
package org.eclipse.ui.internal.dialogs;
16+
17+
import java.util.Collections;
18+
import java.util.Iterator;
19+
import java.util.LinkedHashSet;
20+
import java.util.Set;
21+
22+
/**
23+
* A <code>RecentNewWizardSelection</code> is used to manage the five most
24+
* recent new wizards used or created from the "Other" shortcut, which is part
25+
* of the menu manager with New Wizard actions. The recently used new wizards
26+
* will appear before the "Other" shortcut.
27+
*/
28+
public class RecentNewWizardSelection {
29+
private Set<String> menuIds = new LinkedHashSet<>();
30+
private static RecentNewWizardSelection instance;
31+
private static final int MAX_MENU_SIZE = 5;
32+
33+
public static RecentNewWizardSelection getInstance() {
34+
synchronized (RecentNewWizardSelection.class) {
35+
if (instance == null) {
36+
instance = new RecentNewWizardSelection();
37+
}
38+
return instance;
39+
}
40+
}
41+
42+
/**
43+
* Adds the new wizard menu shortcut ID to the set and removes the oldest one if
44+
* the number of recently used new wizard menu shortcuts exceeds MAX_MENU_SIZE.
45+
*
46+
* @param shortcut the new wizard menu shortcut ID
47+
*/
48+
public void addItem(String shortcut) {
49+
menuIds.add(shortcut);
50+
if (menuIds.size() > MAX_MENU_SIZE) {
51+
Iterator<String> iterator = menuIds.iterator();
52+
iterator.next();
53+
iterator.remove();
54+
}
55+
}
56+
57+
/**
58+
* Returns the set of recently used new wizard menu shortcut IDs.
59+
*
60+
* @return the set of recently used new wizard menu shortcut IDs
61+
*/
62+
63+
public Set<String> getMenuIds() {
64+
return Collections.unmodifiableSet(menuIds);
65+
}
66+
67+
}

bundles/org.eclipse.ui.workbench/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %pluginName
44
Bundle-SymbolicName: org.eclipse.ui.workbench; singleton:=true
5-
Bundle-Version: 3.133.100.qualifier
5+
Bundle-Version: 3.134.0.qualifier
66
Bundle-Activator: org.eclipse.ui.internal.WorkbenchPlugin
77
Bundle-ActivationPolicy: lazy
88
Bundle-Vendor: %providerName

0 commit comments

Comments
 (0)