本文介绍了如何在Netbeans平台上获得项目类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法知道所选项目的类型?我想根据项目类型(例如J2SE项目)执行一些特定的操作.

Is there a way to know the type of a selected project? I would like to do some specific actions depending of the project type like a J2SE project.

下面是我发现的唯一方法:

Below is the only way that I found to do that:

public final class MyAction extends CookieAction {

@Override
public boolean isEnabled() {
  if(this.getActivatedNodes() == null || this.getActivatedNodes().length != 1) {
        return false;
    }

    Lookup lookup = this.getActivatedNodes()[0].getLookup();

    // gets the selected project
    Project currentProject = lookup.lookup(Project.class);

    // checks if the selected project is a J2SE Project or a Maven Project
    if(currentProject != null && (currentProject.getClass().getSimpleName().equals("J2SEProject")
            || currentProject.getClass().getSimpleName().equals("NbMavenProjectImpl"))) {
        return true;
    }

    return false;

}}

推荐答案

简单的新功能->操作->有条件地启用(项目),一切就绪.

simple new -> action -> conditionaly enabled (Project) and it is all.

 package project.action;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import org.netbeans.api.project.Project;

import org.openide.awt.ActionRegistration;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionID;
import org.openide.util.NbBundle.Messages;

@ActionID(category = "Build",
id = "project.action.SomeAction")
@ActionRegistration(displayName = "#CTL_SomeAction")
@ActionReferences({
    @ActionReference(path = "Menu/File", position = 0)
})
@Messages("CTL_SomeAction=SomeAction")
public final class SomeAction implements ActionListener {

private final Project context;

public SomeAction(Project context) { // this is enable !!
    this.context = context;
}

public void actionPerformed(ActionEvent ev) {
    // TODO use context
}
}

Jirka

这篇关于如何在Netbeans平台上获得项目类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 15:43