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