X

Geertjan's Blog

  • June 1, 2010

Not Enabled for Henry

Geertjan Wielenga
Product Manager
If the command line does not contain the correct name, i.e., as follows:
run.args.extra=--name admin

...then the Admin toolbar button (on the extreme left below) is disabled, e.g., here "henry" was passed in:

...otherwise, i.e., if "admin" is passed in, the menu item is enabled:

How to do this, as simple as possible:

  1. Create a User POJO:
    public class User {
    static String name;
    public static String getName() {
    return name;
    }
    public void setName(String name) {
    User.name = name;
    }
    }

  2. Create an OptionProcessor and register it in META-INF/services via the @ServiceProvider annotation:
    @ServiceProvider(service = OptionProcessor.class)
    public class ArgumentsOptionProcessor extends OptionProcessor {
    private static Option name = Option.requiredArgument(Option.NO_SHORT_NAME, "name");
    @Override
    public Set<org.netbeans.spi.sendopts.Option> getOptions() {
    return Collections.singleton(name);
    }
    @Override
    protected void process(Env env, Map<Option, String[]> values) throws CommandException {
    String[] args = (String[]) values.get(name);
    if (args.length > 0) {
    User user = new User();
    user.setName(args[0]);
    }
    }
    }

  3. Create an Action class that is enabled if the correct user name is set:
    public final class SomeAction extends AbstractAction {
    Image icon = ImageUtilities.loadImage("/org/simple/demo/icon.png", true);
    public SomeAction() {
    putValue(Action.SMALL_ICON, icon);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
    //do something relevant to admin users
    }
    @Override
    public boolean isEnabled() {
    if (User.getName().equals("admin")) {
    putValue(Action.NAME, "admin functionality");
    return true;
    }
    putValue(Action.NAME, "not enabled for you, " + User.getName() + "!");
    return false;
    }
    }

  4. Register the Action so that it is loaded eagerly, i.e., not lazily via a Actions\* factory method:
    <folder name="Actions">
    <folder name="Build">
    <file name="org-simple-demo-SomeAction.instance">
    <attr name="instanceCreate" newvalue="org.simple.demo.SomeAction"/>
    </file>
    </folder>
    </folder>
    <folder name="Toolbars">
    <folder name="File">
    <file name="org-simple-demo-SomeAction.shadow">
    <attr name="originalFile" stringvalue="Actions/Build/org-simple-demo-SomeAction.instance"/>
    <attr name="position" intvalue="0"/>
    </file>
    </folder>
    </folder>

That's it, you're done, the menu item is only enabled when the correct user signs in from the command line.

Join the discussion

Comments ( 6 )
  • Thierry Tuesday, June 1, 2010

    Hello Geertjean

    As you have done for the macros (http://netbeans.dzone.com/nb-java-editor-macro-syntax) is it possible to list all available actions for the IDE or for the Nb platform?

    Thanks.


  • Geertjan Wielenga Tuesday, June 1, 2010

    Here all the actions are found:

    Tools | Options | Keymap


  • Santhosh Kumar T Wednesday, June 2, 2010

    > User user = new User();

    > user.setName(args[0])

    here no need to instantiate User class. You can simply do:

    User.setName(args[0])


  • John Sunday, June 6, 2010

    Hello Geertijan,

    About how to config different user with different menu function and window layout when they log in?

    This give me a hint to handle the menu function

    http://blogs.sun.com/geertjan/entry/injecting_a_layer_file_into

    But how about the top component window layout, do you have any suggestions?

    Can NetBeans have any method to change the "user dir" at runtime?

    Thanks


  • John Sunday, June 6, 2010

    Hello Geertjan,

    About how to config different user with different menu function and window layout when they log in.

    How can i save/load the last log in status of the top component window layout with different user?

    Do you have any suggestion to help me?

    Thanks


  • Jesse Glick Monday, June 7, 2010

    BTW the instanceCreate is unnecessary in this case.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha