Skip to main content
summaryrefslogtreecommitdiffstats
blob: 269b788300476e008c7eb2174d3556d8f6f1c07e (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package org.eclipse.debug.internal.ui;

/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */

import java.util.Iterator;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.debug.core.*;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.*;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.help.WorkbenchHelp;

/**
 * This wizard is used when the debug or run button is pressed, and
 * the launcher/element resolution is not 1:1. It allows the user to
 * choose a launcher and element to launch.
 *
 * <p>The renderer used to render elements to launch is pluggable,
 * allowing launchers to provide custom renderers for the elements
 * they can launch.
 */

public class LaunchWizard extends Wizard {
	
	//NLS
	private static final String PREFIX = "launch_wizard.";
	private static final String DEBUG = PREFIX + "title.debug";
	private static final String RUN = PREFIX + "title.run";

	/**
	 * The collection of available launchers
	 */
	protected Object[] fLaunchers;

	/**
	 * The selection providing context to determine launchables
	 */
	protected IStructuredSelection fSelection;

	/**
	 * The launch page
	 */
	protected LaunchWizardSelectionPage fPage;

	/** 
	 * The mode of the wizard.
	 * @see ExecutionAction#getMode()
	 */
	protected String fMode;
	
	/**
	 * The old default launcher set for the <code>IProject</code>
	 * associated with the current selection.
	 */
	protected ILauncher fOldDefaultLauncher= null;
	
	/**
	 * Indicates if the default launcher has been set for the <code>IProject</code>
	 * associated with the current selection.
	 */
	 protected boolean fDefaultLauncherSet= false;

	/**
	 * Indicates if the wizard needs to determine the launcher to use
	 */
	 protected boolean fSelectLauncher;
	 
	/**
	 * Constructs a wizard with a set of launchers, a selection, a mode 
	 * and whether to select a launcher.
	 */
	public LaunchWizard(Object[] allLaunchers, IStructuredSelection selection, String mode, boolean selectLauncher) {
		fSelectLauncher= selectLauncher;
		fLaunchers= allLaunchers;
		fSelection= selection;
		fMode= mode;
		initialize();
	}
	
	public LaunchWizard(Object[] allLaunchers, IStructuredSelection selection, String mode) {
		this(allLaunchers, selection, mode, true);
	}

	public void createPageControls(Composite pageContainer) {
		super.createPageControls(pageContainer);
		WorkbenchHelp.setHelp(
			pageContainer,
			new Object[] { IDebugHelpContextIds.LAUNCH_WIZARD });
	}
	
	protected void initialize() {
		setNeedsProgressMonitor(true);
		setForcePreviousAndNextButtons(true);
		if (fMode.equals(ILaunchManager.DEBUG_MODE)) {
			setWindowTitle(DebugUIUtils.getResourceString(DEBUG));
		} else {
			setWindowTitle(DebugUIUtils.getResourceString(RUN));
		}
		setDefaultPageImageDescriptor(DebugPluginImages.getImageDescriptor(IDebugUIConstants.IMG_OBJS_LAUNCH_DEBUG));
	}
	/**
	 * @see Wizard#addPages
	 */
	public void addPages() {
		if (fSelection == null || fSelection.isEmpty()) {
			addPage(new LaunchWizardProjectSelectionPage(fMode));
		}
		if (fSelectLauncher) {		
			addPage(fPage= new LaunchWizardSelectionPage(fLaunchers, fMode));	
		}
	}
	
	public IStructuredSelection getSelection() {
		return fSelection;
	}

	/**
	 * Updates the default launcher if required - i.e. if the checkbox is
	 * checked.
	 */
	public void updateDefaultLauncher() {
		IProject project= getProject();
		if (fSelectLauncher && fPage.fSetAsDefaultLauncher.getSelection()) {
			ILauncher launcher= fPage.getLauncher();
			if (launcher != null) {
				try {
					fOldDefaultLauncher= DebugPlugin.getDefault().getLaunchManager().getDefaultLauncher(project);
					DebugPlugin.getDefault().getLaunchManager().setDefaultLauncher(project, launcher);
					fDefaultLauncherSet= true;
				} catch (CoreException e) {
				}
			}
		}
	}
	
	/**
	 * Returns the <code>IProject</code> that is associated with the context selection,
	 * or <code>null</code> if there is no single project associated with the selection.
	 */
	protected IProject getProject() {
		if (fSelection == null) {
			return null;
		}
		IProject project= null;
		Iterator elements= fSelection.iterator();
		while (elements.hasNext()) {
			Object e= elements.next();
			IResource res= null;
			if (e instanceof IAdaptable) {
				res= (IResource) ((IAdaptable) e).getAdapter(IResource.class);
				if (res == null) {
					res= (IResource) ((IAdaptable) e).getAdapter(IProject.class);
				}
			}
			if (res != null) {
				IProject p= res.getProject();
				if (project == null) {
					project= p;
				} else
					if (!project.equals(p)) {
						return null;
					}
			}
		}

		return project;
	}
	
	/**
	 * @see IWizard#performFinish
	 */
	public boolean performFinish() {
		if (!fDefaultLauncherSet) {
			updateDefaultLauncher();
		}
		return true;
	}
	
	/**
	 * @see IWizard#performCancel
	 */
	 public boolean performCancel() {
		if (fDefaultLauncherSet) {
			try {
				DebugPlugin.getDefault().getLaunchManager().setDefaultLauncher(getProject(), fOldDefaultLauncher);
			} catch (CoreException e) {
				return false;
			}
			fDefaultLauncherSet= false;
		}
		return true;
	}
	
	/**
	 * Sets the selection that is the context for the launch.
	 */
	public void setProjectSelection(IStructuredSelection selection) {
		fSelection= selection;
		if (fPage != null) {
			fPage.updateDefaultProject();
		}
	}
	
	/**
	 * @see IWizard#getNextPage(IWizardPage)
	 */
	public IWizardPage getNextPage(IWizardPage page) {
		if (!fSelectLauncher) {
			IWizardNode node= new LaunchWizardNode(page, (ILauncher)fLaunchers[0], fMode);			
			IWizard wizard = node.getWizard();
			wizard.addPages();
			return wizard.getStartingPage();
		}
		return super.getNextPage(page);
	}
	
	/**
	 * @see IWizard#canFinish()
	 */
	public boolean canFinish() {
		//it is the nested wizard that will finish
		return false;
	}
}

Back to the top