Do you mean replace DataObject to Project as I have done in new example? In
this case my action is disabled in context menu. I have added picture with
example.
I need to know DataObject/File and Project in one time.
If I will know Project how can I find what DataObject/File has been selected
for action?
package org.vorlyanskiy.netbeans.groovy;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import org.netbeans.api.project.Project;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.loaders.DataObject;
import org.openide.util.Lookup;
import org.openide.util.NbBundle.Messages;
import org.openide.util.Utilities;
@ActionID(
category = "Build",
id = "org.vorlyanskiy.netbeans.groovy.SomeAction"
)
@ActionRegistration(
displayName = "#CTL_SomeAction"
)
@ActionReferences({
@ActionReference(path = "Loaders/text/x-groovy/Actions", position = 566),
})
@Messages("CTL_SomeAction=My Action")
public final class SomeAction implements ActionListener {
private final Project context;
public SomeAction(Project context) {
this.context = context;
}
@Override
public void actionPerformed(ActionEvent ev) {
Project project =
Utilities.actionsGlobalContext().lookup(Project.class);
Project project2 = context.getLookup().lookup(Project.class);
}
}
From: Geertjan Wielenga <[email protected]>
Sent: Sunday, November 29, 2020 1:41 AM
To: Orlyanskiy Vladimir <[email protected]>
Cc: [email protected]
Subject: Re: How to get Project from custom action ?
Replace DataObject with Project.
Gj
On Sat, 28 Nov 2020 at 23:38, Orlyanskiy Vladimir <[email protected]
<mailto:[email protected]> > wrote:
Hello.
I try to create module with new action for existing file type.
I have created action with code that I have put below.
I have problem with loading Project in method actionPerformed. Project is null
in both cases.
How to load current Project from action ?
@ActionID(
category = "Build",
id = "org.xxx.netbeans.groovy.SomeAction"
)
@ActionRegistration(
displayName = "#CTL_SomeAction"
)
@ActionReferences({
@ActionReference(path = "Loaders/text/x-groovy/Actions", position = 566),
})
@Messages("CTL_SomeAction=My Action")
public final class SomeAction implements ActionListener {
private final DataObject context;
public SomeAction(DataObject context) {
this.context = context;
}
@Override
public void actionPerformed(ActionEvent ev) {
Project project =
Utilities.actionsGlobalContext().lookup(Project.class);
Project project2 = context.getLookup().lookup(Project.class);
}
}