KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > imports > PluginImportWizard


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.imports;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.core.runtime.jobs.Job;
18 import org.eclipse.jface.dialogs.IDialogConstants;
19 import org.eclipse.jface.dialogs.IDialogSettings;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.jface.wizard.IWizardPage;
23 import org.eclipse.jface.wizard.Wizard;
24 import org.eclipse.pde.core.plugin.IPluginModelBase;
25 import org.eclipse.pde.internal.ui.PDEPlugin;
26 import org.eclipse.pde.internal.ui.PDEPluginImages;
27 import org.eclipse.pde.internal.ui.PDEUIMessages;
28 import org.eclipse.pde.internal.ui.wizards.imports.PluginImportOperation.IImportQuery;
29 import org.eclipse.swt.widgets.Display;
30 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.ui.IImportWizard;
32 import org.eclipse.ui.IWorkbench;
33
34 public class PluginImportWizard extends Wizard implements IImportWizard {
35
36     private static final String JavaDoc STORE_SECTION = "PluginImportWizard"; //$NON-NLS-1$
37

38     private IStructuredSelection selection;
39     private PluginImportWizardFirstPage page1;
40     private BaseImportWizardSecondPage page2;
41     private BaseImportWizardSecondPage page3;
42
43     public PluginImportWizard() {
44         IDialogSettings masterSettings = PDEPlugin.getDefault().getDialogSettings();
45         setDialogSettings(getSettingsSection(masterSettings));
46         setDefaultPageImageDescriptor(PDEPluginImages.DESC_PLUGIN_IMPORT_WIZ);
47         setWindowTitle(PDEUIMessages.ImportWizard_title);
48     }
49
50     public void init(IWorkbench workbench, IStructuredSelection selection) {
51         this.selection = selection;
52     }
53
54     public void addPages() {
55         setNeedsProgressMonitor(true);
56         page1 = new PluginImportWizardFirstPage("first"); //$NON-NLS-1$
57
addPage(page1);
58         page2 = new PluginImportWizardExpressPage("express", page1, selection); //$NON-NLS-1$
59
addPage(page2);
60         page3 = new PluginImportWizardDetailedPage("detailed", page1); //$NON-NLS-1$
61
addPage(page3);
62     }
63
64     private IDialogSettings getSettingsSection(IDialogSettings master) {
65         IDialogSettings setting = master.getSection(STORE_SECTION);
66         if (setting == null) {
67             setting = master.addNewSection(STORE_SECTION);
68         }
69         return setting;
70     }
71
72     private IPluginModelBase[] getModelsToImport() {
73         if (page1.getNextPage().equals(page2))
74             return page2.getModelsToImport();
75         return page3.getModelsToImport();
76     }
77
78     public boolean performFinish() {
79         page1.storeSettings();
80         ((BaseImportWizardSecondPage)page1.getNextPage()).storeSettings();
81
82         final IPluginModelBase[] models = getModelsToImport();
83         doImportOperation(getShell(), page1.getImportType(), models, page2.forceAutoBuild());
84         return true;
85     }
86
87     public static void doImportOperation(
88             final Shell shell,
89             final int importType,
90             final IPluginModelBase[] models,
91             final boolean forceAutobuild) {
92         PluginImportOperation.IImportQuery query = new ImportQuery(shell);
93         PluginImportOperation.IImportQuery executionQuery = new ImportQuery(shell);
94         final PluginImportOperation op =
95             new PluginImportOperation(models, importType, query, executionQuery, forceAutobuild);
96         Job job = new Job(PDEUIMessages.ImportWizard_title) {
97             protected IStatus run(IProgressMonitor monitor) {
98                 try {
99                     PDEPlugin.getWorkspace().run(op, monitor);
100                 } catch (CoreException e) {
101                     PDEPlugin.logException(e);
102                     return Status.CANCEL_STATUS;
103                 }
104                 return Status.OK_STATUS;
105             }
106         };
107         job.setUser(true);
108         job.schedule();
109     }
110
111
112     private static class ReplaceDialog extends MessageDialog {
113         public ReplaceDialog(Shell parentShell, String JavaDoc dialogMessage) {
114             super(
115                     parentShell,
116                     PDEUIMessages.ImportWizard_messages_title,
117                     null,
118                     dialogMessage,
119                     MessageDialog.QUESTION,
120                     new String JavaDoc[] {
121                             IDialogConstants.YES_LABEL,
122                             IDialogConstants.YES_TO_ALL_LABEL,
123                             IDialogConstants.NO_LABEL,
124                             PDEUIMessages.ImportWizard_noToAll,
125                             IDialogConstants.CANCEL_LABEL },
126                             0);
127         }
128     }
129
130     public static class ImportQuery implements IImportQuery {
131         public ImportQuery(Shell shell) {}
132
133         private int yesToAll = 0;
134         private int[] RETURNCODES =
135         {
136                 IImportQuery.YES,
137                 IImportQuery.YES,
138                 IImportQuery.NO,
139                 IImportQuery.NO,
140                 IImportQuery.CANCEL };
141
142         public int doQuery(final String JavaDoc message) {
143             if (yesToAll != 0) {
144                 return yesToAll > 0 ? IImportQuery.YES : IImportQuery.NO;
145             }
146
147             final int[] result = { IImportQuery.CANCEL };
148             Display.getDefault().syncExec(new Runnable JavaDoc() {
149                 public void run() {
150                     ReplaceDialog dialog = new ReplaceDialog(Display.getDefault().getActiveShell(), message);
151                     int retVal = dialog.open();
152                     if (retVal >= 0) {
153                         result[0] = RETURNCODES[retVal];
154                         if (retVal == 1) {
155                             yesToAll = 1;
156                         } else if (retVal == 3) {
157                             yesToAll = -1;
158                         }
159                     }
160                 }
161             });
162             return result[0];
163         }
164     }
165
166     public IWizardPage getNextPage(IWizardPage page) {
167         if (page.equals(page1)) {
168             if (page1.getScanAllPlugins()) {
169                 return page3;
170             }
171             return page2;
172         }
173         return null;
174     }
175
176     public IWizardPage getPreviousPage(IWizardPage page) {
177         return page.equals(page1) ? null : page1;
178     }
179
180     public boolean canFinish() {
181         return !page1.isCurrentPage() && page1.getNextPage().isPageComplete();
182     }
183 }
184
Popular Tags