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