[NetBeans] Invoke an Action from the NetBeans Command Line

Ingo Fischerが、NetBeansプラットフォームの開発者メーリングリストで、コマンドラインからカスタムプラグインを起動したいのだが…という質問を投稿しました。

[platform-dev] Trigger custom plugin action from the commandline http://netbeans.org/projects/platform/lists/dev/archive/2011-02/message/58

/home/geertjan/netbeans-dev-201101300000/bin/netbeans --runAction "GreetAction"

@ActionID(category = "MyActions",
id = "org.trigger.action.GreetAction")
@ActionRegistration(displayName = "#CTL_GreetAction")
    @ActionReference(path = "Menu/File", position = 0)
public final class GreetAction implements ActionListener {
    public void actionPerformed(ActionEvent e) {
       JOptionPane.showMessageDialog(null, "hello world");
ここで、"GreatAction"をアクションカテゴリ "MyActions"に登録していることに注目してください。この意味は、中央のレジストリに"Actions/MyActions"というフォルダを作成し、その中に”GreatAction”を登録する、ということです。
@ServiceProvider(service = OptionProcessor.class)
public class TriggerActionCommandLine extends OptionProcessor {

    //Here we specify "runAction" as the new key in the command,
    //but it could be any other string you like, of course:
    private static Option action = Option.requiredArgument(Option.NO_SHORT_NAME, "runAction");
    public Set<org.netbeans.spi.sendopts.Option> getOptions() {
        return Collections.singleton(action);

    protected void process(Env env, Map<Option, String[]> values) throws CommandException {
        String[] args = (String[]) values.get(action);
        if (args.length > 0) {
            //Set the value to be the first argument from the command line,
            //i.e., this is "GreetAction", for example:
            final String actionName = args[0];
            //Wait until the UI is constructed,
            //otherwise you will fail to retrieve your action:
            WindowManager.getDefault().invokeWhenUIReady(new Runnable() {
                public void run() {
                    //Then find & perform the action: 
                    Action a = findAction(actionName);
                    //TODO: a null check

    public Action findAction(String actionName) {
        FileObject myActionsFolder = FileUtil.getConfigFile("Actions/MyActions");
        FileObject[] myActionsFolderKids = myActionsFolder.getChildren();
        for (FileObject fileObject : myActionsFolderKids) {
            //Probably want to make this more robust,
            //but the point is that here we find a particular Action:
            if (fileObject.getName().contains(actionName)) {
                try {
                    DataObject dob = DataObject.find(fileObject);
                    InstanceCookie ic = dob.getLookup().lookup(InstanceCookie.class);
                    if (ic != null) {
                        Object instance = ic.instanceCreate();
                        if (instance instanceof Action) {
                            Action a = (Action) instance;
                            return a;
                } catch (Exception e) {
                    ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
                    return null;
        return null;

Import は以下のような感じです。
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import javax.swing.Action;
import org.netbeans.api.sendopts.CommandException;
import org.netbeans.spi.sendopts.Env;
import org.netbeans.spi.sendopts.Option;
import org.netbeans.spi.sendopts.OptionProcessor;
import org.openide.ErrorManager;
import org.openide.cookies.InstanceCookie;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.loaders.DataObject;
import org.openide.util.lookup.ServiceProvider;
import org.openide.windows.WindowManager;

つまり、NetBeans上で作成した別のアプリケーションのように、IDE起動のたびに ”hello world” がJOptionPaneに表示されました。


0 件のコメント: