Skip to content

Commit 295f697

Browse files
committed
Show recently used File > New wizards
Changes requested in the PR #1837 are addressed here
1 parent 136058f commit 295f697

File tree

7 files changed

+159
-2
lines changed

7 files changed

+159
-2
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> otherItems = new LinkedList<>();
196+
if (!RecentNewWizardSelection.getInstance().getSelectedFromOther().isEmpty()) {
197+
for (String selectedItemsFormOthers : RecentNewWizardSelection.getInstance()
198+
.getSelectedFromOther()) {
199+
IAction action = getAction(selectedItemsFormOthers);
200+
otherItems.add(new ActionContributionItem(action));
201+
}
202+
subMenuShortcutCheck(list, otherItems);
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.132
168170
*/
169-
private IAction getAction(String id) {
171+
protected 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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import java.util.Collection;
2222
import java.util.Collections;
2323
import java.util.HashSet;
24+
import java.util.List;
2425
import java.util.Locale;
26+
import java.util.Set;
2527
import org.eclipse.core.runtime.CoreException;
2628
import org.eclipse.core.runtime.IConfigurationElement;
2729
import org.eclipse.core.runtime.IExtensionPoint;
@@ -53,6 +55,9 @@
5355
import org.eclipse.ui.IWorkingSetManager;
5456
import org.eclipse.ui.PlatformUI;
5557
import org.eclipse.ui.internal.decorators.DecoratorManager;
58+
import org.eclipse.ui.internal.dialogs.NewWizard;
59+
import org.eclipse.ui.internal.dialogs.NewWizard.RecentNewWizardsPreferenceManager;
60+
import org.eclipse.ui.internal.dialogs.RecentNewWizardSelection;
5661
import org.eclipse.ui.internal.dialogs.WorkbenchPreferenceManager;
5762
import org.eclipse.ui.internal.help.CommandHelpServiceImpl;
5863
import org.eclipse.ui.internal.help.HelpServiceImpl;
@@ -202,6 +207,8 @@ public class WorkbenchPlugin extends AbstractUIPlugin {
202207

203208
private ICommandHelpService commandHelpService;
204209

210+
private NewWizard.RecentNewWizardsPreferenceManager recentNewWizardsPreferenceManager;
211+
205212
/**
206213
* Create an instance of the WorkbenchPlugin. The workbench plugin is
207214
* effectively the "application" for the workbench UI. The entire UI operates as
@@ -210,6 +217,7 @@ public class WorkbenchPlugin extends AbstractUIPlugin {
210217
public WorkbenchPlugin() {
211218
super();
212219
inst = this;
220+
recentNewWizardsPreferenceManager = new RecentNewWizardsPreferenceManager();
213221
}
214222

215223
/**
@@ -766,6 +774,10 @@ public void start(BundleContext context) throws Exception {
766774
// to be loaded.s
767775
if (uiBundle != null)
768776
uiBundle.start(Bundle.START_TRANSIENT);
777+
778+
List<String> recentlyUsedNewWizards = recentNewWizardsPreferenceManager.getMenuShortcutsFromPreferences();
779+
recentlyUsedNewWizards.stream().forEach(newPage -> RecentNewWizardSelection.getInstance().addItem(newPage));
780+
769781
} catch (BundleException e) {
770782
WorkbenchPlugin.log("Unable to load UI activator", e); //$NON-NLS-1$
771783
}
@@ -1048,6 +1060,9 @@ public void stop(BundleContext context) throws Exception {
10481060
testableTracker.close();
10491061
testableTracker = null;
10501062
}
1063+
// Store recently used new page shortcuts to preferences
1064+
Set<String> selectedFromOther = RecentNewWizardSelection.getInstance().getSelectedFromOther();
1065+
recentNewWizardsPreferenceManager.setMenuShortcutsToPreferences(selectedFromOther);
10511066
super.stop(context);
10521067
}
10531068

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

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

16+
import java.util.ArrayList;
17+
import java.util.Arrays;
18+
import java.util.List;
19+
import java.util.Set;
1620
import java.util.StringTokenizer;
21+
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
22+
import org.eclipse.core.runtime.preferences.InstanceScope;
1723
import org.eclipse.jface.viewers.IStructuredSelection;
1824
import org.eclipse.jface.wizard.IWizard;
1925
import org.eclipse.jface.wizard.Wizard;
@@ -169,4 +175,37 @@ public boolean canFinish() {
169175
return super.canFinish();
170176
}
171177

178+
/**
179+
* A <code>RecentNewWizardsPreferenceManager</code> is used to store and fetch
180+
* the most recent new wizards used or created from the "Other" shortcut, which
181+
* is part of the menu manager with New Wizard actions, from preferences.
182+
*
183+
* @since 3.5
184+
*/
185+
186+
public static class RecentNewWizardsPreferenceManager {
187+
188+
private static final String PLUGIN_ID = "org.eclipse.ui.workbench"; //$NON-NLS-1$
189+
private static final String RECENT_NEW_MENU_ITEMS = "recentNewMenuItems"; //$NON-NLS-1$
190+
private static final String SEPARATOR = ","; //$NON-NLS-1$
191+
192+
public List<String> getMenuShortcutsFromPreferences() {
193+
IEclipsePreferences pref = InstanceScope.INSTANCE.getNode(PLUGIN_ID);
194+
String preferences = pref.get(RECENT_NEW_MENU_ITEMS, null);
195+
if (preferences != null && !preferences.trim().isEmpty()) {
196+
return Arrays.asList(preferences.split(SEPARATOR));
197+
}
198+
return new ArrayList<>();
199+
}
200+
201+
public void setMenuShortcutsToPreferences(Set<String> items) {
202+
IEclipsePreferences pref = InstanceScope.INSTANCE.getNode(PLUGIN_ID);
203+
pref.put(RECENT_NEW_MENU_ITEMS, String.join(SEPARATOR, items));
204+
try {
205+
pref.flush();
206+
} catch (org.osgi.service.prefs.BackingStoreException e) {
207+
e.printStackTrace();
208+
}
209+
}
210+
}
172211
}

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 Returns the selectedElement.
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public void createControl(Composite parent) {
9494
* they will persist into the next invocation of this wizard page
9595
*/
9696
protected void saveWidgetValues() {
97+
RecentNewWizardSelection.getInstance().addItem(newResourcePage.getSelectedElement().getId());
9798
newResourcePage.saveWidgetValues();
9899
}
99100

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
* @since 3.5
29+
*/
30+
public class RecentNewWizardSelection {
31+
private Set<String> selectedFromOther = new LinkedHashSet<>();
32+
private static RecentNewWizardSelection instance;
33+
private static final int MAX_MENU_SIZE = 5;
34+
35+
public static RecentNewWizardSelection getInstance() {
36+
synchronized (RecentNewWizardSelection.class) {
37+
if (instance == null) {
38+
instance = new RecentNewWizardSelection();
39+
}
40+
return instance;
41+
}
42+
}
43+
44+
/**
45+
* Adds the new wizard menu shortcut ID to the set and removes the oldest one if
46+
* the number of recently used new wizard menu shortcuts exceeds MAX_MENU_SIZE.
47+
*
48+
* @param shortcut the new wizard menu shortcut ID
49+
*/
50+
public void addItem(String shortcut) {
51+
selectedFromOther.add(shortcut);
52+
if (selectedFromOther.size() > MAX_MENU_SIZE) {
53+
Iterator<String> iterator = selectedFromOther.iterator();
54+
iterator.next();
55+
iterator.remove();
56+
}
57+
}
58+
59+
/**
60+
* Returns the set of recently used new wizard menu shortcut IDs.
61+
*
62+
* @return the set of recently used new wizard menu shortcut IDs
63+
*/
64+
65+
public Set<String> getSelectedFromOther() {
66+
return Collections.unmodifiableSet(selectedFromOther);
67+
}
68+
69+
}

0 commit comments

Comments
 (0)