[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"
ここでは、”GreatAction”というアクションを作成することにします。
@ActionID(category = "MyActions",
id = "org.trigger.action.GreetAction")
@ActionRegistration(displayName = "#CTL_GreetAction")
@ActionReferences({
@ActionReference(path = "Menu/File", position = 0)
})
@Messages("CTL_GreetAction=Greet")
public final class GreetAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "hello world");
}
}ここで、"GreatAction"をアクションカテゴリ "MyActions"に登録していることに注目してください。この意味は、中央のレジストリに"Actions/MyActions"というフォルダを作成し、その中に”GreatAction”を登録する、ということです。では、コマンドラインからActionを起動してみましょう。
@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");
@Override
public Set<org.netbeans.spi.sendopts.Option> getOptions() {
return Collections.singleton(action);
}
@Override
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() {
@Override
public void run() {
//Then find & perform the action:
Action a = findAction(actionName);
//TODO: a null check
a.actionPerformed(null);
}
});
}
}
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で試したところ、無事に動作しました。
つまり、NetBeans上で作成した別のアプリケーションのように、IDE起動のたびに ”hello world” がJOptionPaneに表示されました。
原文はこちら。
http://blogs.sun.com/geertjan/entry/invoke_an_action_from_the
0 件のコメント:
コメントを投稿