diff options
4 files changed, 268 insertions, 94 deletions
diff --git a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/editor/LaunchShortcutOverviewPage.java b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/editor/LaunchShortcutOverviewPage.java index 6d7270919c..f7a0aa9278 100644 --- a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/editor/LaunchShortcutOverviewPage.java +++ b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/editor/LaunchShortcutOverviewPage.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2008 IBM Corporation and others. + * Copyright (c) 2007, 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -52,8 +52,15 @@ public abstract class LaunchShortcutOverviewPage extends PDEFormPage implements * @see org.eclipse.ui.forms.events.HyperlinkListener#linkActivated(org.eclipse.ui.forms.events.HyperlinkEvent) */ public void linkActivated(HyperlinkEvent e) { + // target href takes the form of launchShortcut.<mode>.<id> String href = (String) e.getHref(); - getPDELauncherEditor().launch(href, getPDELauncherEditor().getPreLaunchRunnable(), getPDELauncherEditor().getLauncherHelper().getLaunchObject()); + int modeStart = href.indexOf('.'); + if (modeStart != -1) { + int modeEnd = href.indexOf('.', modeStart + 1); + if (modeEnd != -1) { + getPDELauncherEditor().launch(href.substring(modeEnd + 1), href.substring(modeStart + 1, modeEnd), getPDELauncherEditor().getPreLaunchRunnable(), getPDELauncherEditor().getLauncherHelper().getLaunchObject()); + } + } } // returns the indent for each launcher diff --git a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/editor/PDEEditorLaunchManager.java b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/editor/PDEEditorLaunchManager.java new file mode 100644 index 0000000000..afccbab4ad --- /dev/null +++ b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/editor/PDEEditorLaunchManager.java @@ -0,0 +1,104 @@ +/******************************************************************************* + * Copyright (c) 2013 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.pde.internal.ui.editor; + +import java.util.LinkedList; +import java.util.List; +import org.eclipse.jface.dialogs.IDialogSettings; +import org.eclipse.pde.internal.ui.PDEPlugin; + +/** + * Manages the list of launch shortcuts available in the {@link PDELauncherFormEditor}. + * Provides a recommended order for actions that is persisted between workbench launches. + * This could also be applied to {@link LaunchShortcutOverviewPage} in the future. + * @since 4.3 + */ +public class PDEEditorLaunchManager { + + private static final String SETTINGS_SECTION = "PDEFormActionManager"; //$NON-NLS-1$ + private static final String SETTINGS_RECENT_LAUNCHES = "RecentLaunches"; //$NON-NLS-1$ + private static final int MAX_LAUNCHES = 3; + + private static PDEEditorLaunchManager fDefault; + private List<String> recentLaunches; + + /** + * Returns the static default instance of this manager + * @return the default manager + */ + public static PDEEditorLaunchManager getDefault() { + if (fDefault == null) { + fDefault = new PDEEditorLaunchManager(); + } + return fDefault; + } + + /** + * Marks the launcher with the given launcher id as being recently launched + * + * @param id launcher id, 'id' attribute of the PDE launch shortcut extension + */ + public void setRecentLaunch(String id) { + if (recentLaunches == null) { + initialize(); + } + // Reorder list to put this id on top + int currentIndex = recentLaunches.indexOf(id); + if (currentIndex == -1) { + recentLaunches.add(0, id); + } else if (currentIndex != 0) { + recentLaunches.remove(currentIndex); + recentLaunches.add(0, id); + } + + // As only single items can be added to the list, just remove the last item if we are over max + if (recentLaunches.size() > MAX_LAUNCHES) { + recentLaunches.remove(recentLaunches.size() - 1); + } + + // Persist the settings + persist(); + } + + /** + * Returns an ordered list of String launcher ids taken from PDE launch + * shortcut extensions. The most recently launched entry is at + * index 0. + * + * @return ordered list of launcher IDs, possibly empty + */ + public List<String> getRecentLaunches() { + if (recentLaunches == null) { + initialize(); + } + return recentLaunches; + } + + private void initialize() { + recentLaunches = new LinkedList<String>(); + IDialogSettings settings = PDEPlugin.getDefault().getDialogSettingsSection(SETTINGS_SECTION); + String[] result = settings.getArray(SETTINGS_RECENT_LAUNCHES); + if (result != null) { + for (int i = 0; i < result.length; i++) { + recentLaunches.add(result[i]); + } + } + } + + private void persist() { + if (recentLaunches != null) { + IDialogSettings settings = PDEPlugin.getDefault().getDialogSettingsSection(SETTINGS_SECTION); + String[] result = recentLaunches.toArray(new String[recentLaunches.size()]); + settings.put(SETTINGS_RECENT_LAUNCHES, result); + } + } + +} diff --git a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/editor/PDELauncherFormEditor.java b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/editor/PDELauncherFormEditor.java index b6fa490b71..9467815b8b 100644 --- a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/editor/PDELauncherFormEditor.java +++ b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/editor/PDELauncherFormEditor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2010 IBM Corporation and others. + * Copyright (c) 2007, 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -26,7 +26,19 @@ import org.eclipse.pde.internal.ui.editor.actions.ActionMenu; public abstract class PDELauncherFormEditor extends MultiSourceEditor { - Action[][] fActions = null; + protected static final int RUN_LAUNCHER_INDEX = 0; + protected static final int DEBUG_LAUNCHER_INDEX = 1; + protected static final int PROFILE_LAUNCHER_INDEX = 2; + + /** + * Stores the toolbar contributions that contain all the actions. Entries may + * be null if the toolbar hasn't been created or if there are no actions for that + * index. Uses {@link #RUN_LAUNCHER_INDEX}, {@link #DEBUG_LAUNCHER_INDEX} and + * {@link #PROFILE_LAUNCHER_INDEX}. + */ + ActionMenu[] fToolbarActions = new ActionMenu[3]; + + LauncherAction[][] fActions = null; protected abstract ILauncherFormPageHelper getLauncherHelper(); @@ -35,49 +47,58 @@ public abstract class PDELauncherFormEditor extends MultiSourceEditor { // this should never be null (no point in using this class if you don't provide an ILauncherFormPageHelper) // but we'll guard against it anyway if (getLauncherHelper() != null) { - - Action[][] actions = getActions(); + List<String> recentLaunches = PDEEditorLaunchManager.getDefault().getRecentLaunches(); + LauncherAction[][] actions = getActions(); if (actions[RUN_LAUNCHER_INDEX].length > 0) { - Action runAction = new ActionMenu(actions[RUN_LAUNCHER_INDEX]); - manager.add(runAction); + fToolbarActions[RUN_LAUNCHER_INDEX] = new ActionMenu(actions[RUN_LAUNCHER_INDEX]); + fToolbarActions[RUN_LAUNCHER_INDEX].updateActionOrder(recentLaunches); + manager.add(fToolbarActions[RUN_LAUNCHER_INDEX]); } if (actions[DEBUG_LAUNCHER_INDEX].length > 0) { - Action runAction = new ActionMenu(actions[DEBUG_LAUNCHER_INDEX]); - manager.add(runAction); + fToolbarActions[DEBUG_LAUNCHER_INDEX] = new ActionMenu(actions[DEBUG_LAUNCHER_INDEX]); + fToolbarActions[DEBUG_LAUNCHER_INDEX].updateActionOrder(recentLaunches); + manager.add(fToolbarActions[DEBUG_LAUNCHER_INDEX]); } if (actions[PROFILE_LAUNCHER_INDEX].length > 0) { - Action runAction = new ActionMenu(actions[PROFILE_LAUNCHER_INDEX]); - manager.add(runAction); + fToolbarActions[PROFILE_LAUNCHER_INDEX] = new ActionMenu(actions[PROFILE_LAUNCHER_INDEX]); + fToolbarActions[PROFILE_LAUNCHER_INDEX].updateActionOrder(recentLaunches); + manager.add(fToolbarActions[PROFILE_LAUNCHER_INDEX]); } } } - private Action[][] getActions() { + private LauncherAction[][] getActions() { if (fActions == null) { - fActions = new Action[3][]; + fActions = new LauncherAction[3][]; IConfigurationElement[][] elements = getLaunchers(getLauncherHelper().isOSGi()); - fActions[RUN_LAUNCHER_INDEX] = getLauncherActions(elements[RUN_LAUNCHER_INDEX]); - fActions[DEBUG_LAUNCHER_INDEX] = getLauncherActions(elements[DEBUG_LAUNCHER_INDEX]); - fActions[PROFILE_LAUNCHER_INDEX] = getLauncherActions(elements[PROFILE_LAUNCHER_INDEX]); + fActions[RUN_LAUNCHER_INDEX] = getLauncherActions(elements[RUN_LAUNCHER_INDEX], RUN_LAUNCHER_INDEX); + fActions[DEBUG_LAUNCHER_INDEX] = getLauncherActions(elements[DEBUG_LAUNCHER_INDEX], DEBUG_LAUNCHER_INDEX); + fActions[PROFILE_LAUNCHER_INDEX] = getLauncherActions(elements[PROFILE_LAUNCHER_INDEX], PROFILE_LAUNCHER_INDEX); } return fActions; } - private Action[] getLauncherActions(IConfigurationElement[] elements) { - Action[] result = new Action[elements.length]; + private LauncherAction[] getLauncherActions(IConfigurationElement[] elements, final int toolbarIndex) { + LauncherAction[] result = new LauncherAction[elements.length]; for (int i = 0; i < elements.length; i++) { - String label = elements[i].getAttribute("label"); //$NON-NLS-1$ - final String thisLaunchShortcut = getLaunchString(elements[i]); - Action thisAction = new Action(label) { + LauncherAction thisAction = new LauncherAction(elements[i]) { public void run() { doSave(null); - launch(thisLaunchShortcut, getPreLaunchRunnable(), getLauncherHelper().getLaunchObject()); + String id = getConfigurationElement().getAttribute("id"); //$NON-NLS-1$ + String mode = getConfigurationElement().getAttribute("mode"); //$NON-NLS-1$ + launch(id, mode, getPreLaunchRunnable(), getLauncherHelper().getLaunchObject()); + // Have all toolbar items update their order + PDEEditorLaunchManager.getDefault().setRecentLaunch(getConfigurationElement().getAttribute("id")); //$NON-NLS-1$ + List<String> updatedActionOrder = PDEEditorLaunchManager.getDefault().getRecentLaunches(); + for (int j = 0; j < fToolbarActions.length; j++) { + if (fToolbarActions[j] != null) { + fToolbarActions[j].updateActionOrder(updatedActionOrder); + } + } } }; - thisAction.setToolTipText(label); - thisAction.setImageDescriptor(getImageDescriptor(elements[i])); result[i] = thisAction; } return result; @@ -91,53 +112,21 @@ public abstract class PDELauncherFormEditor extends MultiSourceEditor { }; } - public String getLaunchString(IConfigurationElement e) { - StringBuffer sb = new StringBuffer("launchShortcut."); //$NON-NLS-1$ - sb.append(e.getAttribute("mode")); //$NON-NLS-1$ - sb.append("."); //$NON-NLS-1$ - sb.append(e.getAttribute("id")); //$NON-NLS-1$ - return sb.toString(); - } - - private ImageDescriptor getImageDescriptor(IConfigurationElement element) { - String mode = element.getAttribute("mode"); //$NON-NLS-1$ - if (mode == null) - return null; - else if (mode.equals(ILaunchManager.RUN_MODE)) - return PDEPluginImages.DESC_RUN_EXC; - else if (mode.equals(ILaunchManager.DEBUG_MODE)) - return PDEPluginImages.DESC_DEBUG_EXC; - else if (mode.equals(ILaunchManager.PROFILE_MODE)) - return PDEPluginImages.DESC_PROFILE_EXC; - return null; - } - - public void launch(String launchShortcut, Runnable preLaunch, Object launchObject) { - if (launchShortcut.startsWith("launchShortcut.")) { //$NON-NLS-1$ - launchShortcut = launchShortcut.substring(15); - int index = launchShortcut.indexOf('.'); - if (index < 0) - return; // error. Format of launchShortcut should be launchShortcut.<mode>.<launchShortcutId> - String mode = launchShortcut.substring(0, index); - String id = launchShortcut.substring(index + 1); - IExtensionRegistry registry = Platform.getExtensionRegistry(); - IConfigurationElement[] elements = registry.getConfigurationElementsFor("org.eclipse.debug.ui.launchShortcuts"); //$NON-NLS-1$ - for (int i = 0; i < elements.length; i++) { - if (id.equals(elements[i].getAttribute("id"))) //$NON-NLS-1$ - try { - ILaunchShortcut shortcut = (ILaunchShortcut) elements[i].createExecutableExtension("class"); //$NON-NLS-1$ - preLaunch.run(); - shortcut.launch(new StructuredSelection(launchObject), mode); - } catch (CoreException e1) { - } + public void launch(String launcherID, String mode, Runnable preLaunch, Object launchObject) { + IExtensionRegistry registry = Platform.getExtensionRegistry(); + IConfigurationElement[] elements = registry.getConfigurationElementsFor("org.eclipse.debug.ui.launchShortcuts"); //$NON-NLS-1$ + for (int i = 0; i < elements.length; i++) { + if (launcherID.equals(elements[i].getAttribute("id"))) { //$NON-NLS-1$ + try { + ILaunchShortcut shortcut = (ILaunchShortcut) elements[i].createExecutableExtension("class"); //$NON-NLS-1$ + preLaunch.run(); + shortcut.launch(new StructuredSelection(launchObject), mode); + } catch (CoreException e1) { + } } } } - protected static final int RUN_LAUNCHER_INDEX = 0; - protected static final int DEBUG_LAUNCHER_INDEX = 1; - protected static final int PROFILE_LAUNCHER_INDEX = 2; - protected IConfigurationElement[][] getLaunchers(boolean osgi) { IExtensionRegistry registry = Platform.getExtensionRegistry(); IConfigurationElement[] elements = registry.getConfigurationElementsFor("org.eclipse.pde.ui.launchShortcuts"); //$NON-NLS-1$ @@ -186,16 +175,38 @@ public abstract class PDELauncherFormEditor extends MultiSourceEditor { IConfigurationElement[] runElements = runList.toArray(new IConfigurationElement[runList.size()]); IConfigurationElement[] debugElements = debugList.toArray(new IConfigurationElement[debugList.size()]); IConfigurationElement[] profileElements = profileList.toArray(new IConfigurationElement[profileList.size()]); - Comparator<Object> comparator = new Comparator<Object>() { - public int compare(Object arg0, Object arg1) { - String label1 = ((IConfigurationElement) arg0).getAttribute("label"); //$NON-NLS-1$ - String label2 = ((IConfigurationElement) arg1).getAttribute("label"); //$NON-NLS-1$ - return label1.compareTo(label2); - } - }; - Arrays.sort(runElements, comparator); - Arrays.sort(debugElements, comparator); - Arrays.sort(profileElements, comparator); return new IConfigurationElement[][] {runElements, debugElements, profileElements}; } + + /** + * Represents an action that will launch a PDE launch shortcut extension + */ + public static abstract class LauncherAction extends Action { + private IConfigurationElement configElement; + + public LauncherAction(IConfigurationElement configurationElement) { + super(); + configElement = configurationElement; + String label = configElement.getAttribute("label"); //$NON-NLS-1$ + setText(label); + setToolTipText(label); + setImageDescriptor(getImageDescriptor(configurationElement.getAttribute("mode"))); //$NON-NLS-1$ + } + + public IConfigurationElement getConfigurationElement() { + return configElement; + } + + private ImageDescriptor getImageDescriptor(String mode) { + if (mode == null) + return null; + else if (mode.equals(ILaunchManager.RUN_MODE)) + return PDEPluginImages.DESC_RUN_EXC; + else if (mode.equals(ILaunchManager.DEBUG_MODE)) + return PDEPluginImages.DESC_DEBUG_EXC; + else if (mode.equals(ILaunchManager.PROFILE_MODE)) + return PDEPluginImages.DESC_PROFILE_EXC; + return null; + } + } } diff --git a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/editor/actions/ActionMenu.java b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/editor/actions/ActionMenu.java index 6f1ac13b9e..7a209696a8 100644 --- a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/editor/actions/ActionMenu.java +++ b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/editor/actions/ActionMenu.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2008 IBM Corporation and others. + * Copyright (c) 2007, 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -10,30 +10,41 @@ *******************************************************************************/ package org.eclipse.pde.internal.ui.editor.actions; +import java.util.*; import org.eclipse.jface.action.*; +import org.eclipse.pde.internal.ui.editor.PDELauncherFormEditor.LauncherAction; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Menu; public class ActionMenu extends Action implements IMenuCreator { - Action[] fActions; + List<LauncherAction> fActions; Menu fMenu; - public ActionMenu(Action[] actions) { - fActions = actions; - if (fActions.length > 0) { - setToolTipText(fActions[0].getToolTipText()); - setImageDescriptor(fActions[0].getImageDescriptor()); - if (fActions.length > 1) + public ActionMenu(LauncherAction[] actions) { + fActions = new LinkedList<LauncherAction>(); + for (int i = 0; i < actions.length; i++) { + fActions.add(actions[i]); + } + if (!fActions.isEmpty()) { + setToolTipText(fActions.get(0).getToolTipText()); + setImageDescriptor(fActions.get(0).getImageDescriptor()); + if (fActions.size() > 1) setMenuCreator(this); } } + /* (non-Javadoc) + * @see org.eclipse.jface.action.Action#run() + */ public void run() { - if (fActions.length > 0) - fActions[0].run(); + if (!fActions.isEmpty()) + fActions.get(0).run(); } + /* (non-Javadoc) + * @see org.eclipse.jface.action.IMenuCreator#dispose() + */ public void dispose() { if (fMenu != null) { fMenu.dispose(); @@ -41,23 +52,64 @@ public class ActionMenu extends Action implements IMenuCreator { } } + /* (non-Javadoc) + * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Control) + */ public Menu getMenu(Control parent) { if (fMenu != null) fMenu.dispose(); fMenu = new Menu(parent); - - for (int i = 0; i < fActions.length; i++) { - addActionToMenu(fMenu, fActions[i]); + for (Iterator<LauncherAction> iterator = fActions.iterator(); iterator.hasNext();) { + ActionContributionItem item = new ActionContributionItem(iterator.next()); + item.fill(fMenu, -1); } return fMenu; } + /* (non-Javadoc) + * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Menu) + */ public Menu getMenu(Menu parent) { return null; } - protected void addActionToMenu(Menu parent, Action action) { - ActionContributionItem item = new ActionContributionItem(action); - item.fill(parent, -1); + /** + * Reorders the actions in the menu based on the most recently launched + * + * @param orderedLauncherIds list of string launcher ids to order the actions by + */ + public void updateActionOrder(final List<String> orderedLauncherIds) { + if (!fActions.isEmpty()) { + Collections.sort(fActions, new Comparator<LauncherAction>() { + public int compare(LauncherAction o1, LauncherAction o2) { + // Entries in the recent launcher list go first + String id1 = o1.getConfigurationElement().getAttribute("id"); //$NON-NLS-1$ + String id2 = o2.getConfigurationElement().getAttribute("id"); //$NON-NLS-1$ + int index1 = orderedLauncherIds.indexOf(id1); + int index2 = orderedLauncherIds.indexOf(id2); + if (index1 == -1 && index2 == -1) { + return 0; +// if (id1.contains("pde")) +// org.eclipse.pde.ui.runtimeWorkbenchShortcut +// +// String label1 = o1.getConfigurationElement().getAttribute("label"); //$NON-NLS-1$ +// String label2 = o2.getConfigurationElement().getAttribute("label"); //$NON-NLS-1$ +// return label1.compareTo(label2); + } + if (index1 == -1) { + return 1; + } + if (index2 == -1) { + return -1; + } + if (index1 <= index2) { + return -1; + } + return 1; + } + }); + setToolTipText(fActions.get(0).getToolTipText()); + setImageDescriptor(fActions.get(0).getImageDescriptor()); + } } } |