![](/img/trans.png)
[英]Is there any better way to get project type within netbeans platform application?
[英]How can I get a project type on 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;
}}
簡單的新增->操作->有條件地啟用(項目),僅此而已。
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
}
}
耶爾卡
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.