Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 549b7e2c2e189bcae5def5a840e766c1bb720ab5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*******************************************************************************
 * Copyright (c) 2008, 2018 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 {

	@Override
	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<ILaunchConfiguration> 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<ILaunchConfiguration> configList) {
		if (configList.size() == 1) {
			return configList.get(0);
		}
		IDebugModelPresentation labelProvider = DebugUITools.newDebugModelPresentation();
		ElementListSelectionDialog dialog= new ElementListSelectionDialog(DebugUIPlugin.getActiveWorkbenchShell(), labelProvider);
		dialog.setElements(configList.toArray());
		dialog.setTitle("Select Configuraiton"); //$NON-NLS-1$
		dialog.setMessage("&Select an existing configuration:"); //$NON-NLS-1$
		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().
					generateLaunchConfigurationName(
"[" + file.getProject().getName() + "] " + file.getName())); //$NON-NLS-1$ //$NON-NLS-2$
			workingCopy.setAttribute(MidiLaunchDelegate.ATTR_MIDI_FILE, file.getFullPath().toString());
			workingCopy.setMappedResources(new IResource[]{file});
			return workingCopy.doSave();
		} catch (CoreException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void launch(IEditorPart editor, String mode) {
		// nothing - currently no editor
	}
}

Back to the top