Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2000, 2005 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.ui.externaltools.internal.launchConfigurations; |
12 |
|
13 |
|
14 |
import java.util.ArrayList; |
15 |
import java.util.Iterator; |
16 |
import java.util.List; |
17 |
|
18 |
import org.eclipse.core.resources.IProject; |
19 |
import org.eclipse.core.resources.IResource; |
20 |
import org.eclipse.core.resources.IWorkspace; |
21 |
import org.eclipse.core.resources.IWorkspaceRoot; |
22 |
import org.eclipse.core.resources.ResourcesPlugin; |
23 |
import org.eclipse.core.runtime.CoreException; |
24 |
import org.eclipse.debug.core.ILaunchConfiguration; |
25 |
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; |
26 |
import org.eclipse.debug.ui.AbstractLaunchConfigurationTab; |
27 |
import org.eclipse.debug.ui.DebugUITools; |
28 |
import org.eclipse.jface.viewers.IStructuredContentProvider; |
29 |
import org.eclipse.jface.viewers.Viewer; |
30 |
import org.eclipse.jface.window.Window; |
31 |
import org.eclipse.swt.SWT; |
32 |
import org.eclipse.swt.events.SelectionAdapter; |
33 |
import org.eclipse.swt.events.SelectionEvent; |
34 |
import org.eclipse.swt.graphics.Image; |
35 |
import org.eclipse.swt.layout.GridData; |
36 |
import org.eclipse.swt.layout.GridLayout; |
37 |
import org.eclipse.swt.widgets.Button; |
38 |
import org.eclipse.swt.widgets.Composite; |
39 |
import org.eclipse.swt.widgets.Group; |
40 |
import org.eclipse.ui.PlatformUI; |
41 |
import org.eclipse.ui.dialogs.ListSelectionDialog; |
42 |
import org.eclipse.ui.externaltools.internal.model.ExternalToolsImages; |
43 |
import org.eclipse.ui.externaltools.internal.model.ExternalToolsPlugin; |
44 |
import org.eclipse.ui.externaltools.internal.model.IExternalToolConstants; |
45 |
import org.eclipse.ui.externaltools.internal.model.IExternalToolsHelpContextIds; |
46 |
import org.eclipse.ui.model.WorkbenchLabelProvider; |
47 |
|
48 |
/** |
49 |
* A launch configuration tab which allows the user to specify |
50 |
* which resources should be built before a build (a build scope) |
51 |
* <p> |
52 |
* This class may be instantiated; this class is not intended |
53 |
* to be subclassed. |
54 |
* </p> |
55 |
* @since 3.0 |
56 |
*/ |
57 |
public class ExternalToolsBuildTab extends AbstractLaunchConfigurationTab { |
58 |
// Check Buttons |
59 |
private Button fBuildButton; |
60 |
|
61 |
// Group box |
62 |
private Group fGroup; |
63 |
|
64 |
// Radio Buttons |
65 |
private Button fProjectButton; |
66 |
private Button fSpecificProjectsButton; |
67 |
private Button fWorkspaceButton; |
68 |
|
69 |
// Push Button |
70 |
private Button fSelectButton; |
71 |
|
72 |
// whether to include referenced projects |
73 |
private Button fReferencedProjects; |
74 |
|
75 |
// projects to build (empty if none) |
76 |
private List fProjects = new ArrayList(); |
77 |
|
78 |
class ProjectsContentProvider implements IStructuredContentProvider { |
79 |
|
80 |
/* (non-Javadoc) |
81 |
* @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object) |
82 |
*/ |
83 |
public Object[] getElements(Object inputElement) { |
84 |
return ((IWorkspace)inputElement).getRoot().getProjects(); |
85 |
} |
86 |
|
87 |
/* (non-Javadoc) |
88 |
* @see org.eclipse.jface.viewers.IContentProvider#dispose() |
89 |
*/ |
90 |
public void dispose() { |
91 |
} |
92 |
|
93 |
/* (non-Javadoc) |
94 |
* @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object) |
95 |
*/ |
96 |
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
97 |
} |
98 |
|
99 |
} |
100 |
/** |
101 |
* @see org.eclipse.debug.ui.ILaunchConfigurationTab#createControl(org.eclipse.swt.widgets.Composite) |
102 |
*/ |
103 |
public void createControl(Composite parent) { |
104 |
Composite mainComposite = new Composite(parent, SWT.NONE); |
105 |
setControl(mainComposite); |
106 |
PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IExternalToolsHelpContextIds.EXTERNAL_TOOLS_LAUNCH_CONFIGURATION_DIALOG_BUILD_TAB); |
107 |
|
108 |
GridLayout layout = new GridLayout(); |
109 |
GridData gd = new GridData(GridData.FILL_HORIZONTAL); |
110 |
mainComposite.setLayout(layout); |
111 |
mainComposite.setLayoutData(gd); |
112 |
mainComposite.setFont(parent.getFont()); |
113 |
|
114 |
fBuildButton = createCheckButton(mainComposite, ExternalToolsLaunchConfigurationMessages.ExternalToolsBuildTab_1); |
115 |
fBuildButton.addSelectionListener(new SelectionAdapter() { |
116 |
public void widgetSelected(SelectionEvent e) { |
117 |
updateEnabledState(); |
118 |
updateLaunchConfigurationDialog(); |
119 |
} |
120 |
}); |
121 |
|
122 |
fGroup = new Group(mainComposite, SWT.NONE); |
123 |
fGroup.setFont(mainComposite.getFont()); |
124 |
layout = new GridLayout(); |
125 |
layout.numColumns = 2; |
126 |
layout.makeColumnsEqualWidth = false; |
127 |
fGroup.setLayout(layout); |
128 |
gd = new GridData(GridData.FILL_HORIZONTAL); |
129 |
gd.horizontalSpan = 2; |
130 |
fGroup.setLayoutData(gd); |
131 |
|
132 |
SelectionAdapter adapter = new SelectionAdapter() { |
133 |
public void widgetSelected(SelectionEvent e) { |
134 |
if (((Button)e.getSource()).getSelection()) { |
135 |
updateEnabledState(); |
136 |
updateLaunchConfigurationDialog(); |
137 |
} |
138 |
} |
139 |
}; |
140 |
|
141 |
fWorkspaceButton = createRadioButton(fGroup, ExternalToolsLaunchConfigurationMessages.ExternalToolsBuildTab_2); |
142 |
gd = new GridData(GridData.FILL_HORIZONTAL); |
143 |
gd.horizontalSpan = 2; |
144 |
fWorkspaceButton.setLayoutData(gd); |
145 |
fWorkspaceButton.addSelectionListener(adapter); |
146 |
|
147 |
fProjectButton = createRadioButton(fGroup, ExternalToolsLaunchConfigurationMessages.ExternalToolsBuildTab_3); |
148 |
gd = new GridData(GridData.FILL_HORIZONTAL); |
149 |
gd.horizontalSpan = 2; |
150 |
fProjectButton.setLayoutData(gd); |
151 |
fProjectButton.addSelectionListener(adapter); |
152 |
|
153 |
fSpecificProjectsButton = createRadioButton(fGroup, ExternalToolsLaunchConfigurationMessages.ExternalToolsBuildTab_4); |
154 |
gd = new GridData(GridData.FILL_HORIZONTAL); |
155 |
gd.horizontalSpan = 1; |
156 |
fSpecificProjectsButton.setLayoutData(gd); |
157 |
fSpecificProjectsButton.addSelectionListener(adapter); |
158 |
|
159 |
fSelectButton = createPushButton(fGroup, ExternalToolsLaunchConfigurationMessages.ExternalToolsBuildTab_5, null); |
160 |
gd = (GridData)fSelectButton.getLayoutData(); |
161 |
gd.horizontalAlignment = GridData.HORIZONTAL_ALIGN_END; |
162 |
fSelectButton.addSelectionListener(new SelectionAdapter() { |
163 |
public void widgetSelected(SelectionEvent e) { |
164 |
selectResources(); |
165 |
} |
166 |
}); |
167 |
|
168 |
createVerticalSpacer(mainComposite, 1); |
169 |
fReferencedProjects = createCheckButton(mainComposite, ExternalToolsLaunchConfigurationMessages.ExternalToolsBuildTab_6); |
170 |
} |
171 |
|
172 |
/** |
173 |
* Prompts the user to select the projects to build. |
174 |
*/ |
175 |
private void selectResources() { |
176 |
ListSelectionDialog dialog = new ListSelectionDialog(getShell(), ResourcesPlugin.getWorkspace(), new ProjectsContentProvider(), new WorkbenchLabelProvider(), ExternalToolsLaunchConfigurationMessages.ExternalToolsBuildTab_7); |
177 |
dialog.setInitialElementSelections(fProjects); |
178 |
if (dialog.open() == Window.CANCEL) { |
179 |
return; |
180 |
} |
181 |
Object[] res = dialog.getResult(); |
182 |
fProjects = new ArrayList(res.length); |
183 |
for (int i = 0; i < res.length; i++) { |
184 |
fProjects.add(res[i]); |
185 |
} |
186 |
updateLaunchConfigurationDialog(); |
187 |
} |
188 |
|
189 |
/** |
190 |
* @see org.eclipse.debug.ui.ILaunchConfigurationTab#setDefaults(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy) |
191 |
*/ |
192 |
public void setDefaults(ILaunchConfigurationWorkingCopy configuration) { |
193 |
} |
194 |
|
195 |
/** |
196 |
* @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration) |
197 |
*/ |
198 |
public void initializeFrom(ILaunchConfiguration configuration) { |
199 |
updateScope(configuration); |
200 |
updateReferencedProjects(configuration); |
201 |
updateEnabledState(); |
202 |
} |
203 |
|
204 |
private void updateReferencedProjects(ILaunchConfiguration configuration) { |
205 |
boolean ref = false; |
206 |
try { |
207 |
ref = configuration.getAttribute(getAttrIncludeReferencedProjectsId(), true); |
208 |
} catch (CoreException e) { |
209 |
ExternalToolsPlugin.getDefault().log("Exception reading launch configuration", e); //$NON-NLS-1$ |
210 |
} |
211 |
fReferencedProjects.setSelection(ref); |
212 |
} |
213 |
|
214 |
/** |
215 |
* Updates the tab to display the build scope specified by the launch config |
216 |
*/ |
217 |
private void updateScope(ILaunchConfiguration configuration) { |
218 |
String scope = null; |
219 |
try { |
220 |
scope= configuration.getAttribute(getAttrBuildScopeId(), (String)null); |
221 |
} catch (CoreException ce) { |
222 |
ExternalToolsPlugin.getDefault().log("Exception reading launch configuration", ce); //$NON-NLS-1$ |
223 |
} |
224 |
fBuildButton.setSelection(scope != null); |
225 |
fWorkspaceButton.setSelection(false); |
226 |
fProjectButton.setSelection(false); |
227 |
fSpecificProjectsButton.setSelection(false); |
228 |
fProjects.clear(); |
229 |
if (scope == null) { |
230 |
// select the workspace by default |
231 |
fBuildButton.setSelection(true); |
232 |
fWorkspaceButton.setSelection(true); |
233 |
} else { |
234 |
if (scope.equals("${none}")) { //$NON-NLS-1$ |
235 |
fBuildButton.setSelection(false); |
236 |
} else if (scope.equals("${project}")) { //$NON-NLS-1$ |
237 |
fProjectButton.setSelection(true); |
238 |
} else if (scope.startsWith("${projects:")) { //$NON-NLS-1$ |
239 |
fSpecificProjectsButton.setSelection(true); |
240 |
IProject[] projects = getBuildProjects(configuration, getAttrBuildScopeId()); |
241 |
fProjects = new ArrayList(projects.length); |
242 |
for (int i = 0; i < projects.length; i++) { |
243 |
fProjects.add(projects[i]); |
244 |
} |
245 |
} |
246 |
} |
247 |
} |
248 |
/** |
249 |
* @see org.eclipse.debug.ui.ILaunchConfigurationTab#performApply(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy) |
250 |
*/ |
251 |
public void performApply(ILaunchConfigurationWorkingCopy configuration) { |
252 |
String scope = generateScopeMemento(); |
253 |
configuration.setAttribute(getAttrBuildScopeId(), scope); |
254 |
if (fReferencedProjects.getSelection()) { |
255 |
// default is true |
256 |
configuration.setAttribute(getAttrIncludeReferencedProjectsId(), (String)null); |
257 |
} else { |
258 |
configuration.setAttribute(getAttrIncludeReferencedProjectsId(), false); |
259 |
} |
260 |
} |
261 |
|
262 |
/** |
263 |
* Generates a memento for the build scope. |
264 |
*/ |
265 |
private String generateScopeMemento() { |
266 |
if (fBuildButton.getSelection()) { |
267 |
if (fWorkspaceButton.getSelection()) { |
268 |
return null; |
269 |
} |
270 |
if (fProjectButton.getSelection()) { |
271 |
return "${project}"; //$NON-NLS-1$ |
272 |
} |
273 |
if (fSpecificProjectsButton.getSelection()) { |
274 |
return getBuildScopeAttribute(fProjects); |
275 |
} |
276 |
return null; |
277 |
|
278 |
} |
279 |
return "${none}"; //$NON-NLS-1$ |
280 |
} |
281 |
|
282 |
/** |
283 |
* @see org.eclipse.debug.ui.ILaunchConfigurationTab#getName() |
284 |
*/ |
285 |
public String getName() { |
286 |
return ExternalToolsLaunchConfigurationMessages.ExternalToolsBuildTab_8; |
287 |
} |
288 |
|
289 |
/** |
290 |
* Updates the enablement state of the fields. |
291 |
*/ |
292 |
private void updateEnabledState() { |
293 |
boolean enabled= fBuildButton.getSelection(); |
294 |
fGroup.setEnabled(enabled); |
295 |
fWorkspaceButton.setEnabled(enabled); |
296 |
fProjectButton.setEnabled(enabled); |
297 |
fSpecificProjectsButton.setEnabled(enabled); |
298 |
fSelectButton.setEnabled(enabled && fSpecificProjectsButton.getSelection()); |
299 |
if (!enabled) { |
300 |
super.setErrorMessage(null); |
301 |
} |
302 |
if (enabled) { |
303 |
if (!fWorkspaceButton.getSelection() && !fProjectButton.getSelection() && |
304 |
!fSpecificProjectsButton.getSelection()) { |
305 |
fWorkspaceButton.setSelection(true); |
306 |
} |
307 |
} |
308 |
fReferencedProjects.setEnabled(fBuildButton.getSelection() && (fProjectButton.getSelection() || fSpecificProjectsButton.getSelection())); |
309 |
} |
310 |
|
311 |
/** |
312 |
* @see org.eclipse.debug.ui.ILaunchConfigurationTab#getImage() |
313 |
*/ |
314 |
public Image getImage() { |
315 |
return ExternalToolsImages.getImage(IExternalToolConstants.IMG_TAB_BUILD); |
316 |
} |
317 |
|
318 |
public boolean isValid(ILaunchConfiguration launchConfig) { |
319 |
setErrorMessage(null); |
320 |
setMessage(null); |
321 |
if (fBuildButton.getSelection() && fSpecificProjectsButton.getSelection() && fProjects.isEmpty()) { |
322 |
setErrorMessage(ExternalToolsLaunchConfigurationMessages.ExternalToolsBuildTab_9); |
323 |
return false; |
324 |
} |
325 |
return true; |
326 |
} |
327 |
|
328 |
/** |
329 |
* Returns a collection of projects referenced by a build scope attribute. |
330 |
* |
331 |
* @return collection of projects referred to by configuration |
332 |
*/ |
333 |
public static IProject[] getBuildProjects(ILaunchConfiguration configuration, String buildScopeId) { |
334 |
String scope = null; |
335 |
String id = buildScopeId ; |
336 |
if (id == null) |
337 |
id = IExternalToolConstants.ATTR_BUILD_SCOPE ; |
338 |
try { |
339 |
scope = configuration.getAttribute(id, (String)null); |
340 |
} catch (CoreException e) { |
341 |
return null; |
342 |
} |
343 |
if (scope == null) { |
344 |
return null; |
345 |
} |
346 |
if (scope.startsWith("${projects:")) { //$NON-NLS-1$ |
347 |
String pathString = scope.substring(11, scope.length() - 1); |
348 |
if (pathString.length() > 1) { |
349 |
String[] names = pathString.split(","); //$NON-NLS-1$ |
350 |
IProject[] projects = new IProject[names.length]; |
351 |
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); |
352 |
for (int i = 0; i < names.length; i++) { |
353 |
projects[i] = root.getProject(names[i]); |
354 |
} |
355 |
return projects; |
356 |
} |
357 |
} else if (scope.equals("${project}")) { //$NON-NLS-1$ |
358 |
IResource resource = DebugUITools.getSelectedResource(); |
359 |
if (resource != null) { |
360 |
return new IProject[]{resource.getProject()}; |
361 |
} |
362 |
} |
363 |
return new IProject[0]; |
364 |
} |
365 |
|
366 |
/** |
367 |
* Whether referenced projects should be considered when building. Only valid |
368 |
* when a set of projects is to be built. |
369 |
* |
370 |
* @param configuration |
371 |
* @return whether referenced projects should be considerd when building |
372 |
* @throws CoreException if unable to access the associated attribute |
373 |
*/ |
374 |
public static boolean isIncludeReferencedProjects(ILaunchConfiguration configuration, String includeReferencedProjectsId) throws CoreException { |
375 |
String id = includeReferencedProjectsId; |
376 |
if (id == null) |
377 |
id = IExternalToolConstants.ATTR_INCLUDE_REFERENCED_PROJECTS ; |
378 |
return configuration.getAttribute(id, true); |
379 |
} |
380 |
|
381 |
/** |
382 |
* Creates and returns a memento for the given project set, to be used as a |
383 |
* build scope attribute. |
384 |
* |
385 |
* @param projects list of projects |
386 |
* @return an equivalent refresh attribute |
387 |
*/ |
388 |
public static String getBuildScopeAttribute(List projects) { |
389 |
StringBuffer buf = new StringBuffer(); |
390 |
buf.append("${projects:"); //$NON-NLS-1$ |
391 |
Iterator iterator = projects.iterator(); |
392 |
while (iterator.hasNext()) { |
393 |
IProject project = (IProject) iterator.next(); |
394 |
buf.append(project.getName()); |
395 |
if (iterator.hasNext()) { |
396 |
buf.append(","); //$NON-NLS-1$ |
397 |
} |
398 |
} |
399 |
buf.append("}"); //$NON-NLS-1$ |
400 |
return buf.toString(); |
401 |
} |
402 |
|
403 |
/* (non-Javadoc) |
404 |
* @see org.eclipse.debug.ui.ILaunchConfigurationTab#activated(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy) |
405 |
*/ |
406 |
public void activated(ILaunchConfigurationWorkingCopy workingCopy) { |
407 |
// do nothing on activation |
408 |
} |
409 |
|
410 |
/* (non-Javadoc) |
411 |
* @see org.eclipse.debug.ui.ILaunchConfigurationTab#deactivated(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy) |
412 |
*/ |
413 |
public void deactivated(ILaunchConfigurationWorkingCopy workingCopy) { |
414 |
// do nothing on deactivation |
415 |
} |
416 |
|
417 |
/** |
418 |
* For backward compatibility - overridden in AntBuildTab |
419 |
* @return id for referenced projects |
420 |
*/ |
421 |
protected String getAttrIncludeReferencedProjectsId() { |
422 |
return IExternalToolConstants.ATTR_INCLUDE_REFERENCED_PROJECTS; |
423 |
} |
424 |
|
425 |
/** |
426 |
* For backward compatibility - overridden in AntBuildTab |
427 |
* @return id for build scope |
428 |
*/ |
429 |
protected String getAttrBuildScopeId() { |
430 |
return IExternalToolConstants.ATTR_BUILD_SCOPE; |
431 |
} |
432 |
|
433 |
} |