diff options
author | Darin Wright | 2008-01-29 04:01:21 +0000 |
---|---|---|
committer | Darin Wright | 2008-01-29 04:01:21 +0000 |
commit | 66fbc197a05c15c438887d40eeec73efefd59df1 (patch) | |
tree | 3d7a7918b7d9fc59ba758cad6d7026908f1ee190 /org.eclipse.debug.examples.ui | |
parent | 0bc10a3da40bb22a0a82290efaee0657c768b245 (diff) | |
download | eclipse.platform.debug-66fbc197a05c15c438887d40eeec73efefd59df1.tar.gz eclipse.platform.debug-66fbc197a05c15c438887d40eeec73efefd59df1.tar.xz eclipse.platform.debug-66fbc197a05c15c438887d40eeec73efefd59df1.zip |
Launch shortcut for MIDI files
Diffstat (limited to 'org.eclipse.debug.examples.ui')
4 files changed, 217 insertions, 0 deletions
diff --git a/org.eclipse.debug.examples.ui/plugin.xml b/org.eclipse.debug.examples.ui/plugin.xml index 6809c0442..41fd1c716 100644 --- a/org.eclipse.debug.examples.ui/plugin.xml +++ b/org.eclipse.debug.examples.ui/plugin.xml @@ -49,6 +49,27 @@ </enablement> </contextualLaunch> </shortcut> + <shortcut + class="org.eclipse.debug.examples.ui.midi.launcher.MidiLaunchShortcut" + description="Plays a standard MIDI file" + icon="icons/full/obj16/note.gif" + id="midi.launchShortcut" + label="MIDI File" + modes="run, debug"> + <contextualLaunch> + <enablement> + <with variable="selection"> + <count value="1"/> + <iterate> + <instanceof value="org.eclipse.core.resources.IFile"/> + <test + value="*.mid" + property="org.eclipse.debug.ui.matchesPattern"/> + </iterate> + </with> + </enablement> + </contextualLaunch> + </shortcut> </extension> <!--#endif --> <extension diff --git a/org.eclipse.debug.examples.ui/src/org/eclipse/debug/examples/ui/midi/launcher/MidiLaunchShortcut.java b/org.eclipse.debug.examples.ui/src/org/eclipse/debug/examples/ui/midi/launcher/MidiLaunchShortcut.java new file mode 100644 index 000000000..f20448b53 --- /dev/null +++ b/org.eclipse.debug.examples.ui/src/org/eclipse/debug/examples/ui/midi/launcher/MidiLaunchShortcut.java @@ -0,0 +1,163 @@ +/******************************************************************************* + * Copyright (c) 2008 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.debug.examples.ui.midi.launcher; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.debug.core.DebugPlugin; +import org.eclipse.debug.core.ILaunchConfiguration; +import org.eclipse.debug.core.ILaunchConfigurationType; +import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; +import org.eclipse.debug.core.ILaunchManager; +import org.eclipse.debug.examples.core.midi.launcher.MidiLaunchDelegate; +import org.eclipse.debug.examples.ui.pda.DebugUIPlugin; +import org.eclipse.debug.ui.DebugUITools; +import org.eclipse.debug.ui.IDebugModelPresentation; +import org.eclipse.debug.ui.ILaunchShortcut; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.window.Window; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.dialogs.ElementListSelectionDialog; + +/** + * Launch shortcut for a MIDI file. + * + * @since 1.0 + */ +public class MidiLaunchShortcut implements ILaunchShortcut { + + /* (non-Javadoc) + * @see org.eclipse.debug.ui.ILaunchShortcut#launch(org.eclipse.jface.viewers.ISelection, java.lang.String) + */ + public void launch(ISelection selection, String mode) { + if (selection instanceof IStructuredSelection) { + IStructuredSelection ss = (IStructuredSelection) selection; + if (ss.size() == 1) { + Object element = ss.getFirstElement(); + if (element instanceof IFile) { + IFile file = (IFile) element; + ILaunchConfiguration configuration = getConfiguration(file); + if (configuration != null) { + DebugUITools.launch(configuration, mode); + } + } + } + } + } + + /** + * Returns a MIDI configuration to use for the given file or + * <code>null</code> to cancel. Creates a new configuration + * if required. + * + * @param file file + * @return associated launch configuration or <code>null</code> + */ + private ILaunchConfiguration getConfiguration(IFile file) { + List candiates = new ArrayList(); + try { + ILaunchConfiguration[] configurations = getLaunchManager().getLaunchConfigurations(getLaunchType()); + for (int i = 0; i < configurations.length; i++) { + ILaunchConfiguration configuration = configurations[i]; + IResource[] resources = configuration.getMappedResources(); + if (resources != null && resources.length == 1 && + resources[0].equals(file)) { + candiates.add(configuration); + } + } + } catch (CoreException e) { + } + if (!candiates.isEmpty()) { + return chooseConfiguration(candiates); + } + return newConfiguration(file); + } + + /** + * Returns the MIDI launch configuration type. + * + * @return the MIDI launch configuration type + */ + private ILaunchConfigurationType getLaunchType() { + ILaunchManager manager = getLaunchManager(); + ILaunchConfigurationType type = manager.getLaunchConfigurationType(MidiLaunchDelegate.ID_MIDI_LAUNCH_CONFIGURATION_TYPE); + return type; + } + + /** + * Returns the launch manager. + * + * @return launch manager + */ + private ILaunchManager getLaunchManager() { + ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager(); + return manager; + } + + /** + * Returns a configuration from the given collection of configurations that should be launched, + * or <code>null</code> to cancel. + * + * @param configList list of configurations to choose from + * @return configuration to launch or <code>null</code> to cancel + */ + private ILaunchConfiguration chooseConfiguration(List configList) { + if (configList.size() == 1) { + return (ILaunchConfiguration) configList.get(0); + } + IDebugModelPresentation labelProvider = DebugUITools.newDebugModelPresentation(); + ElementListSelectionDialog dialog= new ElementListSelectionDialog(DebugUIPlugin.getActiveWorkbenchShell(), labelProvider); + dialog.setElements(configList.toArray()); + dialog.setTitle("Select Configuraiton"); + dialog.setMessage("&Select an existing configuration:"); + dialog.setMultipleSelection(false); + int result = dialog.open(); + labelProvider.dispose(); + if (result == Window.OK) { + return (ILaunchConfiguration) dialog.getFirstResult(); + } + return null; + } + + /** + * Creates and returns a new MIDI launch configuration for the + * given file. + * + * @param file MIDI file + * @return new launch configuration + */ + private ILaunchConfiguration newConfiguration(IFile file) { + ILaunchConfigurationType type = getLaunchType(); + try { + ILaunchConfigurationWorkingCopy workingCopy = type.newInstance(null, getLaunchManager(). + generateUniqueLaunchConfigurationNameFrom( + "[" + file.getProject().getName() + "] " + file.getName())); + workingCopy.setAttribute(MidiLaunchDelegate.ATTR_MIDI_FILE, file.getFullPath().toString()); + workingCopy.setMappedResources(new IResource[]{file}); + return workingCopy.doSave(); + } catch (CoreException e) { + e.printStackTrace(); + } + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.debug.ui.ILaunchShortcut#launch(org.eclipse.ui.IEditorPart, java.lang.String) + */ + public void launch(IEditorPart editor, String mode) { + // nothing - currently no editor + } +} diff --git a/org.eclipse.debug.examples.ui/src/org/eclipse/debug/examples/ui/midi/launcher/MidiMainTab.java b/org.eclipse.debug.examples.ui/src/org/eclipse/debug/examples/ui/midi/launcher/MidiMainTab.java index 80dff6a7b..fca24e5b4 100644 --- a/org.eclipse.debug.examples.ui/src/org/eclipse/debug/examples/ui/midi/launcher/MidiMainTab.java +++ b/org.eclipse.debug.examples.ui/src/org/eclipse/debug/examples/ui/midi/launcher/MidiMainTab.java @@ -130,7 +130,16 @@ public class MidiMainTab extends AbstractLaunchConfigurationTab { if (file.length() == 0) { file = null; } + IResource[] resources = null; + if (file!= null) { + IPath path = new Path(file); + IResource res = ResourcesPlugin.getWorkspace().getRoot().findMember(path); + if (res != null) { + resources = new IResource[]{res}; + } + } configuration.setAttribute(MidiLaunchDelegate.ATTR_MIDI_FILE, file); + configuration.setMappedResources(resources); } /* (non-Javadoc) diff --git a/org.eclipse.debug.examples.ui/src/org/eclipse/debug/examples/ui/pda/DebugUIPlugin.java b/org.eclipse.debug.examples.ui/src/org/eclipse/debug/examples/ui/pda/DebugUIPlugin.java index 743b82ffd..f46234338 100644 --- a/org.eclipse.debug.examples.ui/src/org/eclipse/debug/examples/ui/pda/DebugUIPlugin.java +++ b/org.eclipse.debug.examples.ui/src/org/eclipse/debug/examples/ui/pda/DebugUIPlugin.java @@ -24,6 +24,8 @@ import org.eclipse.jface.resource.ImageRegistry; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.internal.util.BundleUtility; import org.eclipse.ui.plugin.AbstractUIPlugin; import org.osgi.framework.BundleContext; @@ -185,4 +187,26 @@ public class DebugUIPlugin extends AbstractUIPlugin { return color; } + /** + * Returns the active workbench window + * + * @return the active workbench window + */ + public static IWorkbenchWindow getActiveWorkbenchWindow() { + return getDefault().getWorkbench().getActiveWorkbenchWindow(); + } + + /** + * Returns the active workbench shell or <code>null</code> if none + * + * @return the active workbench shell or <code>null</code> if none + */ + public static Shell getActiveWorkbenchShell() { + IWorkbenchWindow window = getActiveWorkbenchWindow(); + if (window != null) { + return window.getShell(); + } + return null; + } + } |