Text On Splash Screen

You may have heard a variation of this at some point …

A lazy programmer is not necessarily a good programmer … but a good programmer is a lazy programmer.

I fully subscribe to that statement … good programmers are always looking for ways to make their jobs easier.  Either by automating some repetitive function or making some functionality easier to change in the future.

Recently I had to do some re-branding on one of my eclipse based applications.  One thing that I had to do was update the splash screen that was displayed in the Eclipse RCP to display different text.

We had the basic graphic and all we needed was to have our designer update the text that was displayed on the bottom.  I sent the graphic over and the new text and the designer sent it back to me.

The problem was, I got the text wrong.  I knew I was going to have to go back to the designer and ask her to redo the graphic.  Luckily she’s a very nice person and never had a problem with this.

But that got me thinking … what if I could overlay text on the graphic at runtime so I wouldn’t have to go back to the designer every time the text had to be updated.

I did a bit of digging and figured out a way to do it.

Example splash with text

First you have to register the splash handler class and bind it to the RCP product id.

This goes into the plugin.xml file:

 <extension
       point="org.eclipse.ui.splashHandlers">
    <splashHandler
          class="net.geekyramblings.rcp.splashHandlers.TextOverlaySplashHandler"
          id="net.geekyramblings.rcp.splashHandlers.text">
    </splashHandler>
    <splashHandlerProductBinding
          productId="net.geekyramblings.rcp.product"
          splashId="net.geekyramblings.rcp.splashHandlers.text">
    </splashHandlerProductBinding>
 </extension>

Next you have the actual code …

[java highlight=”68,54″]package net.geekyramblings.rcp.splashHandlers;

import org.eclipse.core.runtime.Platform;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.splash.AbstractSplashHandler;
import org.osgi.framework.Bundle;

/**
* Display standard splash screen text while splash screen
* is displaying
*/
public class TextOverlaySplashHandler
extends AbstractSplashHandler {

private Composite textPanel = null;

public TextOverlaySplashHandler() {
// nothing
}

private String getSplashText() {
String splashText = "This text will be displayed on " +
"the splash screen as it is shown";

return splashText;
}

public void init(final Shell splash) {
// Store the shell
super.init(splash);

// Configure the shell layout
configureUISplash();
// Create UI
createUI();
// Force the UI to layout
splash.layout(true);

// Keep the splash screen visible and prevent the RCP
// application from loading until the close button is
// clicked.
doEventLoop();
}

private void doEventLoop() {
// pause for 3 seconds so the text displays
try {
Display.getCurrent().update();
Thread.sleep(3000);
} catch (InterruptedException e) {
// nothing
}
}

private void createUI() {

Shell splash = getSplash();
// set background for Label text so it is transparent
splash.setBackgroundMode(SWT.INHERIT_DEFAULT);

// create a text panel positioned at the bottom center
// of the splash screen graphic.
textPanel = new Composite(splash, SWT.NONE);
textPanel.setLayoutData(new GridData(SWT.CENTER, SWT.END,
true,true));

textPanel.setLayout(new GridLayout(1, true));

Label l = new Label(textPanel, SWT.NONE);
l.setText(getSplashText());
Display d = l.getDisplay();
Color white = d.getSystemColor(SWT.COLOR_BLACK);
l.setForeground(white);
l.setLayoutData(new GridData(SWT.CENTER, SWT.END,
true,true));
}

private void configureUISplash() {
GridLayout layout = new GridLayout(1, true);
getSplash().setLayout(layout);
}

/**
* @see org.eclipse.ui.splash.AbstractSplashHandler#dispose()
*/
@Override
public void dispose() {
textPanel.dispose();

super.dispose();
}

}[/java]

This example displays static text at the bottom of the splash screen image.

You can use standard SWT layout methods to style & position the text that’s being displayed.

Setting the background mode for the splash screen shell to SWT.INHERIT_DEFAULT (line 68) makes the background of the Label text transparent.

The logic in the doEventLoop() method (line 54) causes the splash screen to render and then wait 3 seconds so the text can be read.

One thing to be aware of … there is a pause between when the splash screen graphic is displayed and the text is overlayed.

If you want the splash screen text to come from a property file, you can use the standard methods … or, if you would like the text to come from the plugin.property file, you can use this technique…

[java]Bundle bundle = plugin.getBundle();

String splashText = Platform.getResourceString(bundle,
"%splashText");[/java]

This code assumes you can get a handle to your plug-in object.  From the plug-in object, you get it’s Bundle, and using that you can get a resource string from the plugin.properties file. The entry in the property file would be:

splashText=This text will be displayed on the splash screen as it is shown

I always put in a static ‘getDefault()’ method in my plug-in class so I can easily get a handle to the singleton plug-in object.

Leave a Reply

Your email address will not be published. Required fields are marked *