sample project as-is
[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         private ArrayList<Image> fImageList;
27
28         private ArrayList<String> fTooltipList;
29
30         private static final String F_SPLASH_EXTENSION_ID = "Sample.splashExtension"; // NON-NLS-1
31
32         private static final String F_ELEMENT_ICON = "icon"; // NON-NLS-1
33
34         private static final String F_ELEMENT_TOOLTIP = "tooltip"; // NON-NLS-1
35
36         private static final String F_DEFAULT_TOOLTIP = "Image"; // NON-NLS-1
37
38         private static final int F_IMAGE_WIDTH = 50;
39
40         private static final int F_IMAGE_HEIGHT = 50;
41
42         private static final int F_SPLASH_SCREEN_BEVEL = 5;
43
44         private Composite fIconPanel;
45
46         /**
47          * 
48          */
49         public ExtensibleSplashHandler() {
50                 fImageList = new ArrayList<>();
51                 fTooltipList = new ArrayList<>();
52                 fIconPanel = null;
53         }
54
55         /*
56          * (non-Javadoc)
57          * @see
58          * org.eclipse.ui.splash.AbstractSplashHandler#init(org.eclipse.swt.widgets.
59          * Shell)
60          */
61         @Override
62         public void init(Shell splash) {
63                 // Store the shell
64                 super.init(splash);
65                 // Configure the shell layout
66                 configureUISplash();
67                 // Load all splash extensions
68                 loadSplashExtensions();
69                 // If no splash extensions were loaded abort the splash handler
70                 if (!hasSplashExtensions()) {
71                         return;
72                 }
73                 // Create UI
74                 createUI();
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
79                 doEventLoop();
80         }
81
82         /**
83          * @return
84          */
85         private boolean hasSplashExtensions() {
86                 return !fImageList.isEmpty();
87         }
88
89         /**
90          * 
91          */
92         private void createUI() {
93                 // Create the icon panel
94                 createUICompositeIconPanel();
95                 // Create the images
96                 createUIImages();
97         }
98
99         /**
100          * 
101          */
102         private void createUIImages() {
103                 Iterator<Image> imageIterator = fImageList.iterator();
104                 Iterator<String> tooltipIterator = fTooltipList.iterator();
105                 int i = 1;
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);
115                         i++;
116                 }
117         }
118
119         /**
120          * @param image
121          * @param tooltip
122          */
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);
128         }
129
130         /**
131          * 
132          */
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
142                 // required.
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);
151         }
152
153         /**
154          * 
155          */
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);
160
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;
165
166                 fIconPanel.setBounds(xCoord, yCoord, xWidth, yWidth);
167         }
168
169         /**
170          * @return
171          */
172         private int getUsableSplashScreenWidth() {
173                 // Splash screen width minus two graphic border bevel widths
174                 return getSplash().getSize().x - (F_SPLASH_SCREEN_BEVEL * 2);
175         }
176
177         /**
178          * 
179          */
180         private void loadSplashExtensions() {
181                 // Get all splash handler extensions
182                 IExtension[] extensions = Platform.getExtensionRegistry().getExtensionPoint(F_SPLASH_EXTENSION_ID)
183                                 .getExtensions();
184                 // Process all splash handler extensions
185                 for (int i = 0; i < extensions.length; i++) {
186                         processSplashExtension(extensions[i]);
187                 }
188         }
189
190         /**
191          * @param extension
192          */
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]);
199                 }
200         }
201
202         /**
203          * @param configurationElement
204          */
205         private void processSplashElements(IConfigurationElement configurationElement) {
206                 // Attribute: icon
207                 processSplashElementIcon(configurationElement);
208                 // Attribute: tooltip
209                 processSplashElementTooltip(configurationElement);
210         }
211
212         /**
213          * @param configurationElement
214          */
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);
221                 } else {
222                         fTooltipList.add(tooltip);
223                 }
224         }
225
226         /**
227          * @param configurationElement
228          */
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)) {
234                         return;
235                 }
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) {
241                         return;
242                 }
243                 // Create the image
244                 Image image = descriptor.createImage();
245                 // Abort if image creation failed
246                 if (image == null) {
247                         return;
248                 }
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
252                         image.dispose();
253                         return;
254                 }
255                 // Store the image and tooltip
256                 fImageList.add(image);
257         }
258
259         /**
260          * 
261          */
262         private void configureUISplash() {
263                 // Configure layout
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);
268         }
269
270         /**
271          * 
272          */
273         private void doEventLoop() {
274                 Shell splash = getSplash();
275                 if (!splash.getDisplay().readAndDispatch()) {
276                         splash.getDisplay().sleep();
277                 }
278         }
279
280         /*
281          * (non-Javadoc)
282          * @see org.eclipse.ui.splash.AbstractSplashHandler#dispose()
283          */
284         @Override
285         public void dispose() {
286                 super.dispose();
287                 // Check to see if any images were defined
288                 if ((fImageList == null) || fImageList.isEmpty()) {
289                         return;
290                 }
291                 // Dispose of all the images
292                 Iterator<Image> iterator = fImageList.iterator();
293                 while (iterator.hasNext()) {
294                         Image image = iterator.next();
295                         image.dispose();
296                 }
297         }
298 }