Getting the User's Name

When building plugins, it can be useful to know the user’s name, like the track changes functionality does. There are two key parts to this:

  1. Retrieving the current user name
  2. Prompting the user for their name if one hasn’t already been set

Retrieving the Current User Name

The current user name can easily be retrieved from the OperationManager - the class that manages all the track changes data:

String username = editliveBean.getOperationManager().getUsername();

In some situations however, the username may be returned as null.  This occurs when the username has not been previously set. In that case we need to prompt for the user name.

Prompting the User for Their Name

While you could just display your own custom dialog to ask the user for their name, if they later used track changes they would be prompted a second time.  By reusing the same dialog the user name will be set once and used whenever it’s needed. To do this, simply fire the TextEvent.SET_USERNAME event with null as the extra string:

editliveBean.raiseEvent(new TextEvent(this, TextEvent.SET_USERNAME, null, -1));

Once the method returns, retrieve the username from the operation manager as normal.

It is possible for the user to cancel the dialog that asks them for their name.  If the username is still returned as null after firing the SET_USERNAME event, you should either proceed without a username or cancel the operation.