1 package sampleercp.splashhandlers;
3 import java.util.ArrayList;
4 import java.util.Iterator;
6 import org.eclipse.core.runtime.IConfigurationElement;
7 import org.eclipse.core.runtime.IExtension;
8 import org.eclipse.core.runtime.Platform;
9 import org.eclipse.jface.resource.ImageDescriptor;
10 import org.eclipse.swt.SWT;
11 import org.eclipse.swt.graphics.Image;
12 import org.eclipse.swt.graphics.Point;
13 import org.eclipse.swt.layout.GridLayout;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.swt.widgets.Label;
16 import org.eclipse.swt.widgets.Shell;
17 import org.eclipse.ui.plugin.AbstractUIPlugin;
18 import org.eclipse.ui.splash.AbstractSplashHandler;
24 public class ExtensibleSplashHandler extends AbstractSplashHandler {
26 private ArrayList<Image> fImageList;
28 private ArrayList<String> fTooltipList;
30 private static final String F_SPLASH_EXTENSION_ID = "Sample.splashExtension"; // NON-NLS-1
32 private static final String F_ELEMENT_ICON = "icon"; // NON-NLS-1
34 private static final String F_ELEMENT_TOOLTIP = "tooltip"; // NON-NLS-1
36 private static final String F_DEFAULT_TOOLTIP = "Image"; // NON-NLS-1
38 private static final int F_IMAGE_WIDTH = 50;
40 private static final int F_IMAGE_HEIGHT = 50;
42 private static final int F_SPLASH_SCREEN_BEVEL = 5;
44 private Composite fIconPanel;
49 public ExtensibleSplashHandler() {
50 fImageList = new ArrayList<>();
51 fTooltipList = new ArrayList<>();
58 * org.eclipse.ui.splash.AbstractSplashHandler#init(org.eclipse.swt.widgets.
62 public void init(Shell splash) {
65 // Configure the shell layout
67 // Load all splash extensions
68 loadSplashExtensions();
69 // If no splash extensions were loaded abort the splash handler
70 if (!hasSplashExtensions()) {
75 // Configure the image panel bounds
76 configureUICompositeIconPanelBounds();
77 // Enter event loop and prevent the RCP application from
78 // loading until all work is done
85 private boolean hasSplashExtensions() {
86 return !fImageList.isEmpty();
92 private void createUI() {
93 // Create the icon panel
94 createUICompositeIconPanel();
102 private void createUIImages() {
103 Iterator<Image> imageIterator = fImageList.iterator();
104 Iterator<String> tooltipIterator = fTooltipList.iterator();
106 int columnCount = ((GridLayout) fIconPanel.getLayout()).numColumns;
107 // Create all the images
108 // Abort if we run out of columns (left-over images will not fit within
109 // the usable splash screen width)
110 while (imageIterator.hasNext() && (i <= columnCount)) {
111 Image image = imageIterator.next();
112 String tooltip = tooltipIterator.next();
113 // Create the image using a label widget
114 createUILabel(image, tooltip);
123 private void createUILabel(Image image, String tooltip) {
124 // Create the label (no text)
125 Label label = new Label(fIconPanel, SWT.NONE);
126 label.setImage(image);
127 label.setToolTipText(tooltip);
133 private void createUICompositeIconPanel() {
134 Shell splash = getSplash();
135 // Create the composite
136 fIconPanel = new Composite(splash, SWT.NONE);
137 // Determine the maximum number of columns that can fit on the splash
138 // screen. One 50x50 image per column.
139 int maxColumnCount = getUsableSplashScreenWidth() / F_IMAGE_WIDTH;
140 // Limit size to the maximum number of columns if the number of images
141 // exceed this amount; otherwise, use the exact number of columns
143 int actualColumnCount = Math.min(fImageList.size(), maxColumnCount);
144 // Configure the layout
145 GridLayout layout = new GridLayout(actualColumnCount, true);
146 layout.horizontalSpacing = 0;
147 layout.verticalSpacing = 0;
148 layout.marginHeight = 0;
149 layout.marginWidth = 0;
150 fIconPanel.setLayout(layout);
156 private void configureUICompositeIconPanelBounds() {
157 // Determine the size of the panel and position it at the bottom-right
158 // of the splash screen.
159 Point panelSize = fIconPanel.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
161 int xCoord = getSplash().getSize().x - F_SPLASH_SCREEN_BEVEL - panelSize.x;
162 int yCoord = getSplash().getSize().y - F_SPLASH_SCREEN_BEVEL - panelSize.y;
163 int xWidth = panelSize.x;
164 int yWidth = panelSize.y;
166 fIconPanel.setBounds(xCoord, yCoord, xWidth, yWidth);
172 private int getUsableSplashScreenWidth() {
173 // Splash screen width minus two graphic border bevel widths
174 return getSplash().getSize().x - (F_SPLASH_SCREEN_BEVEL * 2);
180 private void loadSplashExtensions() {
181 // Get all splash handler extensions
182 IExtension[] extensions = Platform.getExtensionRegistry().getExtensionPoint(F_SPLASH_EXTENSION_ID)
184 // Process all splash handler extensions
185 for (int i = 0; i < extensions.length; i++) {
186 processSplashExtension(extensions[i]);
193 private void processSplashExtension(IExtension extension) {
194 // Get all splash handler configuration elements
195 IConfigurationElement[] elements = extension.getConfigurationElements();
196 // Process all splash handler configuration elements
197 for (int j = 0; j < elements.length; j++) {
198 processSplashElements(elements[j]);
203 * @param configurationElement
205 private void processSplashElements(IConfigurationElement configurationElement) {
207 processSplashElementIcon(configurationElement);
208 // Attribute: tooltip
209 processSplashElementTooltip(configurationElement);
213 * @param configurationElement
215 private void processSplashElementTooltip(IConfigurationElement configurationElement) {
216 // Get attribute tooltip
217 String tooltip = configurationElement.getAttribute(F_ELEMENT_TOOLTIP);
218 // If a tooltip is not defined, give it a default
219 if ((tooltip == null) || (tooltip.length() == 0)) {
220 fTooltipList.add(F_DEFAULT_TOOLTIP);
222 fTooltipList.add(tooltip);
227 * @param configurationElement
229 private void processSplashElementIcon(IConfigurationElement configurationElement) {
230 // Get attribute icon
231 String iconImageFilePath = configurationElement.getAttribute(F_ELEMENT_ICON);
232 // Abort if an icon attribute was not specified
233 if ((iconImageFilePath == null) || (iconImageFilePath.length() == 0)) {
236 // Create a corresponding image descriptor
237 ImageDescriptor descriptor = AbstractUIPlugin
238 .imageDescriptorFromPlugin(configurationElement.getNamespaceIdentifier(), iconImageFilePath);
239 // Abort if no corresponding image was found
240 if (descriptor == null) {
244 Image image = descriptor.createImage();
245 // Abort if image creation failed
249 // Abort if the image does not have dimensions of 50x50
250 if ((image.getBounds().width != F_IMAGE_WIDTH) || (image.getBounds().height != F_IMAGE_HEIGHT)) {
251 // Dipose of the image
255 // Store the image and tooltip
256 fImageList.add(image);
262 private void configureUISplash() {
264 GridLayout layout = new GridLayout(1, true);
265 getSplash().setLayout(layout);
266 // Force shell to inherit the splash background
267 getSplash().setBackgroundMode(SWT.INHERIT_DEFAULT);
273 private void doEventLoop() {
274 Shell splash = getSplash();
275 if (!splash.getDisplay().readAndDispatch()) {
276 splash.getDisplay().sleep();
282 * @see org.eclipse.ui.splash.AbstractSplashHandler#dispose()
285 public void dispose() {
287 // Check to see if any images were defined
288 if ((fImageList == null) || fImageList.isEmpty()) {
291 // Dispose of all the images
292 Iterator<Image> iterator = fImageList.iterator();
293 while (iterator.hasNext()) {
294 Image image = iterator.next();