KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > exports > ProductExportWizard


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.exports;
12
13 import java.io.File JavaDoc;
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.util.ArrayList JavaDoc;
16
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.osgi.service.resolver.BundleDescription;
21 import org.eclipse.osgi.service.resolver.State;
22 import org.eclipse.osgi.util.NLS;
23 import org.eclipse.pde.core.plugin.IPluginModelBase;
24 import org.eclipse.pde.core.plugin.PluginRegistry;
25 import org.eclipse.pde.internal.core.FeatureModelManager;
26 import org.eclipse.pde.internal.core.PDECore;
27 import org.eclipse.pde.internal.core.TargetPlatformHelper;
28 import org.eclipse.pde.internal.core.exports.FeatureExportInfo;
29 import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
30 import org.eclipse.pde.internal.core.iproduct.IProductFeature;
31 import org.eclipse.pde.internal.core.iproduct.IProductPlugin;
32 import org.eclipse.pde.internal.core.product.WorkspaceProductModel;
33 import org.eclipse.pde.internal.ui.PDEPluginImages;
34 import org.eclipse.pde.internal.ui.PDEUIMessages;
35 import org.eclipse.pde.internal.ui.build.ProductExportJob;
36 import org.eclipse.pde.internal.ui.wizards.product.SynchronizationOperation;
37 import org.eclipse.ui.progress.IProgressConstants;
38
39
40 public class ProductExportWizard extends BaseExportWizard {
41     
42     private static final String JavaDoc STORE_SECTION = "ProductExportWizard"; //$NON-NLS-1$
43
private WorkspaceProductModel fProductModel;
44     private CrossPlatformExportPage fPage2;
45     private ProductExportWizardPage fPage;
46     private IProject fProject;
47
48     public ProductExportWizard() {
49         this(null);
50     }
51
52     public ProductExportWizard(IProject project) {
53         setDefaultPageImageDescriptor(PDEPluginImages.DESC_PRODUCT_EXPORT_WIZ);
54         fProject = project;
55     }
56     
57     public void addPages() {
58         fPage = new ProductExportWizardPage(getSelection());
59         addPage(fPage);
60         
61         IFeatureModel model = PDECore.getDefault().getFeatureModelManager().getDeltaPackFeature();
62         if (model != null) {
63             fPage2 = new CrossPlatformExportPage("environment", model); //$NON-NLS-1$
64
addPage(fPage2);
65         }
66     }
67     
68     protected String JavaDoc getSettingsSectionName() {
69         return STORE_SECTION;
70     }
71
72     protected void scheduleExportJob() {
73         FeatureExportInfo info = new FeatureExportInfo();
74         info.toDirectory = fPage.doExportToDirectory();
75         info.exportSource = fPage.doExportSource();
76         info.destinationDirectory = fPage.getDestination();
77         info.zipFileName = fPage.getFileName();
78         if (fPage2 != null && fPage.doMultiPlatform())
79             info.targets = fPage2.getTargets();
80         if (fProductModel.getProduct().useFeatures())
81             info.items = getFeatureModels();
82         else
83             info.items = getPluginModels();
84         
85         String JavaDoc rootDirectory = fPage.getRootDirectory();
86         if ("".equals(rootDirectory.trim())) //$NON-NLS-1$
87
rootDirectory = "."; //$NON-NLS-1$
88
ProductExportJob job = new ProductExportJob(info, fProductModel, rootDirectory);
89         job.setUser(true);
90         job.schedule();
91         job.setProperty(IProgressConstants.ICON_PROPERTY, PDEPluginImages.DESC_FEATURE_OBJ);
92     }
93     
94     private IFeatureModel[] getFeatureModels() {
95         ArrayList JavaDoc list = new ArrayList JavaDoc();
96         FeatureModelManager manager = PDECore.getDefault()
97                 .getFeatureModelManager();
98         IProductFeature[] features = fProductModel.getProduct().getFeatures();
99         for (int i = 0; i < features.length; i++) {
100             IFeatureModel model = manager.findFeatureModel(features[i].getId(),
101                     features[i].getVersion());
102             if (model != null)
103                 list.add(model);
104         }
105         return (IFeatureModel[]) list.toArray(new IFeatureModel[list.size()]);
106     }
107
108     private BundleDescription[] getPluginModels() {
109         ArrayList JavaDoc list = new ArrayList JavaDoc();
110         State state = TargetPlatformHelper.getState();
111         IProductPlugin[] plugins = fProductModel.getProduct().getPlugins();
112         for (int i = 0; i < plugins.length; i++) {
113             BundleDescription bundle = state.getBundle(plugins[i].getId(), null);
114             if (bundle != null)
115                 list.add(bundle);
116         }
117         // implicitly add the new launcher plug-in/fragment if we are to use the
118
// new launching story and the launcher plug-in/fragment are not already included in the .product file
119
IPluginModelBase launcherPlugin = PluginRegistry.findModel("org.eclipse.equinox.launcher"); //$NON-NLS-1$
120
if (launcherPlugin != null) {
121             BundleDescription bundle = launcherPlugin.getBundleDescription();
122             if (bundle != null && !list.contains(bundle)) {
123                 list.add(bundle);
124                 BundleDescription[] fragments = bundle.getFragments();
125                 for (int i = 0; i < fragments.length; i++) {
126                     if (!list.contains(fragments[i])) {
127                         list.add(fragments[i]);
128                     }
129                 }
130             }
131         }
132         return (BundleDescription[]) list.toArray(new BundleDescription[list.size()]);
133     }
134     
135     protected boolean performPreliminaryChecks() {
136         fProductModel = new WorkspaceProductModel(fPage.getProductFile(), false);
137         try {
138             fProductModel.load();
139             if (!fProductModel.isLoaded()) {
140                 MessageDialog.openError(getContainer().getShell(), PDEUIMessages.ProductExportWizard_error, PDEUIMessages.ProductExportWizard_corrupt); //
141
return false;
142             }
143         } catch (CoreException e) {
144             MessageDialog.openError(getContainer().getShell(), PDEUIMessages.ProductExportWizard_error, PDEUIMessages.ProductExportWizard_corrupt); //
145
return false;
146         }
147
148         if (fPage.doSync()) {
149             try {
150                 getContainer().run(false, false, new SynchronizationOperation(fProductModel.getProduct(), getContainer().getShell(), fProject));
151             } catch (InvocationTargetException JavaDoc e) {
152                 MessageDialog.openError(getContainer().getShell(), PDEUIMessages.ProductExportWizard_syncTitle, e.getTargetException().getMessage());
153                 return false;
154             } catch (InterruptedException JavaDoc e) {
155                 return false;
156             }
157         }
158         return true;
159     }
160     
161     protected boolean confirmDelete() {
162         if (!fPage.doExportToDirectory()) {
163             File JavaDoc zipFile = new File JavaDoc(fPage.getDestination(), fPage.getFileName());
164             if (zipFile.exists()) {
165                 if (!MessageDialog.openQuestion(getContainer().getShell(),
166                         PDEUIMessages.BaseExportWizard_confirmReplace_title,
167                         NLS.bind(PDEUIMessages.BaseExportWizard_confirmReplace_desc, zipFile.getAbsolutePath())))
168                     return false;
169                 zipFile.delete();
170             }
171         }
172         return true;
173     }
174     
175     public boolean canFinish() {
176         return (fPage.getNextPage() != null) ? super.canFinish() : fPage.isPageComplete();
177     }
178
179 }
180
Popular Tags