KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.NoSuchElementException JavaDoc;
31 import java.util.Set JavaDoc;
32 import javax.swing.event.ChangeEvent JavaDoc;
33 import javax.swing.event.ChangeListener JavaDoc;
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.GuiUtils;
38 import org.netbeans.modules.junit.JUnitPluginTrampoline;
39 import org.netbeans.modules.junit.JUnitSettings;
40 import org.netbeans.modules.junit.TestUtil;
41 import org.netbeans.modules.junit.plugin.JUnitPlugin;
42 import org.netbeans.modules.junit.plugin.JUnitPlugin.CreateTestParam;
43 import org.netbeans.spi.java.project.support.ui.templates.JavaTemplates;
44 import org.netbeans.spi.project.ui.templates.support.Templates;
45 import org.openide.DialogDisplayer;
46 import org.openide.NotifyDescriptor;
47 import org.openide.WizardDescriptor;
48 import org.openide.filesystems.FileObject;
49 import org.openide.filesystems.Repository;
50 import org.openide.loaders.DataFolder;
51 import org.openide.loaders.DataObject;
52 import org.openide.loaders.DataObjectNotFoundException;
53 import org.openide.loaders.TemplateWizard;
54 import org.openide.util.NbBundle;
55 //XXX: retouche
56
//import org.netbeans.jmi.javamodel.*;
57
//import org.netbeans.modules.javacore.api.JavaModel;
58

59 /**
60  * @author Marian Petras
61  */

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

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

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

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

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

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

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

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

155     public WizardDescriptor.Panel current() {
156         switch (current) {
157             case INDEX_TARGET:
158                 return getTargetPanel();
159             default:
160                 throw new IllegalStateException JavaDoc();
161         }
162     }
163
164     private WizardDescriptor.Panel getTargetPanel() {
165         final Project project = Templates.getProject(wizard);
166         if (targetPanel == null || project != lastSelectedProject) {
167             Collection JavaDoc<SourceGroup> sourceGroups = Utils.getTestTargets(project, true);
168             if (sourceGroups.isEmpty()) {
169                 targetPanel = new StepProblemMessage(
170                         project,
171                         NbBundle.getMessage(EmptyTestCaseWizardIterator.class,
172                                             "MSG_NoTestSourceGroup")); //NOI18N
173
} else {
174                 SourceGroup[] testSrcGroups;
175                 sourceGroups.toArray(
176                         testSrcGroups = new SourceGroup[sourceGroups.size()]);
177                 if (optionsPanel == null) {
178                     optionsPanel = new EmptyTestStepLocation();
179                 }
180                 targetPanel = JavaTemplates.createPackageChooser(project,
181                                                                  testSrcGroups,
182                                                                  optionsPanel);
183             }
184             lastSelectedProject = project;
185         }
186
187         return targetPanel;
188     }
189
190     /**
191      */

192     public String JavaDoc name() {
193         switch (current) {
194             case INDEX_TARGET:
195                 return nameTarget;
196             default:
197                 throw new AssertionError JavaDoc(current);
198         }
199     }
200
201     private void loadSettings(TemplateWizard wizard) {
202         JUnitSettings settings = JUnitSettings.getDefault();
203         
204         wizard.putProperty(GuiUtils.CHK_SETUP,
205                            Boolean.valueOf(settings.isGenerateSetUp()));
206         wizard.putProperty(GuiUtils.CHK_TEARDOWN,
207                            Boolean.valueOf(settings.isGenerateTearDown()));
208         wizard.putProperty(GuiUtils.CHK_HINTS,
209                            Boolean.valueOf(settings.isBodyComments()));
210     }
211
212     private void saveSettings(TemplateWizard wizard) {
213         JUnitSettings settings = JUnitSettings.getDefault();
214         
215         settings.setGenerateSetUp(
216                 Boolean.TRUE.equals(wizard.getProperty(GuiUtils.CHK_SETUP)));
217         settings.setGenerateTearDown(
218                 Boolean.TRUE.equals(wizard.getProperty(GuiUtils.CHK_TEARDOWN)));
219         settings.setBodyComments(
220                 Boolean.TRUE.equals(wizard.getProperty(GuiUtils.CHK_HINTS)));
221     }
222
223     /**
224      * <!-- PENDING -->
225      */

226     public void initialize(TemplateWizard wiz) {
227         this.wizard = wiz;
228         current = INDEX_TARGET;
229         loadSettings(wiz);
230         
231
232         String JavaDoc [] panelNames = new String JavaDoc [] {
233           NbBundle.getMessage(EmptyTestCaseWizardIterator.class,"LBL_panel_chooseFileType"),
234           NbBundle.getMessage(EmptyTestCaseWizardIterator.class,"LBL_panel_Target")};
235
236         ((javax.swing.JComponent JavaDoc)getTargetPanel().getComponent()).putClientProperty("WizardPanel_contentData", panelNames);
237         ((javax.swing.JComponent JavaDoc)getTargetPanel().getComponent()).putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(0));
238
239
240     }
241
242     /**
243      * <!-- PENDING -->
244      */

245     public void uninitialize(TemplateWizard wiz) {
246         this.wizard = null;
247         
248         targetPanel = null;
249         lastSelectedProject = null;
250         optionsPanel = null;
251         
252         changeListeners = null;
253     }
254
255     public Set JavaDoc<DataObject> instantiate(TemplateWizard wizard) throws IOException JavaDoc {
256         saveSettings(wizard);
257         
258         /* collect and build necessary data: */
259         String JavaDoc name = Templates.getTargetName(wizard);
260         FileObject targetFolder = Templates.getTargetFolder(wizard);
261         
262         Map JavaDoc<CreateTestParam, Object JavaDoc> params
263                 = TestUtil.getSettingsMap(false);
264         params.put(CreateTestParam.CLASS_NAME,
265                    Templates.getTargetName(wizard));
266                 
267         /* create the test class: */
268         JUnitPlugin plugin = TestUtil.getPluginForProject(
269                                                 Templates.getProject(wizard));
270         /*
271          * The JUnitPlugin instance must be initialized _before_ field
272          * JUnitPluginTrampoline.DEFAULT gets accessed.
273          * See issue #74744.
274          */

275         final FileObject[] testFileObjects
276                 = JUnitPluginTrampoline.DEFAULT.createTests(
277                      plugin,
278                      null,
279                      targetFolder,
280                      params);
281         
282         if (testFileObjects == null) {
283             throw new IOException JavaDoc();
284         }
285         
286         DataObject testDataObject;
287         try {
288             testDataObject = DataObject.find(testFileObjects[0]);
289         } catch (DataObjectNotFoundException ex) {
290             throw new IOException JavaDoc();
291         }
292         
293         return Collections.singleton(testDataObject);
294     }
295
296     /**
297      */

298     public static EmptyTestCaseWizardIterator singleton() {
299         if (instance == null) {
300             // PENDING - it should not be kept forever
301
instance = new EmptyTestCaseWizardIterator();
302         }
303         return instance;
304     }
305
306 }
307
Popular Tags