diff --git a/bundles/org.eclipse.ui.ide/extensions/org/eclipse/ui/actions/NewWizardMenu.java b/bundles/org.eclipse.ui.ide/extensions/org/eclipse/ui/actions/NewWizardMenu.java index f076931e1ba..a0d80ee7603 100644 --- a/bundles/org.eclipse.ui.ide/extensions/org/eclipse/ui/actions/NewWizardMenu.java +++ b/bundles/org.eclipse.ui.ide/extensions/org/eclipse/ui/actions/NewWizardMenu.java @@ -11,12 +11,17 @@ * Contributors: * IBM Corporation - initial API and implementation * Alexander Fedorov - Bug 548428 + * Dinesh Palanisamy (ETAS GmbH) - Issue 1530 *******************************************************************************/ 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; @@ -28,6 +33,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.DynamicMenuSelection; import org.eclipse.ui.internal.dialogs.WorkbenchWizardElement; import org.eclipse.ui.internal.registry.WizardsRegistryReader; import org.eclipse.ui.wizards.IWizardCategory; @@ -186,9 +192,29 @@ protected void addItems(List list) { list.add(new ActionContributionItem(newExampleAction)); list.add(new Separator()); } + + // To add shortcuts from OTHER... wizard regardless of perspective + Collection otherItems = new LinkedList<>(); + if (!DynamicMenuSelection.getInstance().getSelectedFromOther().isEmpty()) { + for (String selectedItemsFormOthers : DynamicMenuSelection.getInstance().getSelectedFromOther()) { + IAction action = getAction(selectedItemsFormOthers); + otherItems.add(new ActionContributionItem(action)); + } + dynamicMenuCheck(list, otherItems); + } list.add(new ActionContributionItem(getShowDialogAction())); } + private void dynamicMenuCheck(List list, Collection otherItems) { + Set 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(); diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/actions/BaseNewWizardMenu.java b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/actions/BaseNewWizardMenu.java index a94e8afe169..d313af3d471 100644 --- a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/actions/BaseNewWizardMenu.java +++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/actions/BaseNewWizardMenu.java @@ -166,7 +166,10 @@ public void dispose() { /* * Returns the action for the given wizard id, or null if not found. */ - private IAction getAction(String id) { + /** + * @since 3.132 + */ + protected 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); diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/WorkbenchPlugin.java b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/WorkbenchPlugin.java index 17e5ca31874..d075dc9f4c8 100644 --- a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/WorkbenchPlugin.java +++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/WorkbenchPlugin.java @@ -22,6 +22,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.Locale; +import java.util.Set; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IConfigurationElement; import org.eclipse.core.runtime.IExtensionPoint; @@ -53,6 +54,7 @@ import org.eclipse.ui.IWorkingSetManager; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.internal.decorators.DecoratorManager; +import org.eclipse.ui.internal.dialogs.DynamicMenuSelection; import org.eclipse.ui.internal.dialogs.WorkbenchPreferenceManager; import org.eclipse.ui.internal.help.CommandHelpServiceImpl; import org.eclipse.ui.internal.help.HelpServiceImpl; @@ -80,6 +82,7 @@ import org.eclipse.ui.testing.TestableObject; import org.eclipse.ui.views.IViewRegistry; import org.eclipse.ui.wizards.IWizardRegistry; +import org.eclipse.ui.workbench.DynamicMenuItem; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.BundleEvent; @@ -202,6 +205,8 @@ public class WorkbenchPlugin extends AbstractUIPlugin { private ICommandHelpService commandHelpService; + private DynamicMenuItem dynamicMenuItem; + /** * Create an instance of the WorkbenchPlugin. The workbench plugin is * effectively the "application" for the workbench UI. The entire UI operates as @@ -210,6 +215,7 @@ public class WorkbenchPlugin extends AbstractUIPlugin { public WorkbenchPlugin() { super(); inst = this; + dynamicMenuItem = new DynamicMenuItem(); } /** @@ -766,6 +772,10 @@ public void start(BundleContext context) throws Exception { // to be loaded.s if (uiBundle != null) uiBundle.start(Bundle.START_TRANSIENT); + + // For dynamic menu items + Set selectedFromOther = DynamicMenuSelection.getInstance().getSelectedFromOther(); + selectedFromOther.addAll(dynamicMenuItem.getDynamicMenuItems()); } catch (BundleException e) { WorkbenchPlugin.log("Unable to load UI activator", e); //$NON-NLS-1$ } @@ -1048,6 +1058,9 @@ public void stop(BundleContext context) throws Exception { testableTracker.close(); testableTracker = null; } + // For dynamic menu items + Set selectedFromOther = DynamicMenuSelection.getInstance().getSelectedFromOther(); + dynamicMenuItem.setDynamicMenuItems(selectedFromOther); super.stop(context); } diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/dialogs/DynamicMenuSelection.java b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/dialogs/DynamicMenuSelection.java new file mode 100644 index 00000000000..fb4e3d1fcd5 --- /dev/null +++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/dialogs/DynamicMenuSelection.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * 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.Iterator; +import java.util.LinkedHashSet; +import java.util.Set; + +/** + * @since 3.5 + * + */ +public class DynamicMenuSelection { + private Set selectedFromOther = new LinkedHashSet<>(); + private static DynamicMenuSelection instance; + private static final int MAX_MENU_SIZE = 5; + + public static DynamicMenuSelection getInstance() { + synchronized (DynamicMenuSelection.class) { + if (instance == null) { + instance = new DynamicMenuSelection(); + } + return instance; + } + } + + public void addItems(String shortcuts) { + selectedFromOther.add(shortcuts); + if (selectedFromOther.size() > MAX_MENU_SIZE) { + Iterator iterator = selectedFromOther.iterator(); + iterator.next(); + iterator.remove(); + } + } + + /** + * @return Returns the selectedFromOther. + */ + public Set getSelectedFromOther() { + return selectedFromOther; + } + +} diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/dialogs/NewWizardNewPage.java b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/dialogs/NewWizardNewPage.java index 9b75c8fd162..991865bb717 100644 --- a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/dialogs/NewWizardNewPage.java +++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/dialogs/NewWizardNewPage.java @@ -689,4 +689,11 @@ public IWorkbenchWizard createWizard() throws CoreException { updateDescription(selectedObject); } + + /** + * @return Returns the selectedElement. + */ + public IWizardDescriptor getSelectedElement() { + return selectedElement; + } } diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/dialogs/NewWizardSelectionPage.java b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/dialogs/NewWizardSelectionPage.java index 1fc475163f2..7c31d302617 100644 --- a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/dialogs/NewWizardSelectionPage.java +++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/dialogs/NewWizardSelectionPage.java @@ -94,6 +94,8 @@ public void createControl(Composite parent) { * they will persist into the next invocation of this wizard page */ protected void saveWidgetValues() { + // For dynamic menu + DynamicMenuSelection.getInstance().addItems(newResourcePage.getSelectedElement().getId()); newResourcePage.saveWidgetValues(); } diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/workbench/DynamicMenuItem.java b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/workbench/DynamicMenuItem.java new file mode 100644 index 00000000000..b29e295f272 --- /dev/null +++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/workbench/DynamicMenuItem.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * 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.workbench; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import org.eclipse.core.runtime.preferences.IEclipsePreferences; +import org.eclipse.core.runtime.preferences.InstanceScope; + +public class DynamicMenuItem { + + private static final String PLUGIN_ID = "org.eclipse.ui.workbench"; //$NON-NLS-1$ + private static final String DYNAMIC_MENU_ITEMS = "dynamicMenuItems"; //$NON-NLS-1$ + + public List getDynamicMenuItems() { + IEclipsePreferences pref = InstanceScope.INSTANCE.getNode(PLUGIN_ID); + String jsn = pref.get(DYNAMIC_MENU_ITEMS, null); + if (jsn != null) { + Gson gsn = new Gson(); + Type typ = new TypeToken>() { + }.getType(); + return gsn.fromJson(jsn, typ); + } + return new ArrayList<>(); + } + + public void setDynamicMenuItems(Set set) { + IEclipsePreferences pref = InstanceScope.INSTANCE.getNode(PLUGIN_ID); + Gson gsn = new Gson(); + String jsn = gsn.toJson(set); + pref.put(DYNAMIC_MENU_ITEMS, jsn); + try { + pref.flush(); + } catch (org.osgi.service.prefs.BackingStoreException e) { + e.printStackTrace(); + } + } + +} diff --git a/bundles/org.eclipse.ui.workbench/META-INF/MANIFEST.MF b/bundles/org.eclipse.ui.workbench/META-INF/MANIFEST.MF index e5c807580b1..5ea1cf99fb9 100644 --- a/bundles/org.eclipse.ui.workbench/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.ui.workbench/META-INF/MANIFEST.MF @@ -113,7 +113,8 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.29.0,4.0.0)", org.eclipse.e4.ui.workbench.addons.swt;bundle-version="0.10.0", org.eclipse.e4.ui.services;bundle-version="1.3.0", org.eclipse.emf.ecore.xmi;bundle-version="2.11.0", - org.eclipse.e4.core.di.extensions;bundle-version="0.13.0" + org.eclipse.e4.core.di.extensions;bundle-version="0.13.0", + com.google.gson;bundle-version="2.10.1" Import-Package: com.ibm.icu.util, jakarta.annotation;version="[2.1.0,3.0.0)", jakarta.inject;version="[2.0.0,3.0.0)",