/bin/sh "/home/geertjan/netbeans-6.0m10/bin/netbeans" --name admin
So, if the "name" option is not set to "admin", we will see a greyed out Admin menu item in the Tools menu:
"What?! Does that mean that my application on the NetBeans Platform can somehow parse the command line?!" Yes, that's exactly what that means. "But... wow... isn't that amazing?!" Well, yes, if you think so. :-)
To see how everything fits together, do the following:
@Override
protected boolean enable(Node[] arg0) {
super.enable(arg0);
if (getUserName().equals("admin")) {
return true;
}
return false;
}
So, only if a method called getUserName returns "admin", will the menu item be enabled. In all other cases, it will be disabled, i.e., greyed out.
private static String name;
static void setName(String string) {
name = string;
}
private String getUserName(){
return name;
}
Create a class called MyOptions and let it extend OptionProcessor. You will find that you now need to implement two methods, called getOptions and process. The names of these methods are very clear. The getOptions method is for... getting the options from the command line. The process method is for... doing something useful with the options that the getOptions method got for you.
But let's first define a new option. "Oh my god! I can define new command line options? You've got to be kidding me! That is so cool!" Relax, okay. In the greater scheme of things all this is arbitrary. But, nevertheless, here's our new option, neatly declared at the top of the class:
private static Option name = Option.requiredArgument(Option.NO_SHORT_NAME, "name");
Here, the option is required. However, it could also be an optional option instead. A variety of alternative methods could be set on the Option object. So, here we have an option called 'name'. And there's an 'Option.NO_SHORT_NAME' constant which, when I consult the javadoc, is a "constant that represents no short name indicator". Hmm. Not much wiser about that one then.
Next, we implement the two required methods:
public Set getOptions() {
return Collections.singleton(name);
}
public void process(Env env, Map values) throws CommandException {
String[] args = (String[]) values.get(name );
if (args.length > 0) {
StatusDisplayer.getDefault().setStatusText("Hello " + args[0]);
SomeAction.setName(args[0]);
}
}
So, if getOptions returns an argument, we take the first option and we greet our user in the status bar, plus we pass the name to our CookieAction class. And that's all there is to it, in terms of coding.
Further reading:
And now... happy command line parsing!
Hello Geertjan,
what do I have to do in a NB5.5.1 project to parse command line options?
Currently I cannot move to 6.0
Any hint is welcome
Thanks
Hermann
Then you can't parse command line options, because this API is part of 6.0.
I tried your example with another purpose, disabling certain functionality for some user type at login.
I work with Netbeans 5.5.1 but couldn't get this done. Is there any version limitation for this?
The problem is that I override the method but it's never called.
I am puzzled regarding the Conditionally Enabled. Which should I choose from Project, Open Cookie, Edit Cookie, Editor Cookie, Data Object - Project doesn't work as it needs the Project class and the wizzard is adding the Project API dependency but the Project class seems not to be in there.
Please help me get this done.
Thanks in advance,
Laurentiu Trica
This is a new API in 6.0. You cannot do this in 5.5.1. Please don't even try.
Have NetBeans updates made these instructions erroneous?
With NB 7.1, running the Action wizard produces a class implementing ActionListener, not CookieAction. So the instructions veer off into the woods at step 1 -- enable(Node[]) is not a method defined in the interface.
What is the right thing to do in NB 7.1?
Thanks!
Rewrite the ActionListener to implement CookieAction and remove the annotations, then register the CookieAction into the layer.xml manually.