简体   繁体   English

如何在Netbeans平台上获得项目类型?

[英]How can I get a project type on Netbeans Platform?

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. 我想根据项目类型(如J2SE项目)执行一些特定的操作。

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 耶尔卡

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM