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.HashMap ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.NoSuchElementException ; 31 import java.util.Set ; 32 import javax.swing.event.ChangeEvent ; 33 import javax.swing.event.ChangeListener ; 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 59 62 public class EmptyTestCaseWizardIterator 63 implements TemplateWizard.Iterator { 64 65 66 private static EmptyTestCaseWizardIterator instance; 67 68 69 private TemplateWizard wizard; 70 71 72 private static final int INDEX_TARGET = 2; 73 74 75 private final String nameTarget = NbBundle.getMessage( 76 EmptyTestCaseWizardIterator.class, 77 "LBL_panel_Target"); 79 private int current; 80 81 private List <ChangeListener > changeListeners; 83 private WizardDescriptor.Panel targetPanel; 84 private Project lastSelectedProject = null; 85 86 private WizardDescriptor.Panel optionsPanel; 87 88 90 public void addChangeListener(ChangeListener l) { 91 if (changeListeners == null) { 92 changeListeners = new ArrayList <ChangeListener >(2); 93 } 94 changeListeners.add(l); 95 } 96 97 99 public void removeChangeListener(ChangeListener l) { 100 if (changeListeners != null) { 101 changeListeners.remove(l); 102 if (changeListeners.isEmpty()) { 103 changeListeners = null; 104 } 105 } 106 } 107 108 114 private void fireChange() { 115 if (changeListeners != null) { 116 ChangeEvent e = new ChangeEvent (this); 117 for (ChangeListener l : changeListeners) { 118 l.stateChanged(e); 119 } 120 } 121 } 122 123 125 public boolean hasPrevious() { 126 return current > INDEX_TARGET; 127 } 128 129 131 public boolean hasNext() { 132 return current < INDEX_TARGET; 133 } 134 135 137 public void previousPanel() { 138 if (!hasPrevious()) { 139 throw new NoSuchElementException (); 140 } 141 current--; 142 } 143 144 146 public void nextPanel() { 147 if (!hasNext()) { 148 throw new NoSuchElementException (); 149 } 150 current++; 151 } 152 153 155 public WizardDescriptor.Panel current() { 156 switch (current) { 157 case INDEX_TARGET: 158 return getTargetPanel(); 159 default: 160 throw new IllegalStateException (); 161 } 162 } 163 164 private WizardDescriptor.Panel getTargetPanel() { 165 final Project project = Templates.getProject(wizard); 166 if (targetPanel == null || project != lastSelectedProject) { 167 Collection <SourceGroup> sourceGroups = Utils.getTestTargets(project, true); 168 if (sourceGroups.isEmpty()) { 169 targetPanel = new StepProblemMessage( 170 project, 171 NbBundle.getMessage(EmptyTestCaseWizardIterator.class, 172 "MSG_NoTestSourceGroup")); } 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 192 public String name() { 193 switch (current) { 194 case INDEX_TARGET: 195 return nameTarget; 196 default: 197 throw new AssertionError (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 226 public void initialize(TemplateWizard wiz) { 227 this.wizard = wiz; 228 current = INDEX_TARGET; 229 loadSettings(wiz); 230 231 232 String [] panelNames = new String [] { 233 NbBundle.getMessage(EmptyTestCaseWizardIterator.class,"LBL_panel_chooseFileType"), 234 NbBundle.getMessage(EmptyTestCaseWizardIterator.class,"LBL_panel_Target")}; 235 236 ((javax.swing.JComponent )getTargetPanel().getComponent()).putClientProperty("WizardPanel_contentData", panelNames); 237 ((javax.swing.JComponent )getTargetPanel().getComponent()).putClientProperty("WizardPanel_contentSelectedIndex", new Integer (0)); 238 239 240 } 241 242 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 <DataObject> instantiate(TemplateWizard wizard) throws IOException { 256 saveSettings(wizard); 257 258 259 String name = Templates.getTargetName(wizard); 260 FileObject targetFolder = Templates.getTargetFolder(wizard); 261 262 Map <CreateTestParam, Object > params 263 = TestUtil.getSettingsMap(false); 264 params.put(CreateTestParam.CLASS_NAME, 265 Templates.getTargetName(wizard)); 266 267 268 JUnitPlugin plugin = TestUtil.getPluginForProject( 269 Templates.getProject(wizard)); 270 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 (); 284 } 285 286 DataObject testDataObject; 287 try { 288 testDataObject = DataObject.find(testFileObjects[0]); 289 } catch (DataObjectNotFoundException ex) { 290 throw new IOException (); 291 } 292 293 return Collections.singleton(testDataObject); 294 } 295 296 298 public static EmptyTestCaseWizardIterator singleton() { 299 if (instance == null) { 300 instance = new EmptyTestCaseWizardIterator(); 302 } 303 return instance; 304 } 305 306 } 307 | Popular Tags |