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
27 private ArrayList<Image> fImageList;
29 private ArrayList<String> fTooltipList;
31 private static final String F_SPLASH_EXTENSION_ID = "Sample.splashExtension"; // NON-NLS-1
33 private static final String F_ELEMENT_ICON = "icon"; // NON-NLS-1
35 private static final String F_ELEMENT_TOOLTIP = "tooltip"; // NON-NLS-1
37 private static final String F_DEFAULT_TOOLTIP = "Image"; // NON-NLS-1
39 private static final int F_IMAGE_WIDTH = 50;
41 private static final int F_IMAGE_HEIGHT = 50;
43 private static final int F_SPLASH_SCREEN_BEVEL = 5;
45 private Composite fIconPanel;
50 public ExtensibleSplashHandler()
52 fImageList = new ArrayList<>();
53 fTooltipList = new ArrayList<>();
60 * @see org.eclipse.ui.splash.AbstractSplashHandler#init(org.eclipse.swt.widgets. Shell)
63 public void init(Shell splash)
67 // Configure the shell layout
69 // Load all splash extensions
70 loadSplashExtensions();
71 // If no splash extensions were loaded abort the splash handler
72 if (!hasSplashExtensions())
78 // Configure the image panel bounds
79 configureUICompositeIconPanelBounds();
80 // Enter event loop and prevent the RCP application from
81 // loading until all work is done
88 private boolean hasSplashExtensions()
90 return !fImageList.isEmpty();
96 private void createUI()
98 // Create the icon panel
99 createUICompositeIconPanel();
107 private void createUIImages()
109 Iterator<Image> imageIterator = fImageList.iterator();
110 Iterator<String> tooltipIterator = fTooltipList.iterator();
112 int columnCount = ((GridLayout) fIconPanel.getLayout()).numColumns;
113 // Create all the images
114 // Abort if we run out of columns (left-over images will not fit within
115 // the usable splash screen width)
116 while (imageIterator.hasNext() && (i <= columnCount))
118 Image image = imageIterator.next();
119 String tooltip = tooltipIterator.next();
120 // Create the image using a label widget
121 createUILabel(image, tooltip);
130 private void createUILabel(Image image, String tooltip)
132 // Create the label (no text)
133 Label label = new Label(fIconPanel, SWT.NONE);
134 label.setImage(image);
135 label.setToolTipText(tooltip);
141 private void createUICompositeIconPanel()
143 Shell splash = getSplash();
144 // Create the composite
145 fIconPanel = new Composite(splash, SWT.NONE);
146 // Determine the maximum number of columns that can fit on the splash
147 // screen. One 50x50 image per column.
148 int maxColumnCount = getUsableSplashScreenWidth() / F_IMAGE_WIDTH;
149 // Limit size to the maximum number of columns if the number of images
150 // exceed this amount; otherwise, use the exact number of columns
152 int actualColumnCount = Math.min(fImageList.size(), maxColumnCount);
153 // Configure the layout
154 GridLayout layout = new GridLayout(actualColumnCount, true);
155 layout.horizontalSpacing = 0;
156 layout.verticalSpacing = 0;
157 layout.marginHeight = 0;
158 layout.marginWidth = 0;
159 fIconPanel.setLayout(layout);
165 private void configureUICompositeIconPanelBounds()
167 // Determine the size of the panel and position it at the bottom-right
168 // of the splash screen.
169 Point panelSize = fIconPanel.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
171 int xCoord = getSplash().getSize().x - F_SPLASH_SCREEN_BEVEL - panelSize.x;
172 int yCoord = getSplash().getSize().y - F_SPLASH_SCREEN_BEVEL - panelSize.y;
173 int xWidth = panelSize.x;
174 int yWidth = panelSize.y;
176 fIconPanel.setBounds(xCoord, yCoord, xWidth, yWidth);
182 private int getUsableSplashScreenWidth()
184 // Splash screen width minus two graphic border bevel widths
185 return getSplash().getSize().x - (F_SPLASH_SCREEN_BEVEL * 2);
191 private void loadSplashExtensions()
193 // Get all splash handler extensions
194 IExtension[] extensions = Platform.getExtensionRegistry().getExtensionPoint(F_SPLASH_EXTENSION_ID).getExtensions();
195 // Process all splash handler extensions
196 for (int i = 0; i < extensions.length; i++)
198 processSplashExtension(extensions[i]);
205 private void processSplashExtension(IExtension extension)
207 // Get all splash handler configuration elements
208 IConfigurationElement[] elements = extension.getConfigurationElements();
209 // Process all splash handler configuration elements
210 for (int j = 0; j < elements.length; j++)
212 processSplashElements(elements[j]);
217 * @param configurationElement
219 private void processSplashElements(IConfigurationElement configurationElement)
222 processSplashElementIcon(configurationElement);
223 // Attribute: tooltip
224 processSplashElementTooltip(configurationElement);
228 * @param configurationElement
230 private void processSplashElementTooltip(IConfigurationElement configurationElement)
232 // Get attribute tooltip
233 String tooltip = configurationElement.getAttribute(F_ELEMENT_TOOLTIP);
234 // If a tooltip is not defined, give it a default
235 if ((tooltip == null) || (tooltip.length() == 0))
237 fTooltipList.add(F_DEFAULT_TOOLTIP);
240 fTooltipList.add(tooltip);
245 * @param configurationElement
247 private void processSplashElementIcon(IConfigurationElement configurationElement)
249 // Get attribute icon
250 String iconImageFilePath = configurationElement.getAttribute(F_ELEMENT_ICON);
251 // Abort if an icon attribute was not specified
252 if ((iconImageFilePath == null) || (iconImageFilePath.length() == 0))
256 // Create a corresponding image descriptor
257 ImageDescriptor descriptor = AbstractUIPlugin.imageDescriptorFromPlugin(configurationElement.getNamespaceIdentifier(),
259 // Abort if no corresponding image was found
260 if (descriptor == null)
265 Image image = descriptor.createImage();
266 // Abort if image creation failed
271 // Abort if the image does not have dimensions of 50x50
272 if ((image.getBounds().width != F_IMAGE_WIDTH) || (image.getBounds().height != F_IMAGE_HEIGHT))
274 // Dipose of the image
278 // Store the image and tooltip
279 fImageList.add(image);
285 private void configureUISplash()
288 GridLayout layout = new GridLayout(1, true);
289 getSplash().setLayout(layout);
290 // Force shell to inherit the splash background
291 getSplash().setBackgroundMode(SWT.INHERIT_DEFAULT);
297 private void doEventLoop()
299 Shell splash = getSplash();
300 if (!splash.getDisplay().readAndDispatch())
302 splash.getDisplay().sleep();
309 * @see org.eclipse.ui.splash.AbstractSplashHandler#dispose()
312 public void dispose()
315 // Check to see if any images were defined
316 if ((fImageList == null) || fImageList.isEmpty())
320 // Dispose of all the images
321 Iterator<Image> iterator = fImageList.iterator();
322 while (iterator.hasNext())
324 Image image = iterator.next();