Made formatting uniform - commit for SampleERCP
[Mograsim.git] / SampleERCP / src / sampleercp / splashhandlers / ExtensibleSplashHandler.java
1 package sampleercp.splashhandlers;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5
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;
19
20 /**
21  * @since 3.3
22  *
23  */
24 public class ExtensibleSplashHandler extends AbstractSplashHandler
25 {
26
27         private ArrayList<Image> fImageList;
28
29         private ArrayList<String> fTooltipList;
30
31         private static final String F_SPLASH_EXTENSION_ID = "Sample.splashExtension"; // NON-NLS-1
32
33         private static final String F_ELEMENT_ICON = "icon"; // NON-NLS-1
34
35         private static final String F_ELEMENT_TOOLTIP = "tooltip"; // NON-NLS-1
36
37         private static final String F_DEFAULT_TOOLTIP = "Image"; // NON-NLS-1
38
39         private static final int F_IMAGE_WIDTH = 50;
40
41         private static final int F_IMAGE_HEIGHT = 50;
42
43         private static final int F_SPLASH_SCREEN_BEVEL = 5;
44
45         private Composite fIconPanel;
46
47         /**
48          * 
49          */
50         public ExtensibleSplashHandler()
51         {
52                 fImageList = new ArrayList<>();
53                 fTooltipList = new ArrayList<>();
54                 fIconPanel = null;
55         }
56
57         /*
58          * (non-Javadoc)
59          * 
60          * @see org.eclipse.ui.splash.AbstractSplashHandler#init(org.eclipse.swt.widgets. Shell)
61          */
62         @Override
63         public void init(Shell splash)
64         {
65                 // Store the shell
66                 super.init(splash);
67                 // Configure the shell layout
68                 configureUISplash();
69                 // Load all splash extensions
70                 loadSplashExtensions();
71                 // If no splash extensions were loaded abort the splash handler
72                 if (!hasSplashExtensions())
73                 {
74                         return;
75                 }
76                 // Create UI
77                 createUI();
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
82                 doEventLoop();
83         }
84
85         /**
86          * @return
87          */
88         private boolean hasSplashExtensions()
89         {
90                 return !fImageList.isEmpty();
91         }
92
93         /**
94          * 
95          */
96         private void createUI()
97         {
98                 // Create the icon panel
99                 createUICompositeIconPanel();
100                 // Create the images
101                 createUIImages();
102         }
103
104         /**
105          * 
106          */
107         private void createUIImages()
108         {
109                 Iterator<Image> imageIterator = fImageList.iterator();
110                 Iterator<String> tooltipIterator = fTooltipList.iterator();
111                 int i = 1;
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))
117                 {
118                         Image image = imageIterator.next();
119                         String tooltip = tooltipIterator.next();
120                         // Create the image using a label widget
121                         createUILabel(image, tooltip);
122                         i++;
123                 }
124         }
125
126         /**
127          * @param image
128          * @param tooltip
129          */
130         private void createUILabel(Image image, String tooltip)
131         {
132                 // Create the label (no text)
133                 Label label = new Label(fIconPanel, SWT.NONE);
134                 label.setImage(image);
135                 label.setToolTipText(tooltip);
136         }
137
138         /**
139          * 
140          */
141         private void createUICompositeIconPanel()
142         {
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
151                 // required.
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);
160         }
161
162         /**
163          * 
164          */
165         private void configureUICompositeIconPanelBounds()
166         {
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);
170
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;
175
176                 fIconPanel.setBounds(xCoord, yCoord, xWidth, yWidth);
177         }
178
179         /**
180          * @return
181          */
182         private int getUsableSplashScreenWidth()
183         {
184                 // Splash screen width minus two graphic border bevel widths
185                 return getSplash().getSize().x - (F_SPLASH_SCREEN_BEVEL * 2);
186         }
187
188         /**
189          * 
190          */
191         private void loadSplashExtensions()
192         {
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++)
197                 {
198                         processSplashExtension(extensions[i]);
199                 }
200         }
201
202         /**
203          * @param extension
204          */
205         private void processSplashExtension(IExtension extension)
206         {
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++)
211                 {
212                         processSplashElements(elements[j]);
213                 }
214         }
215
216         /**
217          * @param configurationElement
218          */
219         private void processSplashElements(IConfigurationElement configurationElement)
220         {
221                 // Attribute: icon
222                 processSplashElementIcon(configurationElement);
223                 // Attribute: tooltip
224                 processSplashElementTooltip(configurationElement);
225         }
226
227         /**
228          * @param configurationElement
229          */
230         private void processSplashElementTooltip(IConfigurationElement configurationElement)
231         {
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))
236                 {
237                         fTooltipList.add(F_DEFAULT_TOOLTIP);
238                 } else
239                 {
240                         fTooltipList.add(tooltip);
241                 }
242         }
243
244         /**
245          * @param configurationElement
246          */
247         private void processSplashElementIcon(IConfigurationElement configurationElement)
248         {
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))
253                 {
254                         return;
255                 }
256                 // Create a corresponding image descriptor
257                 ImageDescriptor descriptor = AbstractUIPlugin.imageDescriptorFromPlugin(configurationElement.getNamespaceIdentifier(),
258                                 iconImageFilePath);
259                 // Abort if no corresponding image was found
260                 if (descriptor == null)
261                 {
262                         return;
263                 }
264                 // Create the image
265                 Image image = descriptor.createImage();
266                 // Abort if image creation failed
267                 if (image == null)
268                 {
269                         return;
270                 }
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))
273                 {
274                         // Dipose of the image
275                         image.dispose();
276                         return;
277                 }
278                 // Store the image and tooltip
279                 fImageList.add(image);
280         }
281
282         /**
283          * 
284          */
285         private void configureUISplash()
286         {
287                 // Configure layout
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);
292         }
293
294         /**
295          * 
296          */
297         private void doEventLoop()
298         {
299                 Shell splash = getSplash();
300                 if (!splash.getDisplay().readAndDispatch())
301                 {
302                         splash.getDisplay().sleep();
303                 }
304         }
305
306         /*
307          * (non-Javadoc)
308          * 
309          * @see org.eclipse.ui.splash.AbstractSplashHandler#dispose()
310          */
311         @Override
312         public void dispose()
313         {
314                 super.dispose();
315                 // Check to see if any images were defined
316                 if ((fImageList == null) || fImageList.isEmpty())
317                 {
318                         return;
319                 }
320                 // Dispose of all the images
321                 Iterator<Image> iterator = fImageList.iterator();
322                 while (iterator.hasNext())
323                 {
324                         Image image = iterator.next();
325                         image.dispose();
326                 }
327         }
328 }