KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > wizards > NewTestSuiteCreationWizard


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.jdt.internal.junit.wizards;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.NullProgressMonitor;
17
18 import org.eclipse.core.resources.IResource;
19
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.operation.IRunnableWithProgress;
22
23 import org.eclipse.ui.IEditorPart;
24
25 import org.eclipse.jdt.core.IBuffer;
26 import org.eclipse.jdt.core.ICompilationUnit;
27 import org.eclipse.jdt.core.IMethod;
28 import org.eclipse.jdt.core.IPackageFragment;
29 import org.eclipse.jdt.core.ISourceRange;
30 import org.eclipse.jdt.core.IType;
31 import org.eclipse.jdt.core.JavaModelException;
32
33 import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
34
35 import org.eclipse.jdt.junit.wizards.NewTestSuiteWizardPage;
36
37 import org.eclipse.jdt.internal.junit.Messages;
38 import org.eclipse.jdt.internal.junit.ui.JUnitPlugin;
39
40 /**
41  * A wizard for creating test suites.
42  */

43 public class NewTestSuiteCreationWizard extends JUnitWizard {
44
45     private NewTestSuiteWizardPage fPage;
46     
47     public NewTestSuiteCreationWizard() {
48         super();
49         setWindowTitle(WizardMessages.Wizard_title_new_testsuite);
50         initDialogSettings();
51     }
52
53     /*
54      * @see Wizard#createPages
55      */

56     public void addPages() {
57         super.addPages();
58         fPage= new NewTestSuiteWizardPage();
59         addPage(fPage);
60         fPage.init(getSelection());
61     }
62
63     /*
64      * @see Wizard#performFinish
65      */

66     public boolean performFinish() {
67         IPackageFragment pack= fPage.getPackageFragment();
68         String JavaDoc filename= fPage.getTypeName() + ".java"; //$NON-NLS-1$
69
ICompilationUnit cu= pack.getCompilationUnit(filename);
70         if (cu.exists()) {
71             IEditorPart cu_ep= EditorUtility.isOpenInEditor(cu);
72             if (cu_ep != null && cu_ep.isDirty()) {
73                 boolean saveUnsavedChanges=
74                     MessageDialog.openQuestion(fPage.getShell(),
75                         WizardMessages.NewTestSuiteWiz_unsavedchangesDialog_title,
76                         Messages.format(WizardMessages.NewTestSuiteWiz_unsavedchangesDialog_message,
77                         filename));
78                 if (saveUnsavedChanges) {
79                     try {
80                         getContainer().run(false, false, getRunnableSave(cu_ep));
81                     } catch (Exception JavaDoc e) {
82                         JUnitPlugin.log(e);
83                     }
84                 }
85             }
86             IType suiteType= cu.getType(fPage.getTypeName());
87             IMethod suiteMethod= suiteType.getMethod("suite", new String JavaDoc[] {}); //$NON-NLS-1$
88
if (suiteMethod.exists()) {
89                 try {
90                 ISourceRange range= suiteMethod.getSourceRange();
91                 IBuffer buf= cu.getBuffer();
92                 String JavaDoc originalContent= buf.getText(range.getOffset(), range.getLength());
93                 if (UpdateTestSuite.getTestSuiteClassListRange(originalContent) == null) {
94                     cannotUpdateSuiteError();
95                     return false;
96                 }
97                 } catch (JavaModelException e) {
98                     JUnitPlugin.log(e);
99                     return false;
100                 }
101             }
102         }
103         
104         if (finishPage(fPage.getRunnable())) {
105             if (!fPage.hasUpdatedExistingClass())
106                 postCreatingType();
107             return true;
108         }
109
110         return false;
111     }
112     
113     private void cannotUpdateSuiteError() {
114         MessageDialog.openError(getShell(), WizardMessages.NewTestSuiteWizPage_cannotUpdateDialog_title,
115             Messages.format(WizardMessages.NewTestSuiteWizPage_cannotUpdateDialog_message, new String JavaDoc[] { NewTestSuiteWizardPage.START_MARKER, NewTestSuiteWizardPage.END_MARKER}));
116
117     }
118
119     protected void postCreatingType() {
120         IType newClass= fPage.getCreatedType();
121         if (newClass == null)
122             return;
123         ICompilationUnit cu= newClass.getCompilationUnit();
124         IResource resource= cu.getResource();
125         if (resource != null) {
126             selectAndReveal(resource);
127             openResource(resource);
128         }
129     }
130
131     public NewTestSuiteWizardPage getPage() {
132         return fPage;
133     }
134     
135     protected void initializeDefaultPageImageDescriptor() {
136         setDefaultPageImageDescriptor(JUnitPlugin.getImageDescriptor("wizban/newtest_wiz.png")); //$NON-NLS-1$
137
}
138
139     public IRunnableWithProgress getRunnableSave(final IEditorPart cu_ep) {
140         return new IRunnableWithProgress() {
141             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
142                     if (monitor == null) {
143                         monitor= new NullProgressMonitor();
144                     }
145                     cu_ep.doSave(monitor);
146             }
147         };
148     }
149 }
150
Popular Tags