KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.jface.action.Action;
19 import org.eclipse.osgi.util.NLS;
20 import org.eclipse.pde.core.plugin.IPluginAttribute;
21 import org.eclipse.pde.core.plugin.IPluginElement;
22 import org.eclipse.pde.core.plugin.IPluginExtension;
23 import org.eclipse.pde.core.plugin.IPluginModelBase;
24 import org.eclipse.pde.core.plugin.IPluginObject;
25 import org.eclipse.pde.internal.core.util.PDETextHelper;
26 import org.eclipse.pde.internal.ui.PDEUIMessages;
27
28 /**
29  * RemoveSplashHandlerBindingAction
30  *
31  */

32 public class RemoveSplashHandlerBindingAction extends Action implements
33         ISplashHandlerConstants {
34
35     private IPluginModelBase fModel;
36     
37     private IProgressMonitor fMonitor;
38     
39     private CoreException fException;
40     
41     private String JavaDoc fFieldProductID;
42
43     private String JavaDoc fFieldTargetPackage;
44     
45     /**
46      *
47      */

48     public RemoveSplashHandlerBindingAction() {
49         reset();
50     }
51     
52     /**
53      * @param fieldProductID the fFieldProductID to set
54      */

55     public void setFieldProductID(String JavaDoc fieldProductID) {
56         fFieldProductID = fieldProductID;
57     }
58     
59     /**
60      * @param fieldTargetPackage
61      */

62     public void setFieldTargetPackage(String JavaDoc fieldTargetPackage) {
63         fFieldTargetPackage = fieldTargetPackage;
64     }
65     
66     /**
67      *
68      */

69     public void reset() {
70         fModel = null;
71         fMonitor = null;
72         fException = null;
73         
74         fFieldProductID = null;
75         fFieldTargetPackage = null;
76     }
77     
78     /* (non-Javadoc)
79      * @see org.eclipse.jface.action.Action#run()
80      */

81     public void run() {
82         try {
83             updateModel();
84         } catch (CoreException e) {
85             fException = e;
86         }
87     }
88     
89     /**
90      * @throws CoreException
91      */

92     public void hasException() throws CoreException {
93         // Release any caught exceptions
94
if (fException != null) {
95             throw fException;
96         }
97     }
98     
99     /**
100      * @param model
101      */

102     public void setModel(IPluginModelBase model) {
103         fModel = model;
104     }
105     
106     /**
107      * @param monitor
108      */

109     public void setMonitor(IProgressMonitor monitor) {
110         fMonitor = monitor;
111     }
112     
113     /**
114      * @throws CoreException
115      */

116     private void updateModel() throws CoreException {
117         // Find the first splash handler extension
118
// We don't care about other splash handler extensions manually added
119
// by the user
120
IPluginExtension extension =
121             findFirstExtension(F_SPLASH_HANDLERS_EXTENSION);
122         // Check to see if one was found
123
if (extension == null) {
124             // None found, our job is already done
125
return;
126         }
127         // Update progress work units
128
fMonitor.beginTask(NLS.bind(PDEUIMessages.RemoveSplashHandlerBindingAction_msgProgressRemoveProductBindings,
129                 F_SPLASH_HANDLERS_EXTENSION), 1);
130         // Find all product binding elements
131
IPluginElement[] productBindingElements =
132             findProductBindingElements(extension);
133         // Remove all product binding elements that are malformed or match the
134
// target product ID
135
removeMatchingProductBindingElements(extension, productBindingElements);
136         // Update progress work units
137
fMonitor.done();
138     }
139
140     /**
141      * @param extension
142      * @param productBindingElements
143      * @throws CoreException
144      */

145     private void removeMatchingProductBindingElements(IPluginExtension extension,
146             IPluginElement[] productBindingElements)
147             throws CoreException {
148         // If there are no product binding elements, then our job is done
149
if ((productBindingElements == null) ||
150                 (productBindingElements.length == 0)) {
151             return;
152         }
153         // Process all product binding elements
154
for (int i = 0; i < productBindingElements.length; i++) {
155             IPluginElement element = productBindingElements[i];
156             // Get the product ID attribute
157
IPluginAttribute productIDAttribute =
158                 element.getAttribute(F_ATTRIBUTE_PRODUCT_ID);
159             // Remove any product binding element that does not define a
160
// product ID
161
if ((productIDAttribute == null) ||
162                     (PDETextHelper.isDefined(productIDAttribute.getValue()) == false)) {
163                 extension.remove(element);
164                 continue;
165             }
166             // Get the splash ID attribute
167
IPluginAttribute splashIDAttribute =
168                 element.getAttribute(F_ATTRIBUTE_SPLASH_ID);
169             // Remove any product binding element that does not define a
170
// splash ID
171
if ((splashIDAttribute == null) ||
172                     (PDETextHelper.isDefined(splashIDAttribute.getValue()) == false)) {
173                 extension.remove(element);
174                 continue;
175             }
176             // Remove any product binding element whose product ID matches this
177
// product's ID and whose splash ID match's a generated splash
178
// handler template ID
179
if (productIDAttribute.getValue().equals(fFieldProductID) &&
180                     isGeneratedSplashID(splashIDAttribute.getValue())) {
181                 extension.remove(element);
182             }
183         }
184     }
185
186     /**
187      * @param value
188      * @return
189      */

190     private boolean isGeneratedSplashID(String JavaDoc value) {
191         String JavaDoc[][] choices = ISplashHandlerConstants.F_SPLASH_SCREEN_TYPE_CHOICES;
192         // Check to see if the splash ID matches any of the pre-generated
193
// splash handler template IDs
194
for (int i = 0; i < choices.length; i++) {
195             String JavaDoc splashID = fFieldTargetPackage + '.' + choices[i][0];
196             if (value.equals(splashID)) {
197                 return true;
198             }
199         }
200         return false;
201     }
202     
203     /**
204      * @param extension
205      * @return
206      */

207     private IPluginElement[] findProductBindingElements(IPluginExtension extension) {
208         ArrayList JavaDoc elements = new ArrayList JavaDoc();
209         // Check to see if the extension has any children
210
if (extension.getChildCount() == 0) {
211             // Extension has no children
212
return null;
213         }
214         IPluginObject[] pluginObjects = extension.getChildren();
215         // Process all children
216
for (int j = 0; j < pluginObjects.length; j++) {
217             if (pluginObjects[j] instanceof IPluginElement) {
218                 IPluginElement element = (IPluginElement)pluginObjects[j];
219                 // Find product binding elements
220
if (element.getName().equals(F_ELEMENT_PRODUCT_BINDING)) {
221                     elements.add(element);
222                 }
223             }
224         }
225         // No product binding elements found
226
if (elements.size() == 0) {
227             return null;
228         }
229         // Return product binding elements
230
return (IPluginElement[]) elements.toArray(new IPluginElement[elements.size()]);
231     }
232     
233     /**
234      * @param extensionPointID
235      * @return
236      */

237     private IPluginExtension findFirstExtension(
238             String JavaDoc extensionPointID) {
239         // Get all the extensions
240
IPluginExtension[] extensions = fModel.getPluginBase().getExtensions();
241         // Get the first extension matching the specified extension point ID
242
for (int i = 0; i < extensions.length; i++) {
243             String JavaDoc point = extensions[i].getPoint();
244             if (extensionPointID.equals(point)) {
245                 return extensions[i];
246             }
247         }
248         return null;
249     }
250     
251 }
252
Popular Tags