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.runtime.CoreException;
013: import org.eclipse.core.runtime.IProgressMonitor;
014: import org.eclipse.core.runtime.IStatus;
015: import org.eclipse.core.runtime.Status;
016: import org.eclipse.core.runtime.jobs.Job;
017: import org.eclipse.jface.dialogs.IDialogConstants;
018: import org.eclipse.jface.dialogs.IDialogSettings;
019: import org.eclipse.jface.dialogs.MessageDialog;
020: import org.eclipse.jface.viewers.IStructuredSelection;
021: import org.eclipse.jface.wizard.IWizardPage;
022: import org.eclipse.jface.wizard.Wizard;
023: import org.eclipse.pde.core.plugin.IPluginModelBase;
024: import org.eclipse.pde.internal.ui.PDEPlugin;
025: import org.eclipse.pde.internal.ui.PDEPluginImages;
026: import org.eclipse.pde.internal.ui.PDEUIMessages;
027: import org.eclipse.pde.internal.ui.wizards.imports.PluginImportOperation.IImportQuery;
028: import org.eclipse.swt.widgets.Display;
029: import org.eclipse.swt.widgets.Shell;
030: import org.eclipse.ui.IImportWizard;
031: import org.eclipse.ui.IWorkbench;
032:
033: public class PluginImportWizard extends Wizard implements IImportWizard {
034:
035: private static final String STORE_SECTION = "PluginImportWizard"; //$NON-NLS-1$
036:
037: private IStructuredSelection selection;
038: private PluginImportWizardFirstPage page1;
039: private BaseImportWizardSecondPage page2;
040: private BaseImportWizardSecondPage page3;
041:
042: public PluginImportWizard() {
043: IDialogSettings masterSettings = PDEPlugin.getDefault()
044: .getDialogSettings();
045: setDialogSettings(getSettingsSection(masterSettings));
046: setDefaultPageImageDescriptor(PDEPluginImages.DESC_PLUGIN_IMPORT_WIZ);
047: setWindowTitle(PDEUIMessages.ImportWizard_title);
048: }
049:
050: public void init(IWorkbench workbench,
051: IStructuredSelection selection) {
052: this .selection = selection;
053: }
054:
055: public void addPages() {
056: setNeedsProgressMonitor(true);
057: page1 = new PluginImportWizardFirstPage("first"); //$NON-NLS-1$
058: addPage(page1);
059: page2 = new PluginImportWizardExpressPage(
060: "express", page1, selection); //$NON-NLS-1$
061: addPage(page2);
062: page3 = new PluginImportWizardDetailedPage("detailed", page1); //$NON-NLS-1$
063: addPage(page3);
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: private IPluginModelBase[] getModelsToImport() {
075: if (page1.getNextPage().equals(page2))
076: return page2.getModelsToImport();
077: return page3.getModelsToImport();
078: }
079:
080: public boolean performFinish() {
081: page1.storeSettings();
082: ((BaseImportWizardSecondPage) page1.getNextPage())
083: .storeSettings();
084:
085: final IPluginModelBase[] models = getModelsToImport();
086: doImportOperation(getShell(), page1.getImportType(), models,
087: page2.forceAutoBuild());
088: return true;
089: }
090:
091: public static void doImportOperation(final Shell shell,
092: final int importType, final IPluginModelBase[] models,
093: final boolean forceAutobuild) {
094: PluginImportOperation.IImportQuery query = new ImportQuery(
095: shell);
096: PluginImportOperation.IImportQuery executionQuery = new ImportQuery(
097: shell);
098: final PluginImportOperation op = new PluginImportOperation(
099: models, importType, query, executionQuery,
100: forceAutobuild);
101: Job job = new Job(PDEUIMessages.ImportWizard_title) {
102: protected IStatus run(IProgressMonitor monitor) {
103: try {
104: PDEPlugin.getWorkspace().run(op, monitor);
105: } catch (CoreException e) {
106: PDEPlugin.logException(e);
107: return Status.CANCEL_STATUS;
108: }
109: return Status.OK_STATUS;
110: }
111: };
112: job.setUser(true);
113: job.schedule();
114: }
115:
116: private static class ReplaceDialog extends MessageDialog {
117: public ReplaceDialog(Shell parentShell, String dialogMessage) {
118: super (parentShell,
119: PDEUIMessages.ImportWizard_messages_title, null,
120: dialogMessage, MessageDialog.QUESTION,
121: new String[] { IDialogConstants.YES_LABEL,
122: IDialogConstants.YES_TO_ALL_LABEL,
123: IDialogConstants.NO_LABEL,
124: PDEUIMessages.ImportWizard_noToAll,
125: IDialogConstants.CANCEL_LABEL }, 0);
126: }
127: }
128:
129: public static class ImportQuery implements IImportQuery {
130: public ImportQuery(Shell shell) {
131: }
132:
133: private int yesToAll = 0;
134: private int[] RETURNCODES = { IImportQuery.YES,
135: IImportQuery.YES, IImportQuery.NO, IImportQuery.NO,
136: IImportQuery.CANCEL };
137:
138: public int doQuery(final String message) {
139: if (yesToAll != 0) {
140: return yesToAll > 0 ? IImportQuery.YES
141: : IImportQuery.NO;
142: }
143:
144: final int[] result = { IImportQuery.CANCEL };
145: Display.getDefault().syncExec(new Runnable() {
146: public void run() {
147: ReplaceDialog dialog = new ReplaceDialog(Display
148: .getDefault().getActiveShell(), message);
149: int retVal = dialog.open();
150: if (retVal >= 0) {
151: result[0] = RETURNCODES[retVal];
152: if (retVal == 1) {
153: yesToAll = 1;
154: } else if (retVal == 3) {
155: yesToAll = -1;
156: }
157: }
158: }
159: });
160: return result[0];
161: }
162: }
163:
164: public IWizardPage getNextPage(IWizardPage page) {
165: if (page.equals(page1)) {
166: if (page1.getScanAllPlugins()) {
167: return page3;
168: }
169: return page2;
170: }
171: return null;
172: }
173:
174: public IWizardPage getPreviousPage(IWizardPage page) {
175: return page.equals(page1) ? null : page1;
176: }
177:
178: public boolean canFinish() {
179: return !page1.isCurrentPage()
180: && page1.getNextPage().isPageComplete();
181: }
182: }
|