KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > junit > wizards > TestSuiteWizardIterator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 2004-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.junit.wizards;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.LinkedList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.NoSuchElementException JavaDoc;
30 import java.util.Set JavaDoc;
31 import javax.swing.event.ChangeEvent JavaDoc;
32 import javax.swing.event.ChangeListener JavaDoc;
33 import org.netbeans.api.java.classpath.ClassPath;
34 import org.netbeans.api.project.Project;
35 import org.netbeans.api.project.SourceGroup;
36 import org.netbeans.modules.junit.CreateTestAction;
37 import org.netbeans.modules.junit.DefaultPlugin;
38 import org.netbeans.modules.junit.GuiUtils;
39 import org.netbeans.modules.junit.JUnitSettings;
40 import org.netbeans.modules.junit.TestCreator;
41 import org.netbeans.modules.junit.TestUtil;
42 import org.netbeans.modules.junit.plugin.JUnitPlugin;
43 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
44 import org.netbeans.spi.java.project.support.ui.templates.JavaTemplates;
45 import org.netbeans.spi.project.ui.templates.support.Templates;
46 import org.openide.DialogDisplayer;
47 import org.openide.NotifyDescriptor;
48 import org.openide.WizardDescriptor;
49 import org.openide.filesystems.FileObject;
50 import org.openide.filesystems.FileUtil;
51 import org.openide.filesystems.Repository;
52 import org.openide.loaders.DataFolder;
53 import org.openide.loaders.DataObject;
54 import org.openide.loaders.DataObjectNotFoundException;
55 import org.openide.loaders.TemplateWizard;
56 import org.openide.util.NbBundle;
57
58 /**
59  */

60 public class TestSuiteWizardIterator
61         implements TemplateWizard.Iterator {
62
63     /** */
64     private static TestSuiteWizardIterator instance;
65
66     /** */
67     private TemplateWizard wizard;
68
69     /** index of step "Name & Location" */
70     private static final int INDEX_TARGET = 2;
71
72     /** name of panel "Name & Location" */
73     private final String JavaDoc nameTarget = NbBundle.getMessage(
74             TestSuiteWizardIterator.class,
75             "LBL_panel_Target"); //NOI18N
76
/** index of the current panel */
77     private int current;
78     /** registered change listeners */
79     private List JavaDoc<ChangeListener JavaDoc> changeListeners;
80     /** panel for choosing name and target location of the test class */
81     private WizardDescriptor.Panel targetPanel;
82     private Project lastSelectedProject = null;
83     /** */
84     private WizardDescriptor.Panel optionsPanel;
85
86     /** */
87     private SourceGroup[] testSrcGroups;
88     
89     /**
90      */

91     public void addChangeListener(ChangeListener JavaDoc l) {
92         if (changeListeners == null) {
93             changeListeners = new ArrayList JavaDoc<ChangeListener JavaDoc>(2);
94         }
95         changeListeners.add(l);
96     }
97
98     /**
99      */

100     public void removeChangeListener(ChangeListener JavaDoc l) {
101         if (changeListeners != null) {
102             changeListeners.remove(l);
103             if (changeListeners.isEmpty()) {
104                 changeListeners = null;
105             }
106         }
107     }
108
109     /**
110      * Notifies all registered listeners about a change.
111      *
112      * @see #addChangeListener
113      * @see #removeChangeListener
114      */

115     private void fireChange() {
116         if (changeListeners != null) {
117             ChangeEvent JavaDoc e = new ChangeEvent JavaDoc(this);
118             for (ChangeListener JavaDoc l : changeListeners) {
119                 l.stateChanged(e);
120             }
121         }
122     }
123
124     /**
125      */

126     public boolean hasPrevious() {
127         return current > INDEX_TARGET;
128     }
129
130     /**
131      */

132     public boolean hasNext() {
133         return current < INDEX_TARGET;
134     }
135
136     /**
137      */

138     public void previousPanel() {
139         if (!hasPrevious()) {
140             throw new NoSuchElementException JavaDoc();
141         }
142         current--;
143     }
144
145     /**
146      */

147     public void nextPanel() {
148         if (!hasNext()) {
149             throw new NoSuchElementException JavaDoc();
150         }
151         current++;
152     }
153
154     /**
155      */

156     public WizardDescriptor.Panel current() {
157         switch (current) {
158             case INDEX_TARGET:
159                 return getTargetPanel();
160             default:
161                 throw new IllegalStateException JavaDoc();
162         }
163     }
164
165     /**
166      * Returns a panel for choosing name and target location of the test
167      * class. If the panel already exists, returns the existing panel,
168      * otherwise creates a new panel.
169      *
170      * @return existing panel or a newly created panel if it did not exist
171      */

172     private WizardDescriptor.Panel getTargetPanel() {
173         final Project project = Templates.getProject(wizard);
174         if (targetPanel == null || project != lastSelectedProject) {
175             JUnitPlugin plugin = TestUtil.getPluginForProject(project);
176             if (plugin.getClass() != DefaultPlugin.class) {
177                 targetPanel = new StepProblemMessage(
178                         project,
179                         NbBundle.getMessage(TestSuiteWizardIterator.class,
180                                             "MSG_UnsupportedPlugin")); //NOI18N
181
} else {
182                 Collection JavaDoc<SourceGroup> sourceGroups = Utils.getTestTargets(project, true);
183                 if (sourceGroups.isEmpty()) {
184                     targetPanel = new StepProblemMessage(
185                             project,
186                             NbBundle.getMessage(TestSuiteWizardIterator.class,
187                                               "MSG_NoTestSourceGroup"));//NOI18N
188
} else {
189                     sourceGroups.toArray(
190                           testSrcGroups = new SourceGroup[sourceGroups.size()]);
191                     if (optionsPanel == null) {
192                         optionsPanel = new TestSuiteStepLocation();
193                     }
194                     targetPanel = JavaTemplates.createPackageChooser(project,
195                                                                   testSrcGroups,
196                                                                   optionsPanel);
197                 }
198             }
199             lastSelectedProject = project;
200         }
201         return targetPanel;
202     }
203
204     /**
205      */

206     public String JavaDoc name() {
207         switch (current) {
208             case INDEX_TARGET:
209                 return nameTarget;
210             default:
211                 throw new AssertionError JavaDoc(current);
212         }
213     }
214
215     private void loadSettings(TemplateWizard wizard) {
216         JUnitSettings settings = JUnitSettings.getDefault();
217         
218         wizard.putProperty(GuiUtils.CHK_SETUP,
219                            Boolean.valueOf(settings.isGenerateSetUp()));
220         wizard.putProperty(GuiUtils.CHK_TEARDOWN,
221                            Boolean.valueOf(settings.isGenerateTearDown()));
222         wizard.putProperty(GuiUtils.CHK_HINTS,
223                            Boolean.valueOf(settings.isBodyComments()));
224     }
225
226     private void saveSettings(TemplateWizard wizard) {
227         JUnitSettings settings = JUnitSettings.getDefault();
228         
229         settings.setGenerateSetUp(
230                 Boolean.TRUE.equals(wizard.getProperty(GuiUtils.CHK_SETUP)));
231         settings.setGenerateTearDown(
232                 Boolean.TRUE.equals(wizard.getProperty(GuiUtils.CHK_TEARDOWN)));
233         settings.setBodyComments(
234                 Boolean.TRUE.equals(wizard.getProperty(GuiUtils.CHK_HINTS)));
235     }
236
237     /**
238      * <!-- PENDING -->
239      */

240     public void initialize(TemplateWizard wiz) {
241         this.wizard = wiz;
242         current = INDEX_TARGET;
243         loadSettings(wiz);
244
245         String JavaDoc [] panelNames = new String JavaDoc [] {
246           NbBundle.getMessage(EmptyTestCaseWizardIterator.class,"LBL_panel_chooseFileType"),
247           NbBundle.getMessage(EmptyTestCaseWizardIterator.class,"LBL_panel_Target")};
248
249         ((javax.swing.JComponent JavaDoc)getTargetPanel().getComponent()).putClientProperty("WizardPanel_contentData", panelNames);
250         ((javax.swing.JComponent JavaDoc)getTargetPanel().getComponent()).putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(0));
251
252     }
253
254     /**
255      * <!-- PENDING -->
256      */

257     public void uninitialize(TemplateWizard wiz) {
258         this.wizard = null;
259         
260         targetPanel = null;
261         lastSelectedProject = null;
262         optionsPanel = null;
263         testSrcGroups = null;
264         
265         changeListeners = null;
266     }
267
268     public Set JavaDoc<DataObject> instantiate(TemplateWizard wiz) throws IOException JavaDoc {
269         saveSettings(wiz);
270         
271         /* collect and build necessary data: */
272         String JavaDoc name = Templates.getTargetName(wizard);
273         FileObject targetFolder = Templates.getTargetFolder(wizard);
274         DataFolder targetDataFolder = DataFolder.findFolder(targetFolder);
275         FileObject testRootFolder = findTestRootFolder(targetFolder);
276         assert testRootFolder != null;
277         
278         
279         ClassPath testClassPath = ClassPathSupport.createClassPath(
280                 new FileObject[] {testRootFolder});
281         List JavaDoc testClassNames = TestUtil.getJavaFileNames(targetFolder,
282                                                         testClassPath);
283         
284         /* create test class(es) for the selected source class: */
285         DataObject suite = new DefaultPlugin().createSuiteTest(
286                 testRootFolder,
287                 targetFolder,
288                 name,
289                 TestUtil.getSettingsMap(true));
290         if (suite != null) {
291             return Collections.singleton(suite);
292         } else {
293             throw new IOException JavaDoc();
294         }
295     }
296     
297     /** */
298     private FileObject findTestRootFolder(FileObject targetFolder) {
299         for (int i = 0; i < testSrcGroups.length; i++) {
300             FileObject rootFolder = testSrcGroups[i].getRootFolder();
301             if (rootFolder == targetFolder
302                     || FileUtil.isParentOf(rootFolder, targetFolder)) {
303                 return rootFolder;
304             }
305         }
306         return null;
307     }
308
309     /**
310      */

311     public static TestSuiteWizardIterator singleton() {
312         if (instance == null) {
313             // PENDING - it should not be kept forever
314
instance = new TestSuiteWizardIterator();
315         }
316         return instance;
317     }
318
319 }
320
Popular Tags