1 19 20 package org.netbeans.modules.junit.wizards; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.Collections ; 26 import java.util.Iterator ; 27 import java.util.LinkedList ; 28 import java.util.List ; 29 import java.util.NoSuchElementException ; 30 import java.util.Set ; 31 import javax.swing.event.ChangeEvent ; 32 import javax.swing.event.ChangeListener ; 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 60 public class TestSuiteWizardIterator 61 implements TemplateWizard.Iterator { 62 63 64 private static TestSuiteWizardIterator instance; 65 66 67 private TemplateWizard wizard; 68 69 70 private static final int INDEX_TARGET = 2; 71 72 73 private final String nameTarget = NbBundle.getMessage( 74 TestSuiteWizardIterator.class, 75 "LBL_panel_Target"); 77 private int current; 78 79 private List <ChangeListener > changeListeners; 80 81 private WizardDescriptor.Panel targetPanel; 82 private Project lastSelectedProject = null; 83 84 private WizardDescriptor.Panel optionsPanel; 85 86 87 private SourceGroup[] testSrcGroups; 88 89 91 public void addChangeListener(ChangeListener l) { 92 if (changeListeners == null) { 93 changeListeners = new ArrayList <ChangeListener >(2); 94 } 95 changeListeners.add(l); 96 } 97 98 100 public void removeChangeListener(ChangeListener l) { 101 if (changeListeners != null) { 102 changeListeners.remove(l); 103 if (changeListeners.isEmpty()) { 104 changeListeners = null; 105 } 106 } 107 } 108 109 115 private void fireChange() { 116 if (changeListeners != null) { 117 ChangeEvent e = new ChangeEvent (this); 118 for (ChangeListener l : changeListeners) { 119 l.stateChanged(e); 120 } 121 } 122 } 123 124 126 public boolean hasPrevious() { 127 return current > INDEX_TARGET; 128 } 129 130 132 public boolean hasNext() { 133 return current < INDEX_TARGET; 134 } 135 136 138 public void previousPanel() { 139 if (!hasPrevious()) { 140 throw new NoSuchElementException (); 141 } 142 current--; 143 } 144 145 147 public void nextPanel() { 148 if (!hasNext()) { 149 throw new NoSuchElementException (); 150 } 151 current++; 152 } 153 154 156 public WizardDescriptor.Panel current() { 157 switch (current) { 158 case INDEX_TARGET: 159 return getTargetPanel(); 160 default: 161 throw new IllegalStateException (); 162 } 163 } 164 165 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")); } else { 182 Collection <SourceGroup> sourceGroups = Utils.getTestTargets(project, true); 183 if (sourceGroups.isEmpty()) { 184 targetPanel = new StepProblemMessage( 185 project, 186 NbBundle.getMessage(TestSuiteWizardIterator.class, 187 "MSG_NoTestSourceGroup")); } 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 206 public String name() { 207 switch (current) { 208 case INDEX_TARGET: 209 return nameTarget; 210 default: 211 throw new AssertionError (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 240 public void initialize(TemplateWizard wiz) { 241 this.wizard = wiz; 242 current = INDEX_TARGET; 243 loadSettings(wiz); 244 245 String [] panelNames = new String [] { 246 NbBundle.getMessage(EmptyTestCaseWizardIterator.class,"LBL_panel_chooseFileType"), 247 NbBundle.getMessage(EmptyTestCaseWizardIterator.class,"LBL_panel_Target")}; 248 249 ((javax.swing.JComponent )getTargetPanel().getComponent()).putClientProperty("WizardPanel_contentData", panelNames); 250 ((javax.swing.JComponent )getTargetPanel().getComponent()).putClientProperty("WizardPanel_contentSelectedIndex", new Integer (0)); 251 252 } 253 254 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 <DataObject> instantiate(TemplateWizard wiz) throws IOException { 269 saveSettings(wiz); 270 271 272 String 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 testClassNames = TestUtil.getJavaFileNames(targetFolder, 282 testClassPath); 283 284 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 (); 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 311 public static TestSuiteWizardIterator singleton() { 312 if (instance == null) { 313 instance = new TestSuiteWizardIterator(); 315 } 316 return instance; 317 } 318 319 } 320 | Popular Tags |