001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.wizards.exports;
011:
012: import java.io.File;
013: import java.lang.reflect.InvocationTargetException;
014: import java.util.ArrayList;
015:
016: import org.eclipse.core.resources.IProject;
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.jface.dialogs.MessageDialog;
019: import org.eclipse.osgi.service.resolver.BundleDescription;
020: import org.eclipse.osgi.service.resolver.State;
021: import org.eclipse.osgi.util.NLS;
022: import org.eclipse.pde.core.plugin.IPluginModelBase;
023: import org.eclipse.pde.core.plugin.PluginRegistry;
024: import org.eclipse.pde.internal.core.FeatureModelManager;
025: import org.eclipse.pde.internal.core.PDECore;
026: import org.eclipse.pde.internal.core.TargetPlatformHelper;
027: import org.eclipse.pde.internal.core.exports.FeatureExportInfo;
028: import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
029: import org.eclipse.pde.internal.core.iproduct.IProductFeature;
030: import org.eclipse.pde.internal.core.iproduct.IProductPlugin;
031: import org.eclipse.pde.internal.core.product.WorkspaceProductModel;
032: import org.eclipse.pde.internal.ui.PDEPluginImages;
033: import org.eclipse.pde.internal.ui.PDEUIMessages;
034: import org.eclipse.pde.internal.ui.build.ProductExportJob;
035: import org.eclipse.pde.internal.ui.wizards.product.SynchronizationOperation;
036: import org.eclipse.ui.progress.IProgressConstants;
037:
038: public class ProductExportWizard extends BaseExportWizard {
039:
040: private static final String STORE_SECTION = "ProductExportWizard"; //$NON-NLS-1$
041: private WorkspaceProductModel fProductModel;
042: private CrossPlatformExportPage fPage2;
043: private ProductExportWizardPage fPage;
044: private IProject fProject;
045:
046: public ProductExportWizard() {
047: this (null);
048: }
049:
050: public ProductExportWizard(IProject project) {
051: setDefaultPageImageDescriptor(PDEPluginImages.DESC_PRODUCT_EXPORT_WIZ);
052: fProject = project;
053: }
054:
055: public void addPages() {
056: fPage = new ProductExportWizardPage(getSelection());
057: addPage(fPage);
058:
059: IFeatureModel model = PDECore.getDefault()
060: .getFeatureModelManager().getDeltaPackFeature();
061: if (model != null) {
062: fPage2 = new CrossPlatformExportPage("environment", model); //$NON-NLS-1$
063: addPage(fPage2);
064: }
065: }
066:
067: protected String getSettingsSectionName() {
068: return STORE_SECTION;
069: }
070:
071: protected void scheduleExportJob() {
072: FeatureExportInfo info = new FeatureExportInfo();
073: info.toDirectory = fPage.doExportToDirectory();
074: info.exportSource = fPage.doExportSource();
075: info.destinationDirectory = fPage.getDestination();
076: info.zipFileName = fPage.getFileName();
077: if (fPage2 != null && fPage.doMultiPlatform())
078: info.targets = fPage2.getTargets();
079: if (fProductModel.getProduct().useFeatures())
080: info.items = getFeatureModels();
081: else
082: info.items = getPluginModels();
083:
084: String rootDirectory = fPage.getRootDirectory();
085: if ("".equals(rootDirectory.trim())) //$NON-NLS-1$
086: rootDirectory = "."; //$NON-NLS-1$
087: ProductExportJob job = new ProductExportJob(info,
088: fProductModel, rootDirectory);
089: job.setUser(true);
090: job.schedule();
091: job.setProperty(IProgressConstants.ICON_PROPERTY,
092: PDEPluginImages.DESC_FEATURE_OBJ);
093: }
094:
095: private IFeatureModel[] getFeatureModels() {
096: ArrayList list = new ArrayList();
097: FeatureModelManager manager = PDECore.getDefault()
098: .getFeatureModelManager();
099: IProductFeature[] features = fProductModel.getProduct()
100: .getFeatures();
101: for (int i = 0; i < features.length; i++) {
102: IFeatureModel model = manager.findFeatureModel(features[i]
103: .getId(), features[i].getVersion());
104: if (model != null)
105: list.add(model);
106: }
107: return (IFeatureModel[]) list.toArray(new IFeatureModel[list
108: .size()]);
109: }
110:
111: private BundleDescription[] getPluginModels() {
112: ArrayList list = new ArrayList();
113: State state = TargetPlatformHelper.getState();
114: IProductPlugin[] plugins = fProductModel.getProduct()
115: .getPlugins();
116: for (int i = 0; i < plugins.length; i++) {
117: BundleDescription bundle = state.getBundle(plugins[i]
118: .getId(), null);
119: if (bundle != null)
120: list.add(bundle);
121: }
122: // implicitly add the new launcher plug-in/fragment if we are to use the
123: // new launching story and the launcher plug-in/fragment are not already included in the .product file
124: IPluginModelBase launcherPlugin = PluginRegistry
125: .findModel("org.eclipse.equinox.launcher"); //$NON-NLS-1$
126: if (launcherPlugin != null) {
127: BundleDescription bundle = launcherPlugin
128: .getBundleDescription();
129: if (bundle != null && !list.contains(bundle)) {
130: list.add(bundle);
131: BundleDescription[] fragments = bundle.getFragments();
132: for (int i = 0; i < fragments.length; i++) {
133: if (!list.contains(fragments[i])) {
134: list.add(fragments[i]);
135: }
136: }
137: }
138: }
139: return (BundleDescription[]) list
140: .toArray(new BundleDescription[list.size()]);
141: }
142:
143: protected boolean performPreliminaryChecks() {
144: fProductModel = new WorkspaceProductModel(fPage
145: .getProductFile(), false);
146: try {
147: fProductModel.load();
148: if (!fProductModel.isLoaded()) {
149: MessageDialog.openError(getContainer().getShell(),
150: PDEUIMessages.ProductExportWizard_error,
151: PDEUIMessages.ProductExportWizard_corrupt); //
152: return false;
153: }
154: } catch (CoreException e) {
155: MessageDialog.openError(getContainer().getShell(),
156: PDEUIMessages.ProductExportWizard_error,
157: PDEUIMessages.ProductExportWizard_corrupt); //
158: return false;
159: }
160:
161: if (fPage.doSync()) {
162: try {
163: getContainer().run(
164: false,
165: false,
166: new SynchronizationOperation(fProductModel
167: .getProduct(), getContainer()
168: .getShell(), fProject));
169: } catch (InvocationTargetException e) {
170: MessageDialog.openError(getContainer().getShell(),
171: PDEUIMessages.ProductExportWizard_syncTitle, e
172: .getTargetException().getMessage());
173: return false;
174: } catch (InterruptedException e) {
175: return false;
176: }
177: }
178: return true;
179: }
180:
181: protected boolean confirmDelete() {
182: if (!fPage.doExportToDirectory()) {
183: File zipFile = new File(fPage.getDestination(), fPage
184: .getFileName());
185: if (zipFile.exists()) {
186: if (!MessageDialog
187: .openQuestion(
188: getContainer().getShell(),
189: PDEUIMessages.BaseExportWizard_confirmReplace_title,
190: NLS
191: .bind(
192: PDEUIMessages.BaseExportWizard_confirmReplace_desc,
193: zipFile
194: .getAbsolutePath())))
195: return false;
196: zipFile.delete();
197: }
198: }
199: return true;
200: }
201:
202: public boolean canFinish() {
203: return (fPage.getNextPage() != null) ? super.canFinish()
204: : fPage.isPageComplete();
205: }
206:
207: }
|