java - 如何在 Netbeans 平台上获取项目类型?

标签 java netbeans netbeans-platform

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

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

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;

}}

最佳答案

简单的 new -> action -> conditionaly enabled (Project) 就可以了。

 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
}
}

吉尔卡

关于java - 如何在 Netbeans 平台上获取项目类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3023554/

相关文章:

java - Netbeans 属性表 - 行高

java - NetBeans Visual Library 编写的 GUI 编辑器的最佳选择是什么? IconWidget/ComponentWidget

java - 如何在 JUnit 测试容器中的 MySQLContainer 中导入 sql 转储文件

java - 如何将创建的对象分配到不同类的数组中

java - 在 freemarker 页面获取 long 类型值内的逗号

java - Ubuntu 上的 Netbeans 不保存面板位置

java - 在自动装箱中,转换是基于我们分配给的变量/引用还是相反?

regex - Netbeans - 查找不包含特定文本的文件

netbeans - 未跟踪 Mercurial Netbeans 输出文件!

java - 如何在netbeans平台应用程序中添加或使用.DDL文件