KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 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 package org.eclipse.pde.internal.ui.wizards.product;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.Locale JavaDoc;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.osgi.util.NLS;
25 import org.eclipse.pde.core.IBaseModel;
26 import org.eclipse.pde.core.plugin.IPluginAttribute;
27 import org.eclipse.pde.core.plugin.IPluginBase;
28 import org.eclipse.pde.core.plugin.IPluginElement;
29 import org.eclipse.pde.core.plugin.IPluginExtension;
30 import org.eclipse.pde.core.plugin.IPluginModelBase;
31 import org.eclipse.pde.core.plugin.IPluginObject;
32 import org.eclipse.pde.core.plugin.PluginRegistry;
33 import org.eclipse.pde.internal.core.TargetPlatformHelper;
34 import org.eclipse.pde.internal.core.iproduct.IAboutInfo;
35 import org.eclipse.pde.internal.core.iproduct.IProduct;
36 import org.eclipse.pde.internal.core.iproduct.ISplashInfo;
37 import org.eclipse.pde.internal.core.iproduct.IWindowImages;
38 import org.eclipse.pde.internal.core.plugin.WorkspacePluginModelBase;
39 import org.eclipse.pde.internal.core.product.SplashInfo;
40 import org.eclipse.pde.internal.core.text.plugin.PluginElementNode;
41 import org.eclipse.pde.internal.ui.PDEPlugin;
42 import org.eclipse.pde.internal.ui.PDEUIMessages;
43 import org.eclipse.pde.internal.ui.util.ModelModification;
44 import org.eclipse.pde.internal.ui.util.PDEModelUtility;
45 import org.eclipse.pde.internal.ui.util.TemplateFileGenerator;
46 import org.eclipse.swt.widgets.Shell;
47 import org.eclipse.ui.branding.IProductConstants;
48
49 public class ProductDefinitionOperation extends BaseManifestOperation {
50
51     private String JavaDoc fProductId;
52     private String JavaDoc fApplication;
53     private IProduct fProduct;
54     
55     protected IProject fProject;
56     
57     private UpdateSplashHandlerAction fUpdateSplashAction;
58     
59     private RemoveSplashHandlerBindingAction fRemoveSplashAction;
60     
61     private UpdateSplashProgressOperation fUpdateSplashProgressOperation;
62
63     public ProductDefinitionOperation(IProduct product, String JavaDoc pluginId, String JavaDoc productId, String JavaDoc application, Shell shell) {
64         super(shell, pluginId);
65         fProductId = productId;
66         fApplication = application;
67         fProduct = product;
68         fProject = null;
69     }
70
71     /**
72      * @param product
73      * @param pluginId
74      * @param productId
75      * @param application
76      * @param shell
77      * @param project
78      */

79     public ProductDefinitionOperation(IProduct product, String JavaDoc pluginId,
80             String JavaDoc productId, String JavaDoc application, Shell shell, IProject project) {
81         super(shell, pluginId);
82         fProductId = productId;
83         fApplication = application;
84         fProduct = product;
85         // Needed for splash handler updates (file copying)
86
fProject = project;
87     }
88     
89     /**
90      * @param id
91      * @return
92      */

93     protected String JavaDoc getFormattedPackageName(String JavaDoc id){
94         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
95         for (int i = 0; i < id.length(); i++) {
96             char ch = id.charAt(i);
97             if (buffer.length() == 0) {
98                 if (Character.isJavaIdentifierStart(ch))
99                     buffer.append(Character.toLowerCase(ch));
100             } else {
101                 if (Character.isJavaIdentifierPart(ch) || ch == '.')
102                     buffer.append(ch);
103             }
104         }
105         return buffer.toString().toLowerCase(Locale.ENGLISH);
106     }
107     
108     /**
109      * @return
110      */

111     protected String JavaDoc createTargetPackage() {
112         // Package name addition to create a location for containing
113
// any classes required by the splash handlers.
114
String JavaDoc packageName = getFormattedPackageName(fPluginId);
115         // Unqualifed
116
if (packageName.length() == 0) {
117             return ISplashHandlerConstants.F_UNQUALIFIED_EXTENSION_ID;
118         }
119         // Qualified
120
return packageName + '.' + ISplashHandlerConstants.F_UNQUALIFIED_EXTENSION_ID;
121     }
122     
123     /**
124      * @return fully-qualified class (with package)
125      */

126     private String JavaDoc createAttributeValueClass() {
127         String JavaDoc targetPackage = createTargetPackage();
128         String JavaDoc targetClass = createTargetClass();
129         // Ensure target class is defined
130
if (targetClass == null) {
131             return null;
132         }
133         
134         return targetPackage +
135                "." + //$NON-NLS-1$
136
targetClass;
137     }
138     
139     /**
140      * @return unqualified class
141      */

142     private String JavaDoc createTargetClass() {
143         // Get the splash handler type
144
String JavaDoc splashHandlerType = getSplashHandlerType();
145         // Ensure splash handler type was specfied
146
if (splashHandlerType == null) {
147             return null;
148         }
149         // Update the class name depending on the splash screen type
150
for (int i = 0; i < ISplashHandlerConstants.F_SPLASH_SCREEN_TYPE_CHOICES.length; i++) {
151             String JavaDoc choice = ISplashHandlerConstants.F_SPLASH_SCREEN_TYPE_CHOICES[i][0];
152             if (splashHandlerType.equals(choice)) {
153                 return ISplashHandlerConstants.F_SPLASH_SCREEN_CLASSES[i];
154             }
155         }
156         return null;
157     }
158     
159     /**
160      * @return splash screen type qualified with package name
161      */

162     private String JavaDoc createAttributeValueID() {
163         // Create the ID based on the splash screen type
164
return createTargetPackage() +
165                 "." + //$NON-NLS-1$
166
getSplashHandlerType();
167     }
168
169     /**
170      * @return
171      */

172     private UpdateSplashProgressOperation getUpdateSplashProgressOperation() {
173         if (fUpdateSplashProgressOperation == null) {
174             fUpdateSplashProgressOperation = new UpdateSplashProgressOperation();
175         } else {
176             fUpdateSplashProgressOperation.reset();
177         }
178         return fUpdateSplashProgressOperation;
179     }
180     
181     /**
182      * @param model
183      * @param monitor
184      * @throws CoreException
185      */

186     private void updateSplashProgress(IPluginModelBase model,
187             IProgressMonitor monitor) throws CoreException {
188         // Sanity checks
189
if (fProject == null) {
190             return;
191         } else if (model == null) {
192             return;
193         } else if (monitor == null) {
194             return;
195         }
196         // Get the action
197
UpdateSplashProgressOperation operation = getUpdateSplashProgressOperation();
198         operation.setModel(model);
199         operation.setShowProgress(isProgressDefined());
200         operation.setProject(fProject);
201         operation.setProductID(fProduct.getId());
202         operation.setPluginID(fPluginId);
203         // Execute the action
204
operation.run(monitor);
205     }
206     
207     /**
208      * @return
209      */

210     private boolean isProgressDefined() {
211         // Get the splash info from the model
212
ISplashInfo info = fProduct.getProduct().getSplashInfo();
213         // Ensure splash info was defined
214
if (info == null) {
215             return false;
216         }
217         // Ensure splash progress was defined
218
return info.isDefinedGeometry();
219     }
220
221     /**
222      * @return
223      */

224     private String JavaDoc getSplashHandlerType() {
225         // Get the splash info from the model
226
ISplashInfo info = fProduct.getProduct().getSplashInfo();
227         // Ensure splash info was defined
228
if (info == null) {
229             return null;
230         }
231         // Ensure splash type was defined
232
if (info.isDefinedSplashHandlerType() == false) {
233             return null;
234         }
235         return info.getFieldSplashHandlerType();
236     }
237     
238     /**
239      * @param model
240      * @param monitor
241      * @throws CoreException
242      */

243     private void updateSplashHandler(IPluginModelBase model,
244             IProgressMonitor monitor) throws CoreException {
245         // Copy the applicable splash handler artifacts and perform parameter
246
// substitution (like in templates plug-in)
247
// Artifacts may include code, images and extension point schemas
248
updateSplashHandlerFiles(model, monitor);
249         // Update the plug-in model with the applicable splash handler extension
250
// and extension point related mark-up
251
updateSplashHandlerModel(model, monitor);
252     }
253     
254     /**
255      * @param model
256      * @param monitor
257      * @throws CoreException
258      */

259     private void updateSplashHandlerFiles(IPluginModelBase model,
260             IProgressMonitor monitor) throws CoreException {
261         // If the project is not defined, abort this operation
262
if (fProject == null) {
263             return;
264         }
265         // Get the splash handler type
266
String JavaDoc splashHandlerType = getSplashHandlerType();
267         // If the splash handler type was not defined, abort this operation
268
if (splashHandlerType == null) {
269             return;
270         }
271         // Create and configure the template file generator
272
// Note: Plug-in ID must be passed in separately from model, because
273
// the underlying model does not contain the ID (even when it is
274
// a workspace model)
275
TemplateFileGenerator generator = new TemplateFileGenerator(
276                 fProject, model, fPluginId, createTargetPackage(),
277                 createTargetClass(), splashHandlerType);
278         // Generate the necessary files
279
generator.generateFiles(monitor);
280     }
281     
282     /**
283      * @param model
284      * @param monitor
285      * @throws CoreException
286      */

287     private void updateSplashHandlerModel(IPluginModelBase model,
288             IProgressMonitor monitor) throws CoreException {
289         // Get the splash handler type
290
String JavaDoc splashHandlerType = getSplashHandlerType();
291         // If the splash handler type is not defined, abort this operation
292
if (splashHandlerType == null) {
293             runRemoveSplashAction(model, monitor);
294         } else {
295             runUpdateSplashAction(model, monitor, splashHandlerType);
296         }
297     }
298
299     /**
300      * @param model
301      * @param monitor
302      */

303     private void runRemoveSplashAction(IPluginModelBase model,
304             IProgressMonitor monitor) throws CoreException {
305         // Create the remove splash handler action
306
fRemoveSplashAction = new RemoveSplashHandlerBindingAction();
307         // Configure the action
308
fRemoveSplashAction.setFieldProductID(fProduct.getId());
309         fRemoveSplashAction.setFieldTargetPackage(createTargetPackage());
310         
311         fRemoveSplashAction.setModel(model);
312         fRemoveSplashAction.setMonitor(monitor);
313         // Execute the action
314
fRemoveSplashAction.run();
315         // If an core exception was thrown and caught, release it
316
fRemoveSplashAction.hasException();
317     }
318
319     /**
320      * @param model
321      * @param monitor
322      * @param splashHandlerType
323      * @throws CoreException
324      */

325     private void runUpdateSplashAction(IPluginModelBase model,
326             IProgressMonitor monitor, String JavaDoc splashHandlerType)
327             throws CoreException {
328         // Create the update splash handler action
329
fUpdateSplashAction = new UpdateSplashHandlerAction();
330         // Configure the action
331
String JavaDoc id = createAttributeValueID();
332         fUpdateSplashAction.setFieldID(id);
333         fUpdateSplashAction.setFieldClass(createAttributeValueClass());
334         fUpdateSplashAction.setFieldSplashID(id);
335         fUpdateSplashAction.setFieldProductID(fProduct.getId());
336         fUpdateSplashAction.setFieldTemplate(splashHandlerType);
337         fUpdateSplashAction.setFieldPluginID(fPluginId);
338
339         fUpdateSplashAction.setModel(model);
340         fUpdateSplashAction.setMonitor(monitor);
341         // Execute the action
342
fUpdateSplashAction.run();
343         // If an core exception was thrown and caught, release it
344
fUpdateSplashAction.hasException();
345     }
346     
347     public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc,
348             InterruptedException JavaDoc {
349         try {
350             IFile file = getFile();
351             if (!file.exists()) {
352                 createNewFile(file, monitor);
353             } else {
354                 modifyExistingFile(file, monitor);
355             }
356             updateSingleton(monitor);
357         } catch (CoreException e) {
358             throw new InvocationTargetException JavaDoc(e);
359         }
360     }
361     
362     private void createNewFile(IFile file, IProgressMonitor monitor) throws CoreException {
363         WorkspacePluginModelBase model = (WorkspacePluginModelBase)getModel(file);
364         IPluginBase base = model.getPluginBase();
365         base.setSchemaVersion(TargetPlatformHelper.getTargetVersion() < 3.2 ? "3.0" : "3.2"); //$NON-NLS-1$ //$NON-NLS-2$
366
base.add(createExtension(model));
367         // Update the splash handler. Update plug-in model and copy files
368
updateSplashHandler(model, monitor);
369         // Update splash progress. Update plug-in model and copy files
370
updateSplashProgress(model, monitor);
371         
372         model.save();
373     }
374     
375     private IPluginExtension createExtension(IPluginModelBase model) throws CoreException{
376         IPluginExtension extension = model.getFactory().createExtension();
377         extension.setPoint("org.eclipse.core.runtime.products"); //$NON-NLS-1$
378
extension.setId(fProductId);
379         extension.add(createExtensionContent(extension));
380         return extension;
381     }
382     
383     private IPluginElement createExtensionContent(IPluginExtension extension) throws CoreException {
384         IPluginElement element = extension.getModel().getFactory().createElement(extension);
385         element.setName("product"); //$NON-NLS-1$
386
element.setAttribute("name", fProduct.getName()); //$NON-NLS-1$
387
element.setAttribute("application", fApplication); //$NON-NLS-1$
388

389         IPluginElement child = createElement(element, IProductConstants.WINDOW_IMAGES, getWindowImagesString());
390         if (child != null)
391             element.add(child);
392         
393         child = createElement(element, IProductConstants.ABOUT_TEXT, getAboutText());
394         if (child != null)
395             element.add(child);
396             
397         child = createElement(element, IProductConstants.ABOUT_IMAGE, getAboutImage());
398         if (child != null)
399             element.add(child);
400         
401         child = createElement(element, IProductConstants.STARTUP_FOREGROUND_COLOR, getForegroundColor());
402         if (child != null)
403             element.add(child);
404         
405         child = createElement(element, IProductConstants.STARTUP_PROGRESS_RECT, getProgressRect());
406         if (child != null)
407             element.add(child);
408         
409         child = createElement(element, IProductConstants.STARTUP_MESSAGE_RECT, getMessageRect());
410         if (child != null)
411             element.add(child);
412         
413         return element;
414     }
415     
416     private IPluginElement createElement(IPluginElement parent, String JavaDoc name, String JavaDoc value) throws CoreException {
417         IPluginElement element = null;
418         if (value != null && value.length() > 0) {
419             element = parent.getModel().getFactory().createElement(parent);
420             element.setName("property"); //$NON-NLS-1$
421
element.setAttribute("name", name); //$NON-NLS-1$
422
element.setAttribute("value", value); //$NON-NLS-1$
423
}
424         return element;
425     }
426     
427     private String JavaDoc getAboutText() {
428         IAboutInfo info = fProduct.getAboutInfo();
429         if (info != null) {
430             String JavaDoc text = info.getText();
431             return text == null || text.length() == 0 ? null : text;
432         }
433         return null;
434     }
435     
436     private String JavaDoc getAboutImage() {
437         IAboutInfo info = fProduct.getAboutInfo();
438         return info != null ? getURL(info.getImagePath()) : null;
439     }
440     
441     private String JavaDoc getURL(String JavaDoc location) {
442         if (location == null || location.trim().length() == 0)
443             return null;
444         IPath path = new Path(location);
445         if (!path.isAbsolute())
446             return location;
447         String JavaDoc projectName = path.segment(0);
448         IProject project = PDEPlugin.getWorkspace().getRoot().getProject(projectName);
449         if (project.exists()) {
450             IPluginModelBase model = PluginRegistry.findModel(project);
451             if (model != null) {
452                 String JavaDoc id = model.getPluginBase().getId();
453                 if (fPluginId.equals(id))
454                     return path.removeFirstSegments(1).toString();
455                 return "platform:/plugin/" + id + "/" + path.removeFirstSegments(1); //$NON-NLS-1$ //$NON-NLS-2$
456
}
457         }
458         return location;
459     }
460     
461     private String JavaDoc getWindowImagesString() {
462         IWindowImages images = fProduct.getWindowImages();
463         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
464         if (images != null) {
465             for (int i = 0; i < IWindowImages.TOTAL_IMAGES; i++) {
466                 String JavaDoc image = getURL(images.getImagePath(i));
467                 if (image != null) {
468                     if (buffer.length() > 0)
469                         buffer.append(","); //$NON-NLS-1$
470
buffer.append(image);
471                 }
472                 
473             }
474         }
475         return buffer.length() == 0 ? null : buffer.toString(); //$NON-NLS-1$
476
}
477     
478     private String JavaDoc getForegroundColor() {
479         ISplashInfo info = fProduct.getSplashInfo();
480         return info != null ? info.getForegroundColor() : null;
481     }
482     
483     private String JavaDoc getProgressRect() {
484         ISplashInfo info = fProduct.getSplashInfo();
485         return info != null ? SplashInfo.getGeometryString(info.getProgressGeometry()) : null;
486     }
487     
488     private String JavaDoc getMessageRect() {
489         ISplashInfo info = fProduct.getSplashInfo();
490         return info != null ? SplashInfo.getGeometryString(info.getMessageGeometry()) : null;
491     }
492     
493     private void modifyExistingFile(IFile file, IProgressMonitor monitor) throws CoreException {
494         IStatus status = PDEPlugin.getWorkspace().validateEdit(new IFile[] {file}, getShell());
495         if (status.getSeverity() != IStatus.OK)
496             throw new CoreException(new Status(IStatus.ERROR, "org.eclipse.pde.ui", IStatus.ERROR, NLS.bind(PDEUIMessages.ProductDefinitionOperation_readOnly, fPluginId), null)); //$NON-NLS-1$
497

498         ModelModification mod = new ModelModification(file) {
499             protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException {
500                 if (!(model instanceof IPluginModelBase))
501                     return;
502                 IPluginExtension extension = findProductExtension((IPluginModelBase)model);
503                 if (extension == null)
504                     insertNewExtension((IPluginModelBase)model);
505                 else
506                     modifyExistingExtension(extension);
507                 // Update the splash handler. Update plug-in model and copy files
508
updateSplashHandler((IPluginModelBase)model, monitor);
509                 // Update splash progress. Update plug-in model and copy files
510
updateSplashProgress((IPluginModelBase)model, monitor);
511             }
512         };
513         PDEModelUtility.modifyModel(mod, monitor);
514     }
515     
516     private IPluginExtension findProductExtension(IPluginModelBase model) {
517         IPluginExtension[] extensions = model.getPluginBase().getExtensions();
518         for (int i = 0; i < extensions.length; i++) {
519             String JavaDoc point = extensions[i].getPoint();
520             String JavaDoc id = extensions[i].getId();
521             if (fProductId.equals(id) && "org.eclipse.core.runtime.products".equals(point)) { //$NON-NLS-1$
522
return extensions[i];
523             }
524         }
525         return null;
526     }
527     
528     private void insertNewExtension(IPluginModelBase model) throws CoreException {
529         IPluginExtension extension = createExtension(model);
530         model.getPluginBase().add(extension);
531     }
532     
533     private void modifyExistingExtension(IPluginExtension extension) throws CoreException {
534         if (extension.getChildCount() == 0) {
535             insertNewProductElement(extension);
536             return;
537         }
538         
539         PluginElementNode element = (PluginElementNode)extension.getChildren()[0];
540         
541         if (!"product".equals(element.getName())) { //$NON-NLS-1$
542
insertNewProductElement(extension);
543             return;
544         }
545         
546         element.setAttribute("application", fApplication); //$NON-NLS-1$
547
element.setAttribute("name", fProduct.getName()); //$NON-NLS-1$
548
synchronizeChild(element, IProductConstants.APP_NAME, fProduct.getName());
549         
550         synchronizeChild(element, IProductConstants.ABOUT_IMAGE, getAboutImage());
551         synchronizeChild(element, IProductConstants.ABOUT_TEXT, getAboutText());
552         synchronizeChild(element, IProductConstants.WINDOW_IMAGES, getWindowImagesString());
553         synchronizeChild(element, IProductConstants.STARTUP_FOREGROUND_COLOR, getForegroundColor());
554         synchronizeChild(element, IProductConstants.STARTUP_MESSAGE_RECT, getMessageRect());
555         synchronizeChild(element, IProductConstants.STARTUP_PROGRESS_RECT, getProgressRect());
556         
557     }
558     
559     private void synchronizeChild(IPluginElement element, String JavaDoc propertyName, String JavaDoc value) throws CoreException {
560         IPluginElement child = null;
561         IPluginObject[] children = element.getChildren();
562         for (int i = 0; i < children.length; i++) {
563             IPluginElement candidate = (IPluginElement)children[i];
564             if (candidate.getName().equals("property")) { //$NON-NLS-1$
565
IPluginAttribute attr = candidate.getAttribute("name"); //$NON-NLS-1$
566
if (attr != null && attr.getValue().equals(propertyName)) {
567                     child = candidate;
568                     break;
569                 }
570             }
571         }
572         if (child != null && value == null)
573             element.remove(child);
574         
575         if (value == null)
576             return;
577         
578         if (child == null) {
579             child = element.getModel().getFactory().createElement(element);
580             child.setName("property"); //$NON-NLS-1$
581
element.add(child);
582         }
583         child.setAttribute("value", value); //$NON-NLS-1$
584
child.setAttribute("name", propertyName); //$NON-NLS-1$
585
}
586     
587     private void insertNewProductElement(IPluginExtension extension) throws CoreException {
588         IPluginElement element = createExtensionContent(extension);
589         extension.add(element);
590     }
591     
592 }
593
Popular Tags