a netbeans rcp blog

How to add functionality to NetBeans part 2

Posted by Nicklas Löf on February 28, 2011

In my previous post about how to add functionality into NetBeans I did hard code my FaceBook access token into the class which is a bad idea, especially if you want to share the module with someone else. Luckily NetBeans makes all this very easy to solve.

What APIs are we gonna cover?

  • Preferences
  • Dialogs
  • Lets get started!

    First we need to add a dependency to the Dialogs API by right clicking our module, select properties and in the Library panel add it.

    With that done we gonna modify our FaceBookServiceImpl a bit. We gonna add a private method that will make sure that we have a FaceBook access token set and if it isn’t it’s gonna prompt us for it. We also gonna change the ACCESS_TOKEN variable and call the new method from the constructor. So this is how the modified parts will look like:

    public class FaceBookServiceImpl implements FaceBookService {
        private String ACCESS_TOKEN;
        private final InstanceContent instanceContent;
        private final AbstractLookup abstractLookup;
        public FaceBookServiceImpl() {
            instanceContent = new InstanceContent();
            abstractLookup = new AbstractLookup(instanceContent);
        private void initAccessToken() {
            ACCESS_TOKEN = NbPreferences.forModule(FaceBookServiceImpl.class).get("faceBookAccessToken", "");
            if (ACCESS_TOKEN.isEmpty()) {
                NotifyDescriptor.InputLine i = new NotifyDescriptor.InputLine("Please enter your Facebbok Access Token", "Access token", NotifyDescriptor.OK_CANCEL_OPTION, NotifyDescriptor.QUESTION_MESSAGE);
                if (DialogDisplayer.getDefault().notify(i) == NotifyDescriptor.OK_OPTION) {
                    String accesstoken = i.getInputText();
                    if (!accesstoken.isEmpty()) {
                        NbPreferences.forModule(FaceBookService.class).put("faceBookAccessToken", accesstoken);
                        ACCESS_TOKEN = accesstoken;

    First of all it calls the Preferences API and asks for the faceBookAccessToken. If it doesn’t exist we will get back an empty String. So now we need to ask the user for the access token. As you can see we doesn’t have to build any UI components here, NetBeans will take care of this with a NotifyDescriptor where we feed it with the title of the window, the text and what buttons should be visible (Ok and Cancel, Yes and No etc..). Then we take this descriptor and gives it to the DialogDisplayer which will wait until the user has done something and if the user did press Ok we can execute some code.

    From the Notifydescriptor we will now get the string the user did enter. If isn’t empty we will store it into our preferences so the next time the application is started the user doesn’t have to enter the same information again. And don’t forget to actually add the access token into the field ACCESS_TOKEN.

    And thats all! Now our module will remember our access token and you can pass this module around to other users even if the way of getting the access token is a bit non user friendly.

    Home work

    What if the user is entering the wrong access token? Then we will end up with no way to re enter it. You could extend the method to make a call to FaceBook trough the RestFB library and make check that it doesn’t give an access denied message and display this to the user and don’t set the preference until it’s a valid access token.


    Leave a Reply

    Fill in your details below or click an icon to log in: Logo

    You are commenting using your account. Log Out /  Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out /  Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out /  Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out /  Change )


    Connecting to %s

    %d bloggers like this: