Create Reusable OSGi Components for Eclipse Equinox
Plug-in Extensions
Because this bundle uses two extension points (namely, Splash Handler and Preference Pages), you need to specify the same in plug-in.xml
. The class node specifies the class name that extends the specific extension point. Below is a snapshot of plug-in.xml
:
<plugin>
<extension
point="org.eclipse.ui.splashHandlers">
<splashHandler
class="com.infy.setl.common.login.ui.LoginHandler"
id="CollaborationCommonServices">
</splashHandler>
<splashHandlerProductBinding
splashId="CollaborationCommonServices"
productId="org.eclipse.platform.ide">
</splashHandlerProductBinding>
</extension>
<extension
point="org.eclipse.ui.preferencePages">
<page
name="UserConfiguration"
class="com.infy.setl.common.login.preferences.DeWConfigurationPreferencePage"
id="DeW.User.Preferences">
</page>
</extension>
</plugin>
Splash Handler is an Eclipse extension point that produces a splash screen, a UI screen that appears at boot time. It is primarily provided to allow you to customize the look of the Eclipse boot time screen. As such, you can configure the screen further to have different animations, images, etc. The Splash Handler extension point definition in plug-in.xml
contains a couple of extra nodes, namely splashId and productId. All the IDs defined in the plug-in.xml
should always be unique because Equinox uses these IDs to uniquely identify a particular plug-in. During the bootstrapping process, you can customize the image location so that Eclipse picks up your defined location image for display rather than the default one.
<extension
point="org.eclipse.ui.splashHandlers">
<splashHandler
class="com.infy.setl.common.login.ui.LoginHandler"
id="CollaborationCommonServices">
</splashHandler>
<splashHandlerProductBinding
splashId="CollaborationCommonServices"
productId="org.eclipse.platform.ide">
</splashHandlerProductBinding>
</extension>
Next, you create a class extending AbstractSplashHandler. You override the init
method to customize the way Eclipse loads.
public void init(final Shell splash) {
// Store the shell
//javaLogger.entering("In Login Handler", "In init");
javaLogger.log(Level.INFO, "In Init");
super.init(splash);
// Configure the shell layout
configureUISplash();
// Create UI
initShell(splash);
createUI();
// Create UI listeners
addListeners();
// Force the splash screen to layout
splash.layout(true);
// Keep the splash screen visible and prevent the RCP application from
// loading until the close button is clicked.
closeWindow();
}
You then add a few lines of code to specify the image location for Eclipse to load.
private void configureUISplash() {
// Configure layout
javaLogger.log(Level.INFO, "In UI Splash");
FillLayout layout = new FillLayout();
getSplash().setLayout(layout);
// Force shell to inherit the splash background
getSplash().setBackgroundMode(SWT.INHERIT_DEFAULT);
try {
getSplash().setBackgroundImage(getImage());
}
catch (IOException e) {
javaLogger.log(Level.WARNING,"Image not found",e);
}
}
private Image getImage() throws IOException {
Bundle bundle = Platform.getBundle(ICommonConstants.MY_PLUGIN_ID);
Path path = new Path(ICommonConstants.RELATIVE_PATH_FOR_COLLABORATION_ECLIPSE_IMAGE);
URL fileURL = FileLocator.find(bundle, path, null);
InputStream in = fileURL.openStream();
return new Image(null,in);
}
Now you can add your desired number of widgets to get an output like Figure 9. In this example, you have added two textboxes and two buttons for the login screen.
Now you're ready to move on to preference pages. The Eclipse Preference Page is a common dialog box provided where the user can configure the way Eclipse behaves. The user can set or edit his/her credentials from this page. Below is the way to configure Preference Pages in the plug-in.xml
.
<extension
point="org.eclipse.ui.preferencePages">
<page
name="UserConfiguration"
class="com.infy.setl.common.login.preferences.DeWConfigurationPreferencePage"
id="DeW.User.Preferences">
</page>
</extension>
You extend the PreferencePage class to add a new block to the Preference Page, and then you override the createContents method.
protected Control createContents(final Composite parent)
{
javaLogger.log(Level.INFO,"In PreferencePage");
super.noDefaultAndApplyButton();
setParent(parent);
Composite composite = new Composite(parent, SWT.NULL);
composite.setSize(300,120);
Group groupMainShell = new Group(composite, SWT.NULL);
groupMainShell.setLayout(new GridLayout(2, true));
groupMainShell.setText("Login Credentials");
groupMainShell.setBounds(10, 10, 220,95);
GridData data;
labelUserName = new Label(groupMainShell, SWT.NONE);
labelUserName.setText("UserName");
data = new GridData(GridData.FILL_HORIZONTAL);
data.grabExcessHorizontalSpace = true;
labelUserName.setLayoutData(data);
labelUserName.setLocation(15, 30);
labelUserName.pack();
textBoxUserLogin = new Text(groupMainShell, SWT.BORDER);
data = new GridData(GridData.FILL_HORIZONTAL);
data.grabExcessHorizontalSpace = true;
textBoxUserLogin.setLayoutData(data);
textBoxUserLogin.setLocation(80, 28);
textBoxUserLogin.setSize(120, 20);
labelPassword = new Label(groupMainShell, SWT.NONE);
labelPassword.setText("Password");
data = new GridData(GridData.FILL_HORIZONTAL);
data.grabExcessHorizontalSpace = true;
labelPassword.setLayoutData(data);
labelPassword.setLocation(15, 60);
labelPassword.pack();
textBoxPassword = new Text(groupMainShell, SWT.BORDER|SWT.PASSWORD);
data = new GridData(GridData.FILL_HORIZONTAL);
data.grabExcessHorizontalSpace = true;
textBoxPassword.setLayoutData(data);
textBoxPassword.setLocation(80, 58);
textBoxPassword.setSize(120, 20);
// System.out.println("In Preference Page: "+ System.currentTimeMillis());
userCreds = new UserCredentials();
userSessionService = DeWCommonAuthorisationUtil.getUserSessionObject();
if(userSessionService!=null){
if (userSessionService.get(System.getenv("USERNAME")) == null )
{
userCreds.setUserId(System.getenv("USERNAME"));
textBoxUserLogin.setText(userCreds.getUserId());
}
else {
textBoxUserLogin.setText(((IUserCredentials)(userSessionService.get(System.getenv("USERNAME")))).getUserId());
textBoxPassword.setText(((IUserCredentials)(userSessionService.get(System.getenv("USERNAME")))).getPassword());
setPassword(((IUserCredentials)(userSessionService.get(System.getenv("USERNAME")))).getPassword());
userCreds.setUserId(((IUserCredentials)(userSessionService.get(System.getenv("USERNAME")))).getUserId());
}
}
textBoxPassword.addModifyListener(new ModifyListener(){
public void modifyText(ModifyEvent arg0) {
// TODO Auto-generated method stub
setPassword(textBoxPassword.getText());
}} );
return null;
}
The output of the above code would look like similar to Figure 10.
The final piece is a login pop-up window, which is a simple SWT window with user name and password text fields. It is an event-based pop-up window that collects the credentials from the user (see Figure 11).
Page 2 of 3
This article was originally published on March 18, 2010