KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > junit > launcher > JUnitLaunchConfigurationTab


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  * Sebastian Davids: sdavids@gmx.de bug: 26293, 27889
11  * David Saff (saff@mit.edu) - bug 102632: [JUnit] Support for JUnit 4.
12  *******************************************************************************/

13 package org.eclipse.jdt.junit.launcher;
14
15 import java.lang.reflect.InvocationTargetException JavaDoc;
16 import java.util.ArrayList JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Path;
22
23 import org.eclipse.core.resources.IProject;
24 import org.eclipse.core.resources.IResource;
25 import org.eclipse.core.resources.IWorkspaceRoot;
26 import org.eclipse.core.resources.ResourcesPlugin;
27
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.ModifyEvent;
30 import org.eclipse.swt.events.ModifyListener;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.events.SelectionListener;
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.Label;
40 import org.eclipse.swt.widgets.Shell;
41 import org.eclipse.swt.widgets.Text;
42
43 import org.eclipse.jface.dialogs.Dialog;
44 import org.eclipse.jface.viewers.ArrayContentProvider;
45 import org.eclipse.jface.viewers.ComboViewer;
46 import org.eclipse.jface.viewers.ILabelProvider;
47 import org.eclipse.jface.viewers.ISelection;
48 import org.eclipse.jface.viewers.ISelectionChangedListener;
49 import org.eclipse.jface.viewers.IStructuredSelection;
50 import org.eclipse.jface.viewers.LabelProvider;
51 import org.eclipse.jface.viewers.SelectionChangedEvent;
52 import org.eclipse.jface.viewers.StructuredSelection;
53 import org.eclipse.jface.viewers.Viewer;
54 import org.eclipse.jface.viewers.ViewerFilter;
55 import org.eclipse.jface.window.Window;
56
57 import org.eclipse.ui.IEditorInput;
58 import org.eclipse.ui.IEditorPart;
59 import org.eclipse.ui.IWorkbenchPage;
60 import org.eclipse.ui.IWorkbenchWindow;
61 import org.eclipse.ui.PlatformUI;
62 import org.eclipse.ui.dialogs.ElementListSelectionDialog;
63 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
64 import org.eclipse.ui.dialogs.SelectionDialog;
65
66 import org.eclipse.debug.core.ILaunchConfiguration;
67 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
68
69 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
70
71 import org.eclipse.jdt.core.IJavaElement;
72 import org.eclipse.jdt.core.IJavaModel;
73 import org.eclipse.jdt.core.IJavaProject;
74 import org.eclipse.jdt.core.IPackageFragment;
75 import org.eclipse.jdt.core.IPackageFragmentRoot;
76 import org.eclipse.jdt.core.ISourceReference;
77 import org.eclipse.jdt.core.IType;
78 import org.eclipse.jdt.core.JavaCore;
79 import org.eclipse.jdt.core.JavaModelException;
80
81 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
82
83 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
84
85 import org.eclipse.jdt.ui.JavaElementComparator;
86 import org.eclipse.jdt.ui.JavaElementLabelProvider;
87 import org.eclipse.jdt.ui.StandardJavaElementContentProvider;
88
89 import org.eclipse.jdt.internal.ui.wizards.TypedElementSelectionValidator;
90 import org.eclipse.jdt.internal.ui.wizards.TypedViewerFilter;
91
92 import org.eclipse.jdt.internal.junit.Messages;
93 import org.eclipse.jdt.internal.junit.launcher.ITestKind;
94 import org.eclipse.jdt.internal.junit.launcher.JUnitLaunchConfigurationConstants;
95 import org.eclipse.jdt.internal.junit.launcher.JUnitMigrationDelegate;
96 import org.eclipse.jdt.internal.junit.launcher.TestKind;
97 import org.eclipse.jdt.internal.junit.launcher.TestKindRegistry;
98 import org.eclipse.jdt.internal.junit.launcher.TestSelectionDialog;
99 import org.eclipse.jdt.internal.junit.ui.IJUnitHelpContextIds;
100 import org.eclipse.jdt.internal.junit.ui.JUnitMessages;
101 import org.eclipse.jdt.internal.junit.ui.JUnitPlugin;
102 import org.eclipse.jdt.internal.junit.util.LayoutUtil;
103 import org.eclipse.jdt.internal.junit.util.TestSearchEngine;
104
105 /**
106  * The launch configuration tab for JUnit.
107  * <p>
108  * This class may be instantiated but is not intended to be subclassed.
109  * </p>
110  * @since 3.3
111  */

112 public class JUnitLaunchConfigurationTab extends AbstractLaunchConfigurationTab {
113     
114     // Project UI widgets
115
private Label fProjLabel;
116     private Text fProjText;
117     private Button fProjButton;
118     private Button fKeepRunning;
119     
120     // Test class UI widgets
121
private Text fTestText;
122     private Button fSearchButton;
123     private final Image fTestIcon= createImage("obj16/test.gif"); //$NON-NLS-1$
124
private String JavaDoc fOriginalTestMethodName;
125     private Label fTestMethodLabel;
126     private Text fContainerText;
127     private IJavaElement fContainerElement;
128     private final ILabelProvider fJavaElementLabelProvider= new JavaElementLabelProvider();
129
130     private Button fContainerSearchButton;
131     private Button fTestContainerRadioButton;
132     private Button fTestRadioButton;
133     private Label fTestLabel;
134     
135     private ComboViewer fTestLoaderViewer;
136     
137     /**
138      * Creates a JUnit launch configuration tab.
139      */

140     public JUnitLaunchConfigurationTab() {
141     }
142     
143
144     /* (non-Javadoc)
145      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#createControl(org.eclipse.swt.widgets.Composite)
146      */

147     public void createControl(Composite parent) {
148         Composite comp = new Composite(parent, SWT.NONE);
149         setControl(comp);
150
151         GridLayout topLayout = new GridLayout();
152         topLayout.numColumns= 3;
153         comp.setLayout(topLayout);
154         
155         createSingleTestSection(comp);
156         createTestContainerSelectionGroup(comp);
157         
158         createSpacer(comp);
159         
160         createTestLoaderGroup(comp);
161
162         createSpacer(comp);
163         
164         createKeepAliveGroup(comp);
165         Dialog.applyDialogFont(comp);
166         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJUnitHelpContextIds.LAUNCH_CONFIGURATION_DIALOG_JUNIT_MAIN_TAB);
167         validatePage();
168     }
169
170     private void createTestLoaderGroup(Composite comp) {
171         Label loaderLabel= new Label(comp, SWT.NONE);
172         loaderLabel.setText(JUnitMessages.JUnitLaunchConfigurationTab_Test_Loader);
173         GridData gd= new GridData();
174         gd.horizontalIndent= 0;
175         loaderLabel.setLayoutData(gd);
176
177         fTestLoaderViewer= new ComboViewer(comp, SWT.DROP_DOWN | SWT.READ_ONLY);
178         fTestLoaderViewer.getCombo().setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
179
180         ArrayList JavaDoc/*<TestKind>*/ items= TestKindRegistry.getDefault().getAllKinds();
181         fTestLoaderViewer.setContentProvider(new ArrayContentProvider());
182         fTestLoaderViewer.setLabelProvider(new LabelProvider() {
183             public String JavaDoc getText(Object JavaDoc element) {
184                 return ((TestKind) element).getDisplayName();
185             }
186         });
187         fTestLoaderViewer.setInput(items);
188         fTestLoaderViewer.addSelectionChangedListener(new ISelectionChangedListener() {
189             public void selectionChanged(SelectionChangedEvent event) {
190                 validatePage();
191                 updateLaunchConfigurationDialog();
192             }
193         });
194     }
195
196     private void createSpacer(Composite comp) {
197         Label label= new Label(comp, SWT.NONE);
198         GridData gd= new GridData();
199         gd.horizontalSpan= 3;
200         label.setLayoutData(gd);
201     }
202     
203     private void createSingleTestSection(Composite comp) {
204         fTestRadioButton= new Button(comp, SWT.RADIO);
205         fTestRadioButton.setText(JUnitMessages.JUnitLaunchConfigurationTab_label_oneTest);
206         GridData gd = new GridData();
207         gd.horizontalSpan = 3;
208         fTestRadioButton.setLayoutData(gd);
209         fTestRadioButton.addSelectionListener(new SelectionAdapter() {
210             public void widgetSelected(SelectionEvent e) {
211                 if (fTestRadioButton.getSelection())
212                     testModeChanged();
213             }
214         });
215         
216         fProjLabel = new Label(comp, SWT.NONE);
217         fProjLabel.setText(JUnitMessages.JUnitLaunchConfigurationTab_label_project);
218         gd= new GridData();
219         gd.horizontalIndent = 25;
220         fProjLabel.setLayoutData(gd);
221         
222         fProjText= new Text(comp, SWT.SINGLE | SWT.BORDER);
223         fProjText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
224         fProjText.addModifyListener(new ModifyListener() {
225             public void modifyText(ModifyEvent evt) {
226                 validatePage();
227                 updateLaunchConfigurationDialog();
228                 fSearchButton.setEnabled(fTestRadioButton.getSelection() && fProjText.getText().length() > 0);
229             }
230         });
231             
232         fProjButton = new Button(comp, SWT.PUSH);
233         fProjButton.setText(JUnitMessages.JUnitLaunchConfigurationTab_label_browse);
234         fProjButton.addSelectionListener(new SelectionAdapter() {
235             public void widgetSelected(SelectionEvent evt) {
236                 handleProjectButtonSelected();
237             }
238         });
239         setButtonGridData(fProjButton);
240         
241         fTestLabel = new Label(comp, SWT.NONE);
242         gd = new GridData();
243         gd.horizontalIndent = 25;
244         fTestLabel.setLayoutData(gd);
245         fTestLabel.setText(JUnitMessages.JUnitLaunchConfigurationTab_label_test);
246         
247     
248         fTestText = new Text(comp, SWT.SINGLE | SWT.BORDER);
249         fTestText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
250         fTestText.addModifyListener(new ModifyListener() {
251             public void modifyText(ModifyEvent evt) {
252                 validatePage();
253                 updateLaunchConfigurationDialog();
254             }
255         });
256         
257         fSearchButton = new Button(comp, SWT.PUSH);
258         fSearchButton.setEnabled(fProjText.getText().length() > 0);
259         fSearchButton.setText(JUnitMessages.JUnitLaunchConfigurationTab_label_search);
260         fSearchButton.addSelectionListener(new SelectionAdapter() {
261             public void widgetSelected(SelectionEvent evt) {
262                 handleSearchButtonSelected();
263             }
264         });
265         setButtonGridData(fSearchButton);
266         
267         new Label(comp, SWT.NONE);
268         
269         fTestMethodLabel= new Label(comp, SWT.NONE);
270         fTestMethodLabel.setText(""); //$NON-NLS-1$
271
gd= new GridData();
272         gd.horizontalSpan = 2;
273         fTestMethodLabel.setLayoutData(gd);
274         
275     }
276
277     private void createTestContainerSelectionGroup(Composite comp) {
278         fTestContainerRadioButton= new Button(comp, SWT.RADIO);
279         fTestContainerRadioButton.setText(JUnitMessages.JUnitLaunchConfigurationTab_label_containerTest);
280         GridData gd = new GridData();
281         gd.horizontalSpan = 3;
282         fTestContainerRadioButton.setLayoutData(gd);
283         fTestContainerRadioButton.addSelectionListener(new SelectionListener() {
284             public void widgetSelected(SelectionEvent e) {
285                 if (fTestContainerRadioButton.getSelection())
286                     testModeChanged();
287             }
288             public void widgetDefaultSelected(SelectionEvent e) {
289             }
290         });
291         
292         fContainerText = new Text(comp, SWT.SINGLE | SWT.BORDER | SWT.READ_ONLY);
293         gd = new GridData(GridData.FILL_HORIZONTAL);
294         gd.horizontalIndent= 25;
295         gd.horizontalSpan = 2;
296         fContainerText.setLayoutData(gd);
297         fContainerText.addModifyListener(new ModifyListener() {
298             public void modifyText(ModifyEvent evt) {
299                 updateLaunchConfigurationDialog();
300             }
301         });
302         
303         fContainerSearchButton = new Button(comp, SWT.PUSH);
304         fContainerSearchButton.setText(JUnitMessages.JUnitLaunchConfigurationTab_label_search);
305         fContainerSearchButton.addSelectionListener(new SelectionAdapter() {
306             public void widgetSelected(SelectionEvent evt) {
307                 handleContainerSearchButtonSelected();
308             }
309         });
310         setButtonGridData(fContainerSearchButton);
311     }
312
313     private void handleContainerSearchButtonSelected() {
314         IJavaElement javaElement= chooseContainer(fContainerElement);
315         if (javaElement != null)
316             setContainerElement(javaElement);
317     }
318
319     private void setContainerElement(IJavaElement javaElement) {
320         fContainerElement= javaElement;
321         fContainerText.setText(getPresentationName(javaElement));
322         validatePage();
323         updateLaunchConfigurationDialog();
324     }
325
326     private void createKeepAliveGroup(Composite comp) {
327         GridData gd;
328         fKeepRunning = new Button(comp, SWT.CHECK);
329         fKeepRunning.addSelectionListener(new SelectionListener() {
330             public void widgetSelected(SelectionEvent e) {
331                 updateLaunchConfigurationDialog();
332             }
333
334             public void widgetDefaultSelected(SelectionEvent e) {
335             }
336         });
337         fKeepRunning.setText(JUnitMessages.JUnitLaunchConfigurationTab_label_keeprunning);
338         gd= new GridData();
339         gd.horizontalAlignment= GridData.FILL;
340         gd.horizontalSpan= 2;
341         fKeepRunning.setLayoutData(gd);
342     }
343     
344     private static Image createImage(String JavaDoc path) {
345         return JUnitPlugin.getImageDescriptor(path).createImage();
346     }
347
348     /* (non-Javadoc)
349      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration)
350      */

351     public void initializeFrom(ILaunchConfiguration config) {
352         updateProjectFromConfig(config);
353         String JavaDoc containerHandle= ""; //$NON-NLS-1$
354
try {
355             containerHandle = config.getAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER, ""); //$NON-NLS-1$
356
} catch (CoreException ce) {
357         }
358         
359         if (containerHandle.length() > 0)
360             updateTestContainerFromConfig(config);
361         else
362             updateTestTypeFromConfig(config);
363         updateKeepRunning(config);
364         updateTestLoaderFromConfig(config);
365     }
366
367
368     private void updateTestLoaderFromConfig(ILaunchConfiguration config) {
369         ITestKind testKind= JUnitLaunchConfigurationConstants.getTestRunnerKind(config);
370         if (testKind.isNull())
371             testKind= TestKindRegistry.getDefault().getKind(TestKindRegistry.JUNIT3_TEST_KIND_ID);
372         fTestLoaderViewer.setSelection(new StructuredSelection(testKind));
373     }
374     
375     private TestKind getSelectedTestKind() {
376         IStructuredSelection selection= (IStructuredSelection) fTestLoaderViewer.getSelection();
377         return (TestKind) selection.getFirstElement();
378     }
379     
380     private void updateKeepRunning(ILaunchConfiguration config) {
381         boolean running= false;
382         try {
383             running= config.getAttribute(JUnitLaunchConfigurationConstants.ATTR_KEEPRUNNING, false);
384         } catch (CoreException ce) {
385         }
386         fKeepRunning.setSelection(running);
387     }
388     
389     private void updateProjectFromConfig(ILaunchConfiguration config) {
390         String JavaDoc projectName= ""; //$NON-NLS-1$
391
try {
392             projectName = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, ""); //$NON-NLS-1$
393
} catch (CoreException ce) {
394         }
395         fProjText.setText(projectName);
396     }
397     
398     private void updateTestTypeFromConfig(ILaunchConfiguration config) {
399         String JavaDoc testTypeName= ""; //$NON-NLS-1$
400
fOriginalTestMethodName= ""; //$NON-NLS-1$
401
try {
402             testTypeName = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, ""); //$NON-NLS-1$
403
fOriginalTestMethodName = config.getAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_METHOD_NAME, ""); //$NON-NLS-1$
404
} catch (CoreException ce) {
405         }
406         fTestRadioButton.setSelection(true);
407         setEnableSingleTestGroup(true);
408         setEnableContainerTestGroup(false);
409         fTestContainerRadioButton.setSelection(false);
410         fTestText.setText(testTypeName);
411         fContainerText.setText(""); //$NON-NLS-1$
412
setTestMethodLabel(fOriginalTestMethodName);
413     }
414
415     private void setTestMethodLabel(String JavaDoc testMethodName) {
416         if (!"".equals(testMethodName)) { //$NON-NLS-1$
417
fTestMethodLabel.setText(JUnitMessages.JUnitLaunchConfigurationTab_label_method+fOriginalTestMethodName);
418         } else {
419             fTestMethodLabel.setText(""); //$NON-NLS-1$
420
}
421     }
422
423     private void updateTestContainerFromConfig(ILaunchConfiguration config) {
424         String JavaDoc containerHandle= ""; //$NON-NLS-1$
425
IJavaElement containerElement = null;
426         try {
427             containerHandle = config.getAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER, ""); //$NON-NLS-1$
428
if (containerHandle.length() > 0) {
429                 containerElement= JavaCore.create(containerHandle);
430             }
431         } catch (CoreException ce) {
432         }
433         if (containerElement != null)
434             fContainerElement = containerElement;
435         fTestContainerRadioButton.setSelection(true);
436         setEnableSingleTestGroup(false);
437         setEnableContainerTestGroup(true);
438         fTestRadioButton.setSelection(false);
439         if (fContainerElement != null)
440             fContainerText.setText(getPresentationName(fContainerElement));
441         fTestText.setText(""); //$NON-NLS-1$
442
}
443     
444     /*
445      * (non-Javadoc)
446      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#performApply(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
447      */

448     public void performApply(ILaunchConfigurationWorkingCopy config) {
449         if (fTestContainerRadioButton.getSelection() && fContainerElement != null) {
450             config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, fContainerElement.getJavaProject().getElementName());
451             config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER, fContainerElement.getHandleIdentifier());
452             config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, ""); //$NON-NLS-1$
453
//workaround for bug 65399
454
config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_METHOD_NAME, ""); //$NON-NLS-1$
455
} else {
456             config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, fProjText.getText());
457             config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, fTestText.getText());
458             config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER, ""); //$NON-NLS-1$
459
config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_METHOD_NAME, fOriginalTestMethodName);
460         }
461         config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_KEEPRUNNING, fKeepRunning.getSelection());
462         try {
463             mapResources(config);
464         } catch (CoreException e) {
465             JUnitPlugin.log(e.getStatus());
466         }
467         IStructuredSelection testKindSelection= (IStructuredSelection) fTestLoaderViewer.getSelection();
468         if (! testKindSelection.isEmpty()) {
469             TestKind testKind= (TestKind) testKindSelection.getFirstElement();
470             config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_RUNNER_KIND, testKind.getId());
471         }
472     }
473     
474     private void mapResources(ILaunchConfigurationWorkingCopy config) throws CoreException {
475         JUnitMigrationDelegate.mapResources(config);
476     }
477
478     
479     /* (non-Javadoc)
480      * @see org.eclipse.debug.ui.AbstractLaunchConfigurationTab#dispose()
481      */

482     public void dispose() {
483         super.dispose();
484         fTestIcon.dispose();
485         fJavaElementLabelProvider.dispose();
486     }
487
488     /* (non-Javadoc)
489      * @see org.eclipse.debug.ui.AbstractLaunchConfigurationTab#getImage()
490      */

491     public Image getImage() {
492         return fTestIcon;
493     }
494
495     /*
496      * Show a dialog that lists all main types
497      */

498     private void handleSearchButtonSelected() {
499         Shell shell = getShell();
500         
501         IJavaProject javaProject = getJavaProject();
502         
503         IType[] types= new IType[0];
504         boolean[] radioSetting= new boolean[2];
505         try {
506             // fix for 66922 Wrong radio behaviour when switching
507
// remember the selected radio button
508
radioSetting[0]= fTestRadioButton.getSelection();
509             radioSetting[1]= fTestContainerRadioButton.getSelection();
510             
511             types= TestSearchEngine.findTests(getLaunchConfigurationDialog(), javaProject, getSelectedTestKind());
512         } catch (InterruptedException JavaDoc e) {
513             setErrorMessage(e.getMessage());
514             return;
515         } catch (InvocationTargetException JavaDoc e) {
516             JUnitPlugin.log(e.getTargetException());
517             return;
518         } finally {
519             fTestRadioButton.setSelection(radioSetting[0]);
520             fTestContainerRadioButton.setSelection(radioSetting[1]);
521         }
522
523         SelectionDialog dialog = new TestSelectionDialog(shell, types);
524         dialog.setTitle(JUnitMessages.JUnitLaunchConfigurationTab_testdialog_title);
525         dialog.setMessage(JUnitMessages.JUnitLaunchConfigurationTab_testdialog_message);
526         if (dialog.open() == Window.CANCEL) {
527             return;
528         }
529         
530         Object JavaDoc[] results = dialog.getResult();
531         if ((results == null) || (results.length < 1)) {
532             return;
533         }
534         IType type = (IType)results[0];
535         
536         if (type != null) {
537             fTestText.setText(type.getFullyQualifiedName('.'));
538             javaProject = type.getJavaProject();
539             fProjText.setText(javaProject.getElementName());
540         }
541     }
542         
543     /*
544      * Show a dialog that lets the user select a project. This in turn provides
545      * context for the main type, allowing the user to key a main type name, or
546      * constraining the search for main types to the specified project.
547      */

548     private void handleProjectButtonSelected() {
549         IJavaProject project = chooseJavaProject();
550         if (project == null) {
551             return;
552         }
553         
554         String JavaDoc projectName = project.getElementName();
555         fProjText.setText(projectName);
556     }
557     
558     /*
559      * Realize a Java Project selection dialog and return the first selected project,
560      * or null if there was none.
561      */

562     private IJavaProject chooseJavaProject() {
563         IJavaProject[] projects;
564         try {
565             projects= JavaCore.create(getWorkspaceRoot()).getJavaProjects();
566         } catch (JavaModelException e) {
567             JUnitPlugin.log(e.getStatus());
568             projects= new IJavaProject[0];
569         }
570         
571         ILabelProvider labelProvider= new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT);
572         ElementListSelectionDialog dialog= new ElementListSelectionDialog(getShell(), labelProvider);
573         dialog.setTitle(JUnitMessages.JUnitLaunchConfigurationTab_projectdialog_title);
574         dialog.setMessage(JUnitMessages.JUnitLaunchConfigurationTab_projectdialog_message);
575         dialog.setElements(projects);
576         
577         IJavaProject javaProject = getJavaProject();
578         if (javaProject != null) {
579             dialog.setInitialSelections(new Object JavaDoc[] { javaProject });
580         }
581         if (dialog.open() == Window.OK) {
582             return (IJavaProject) dialog.getFirstResult();
583         }
584         return null;
585     }
586     
587     /*
588      * Return the IJavaProject corresponding to the project name in the project name
589      * text field, or null if the text does not match a project name.
590      */

591     private IJavaProject getJavaProject() {
592         String JavaDoc projectName = fProjText.getText().trim();
593         if (projectName.length() < 1) {
594             return null;
595         }
596         return getJavaModel().getJavaProject(projectName);
597     }
598     
599     /*
600      * Convenience method to get the workspace root.
601      */

602     private IWorkspaceRoot getWorkspaceRoot() {
603         return ResourcesPlugin.getWorkspace().getRoot();
604     }
605     
606     /*
607      * Convenience method to get access to the java model.
608      */

609     private IJavaModel getJavaModel() {
610         return JavaCore.create(getWorkspaceRoot());
611     }
612
613
614     /* (non-Javadoc)
615      * @see org.eclipse.debug.ui.AbstractLaunchConfigurationTab#isValid(org.eclipse.debug.core.ILaunchConfiguration)
616      */

617     public boolean isValid(ILaunchConfiguration config) {
618         return getErrorMessage() == null;
619     }
620     
621     private void testModeChanged() {
622         boolean isSingleTestMode= fTestRadioButton.getSelection();
623         setEnableSingleTestGroup(isSingleTestMode);
624         setEnableContainerTestGroup(!isSingleTestMode);
625         if (!isSingleTestMode && fContainerText.getText().length() == 0) {
626             String JavaDoc projText= fProjText.getText();
627             if (Path.EMPTY.isValidSegment(projText)) {
628                 IJavaProject javaProject= getJavaModel().getJavaProject(projText);
629                 if (javaProject != null && javaProject.exists())
630                     setContainerElement(javaProject);
631             }
632         }
633         validatePage();
634         updateLaunchConfigurationDialog();
635     }
636
637     private void validatePage() {
638         setErrorMessage(null);
639         setMessage(null);
640
641         if (fTestContainerRadioButton.getSelection()) {
642             if (fContainerElement == null) {
643                 setErrorMessage(JUnitMessages.JUnitLaunchConfigurationTab_error_noContainer);
644                 return;
645             }
646             validateJavaProject(fContainerElement.getJavaProject());
647             return;
648         }
649
650         String JavaDoc projectName= fProjText.getText().trim();
651         if (projectName.length() == 0) {
652             setErrorMessage(JUnitMessages.JUnitLaunchConfigurationTab_error_projectnotdefined);
653             return;
654         }
655
656         IStatus status= ResourcesPlugin.getWorkspace().validatePath(IPath.SEPARATOR + projectName, IResource.PROJECT);
657         if (!status.isOK() || !Path.ROOT.isValidSegment(projectName)) {
658             setErrorMessage(Messages.format(JUnitMessages.JUnitLaunchConfigurationTab_error_invalidProjectName, projectName));
659             return;
660         }
661
662         IProject project= getWorkspaceRoot().getProject(projectName);
663         if (!project.exists()) {
664             setErrorMessage(JUnitMessages.JUnitLaunchConfigurationTab_error_projectnotexists);
665             return;
666         }
667         IJavaProject javaProject= JavaCore.create(project);
668         validateJavaProject(javaProject);
669
670         try {
671             if (!project.hasNature(JavaCore.NATURE_ID)) {
672                 setErrorMessage(JUnitMessages.JUnitLaunchConfigurationTab_error_notJavaProject);
673                 return;
674             }
675             String JavaDoc className= fTestText.getText().trim();
676             if (className.length() == 0) {
677                 setErrorMessage(JUnitMessages.JUnitLaunchConfigurationTab_error_testnotdefined);
678                 return;
679             }
680             if (javaProject.findType(className) == null) {
681                 setErrorMessage(Messages.format(JUnitMessages.JUnitLaunchConfigurationTab_error_test_class_not_found, new String JavaDoc[] { className, projectName }));
682                 return;
683             }
684             
685             
686         } catch (CoreException e) {
687             JUnitPlugin.log(e);
688         }
689
690     }
691
692     private void validateJavaProject(IJavaProject javaProject) {
693         if (! TestSearchEngine.hasTestCaseType(javaProject)) {
694             setErrorMessage(JUnitMessages.JUnitLaunchConfigurationTab_error_testcasenotonpath);
695             return;
696         }
697     }
698
699     private void setEnableContainerTestGroup(boolean enabled) {
700         fContainerSearchButton.setEnabled(enabled);
701         fContainerText.setEnabled(enabled);
702     }
703
704     private void setEnableSingleTestGroup(boolean enabled) {
705         fProjLabel.setEnabled(enabled);
706         fProjText.setEnabled(enabled);
707         fProjButton.setEnabled(enabled);
708         fTestLabel.setEnabled(enabled);
709         fTestText.setEnabled(enabled);
710         fSearchButton.setEnabled(enabled && fProjText.getText().length() > 0);
711         fTestMethodLabel.setEnabled(enabled);
712     }
713
714     /* (non-Javadoc)
715      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#setDefaults(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
716      */

717     public void setDefaults(ILaunchConfigurationWorkingCopy config) {
718         IJavaElement javaElement = getContext();
719         if (javaElement != null) {
720             initializeJavaProject(javaElement, config);
721         } else {
722             // We set empty attributes for project & main type so that when one config is
723
// compared to another, the existence of empty attributes doesn't cause an
724
// incorrect result (the performApply() method can result in empty values
725
// for these attributes being set on a config if there is nothing in the
726
// corresponding text boxes)
727
config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, ""); //$NON-NLS-1$
728
config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER, ""); //$NON-NLS-1$
729
}
730         initializeTestAttributes(javaElement, config);
731     }
732
733     private void initializeTestAttributes(IJavaElement javaElement, ILaunchConfigurationWorkingCopy config) {
734         if (javaElement != null && javaElement.getElementType() < IJavaElement.COMPILATION_UNIT)
735             initializeTestContainer(javaElement, config);
736         else
737             initializeTestType(javaElement, config);
738     }
739
740     private void initializeTestContainer(IJavaElement javaElement, ILaunchConfigurationWorkingCopy config) {
741         config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER, javaElement.getHandleIdentifier());
742         initializeName(config, javaElement.getElementName());
743     }
744
745     private void initializeName(ILaunchConfigurationWorkingCopy config, String JavaDoc name) {
746         if (name == null) {
747             name= ""; //$NON-NLS-1$
748
}
749         if (name.length() > 0) {
750             int index = name.lastIndexOf('.');
751             if (index > 0) {
752                 name = name.substring(index + 1);
753             }
754             name= getLaunchConfigurationDialog().generateName(name);
755             config.rename(name);
756         }
757     }
758
759     /*
760      * Set the main type & name attributes on the working copy based on the IJavaElement
761      */

762     private void initializeTestType(IJavaElement javaElement, ILaunchConfigurationWorkingCopy config) {
763         String JavaDoc name= ""; //$NON-NLS-1$
764
String JavaDoc testKindId= null;
765         try {
766             // we only do a search for compilation units or class files or
767
// or source references
768
if (javaElement instanceof ISourceReference) {
769                 ITestKind testKind= TestKindRegistry.getContainerTestKind(javaElement);
770                 testKindId= testKind.getId();
771
772                 IType[] types = TestSearchEngine.findTests(getLaunchConfigurationDialog(), javaElement, testKind);
773                 if ((types == null) || (types.length < 1)) {
774                     return;
775                 }
776                 // Simply grab the first main type found in the searched element
777
name= JavaModelUtil.getFullyQualifiedName(types[0]);
778                 
779             }
780         } catch (InterruptedException JavaDoc ie) {
781             
782         } catch (InvocationTargetException JavaDoc ite) {
783         }
784         config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, name);
785         if (testKindId != null)
786             config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_RUNNER_KIND, testKindId);
787         initializeName(config, name);
788     }
789     
790     /* (non-Javadoc)
791      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getName()
792      */

793     public String JavaDoc getName() {
794         return JUnitMessages.JUnitLaunchConfigurationTab_tab_label;
795     }
796     
797     private IJavaElement chooseContainer(IJavaElement initElement) {
798         Class JavaDoc[] acceptedClasses= new Class JavaDoc[] { IPackageFragmentRoot.class, IJavaProject.class, IPackageFragment.class };
799         TypedElementSelectionValidator validator= new TypedElementSelectionValidator(acceptedClasses, false) {
800             public boolean isSelectedValid(Object JavaDoc element) {
801                 return true;
802             }
803         };
804         
805         acceptedClasses= new Class JavaDoc[] { IJavaModel.class, IPackageFragmentRoot.class, IJavaProject.class, IPackageFragment.class };
806         ViewerFilter filter= new TypedViewerFilter(acceptedClasses) {
807             public boolean select(Viewer viewer, Object JavaDoc parent, Object JavaDoc element) {
808                 if (element instanceof IPackageFragmentRoot && ((IPackageFragmentRoot)element).isArchive())
809                     return false;
810                 return super.select(viewer, parent, element);
811             }
812         };
813
814         StandardJavaElementContentProvider provider= new StandardJavaElementContentProvider();
815         ILabelProvider labelProvider= new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT);
816         ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(getShell(), labelProvider, provider);
817         dialog.setValidator(validator);
818         dialog.setComparator(new JavaElementComparator());
819         dialog.setTitle(JUnitMessages.JUnitLaunchConfigurationTab_folderdialog_title);
820         dialog.setMessage(JUnitMessages.JUnitLaunchConfigurationTab_folderdialog_message);
821         dialog.addFilter(filter);
822         dialog.setInput(JavaCore.create(getWorkspaceRoot()));
823         dialog.setInitialSelection(initElement);
824         dialog.setAllowMultiple(false);
825         
826         if (dialog.open() == Window.OK) {
827             Object JavaDoc element= dialog.getFirstResult();
828             return (IJavaElement)element;
829         }
830         return null;
831     }
832     
833     private String JavaDoc getPresentationName(IJavaElement element) {
834         return fJavaElementLabelProvider.getText(element);
835     }
836     
837     /*
838      * Returns the current Java element context from which to initialize
839      * default settings, or <code>null</code> if none.
840      *
841      * @return Java element context.
842      */

843     private IJavaElement getContext() {
844         IWorkbenchWindow activeWorkbenchWindow= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
845         if (activeWorkbenchWindow == null) {
846             return null;
847         }
848         IWorkbenchPage page = activeWorkbenchWindow.getActivePage();
849         if (page != null) {
850             ISelection selection = page.getSelection();
851             if (selection instanceof IStructuredSelection) {
852                 IStructuredSelection ss = (IStructuredSelection)selection;
853                 if (!ss.isEmpty()) {
854                     Object JavaDoc obj = ss.getFirstElement();
855                     if (obj instanceof IJavaElement) {
856                         return (IJavaElement)obj;
857                     }
858                     if (obj instanceof IResource) {
859                         IJavaElement je = JavaCore.create((IResource)obj);
860                         if (je == null) {
861                             IProject pro = ((IResource)obj).getProject();
862                             je = JavaCore.create(pro);
863                         }
864                         if (je != null) {
865                             return je;
866                         }
867                     }
868                 }
869             }
870             IEditorPart part = page.getActiveEditor();
871             if (part != null) {
872                 IEditorInput input = part.getEditorInput();
873                 return (IJavaElement) input.getAdapter(IJavaElement.class);
874             }
875         }
876         return null;
877     }
878     
879     private void initializeJavaProject(IJavaElement javaElement, ILaunchConfigurationWorkingCopy config) {
880         IJavaProject javaProject = javaElement.getJavaProject();
881         String JavaDoc name = null;
882         if (javaProject != null && javaProject.exists()) {
883             name = javaProject.getElementName();
884         }
885         config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, name);
886     }
887
888     private void setButtonGridData(Button button) {
889         GridData gridData= new GridData();
890         button.setLayoutData(gridData);
891         LayoutUtil.setButtonDimensionHint(button);
892     }
893 }
894
Popular Tags