KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > product > UpdateSplashHandlerAction


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.pde.internal.ui.wizards.product;
13
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.jface.action.Action;
17 import org.eclipse.osgi.util.NLS;
18 import org.eclipse.pde.core.plugin.IPluginAttribute;
19 import org.eclipse.pde.core.plugin.IPluginElement;
20 import org.eclipse.pde.core.plugin.IPluginExtension;
21 import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
22 import org.eclipse.pde.core.plugin.IPluginModelBase;
23 import org.eclipse.pde.core.plugin.IPluginObject;
24 import org.eclipse.pde.internal.core.util.PDETextHelper;
25 import org.eclipse.pde.internal.ui.PDEUIMessages;
26
27 /**
28  * UpdateSplashHandlerInModelAction
29  *
30  */

31 public class UpdateSplashHandlerAction extends Action implements ISplashHandlerConstants {
32
33     private IPluginModelBase fModel;
34     
35     private IProgressMonitor fMonitor;
36     
37     private CoreException fException;
38     
39     private String JavaDoc fFieldID;
40
41     private String JavaDoc fFieldSplashID;
42     
43     private String JavaDoc fFieldProductID;
44     
45     private String JavaDoc fFieldClass;
46     
47     private String JavaDoc fFieldTemplate;
48
49     private String JavaDoc fFieldPluginID;
50     
51     /**
52      *
53      */

54     public UpdateSplashHandlerAction() {
55         reset();
56     }
57
58     /**
59      * @param fieldID the fFieldID to set
60      */

61     public void setFieldID(String JavaDoc fieldID) {
62         fFieldID = fieldID;
63     }
64
65     /**
66      * @param fieldSplashID the fFieldSplashID to set
67      */

68     public void setFieldSplashID(String JavaDoc fieldSplashID) {
69         fFieldSplashID = fieldSplashID;
70     }
71
72     /**
73      * @param fieldProductID the fFieldProductID to set
74      */

75     public void setFieldProductID(String JavaDoc fieldProductID) {
76         fFieldProductID = fieldProductID;
77     }
78
79     /**
80      * @param fieldClass the fFieldClass to set
81      */

82     public void setFieldClass(String JavaDoc fieldClass) {
83         fFieldClass = fieldClass;
84     }
85
86     /**
87      * @param fieldTemplate the fFieldTemplate to set
88      */

89     public void setFieldTemplate(String JavaDoc fieldTemplate) {
90         fFieldTemplate = fieldTemplate;
91     }
92
93     /**
94      * @param fieldPluginID
95      */

96     public void setFieldPluginID(String JavaDoc fieldPluginID) {
97         fFieldPluginID = fieldPluginID;
98     }
99     
100     /**
101      *
102      */

103     public void reset() {
104         fModel = null;
105         fMonitor = null;
106         fException = null;
107         
108         fFieldID = null;
109         fFieldClass = null;
110         fFieldSplashID = null;
111         fFieldProductID = null;
112         fFieldTemplate = null;
113         fFieldPluginID = null;
114     }
115
116     
117     /**
118      * @param model
119      */

120     public void setModel(IPluginModelBase model) {
121         fModel = model;
122     }
123     
124     /**
125      * @param monitor
126      */

127     public void setMonitor(IProgressMonitor monitor) {
128         fMonitor = monitor;
129     }
130     
131     /* (non-Javadoc)
132      * @see org.eclipse.jface.action.Action#run()
133      */

134     public void run() {
135         try {
136             updateModel();
137         } catch (CoreException e) {
138             fException = e;
139         }
140     }
141
142     /**
143      * @throws CoreException
144      */

145     public void hasException() throws CoreException {
146         // Release any caught exceptions
147
if (fException != null) {
148             throw fException;
149         }
150     }
151     
152     /**
153      * @throws CoreException
154      */

155     private void updateModel() throws CoreException {
156         // Find the first splash handler extension
157
IPluginExtension extension =
158             findFirstExtension(F_SPLASH_HANDLERS_EXTENSION);
159         // Check to see if one was found
160
if (extension == null) {
161             // None found, add a new splash handler extension
162
addExtensionSplashHandlers();
163         } else {
164             // Found one, modify the existing splash handler extension
165
modifyExtensionSplashHandlers(extension);
166         }
167         // Determine whether the extensible template was selected
168
if (isExtensibleTemplateSelected(fFieldTemplate)) {
169             // Extensible template was selected
170
// Extra model modifications required for this template
171
// Find the first spash extension point declaration (should only
172
// ever be one)
173
IPluginExtensionPoint extensionPoint =
174                 findFirstExtensionPoint(F_SPLASH_EXTENSION_POINT);
175             // Check to see if one was found
176
// If one is found, just assume all its values are correct (no sync)
177
if (extensionPoint == null) {
178                 // No splash extension point definition found, add one
179
addExtensionPointSplashExtension();
180             }
181             // Find the first splash extension contribution
182
String JavaDoc fullExtensionPointID = fFieldPluginID +
183                 '.' + F_SPLASH_EXTENSION_POINT;
184             IPluginExtension extensionSplash =
185                 findFirstExtension(fullExtensionPointID);
186             // Check to see if one was found
187
// If one is found, just assume all its values are correct (no sync)
188
if (extensionSplash == null) {
189                 // No splash extension contribution found, add one
190
addExtensionSplash();
191             }
192         }
193     }
194
195     /**
196      * @throws CoreException
197      */

198     private void addExtensionSplash() throws CoreException {
199         // Update progress work units
200
String JavaDoc fullExtensionPointID = fFieldPluginID +
201             '.' + F_SPLASH_EXTENSION_POINT;
202         fMonitor.beginTask(NLS.bind(PDEUIMessages.UpdateSplashHandlerInModelAction_msgAddingExtension,
203                 fullExtensionPointID), 1);
204         // Create the new extension
205
IPluginExtension extension = createExtensionSplash();
206         // Add extension to the model
207
fModel.getPluginBase().add(extension);
208         // Update progress work units
209
fMonitor.done();
210     }
211     
212     /**
213      * @throws CoreException
214      */

215     private void addExtensionPointSplashExtension() throws CoreException {
216         // Update progress work units
217
fMonitor.beginTask(NLS.bind(PDEUIMessages.UpdateSplashHandlerInModelAction_msgAddingExtensionPoint,
218                 F_SPLASH_EXTENSION_POINT), 1);
219         // Create the new extension point
220
IPluginExtensionPoint extensionPoint = createExtensionPointSplash();
221         // Add extension point to the model
222
fModel.getPluginBase().add(extensionPoint);
223         // Update progress work units
224
fMonitor.done();
225     }
226     
227     /**
228      * @return
229      * @throws CoreException
230      */

231     private IPluginExtensionPoint createExtensionPointSplash()
232         throws CoreException {
233         // Create the extension point
234
IPluginExtensionPoint extensionPoint =
235             fModel.getFactory().createExtensionPoint();
236         // ID
237
extensionPoint.setId(F_SPLASH_EXTENSION_POINT);
238         // Name
239
extensionPoint.setName(PDEUIMessages.UpdateSplashHandlerInModelAction_splashExtensionPointName);
240         // Schema
241
extensionPoint.setSchema("schema/splashExtension.exsd"); //$NON-NLS-1$
242

243         return extensionPoint;
244     }
245     
246
247     /**
248      * @param extensionPointID
249      * @return
250      */

251     private IPluginExtension findFirstExtension(
252             String JavaDoc extensionPointID) {
253         // Get all the extensions
254
IPluginExtension[] extensions = fModel.getPluginBase().getExtensions();
255         // Get the first extension matching the specified extension point ID
256
for (int i = 0; i < extensions.length; i++) {
257             String JavaDoc point = extensions[i].getPoint();
258             if (extensionPointID.equals(point)) {
259                 return extensions[i];
260             }
261         }
262         return null;
263     }
264     
265
266     /**
267      * @param extensionPointID
268      * @return
269      */

270     private IPluginExtensionPoint findFirstExtensionPoint(
271             String JavaDoc extensionPointID) {
272         // Get all the extension points
273
IPluginExtensionPoint[] extensionPoints =
274             fModel.getPluginBase().getExtensionPoints();
275         // Get the first extension point (should only be one) matching the
276
// specified extension point ID
277
for (int i = 0; i < extensionPoints.length; i++) {
278             // Not full ID
279
String JavaDoc point = extensionPoints[i].getId();
280             if (extensionPointID.equals(point)) {
281                 return extensionPoints[i];
282             }
283         }
284         return null;
285     }
286     
287     /**
288      * @throws CoreException
289      */

290     private void addExtensionSplashHandlers() throws CoreException {
291         // Update progress work units
292
fMonitor.beginTask(NLS.bind(PDEUIMessages.UpdateSplashHandlerInModelAction_msgAddingExtension,
293                 F_SPLASH_HANDLERS_EXTENSION), 1);
294         // Create the new extension
295
IPluginExtension extension = createExtensionSplashHandlers();
296         fModel.getPluginBase().add(extension);
297         // Update progress work units
298
fMonitor.done();
299     }
300     
301     /**
302      * @return
303      * @throws CoreException
304      */

305     private IPluginExtension createExtensionSplashHandlers()
306         throws CoreException {
307         // Create the extension
308
IPluginExtension extension = fModel.getFactory().createExtension();
309         // Point
310
extension.setPoint(F_SPLASH_HANDLERS_EXTENSION);
311         // NO id
312
// NO name
313
// Create the extension's children
314
createExtensionChildrenSplashHandlers(extension);
315         
316         return extension;
317     }
318     
319     /**
320      * @param extension
321      * @throws CoreException
322      */

323     private void createExtensionChildrenSplashHandlers(
324             IPluginExtension extension) throws CoreException {
325         // Add a splash handler element
326
addElementSplashHandler(extension);
327         // Add a product handler element
328
addElementProductBinding(extension);
329     }
330
331     /**
332      * @param extension
333      * @throws CoreException
334      */

335     private void addElementSplashHandler(IPluginExtension extension)
336             throws CoreException {
337         // Create the element
338
IPluginElement splashHandlerElement =
339             createElementSplashHandler(extension);
340         // Ensure element was defined and add it to the extension
341
if (splashHandlerElement != null) {
342             // Extension uses the first element only when choosing a splash
343
// handler. Always set as the first extension element to
344
// override any previous elements
345
extension.add(0, splashHandlerElement);
346         }
347     }
348
349     /**
350      * @param extension
351      * @throws CoreException
352      */

353     private void addElementProductBinding(IPluginExtension extension)
354             throws CoreException {
355         // Create the element
356
IPluginElement productBindingElement =
357             createElementProductBinding(extension);
358         // Ensure element was defined and add it to the extension
359
if (productBindingElement != null) {
360             // Extension uses the first element only when choosing a splash
361
// handler. Always set as the first extension element to
362
// override any previous elements
363
extension.add(1, productBindingElement);
364         }
365     }
366
367     /**
368      * @param extension
369      * @return
370      * @throws CoreException
371      */

372     private IPluginElement createElementSplashHandler(
373             IPluginExtension extension) throws CoreException{
374         // Create the element
375
IPluginElement element =
376             extension.getModel().getFactory().createElement(extension);
377         // Element: Splash handler
378
element.setName(F_ELEMENT_SPLASH_HANDLER);
379         // Attribute: ID
380
element.setAttribute(F_ATTRIBUTE_ID, fFieldID);
381         // Attribute: Class
382
element.setAttribute(F_ATTRIBUTE_CLASS, fFieldClass);
383         
384         return element;
385     }
386     
387     /**
388      * @param extension
389      * @return
390      * @throws CoreException
391      */

392     private IPluginElement createElementProductBinding(
393             IPluginExtension extension) throws CoreException {
394         // Create the element
395
IPluginElement element =
396             extension.getModel().getFactory().createElement(extension);
397         // Element: Product binding
398
element.setName(F_ELEMENT_PRODUCT_BINDING);
399         // Attribute: Product ID
400
element.setAttribute(F_ATTRIBUTE_PRODUCT_ID, fFieldProductID);
401         // Attribute: Splash ID
402
element.setAttribute(F_ATTRIBUTE_SPLASH_ID, fFieldSplashID);
403         
404         return element;
405     }
406     
407     /**
408      * @param extension
409      * @throws CoreException
410      */

411     private void modifyExtensionSplashHandlers(IPluginExtension extension) throws CoreException {
412         // Update progress work units
413
fMonitor.beginTask(NLS.bind(PDEUIMessages.UpdateSplashHandlerInModelAction_msgModifyingExtension,
414                 F_SPLASH_HANDLERS_EXTENSION), 1);
415         // modify the existing extension children
416
modifyExtensionChildrenSplashHandlers(extension);
417         // Update progress work units
418
fMonitor.done();
419     }
420     
421     /**
422      * @param extension
423      * @throws CoreException
424      */

425     private void modifyExtensionChildrenSplashHandlers(
426             IPluginExtension extension) throws CoreException {
427         // Find a matching pre-generated splash handler element
428
IPluginElement splashHandlerElement = findSplashHandlerElement(extension);
429         // Check to see if one was found
430
if (splashHandlerElement == null) {
431             // No element found, add one
432
addElementSplashHandler(extension);
433         } else {
434             // One element found, synchronize it
435
syncSplashHandlerElement(splashHandlerElement);
436         }
437         // Find a matching pre-generated product binding element
438
IPluginElement productBindingElement = findProductBindingElement(extension);
439         // Remove any product binding elements bound to the target product but
440
// NOT bound to the target splash ID (if any elements are found)
441
// The splash handler extension provider uses the first product
442
// binding it finds in the extension.
443
// By removing all product bindings bound to a single product, we can
444
// override an existing splash handler with another existing
445
// splash handler.
446
removeMatchingProductBindingElements(extension);
447         // Check to see if one was found
448
if (productBindingElement == null) {
449             // No element found, add one
450
addElementProductBinding(extension);
451         } else {
452             // One element found, synchronize it
453
syncProductBindingElement(productBindingElement);
454         }
455     }
456     
457     /**
458      * @param extension
459      */

460     private void removeMatchingProductBindingElements(IPluginExtension extension)
461             throws CoreException {
462         // Check to see if the extension has any children
463
if (extension.getChildCount() == 0) {
464             // Extension has no children
465
return;
466         }
467         IPluginObject[] pluginObjects = extension.getChildren();
468         // Process all children
469
for (int j = 0; j < pluginObjects.length; j++) {
470             if (pluginObjects[j] instanceof IPluginElement) {
471                 IPluginElement element = (IPluginElement)pluginObjects[j];
472                 // Find splash handler elements
473
if (element.getName().equals(F_ELEMENT_PRODUCT_BINDING)) {
474                     // Get the splash ID attribute
475
IPluginAttribute splashIDAttribute =
476                         element.getAttribute(F_ATTRIBUTE_SPLASH_ID);
477                     // Get the product ID attribute
478
IPluginAttribute productIDAttribute =
479                         element.getAttribute(F_ATTRIBUTE_PRODUCT_ID);
480                     // (1) Remove any product binding that has an undefined
481
// product ID or splash ID
482
// (2) Remove any product binding bound to the target
483
// product ID but NOT bound to the target splash ID
484
if ((productIDAttribute == null) ||
485                             (PDETextHelper.isDefined(productIDAttribute.getValue()) == false) ||
486                             (splashIDAttribute == null) ||
487                             (PDETextHelper.isDefined(splashIDAttribute.getValue()) == false)) {
488                         // Remove product binding element
489
extension.remove(element);
490                     } else if (productIDAttribute.getValue().equals(fFieldProductID) &&
491                             (splashIDAttribute.getValue().equals(fFieldSplashID) == false)) {
492                         // Remove product binding element
493
extension.remove(element);
494                     }
495                 }
496             }
497         }
498     }
499
500     /**
501      * @param extension
502      * @return
503      */

504     private IPluginElement findSplashHandlerElement(IPluginExtension extension) {
505         // Check to see if the extension has any children
506
if (extension.getChildCount() == 0) {
507             // Extension has no children
508
return null;
509         }
510         IPluginObject[] pluginObjects = extension.getChildren();
511         // Process all children
512
for (int j = 0; j < pluginObjects.length; j++) {
513             if (pluginObjects[j] instanceof IPluginElement) {
514                 IPluginElement element = (IPluginElement)pluginObjects[j];
515                 // Find splash handler elements
516
if (element.getName().equals(F_ELEMENT_SPLASH_HANDLER)) {
517                     // Get the id attribute
518
IPluginAttribute idAttribute =
519                         element.getAttribute(F_ATTRIBUTE_ID);
520                     // Check for the generated ID
521
if ((idAttribute != null) &&
522                             PDETextHelper.isDefined(idAttribute.getValue()) &&
523                             idAttribute.getValue().equals(fFieldID)) {
524                         // Matching element found
525
return element;
526                     }
527                 }
528             }
529         }
530         return null;
531     }
532     
533     /**
534      * @param element
535      * @throws CoreException
536      */

537     private void syncSplashHandlerElement(IPluginElement element) throws CoreException {
538         // Get the class attribute
539
IPluginAttribute classAttribute =
540             element.getAttribute(F_ATTRIBUTE_CLASS);
541         // Check to see if an update is necessary
542
if ((classAttribute != null) &&
543                 PDETextHelper.isDefined(classAttribute.getValue()) &&
544                 classAttribute.getValue().equals(fFieldClass)) {
545             // Exact match, no update necessary
546
return;
547         }
548         // No match, override
549
element.setAttribute(F_ATTRIBUTE_CLASS, fFieldClass);
550     }
551     
552     /**
553      * @param element
554      * @throws CoreException
555      */

556     private void syncProductBindingElement(IPluginElement element) throws CoreException {
557         // Get the product ID attribute
558
IPluginAttribute productIDAttribute =
559             element.getAttribute(F_ATTRIBUTE_PRODUCT_ID);
560         // Check to see if an update is necessary
561
if ((productIDAttribute != null) &&
562                 PDETextHelper.isDefined(productIDAttribute.getValue()) &&
563                 productIDAttribute.getValue().equals(fFieldProductID)) {
564             // Exact match, no update necessary
565
return;
566         }
567         // No match, override
568
element.setAttribute(F_ATTRIBUTE_PRODUCT_ID, fFieldProductID);
569     }
570     
571     /**
572      * @param extension
573      * @return
574      */

575     private IPluginElement findProductBindingElement(IPluginExtension extension) {
576         // Check to see if the extension has any children
577
if (extension.getChildCount() == 0) {
578             // Extension has no children
579
return null;
580         }
581         IPluginObject[] pluginObjects = extension.getChildren();
582         // Process all children
583
for (int j = 0; j < pluginObjects.length; j++) {
584             if (pluginObjects[j] instanceof IPluginElement) {
585                 IPluginElement element = (IPluginElement)pluginObjects[j];
586                 // Find product binding elements
587
if (element.getName().equals(F_ELEMENT_PRODUCT_BINDING)) {
588                     // Get the id attribute
589
IPluginAttribute splashIDAttribute =
590                         element.getAttribute(F_ATTRIBUTE_SPLASH_ID);
591                     // Check for the generated ID
592
if ((splashIDAttribute != null) &&
593                             PDETextHelper.isDefined(splashIDAttribute.getValue()) &&
594                             splashIDAttribute.getValue().equals(fFieldSplashID)) {
595                         // Matching element found
596
return element;
597                     }
598                 }
599             }
600         }
601         return null;
602     }
603     
604     /**
605      * @return
606      * @throws CoreException
607      */

608     private IPluginExtension createExtensionSplash() throws CoreException {
609         
610         String JavaDoc fullExtensionPointID = fFieldPluginID +
611             '.' + F_SPLASH_EXTENSION_POINT;
612         // Create the extension
613
IPluginExtension extension = fModel.getFactory().createExtension();
614         // Point
615
extension.setPoint(fullExtensionPointID);
616         // NO id
617
// NO name
618
// Create the extension's children
619
createExtensionChildrenSplash(extension);
620             
621         return extension;
622     }
623
624     /**
625      * @param extension
626      * @throws CoreException
627      */

628     private void createExtensionChildrenSplash(IPluginExtension extension) throws CoreException {
629         
630         String JavaDoc iconsDir = "icons" + '/'; //$NON-NLS-1$
631

632         // Splash element: Application Framework
633
IPluginElement splashElementAf =
634             createElementSplash(extension, "af", iconsDir + "af.png", PDEUIMessages.UpdateSplashHandlerInModelAction_nameApplicationFramework); //$NON-NLS-1$ //$NON-NLS-2$
635
if (splashElementAf != null) {
636             extension.add(splashElementAf);
637         }
638         // Splash element: Embedded
639
IPluginElement splashElementEmbedded =
640             createElementSplash(extension, "embedded", iconsDir + "embedded.png", PDEUIMessages.UpdateSplashHandlerInModelAction_nameEmbedded); //$NON-NLS-1$ //$NON-NLS-2$
641
if (splashElementEmbedded != null) {
642             extension.add(splashElementEmbedded);
643         }
644         // Splash element: Enterprise
645
IPluginElement splashElementEnterprise =
646             createElementSplash(extension, "enterprise", iconsDir + "enterprise.png", PDEUIMessages.UpdateSplashHandlerInModelAction_nameEnterprise); //$NON-NLS-1$ //$NON-NLS-2$
647
if (splashElementEnterprise != null) {
648             extension.add(splashElementEnterprise);
649         }
650         // Splash element: Languages
651
IPluginElement splashElementLanguages =
652             createElementSplash(extension, "languages", iconsDir + "languages.png", PDEUIMessages.UpdateSplashHandlerInModelAction_nameLanguages); //$NON-NLS-1$ //$NON-NLS-2$
653
if (splashElementLanguages != null) {
654             extension.add(splashElementLanguages);
655         }
656         // Splash element: RCP
657
IPluginElement splashElementRCP =
658             createElementSplash(extension, "rcp", iconsDir + "rcp.png", PDEUIMessages.UpdateSplashHandlerInModelAction_nameRCP); //$NON-NLS-1$ //$NON-NLS-2$
659
if (splashElementRCP != null) {
660             extension.add(splashElementRCP);
661         }
662     }
663
664     /**
665      * @param extension
666      * @param id
667      * @param icon
668      * @param tooltip
669      * @return
670      * @throws CoreException
671      */

672     private IPluginElement createElementSplash(
673             IPluginExtension extension, String JavaDoc id, String JavaDoc icon,
674             String JavaDoc tooltip) throws CoreException {
675         // Create the element
676
IPluginElement element =
677             extension.getModel().getFactory().createElement(extension);
678         // Element: Splash handler
679
element.setName(F_ELEMENT_SPLASH);
680         // Attribute: ID
681
element.setAttribute(F_ATTRIBUTE_ID, id);
682         // Attribute: Icon
683
element.setAttribute(F_ATTRIBUTE_ICON, icon);
684         // Attribute: Tooltip
685
element.setAttribute(F_ATTRIBUTE_TOOLTIP, tooltip);
686         
687         return element;
688     }
689
690     /**
691      * @param template
692      * @return true if the extension template was selected; false, otherwise
693      */

694     public static boolean isExtensibleTemplateSelected(String JavaDoc template) {
695         if (template.equals(F_SPLASH_SCREEN_TYPE_CHOICES[2][0])) {
696             return true;
697         }
698         return false;
699     }
700     
701 }
702
Popular Tags