Java Code Examples for org.netbeans.spi.project.ActionProvider#invokeAction()

The following examples show how to use org.netbeans.spi.project.ActionProvider#invokeAction() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StepIntoActionProvider.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void invokeAction() {
    Project p = MainProjectManager.getDefault ().getMainProject ();
    ActionProvider actionProvider = p.getLookup ().lookup (
            ActionProvider.class
        );
    if (Arrays.asList(actionProvider.getSupportedActions ()).contains(ActionProvider.COMMAND_DEBUG_STEP_INTO) &&
        actionProvider.isActionEnabled(ActionProvider.COMMAND_DEBUG_STEP_INTO, p.getLookup())) {

        actionProvider.invokeAction (
                ActionProvider.COMMAND_DEBUG_STEP_INTO,
                p.getLookup ()
            );
    } else {
        Utilities.disabledActionBeep();
        setEnabled (
            ActionsManager.ACTION_STEP_INTO,
            false
        );
    }
}
 
Example 2
Source File: TestNGExecutionManager.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void rerun() {
    if ((properties.getProperty("test.includes") != null && properties.getProperty("test.includes").endsWith(".xml")) ||    //NOI18N
            (properties.getProperty("test.class") != null && properties.getProperty("test.class").endsWith(".xml"))) {   //NOI18N
        if (properties.getProperty("continue.after.failing.tests") == null) {   //NOI18N
            properties.setProperty("continue.after.failing.tests", "true");  //NOI18N
        }
        try {
            runAnt(FileUtil.toFileObject(scriptFile), targets, properties);
        } catch (IOException ex) {
            LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
        }
    } else {
        Project project = testSession.getProject();
        if(ProjectManager.getDefault().isValid(project)) {
            ActionProvider actionProvider = project.getLookup().lookup(ActionProvider.class);
            String[] actionNames = getActionNames(targets);
            if (actionProvider != null) {
                if (Arrays.asList(actionProvider.getSupportedActions()).contains(actionNames[0])
                        && actionProvider.isActionEnabled(actionNames[0], lookup)) {
                    actionProvider.invokeAction(actionNames[0], lookup);
                }
            }
        }
    }
}
 
Example 3
Source File: CoverageSideBar.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void runAction(String action) {
    Project project = getProject();
    if (project != null) {
        Lookup lookup = project.getLookup();
        if (fileForDocument != null) {
            try {
                DataObject dobj = DataObject.find(fileForDocument);
                lookup = dobj.getLookup();
            } catch (DataObjectNotFoundException ex) {
                Exceptions.printStackTrace(ex);
            }
        }

        ActionProvider provider = project.getLookup().lookup(ActionProvider.class);
        if (provider != null) {
            if (provider.isActionEnabled(action, lookup)) {
                provider.invokeAction(action, lookup);
            }
        }
    }
}
 
Example 4
Source File: REPLAction2.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@NbBundle.Messages({
    "ERR_CannotBuildProject=Could not build the project",
    "ERR_ProjectBuildFailed=Project build failed, please check Output window",
})
@Override
public void perform(Project project) {
    ActionProvider p = project.getLookup().lookup(ActionProvider.class);
    // check whether the is CoS enabled fo the project
    if (ShellProjectUtils.isCompileOnSave(project)) {
        doRunShell(project);
        return;
    }
    if (p == null || !p.isActionEnabled(ActionProvider.COMMAND_BUILD, Lookups.singleton(project))) {
        StatusDisplayer.getDefault().setStatusText(Bundle.ERR_CannotBuildProject());
        return;
    }
    p.invokeAction(ActionProvider.COMMAND_BUILD, Lookups.fixed(project, new ActionProgress() {
        @Override
        protected void started() {
            // no op
        }

        @Override
        public void finished(boolean success) {
            if (success) {
                doRunShell(project);
            } else {
                StatusDisplayer.getDefault().setStatusText(Bundle.ERR_ProjectBuildFailed());
            }
        }
    }));
}
 
Example 5
Source File: LookupMergerImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void invokeAction(String command, Lookup context) throws IllegalArgumentException {
    for (ActionProvider ap : delegates()) {
        if (Arrays.asList(ap.getSupportedActions()).contains(command) && ap.isActionEnabled(command, context)) {
            LOG.log(Level.FINE, "delegating {0} on {1} to {2}", new Object[] {command, context, ap});
            ap.invokeAction(command, context);
            return;
        }
    }
    throw new IllegalArgumentException(command);
}
 
Example 6
Source File: ProjectUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static void invokeAction(Project project, String s) {
    ActionProvider ap = project.getLookup().lookup(ActionProvider.class);

    if (ap == null) {
        return; // fail early
    }

    ap.invokeAction(s, Lookup.getDefault());
}
 
Example 7
Source File: RunCheckerImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Launcher createLauncher(final Session session) {
    return new Launcher() {

        @Override
        public void launch(boolean rerun) {
            if (rerun) {
                RunConfig config = (RunConfig)session.getAttribute("mvn-run-checker.config");
                if (config != null) {
                    RunUtils.executeMaven(config);
                }
            } else {
                Project p = session.getProject();
                if (p == null) {
                    FileObject f = session.getFile();
                    if (f != null) {
                        p = FileOwnerQuery.getOwner(f);
                    }
                }
                if (p != null) {
                    ActionProvider ap = p.getLookup().lookup(ActionProvider.class);
                    if (ap != null) {
                        ap.invokeAction(session.getCommand(), session.getContext());
                    }
                }
            }
        }
    };
}
 
Example 8
Source File: CreateArchetypeTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void invokeCommand(ActionProvider actions, String cmd, Project prj) throws IllegalArgumentException, InterruptedException {
    CountDownLatch waiter = new CountDownLatch(1);
    AfterBuildActionHook notifier = (action, context, res, out) -> {
        waiter.countDown();
    };
    actions.invokeAction(cmd, Lookups.fixed(prj, notifier));
    waiter.await();
}
 
Example 9
Source File: LookupProviderSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void invokeAction(String command, Lookup context) throws IllegalArgumentException {
    for (ActionProvider ap : lkpResult.allInstances()) {
        if (Arrays.asList(ap.getSupportedActions()).contains(command) &&
            ap.isActionEnabled(command, context)) {
            ap.invokeAction(command, context);
            return;
        }
    }
    throw new IllegalArgumentException(String.format(command));
}
 
Example 10
Source File: CoverageReportTopComponent.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void runAllTests(ActionEvent evt) {//GEN-FIRST:event_runAllTests
    Lookup lookup = project.getLookup();
    ActionProvider actionProvider = project.getLookup().lookup(ActionProvider.class);
    CoverageProvider coverageProvider = CoverageManagerImpl.getProvider(project);
    String action = ActionProvider.COMMAND_TEST;
    if (coverageProvider != null && coverageProvider.getTestAllAction() != null) {
        action = coverageProvider.getTestAllAction();
    }
    if (actionProvider != null) {
        if (actionProvider.isActionEnabled(ActionProvider.COMMAND_TEST, lookup)) {
            actionProvider.invokeAction(action, lookup);
        }
    }
}
 
Example 11
Source File: ArchetypeTemplateHandler.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@NbBundle.Messages({
    "MSG_NoVersion=No version attribute specified for the Maven project",
    "MSG_NoArtifactId=No artifactId attribute specified for the Maven project",
    "MSG_NoGroupId=No groupId attribute specified for the Maven project",
})
@Override
protected List<FileObject> createFromTemplate(CreateDescriptor desc) throws IOException {
    Properties archetype = new Properties();
    try (InputStream is = desc.getTemplate().getInputStream()) {
        archetype.load(is);
    }
    mergeProperties(desc, archetype);
    
    String version = archetype.getProperty("version"); // NOI18N
    if (version == null) {
        throw new IOException(Bundle.MSG_NoVersion());
    }
    String artifactId = archetype.getProperty("artifactId"); // NOI18N
    if (version == null) {
        throw new IOException(Bundle.MSG_NoArtifactId());
    }
    String groupId = archetype.getProperty("groupId"); // NOI18N
    if (version == null) {
        throw new IOException(Bundle.MSG_NoGroupId());
    }
    String packageName = archetype.getProperty("package"); // NOI18N
    ProjectInfo pi = new ProjectInfo(groupId, artifactId, version, packageName);
    Archetype arch = new Archetype();
    arch.setArtifactId(archetype.getProperty("archetypeArtifactId")); // NOI18N
    arch.setGroupId(archetype.getProperty("archetypeGroupId")); // NOI18N
    arch.setVersion(archetype.getProperty("archetypeVersion")); // NOI18N
    File projDir = desc.getValue(CommonProjectActions.PROJECT_PARENT_FOLDER);
    if (projDir == null) {
        projDir = FileUtil.toFile(desc.getTarget()).getParentFile();
    }
    if (projDir == null) {
        throw new IOException(CommonProjectActions.PROJECT_PARENT_FOLDER + " not specified");
    }
    
    Map<String, String> filteredProperties = 
            NbCollections.checkedMapByFilter(archetype, String.class, String.class, false);
    final File toCreate = new File(projDir, pi.artifactId);
    ArchetypeWizards.createFromArchetype(toCreate, pi, arch, filteredProperties, true);
    FileObject fo = FileUtil.toFileObject(toCreate);

    List<FileObject> fos = new ArrayList<>();
    collectPomDirs(fo, fos);
    final String toOpen = archetype.getProperty("archetypeOpen"); // NOI18N
    if (toOpen != null) {
        collectFiles(fo, fos, toOpen.split(",")); // NOI18N
    }

    if ("true".equals(archetype.getProperty("archetypeBuild"))) { // NOI18N
        Project prj = ProjectManager.getDefault().findProject(fo);
        ActionProvider ap = prj == null ? null : prj.getLookup().lookup(ActionProvider.class);
        if (ap != null) {
            ap.invokeAction(ActionProvider.COMMAND_BUILD, prj.getLookup());
        }
    }
    return fos;
}
 
Example 12
Source File: ActionProviderImpl.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void invokeAction(String command, Lookup context) throws IllegalArgumentException {
    if (COMMAND_PROFILE_TEST_SINGLE.equals(command)) {
        for (ActionProvider ap : Lookup.getDefault().lookupAll(ActionProvider.class)) {
            if (new HashSet<>(Arrays.asList(ap.getSupportedActions())).contains(COMMAND_PROFILE_TEST_SINGLE) && ap.isActionEnabled(COMMAND_PROFILE_TEST_SINGLE, context)) {
                ap.invokeAction(COMMAND_PROFILE_TEST_SINGLE, context);
                return ;
            }
        }
    }
    FileObject scriptFO = script;
    Settings settings = project.getLookup().lookup(Settings.class);
    Properties props = new Properties();
    if (settings.isUseAntBuild()) {
        props.put("langtools.build.location", settings.getAntBuildLocation());
    } else {
        scriptFO = genericScript;
        if (COMMAND_BUILD_FAST.equals(command)) {
            command = COMMAND_BUILD_GENERIC_FAST;
        }
    }
    if (COMMAND_BUILD_GENERIC_FAST.equals(command)) {
        switch (settings.getRunBuildSetting()) {
            case NEVER:
                ActionProgress.start(context).finished(true);
                return;
            case ALWAYS:
            default:
                break;
        }
        scriptFO = genericScript;
        command = COMMAND_BUILD_FAST; //XXX: should only do this if genericScript supports it
    }
    FileObject basedirFO = project.currentModule != null ? scriptFO == genericScript ? project.moduleRepository.getJDKRoot()
                                                                                     : repository
                                                         : repository.getParent();
    props.put("basedir", FileUtil.toFile(basedirFO).getAbsolutePath());
    props.put("CONF", project.configurations.getActiveConfiguration().getLocation().getName());
    props.put("nb.jdk.project.target.java.home", BuildUtils.findTargetJavaHome(project.getProjectDirectory()).getAbsolutePath());
    RootKind kind = getKind(context);
    RunSingleConfig singleFileProperty = command2Properties.get(Pair.of(command, kind));
    if (singleFileProperty != null) {
        String srcdir = "";
        String moduleName = "";
        StringBuilder value = new StringBuilder();
        String sep = "";
        for (FileObject file : context.lookupAll(FileObject.class)) {
            value.append(sep);
            ClassPath sourceCP;
            switch (kind) {
                case SOURCE:
                    sourceCP = ClassPath.getClassPath(file, ClassPath.SOURCE);
                    break;
                case TEST:
                    sourceCP = ClassPathSupport.createClassPath(BuildUtils.getFileObject(project.getProjectDirectory(), "../../test"));
                    break;
                default:
                    throw new IllegalStateException(kind.name());
            }
            value.append(singleFileProperty.valueType.convert(sourceCP, file));
            sep = singleFileProperty.separator;
            FileObject ownerRoot = sourceCP.findOwnerRoot(file);
            srcdir = FileUtil.getRelativePath(BuildUtils.getFileObject(project.getProjectDirectory(), "../.."), ownerRoot);
            moduleName = ownerRoot.getParent().getParent().getNameExt();
        }
        props.put(singleFileProperty.propertyName, value.toString());
        props.put("srcdir", srcdir);
        props.put("module.name", moduleName);
    }
    final ActionProgress progress = ActionProgress.start(context);
    try {
        ActionUtils.runTarget(scriptFO, command2Targets.get(Pair.of(command, kind)), props)
                   .addTaskListener(new TaskListener() {
            @Override
            public void taskFinished(Task task) {
                progress.finished(((ExecutorTask) task).result() == 0);
            }
        });
    } catch (IOException ex) {
        //???
        Exceptions.printStackTrace(ex);
        progress.finished(false);
    }
}
 
Example 13
Source File: FileAction.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
protected void actionPerformed( final Lookup context ) {
    Runnable r = new Runnable() {
        //the tricky part here is that context can change in the time between AWT and RP execution.
        //unfortunately the ActionProviders from project need the lookup to see if the command is supported.
        // that sort of renders the ActionUtils.mineData() method useless here. Unless we are able to create a mock lookup with only projects and files.
        @Override
        public void run() {
    
    if (command != null) {
        final Project[] projects = ActionsUtil.getProjectsFromLookup( context, command );
        Runnable r2 = new Runnable() {

            @Override
            public void run() {
                if ( projects.length == 1 ) {            
                    ActionProvider ap = projects[0].getLookup().lookup(ActionProvider.class);
                    ap.invokeAction( command, context );
                    return;
                }

                ActionProvider provider = globalProvider(context);
                if (provider != null) {
                    provider.invokeAction(command, context);
                }
            }
        };
        //ActionProvider is supposed to run in awt
        if (SwingUtilities.isEventDispatchThread()) {
            r2.run();
        } else {
            SwingUtilities.invokeLater(r2);
        }            
    } else if (performer != null) {
        Collection<? extends DataObject> dobjs = context.lookupAll(DataObject.class);
        if (dobjs.size() == 1) {
            performer.perform(dobjs.iterator().next().getPrimaryFile());
        }
    }
               }
    };
    //no clear way of waiting for RP finishing the task, a lot of tests rely on sync execution.
    if (Boolean.getBoolean("sync.project.execution")) {
        r.run();
    } else {
        RP.post(r);
    }

}