001: /*******************************************************************************
002: * Copyright (c) 2000, 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.imports;
011:
012: import org.eclipse.core.resources.IProject;
013: import org.eclipse.core.runtime.CoreException;
014: import org.eclipse.core.runtime.IPath;
015: import org.eclipse.core.runtime.IProgressMonitor;
016: import org.eclipse.core.runtime.IStatus;
017: import org.eclipse.core.runtime.Status;
018: import org.eclipse.core.runtime.jobs.Job;
019: import org.eclipse.jface.dialogs.IDialogConstants;
020: import org.eclipse.jface.dialogs.IDialogSettings;
021: import org.eclipse.jface.dialogs.MessageDialog;
022: import org.eclipse.jface.viewers.IStructuredSelection;
023: import org.eclipse.jface.wizard.Wizard;
024: import org.eclipse.osgi.util.NLS;
025: import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
026: import org.eclipse.pde.internal.ui.PDEPlugin;
027: import org.eclipse.pde.internal.ui.PDEPluginImages;
028: import org.eclipse.pde.internal.ui.PDEUIMessages;
029: import org.eclipse.pde.internal.ui.wizards.imports.FeatureImportOperation.IReplaceQuery;
030: import org.eclipse.swt.widgets.Display;
031: import org.eclipse.swt.widgets.Shell;
032: import org.eclipse.ui.IImportWizard;
033: import org.eclipse.ui.IWorkbench;
034:
035: public class FeatureImportWizard extends Wizard implements
036: IImportWizard {
037:
038: private static final String STORE_SECTION = "FeatureImportWizard"; //$NON-NLS-1$
039: private FeatureImportWizardPage fPage;
040:
041: public FeatureImportWizard() {
042: IDialogSettings masterSettings = PDEPlugin.getDefault()
043: .getDialogSettings();
044: setDialogSettings(getSettingsSection(masterSettings));
045: setDefaultPageImageDescriptor(PDEPluginImages.DESC_FEATURE_IMPORT_WIZ);
046: setWindowTitle(PDEUIMessages.FeatureImportWizard_title);
047: }
048:
049: /*
050: * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
051: */
052: public void init(IWorkbench workbench,
053: IStructuredSelection selection) {
054: }
055:
056: /*
057: * @see org.eclipse.jface.wizard.IWizard#addPages
058: */
059: public void addPages() {
060: setNeedsProgressMonitor(false);
061:
062: fPage = new FeatureImportWizardPage();
063: addPage(fPage);
064: }
065:
066: private IDialogSettings getSettingsSection(IDialogSettings master) {
067: IDialogSettings setting = master.getSection(STORE_SECTION);
068: if (setting == null) {
069: setting = master.addNewSection(STORE_SECTION);
070: }
071: return setting;
072: }
073:
074: /*
075: * @see Wizard#performFinish()
076: */
077: public boolean performFinish() {
078: IFeatureModel[] models = fPage.getSelectedModels();
079: fPage.storeSettings(true);
080: IPath targetPath = computeTargetPath();
081:
082: IReplaceQuery query = new ReplaceQuery(getShell());
083: final FeatureImportOperation op = new FeatureImportOperation(
084: models, fPage.isBinary(), targetPath, query);
085: Job job = new Job(PDEUIMessages.FeatureImportWizard_title) {
086: protected IStatus run(IProgressMonitor monitor) {
087: try {
088: PDEPlugin.getWorkspace().run(op, monitor);
089: } catch (CoreException e) {
090: PDEPlugin.logException(e);
091: return Status.CANCEL_STATUS;
092: }
093: return Status.OK_STATUS;
094: }
095: };
096: job.setUser(true);
097: job.schedule();
098: return true;
099: }
100:
101: /**
102: *
103: * @return IPath or null
104: */
105: private IPath computeTargetPath() {
106: IPath pluginsLocation = PDEPlugin.getWorkspace().getRoot()
107: .getLocation();
108: if ("plugins".equals(pluginsLocation.lastSegment())) //$NON-NLS-1$
109: return pluginsLocation.removeLastSegments(1).append(
110: "features"); //$NON-NLS-1$
111: return null;
112: }
113:
114: private static class ReplaceDialog extends MessageDialog {
115: public ReplaceDialog(Shell parentShell, String dialogMessage) {
116: super (parentShell,
117: PDEUIMessages.FeatureImportWizard_messages_title,
118: null, dialogMessage, MessageDialog.QUESTION,
119: new String[] { IDialogConstants.YES_LABEL,
120: IDialogConstants.YES_TO_ALL_LABEL,
121: IDialogConstants.NO_LABEL,
122: PDEUIMessages.FeatureImportWizard_noToAll,
123: IDialogConstants.CANCEL_LABEL }, 0);
124: }
125: }
126:
127: public static class ReplaceQuery implements IReplaceQuery {
128: public ReplaceQuery(Shell shell) {
129: }
130:
131: private int yesToAll = 0;
132: private int[] RETURNCODES = { IReplaceQuery.YES,
133: IReplaceQuery.YES, IReplaceQuery.NO, IReplaceQuery.NO,
134: IReplaceQuery.CANCEL };
135:
136: public int doQuery(IProject project) {
137: if (yesToAll != 0)
138: return yesToAll > 0 ? IReplaceQuery.YES
139: : IReplaceQuery.NO;
140:
141: final String message = NLS.bind(
142: PDEUIMessages.FeatureImportWizard_messages_exists,
143: project.getName());
144: final int[] result = { IReplaceQuery.CANCEL };
145:
146: Display.getDefault().syncExec(new Runnable() {
147: public void run() {
148: ReplaceDialog dialog = new ReplaceDialog(Display
149: .getDefault().getActiveShell(), message);
150: int retVal = dialog.open();
151: if (retVal >= 0) {
152: result[0] = RETURNCODES[retVal];
153: if (retVal == 1) {
154: yesToAll = 1;
155: } else if (retVal == 3) {
156: yesToAll = -1;
157: }
158: }
159: }
160: });
161: return result[0];
162: }
163: }
164: }
|