1 19 package org.netbeans.modules.apisupport.project.ui; 20 21 import java.awt.event.ActionEvent ; 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.Arrays ; 25 import java.util.Iterator ; 26 import java.util.Properties ; 27 import java.util.logging.Logger ; 28 import javax.swing.AbstractAction ; 29 import javax.swing.Action ; 30 import javax.swing.JComponent ; 31 import javax.swing.JMenu ; 32 import javax.swing.JMenuItem ; 33 import javax.swing.JSeparator ; 34 import org.apache.tools.ant.module.api.AntProjectCookie; 35 import org.apache.tools.ant.module.api.support.ActionUtils; 36 import org.apache.tools.ant.module.api.support.TargetLister; 37 import org.netbeans.api.project.Project; 38 import org.openide.ErrorManager; 39 import org.openide.awt.Actions; 40 import org.openide.awt.Mnemonics; 41 import org.openide.filesystems.FileObject; 42 import org.openide.filesystems.FileStateInvalidException; 43 import org.openide.loaders.DataObject; 44 import org.openide.loaders.DataObjectNotFoundException; 45 import org.openide.util.ContextAwareAction; 46 import org.openide.util.Lookup; 47 import org.openide.util.Task; 48 import org.openide.util.TaskListener; 49 import org.openide.util.actions.Presenter; 50 import org.openide.util.NbBundle; 51 import org.openide.awt.DynamicMenuContent; 52 import org.openide.awt.HtmlBrowser; 53 import org.openide.execution.ExecutorTask; 54 55 77 public final class XTestProjectMenuItem extends AbstractAction implements Presenter.Popup, ContextAwareAction { 78 79 public static final String NAME = NbBundle.getBundle(XTestProjectMenuItem.class).getString("CTL_MenuItem_XTest"); 80 private Lookup context; 81 82 83 public XTestProjectMenuItem() { 84 this(null); 85 } 86 87 90 public XTestProjectMenuItem(Lookup actionContext) { 91 super(NAME); 92 this.context = actionContext; 93 } 94 95 98 public void actionPerformed(ActionEvent ignore) { 99 } 101 102 105 public JMenuItem getPopupPresenter() { 106 return new XTestProjectMenuItems(); 107 } 108 109 public Action createContextAwareInstance(Lookup actionContext) { 110 return new XTestProjectMenuItem(actionContext); 111 } 112 113 114 class XTestProjectMenuItems extends JMenuItem implements DynamicMenuContent { 115 116 public JComponent [] getMenuPresenters() { 117 Action [] actions = actions(); 118 if(actions == null) { 119 return new JComponent [0]; 121 } 122 final JMenu menu = new JMenu (); 123 Mnemonics.setLocalizedText(menu, NAME); 124 for (int i = 0; i < actions.length; i++) { 125 Action action = actions[i]; 126 if (action == null) { 127 menu.add(new JSeparator ()); 128 } else { 129 JMenuItem item = new JMenuItem (); 130 Actions.connect(item, actions[i], false); 131 menu.add(item); 132 } 133 } 134 return new JComponent [] { menu }; 135 } 136 137 public JComponent [] synchMenuPresenters(JComponent [] items) { 138 return items; 139 } 140 } 141 142 143 private Action [] actions() { 144 Project project = (Project)context.lookup(Project.class); 145 if(project == null) { 147 return null; 148 } 149 if(project.getLookup().lookup(ModuleActions.class) == null) { 150 return null; 152 } 153 ArrayList actions = new ArrayList (); 154 155 String [] testTypes = findTestTypes(project); 156 if(testTypes.length > 0) { 157 actions.add(createAction(NbBundle.getMessage(XTestProjectMenuItem.class, "CTL_MenuItem_Clean"), 158 project, "", new String [] {"realclean"}, false)); 159 actions.add(null); 160 } else { 161 return null; 163 } 167 for(int i=0;i<testTypes.length;i++) { 168 actions.add(createAction(NbBundle.getMessage(XTestProjectMenuItem.class, "CTL_MenuItem_BuildTests", testTypes[i]), project, testTypes[i], new String [] {"buildtests"}, false)); actions.add(createAction(NbBundle.getMessage(XTestProjectMenuItem.class, "CTL_MenuItem_RunTests", testTypes[i]), project, testTypes[i], new String [] {"runtests"}, true)); if(targetExists(findTestBuildXml(project), "coverage")) { actions.add(createAction(NbBundle.getMessage(XTestProjectMenuItem.class, "CTL_MenuItem_MeasureCoverage", testTypes[i]), project, testTypes[i], new String [] {"coverage"}, true)); } 179 if(testTypes.length-1 > i) { 181 actions.add(null); 182 } 183 } 184 return (Action [])actions.toArray(new Action [actions.size()]); 185 } 186 187 private AbstractAction createAction(String displayName, final Project project, 188 final String testType, final String [] targets, final boolean showResults) { 189 190 return new AbstractAction (displayName) { 191 192 public boolean isEnabled() { 193 if(context.lookup(new Lookup.Template(Project.class)).allInstances().size() == 1) { 195 return findTestBuildXml(project) != null; 196 } 197 return false; 198 } 199 200 201 public void actionPerformed(ActionEvent ignore) { 202 Properties props = new Properties (); 203 props.setProperty("xtest.testtype", testType); try { 205 ExecutorTask task = ActionUtils.runTarget(findTestBuildXml(project), targets, props); 206 task.addTaskListener(new TaskListener() { 207 public void taskFinished(Task task) { 208 if(((ExecutorTask)task).result() == 0 && showResults) { 209 if(targets[0].equals("coverage")) { showCoverageResults(project); 211 } else { 212 showTestResults(project); 213 } 214 } 215 } 216 }); 217 } catch (IOException e) { 218 ErrorManager.getDefault().notify(e); 219 } 220 } 221 }; 222 } 223 224 225 private static boolean targetExists(FileObject buildXml, String targetName) { 226 if(buildXml == null) { 227 return false; 228 } 229 DataObject d; 230 try { 231 d = DataObject.find(buildXml); 232 } catch (DataObjectNotFoundException ex) { 233 ErrorManager.getDefault().notify(ex); 234 return false; 235 } 236 AntProjectCookie apc = (AntProjectCookie)d.getCookie(AntProjectCookie.class); 237 Iterator iter; 238 try { 239 iter = TargetLister.getTargets(apc).iterator(); 240 } catch (IOException ex) { 241 Logger.getAnonymousLogger().fine(ex.getMessage()); 243 return false; 244 } 245 while(iter.hasNext()) { 246 if(((TargetLister.Target)iter.next()).getName().equals(targetName)) { 247 return true; 248 } 249 } 250 return false; 251 } 252 253 254 private static FileObject findTestBuildXml(Project project) { 255 return project.getProjectDirectory().getFileObject("test/build.xml"); } 257 258 261 private static void showTestResults(Project project) { 262 showBrowser(project.getProjectDirectory().getFileObject("test/results/index.html")); } 264 265 268 private static void showCoverageResults(Project project) { 269 showBrowser(project.getProjectDirectory().getFileObject("test/coverage/coverage.html")); } 271 272 275 private static void showBrowser(FileObject resultsFO) { 276 if(resultsFO != null) { 277 try { 278 HtmlBrowser.URLDisplayer.getDefault().showURL(resultsFO.getURL()); 279 } catch (FileStateInvalidException ex) { 280 ErrorManager.getDefault().notify(ex); 281 } 282 } 283 } 284 285 288 private static String [] findTestTypes(Project project) { 289 FileObject testFO = project.getProjectDirectory().getFileObject("test"); if(testFO == null) { 291 return new String [0]; 292 } 293 FileObject[] fos = testFO.getChildren(); ArrayList testTypes = new ArrayList (); 295 for(int i=0;i<fos.length;i++) { 296 if(fos[i].getExt().equalsIgnoreCase("xml") && fos[i].getName().matches("build-.*")) { String testType = fos[i].getName().substring(fos[i].getName().indexOf('-')+1); 298 if(project.getProjectDirectory().getFileObject("test/cfg-"+testType+".xml") != null) { testTypes.add(testType); 300 } 301 } 302 } 303 String [] result = (String [])testTypes.toArray(new String [testTypes.size()]); 304 Arrays.sort(result); 305 return result; 306 } 307 } 308 | Popular Tags |