KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > samples > SampleWizard


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.samples;
12 import java.lang.reflect.InvocationTargetException JavaDoc;
13 import java.util.HashSet JavaDoc;
14
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IConfigurationElement;
18 import org.eclipse.core.runtime.IExecutableExtension;
19 import org.eclipse.core.runtime.OperationCanceledException;
20 import org.eclipse.core.runtime.Platform;
21 import org.eclipse.jface.dialogs.IDialogConstants;
22 import org.eclipse.jface.dialogs.MessageDialog;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.jface.wizard.Wizard;
25 import org.eclipse.osgi.util.NLS;
26 import org.eclipse.pde.internal.ui.PDEPlugin;
27 import org.eclipse.pde.internal.ui.PDEPluginImages;
28 import org.eclipse.pde.internal.ui.PDEUIMessages;
29 import org.eclipse.swt.widgets.Shell;
30 import org.eclipse.ui.INewWizard;
31 import org.eclipse.ui.IWorkbench;
32 import org.eclipse.ui.IWorkbenchPage;
33 import org.eclipse.ui.PlatformUI;
34 import org.eclipse.ui.activities.IWorkbenchActivitySupport;
35 import org.eclipse.ui.dialogs.IOverwriteQuery;
36 import org.eclipse.ui.ide.IDE;
37
38 public class SampleWizard extends Wizard
39         implements
40             INewWizard,
41             IExecutableExtension {
42     private IConfigurationElement[] samples;
43     private IConfigurationElement selection;
44     private ProjectNamesPage namesPage;
45     private ReviewPage lastPage;
46     
47     private boolean sampleEditorNeeded=true;
48     private boolean switchPerspective=true;
49     private boolean selectRevealEnabled=true;
50     private boolean activitiesEnabled=true;
51     
52     private class ImportOverwriteQuery implements IOverwriteQuery {
53         public String JavaDoc queryOverwrite(String JavaDoc file) {
54             String JavaDoc[] returnCodes = {YES, NO, ALL, CANCEL};
55             int returnVal = openDialog(file);
56             return returnVal < 0 ? CANCEL : returnCodes[returnVal];
57         }
58         private int openDialog(final String JavaDoc file) {
59             final int[] result = {IDialogConstants.CANCEL_ID};
60             getShell().getDisplay().syncExec(new Runnable JavaDoc() {
61                 public void run() {
62                     String JavaDoc title = PDEUIMessages.SampleWizard_title;
63                     String JavaDoc msg = NLS.bind(PDEUIMessages.SampleWizard_overwrite, file);
64                     String JavaDoc[] options = {IDialogConstants.YES_LABEL,
65                             IDialogConstants.NO_LABEL,
66                             IDialogConstants.YES_TO_ALL_LABEL,
67                             IDialogConstants.CANCEL_LABEL};
68                     MessageDialog dialog = new MessageDialog(getShell(), title,
69                             null, msg, MessageDialog.QUESTION, options, 0);
70                     result[0] = dialog.open();
71                 }
72             });
73             return result[0];
74         }
75     }
76     /**
77      * The default constructor.
78      *
79      */

80     public SampleWizard() {
81         PDEPlugin.getDefault().getLabelProvider().connect(this);
82         setDefaultPageImageDescriptor(PDEPluginImages.DESC_NEWEXP_WIZ);
83         samples = Platform.getExtensionRegistry().getConfigurationElementsFor(
84                 "org.eclipse.pde.ui.samples"); //$NON-NLS-1$
85
namesPage= new ProjectNamesPage(this);
86         lastPage = new ReviewPage(this);
87         setNeedsProgressMonitor(true);
88         setWindowTitle(PDEUIMessages.ShowSampleAction_title);
89     }
90     public void dispose() {
91         PDEPlugin.getDefault().getLabelProvider().disconnect(this);
92         super.dispose();
93     }
94     public IConfigurationElement[] getSamples() {
95         return samples;
96     }
97     /**
98      *
99      */

100     public void addPages() {
101         if (selection == null) {
102             addPage(new SelectionPage(this));
103         }
104         addPage(namesPage);
105         addPage(lastPage);
106     }
107     /**
108      *
109      */

110     public boolean performFinish() {
111         try {
112             String JavaDoc perspId = selection.getAttribute("perspectiveId"); //$NON-NLS-1$
113
IWorkbenchPage page = PDEPlugin.getActivePage();
114             if (perspId != null && switchPerspective) {
115                 PlatformUI.getWorkbench().showPerspective(perspId, page.getWorkbenchWindow());
116             }
117             SampleOperation op = new SampleOperation(selection,
118                     namesPage.getProjectNames(),
119                     new ImportOverwriteQuery());
120             getContainer().run(true, true, op);
121             IFile sampleManifest = op.getSampleManifest();
122             if (selectRevealEnabled) {
123                 selectReveal(getShell());
124             }
125             if (activitiesEnabled)
126                 enableActivities();
127             if (sampleEditorNeeded && sampleManifest != null)
128                 IDE.openEditor(page, sampleManifest, true);
129         } catch (InvocationTargetException JavaDoc e) {
130             PDEPlugin.logException(e);
131             return false;
132         } catch (InterruptedException JavaDoc e) {
133             //PDEPlugin.logException(e);
134
return false;
135         } catch (CoreException e) {
136             PDEPlugin.logException(e);
137             return false;
138         } catch (OperationCanceledException e) {
139             return false;
140         }
141         return true;
142     }
143
144     public void selectReveal(Shell shell) {
145         /*
146         shell.getDisplay().asyncExec(new Runnable() {
147             public void run() {
148                 doSelectReveal();
149             }
150         });
151         */

152     }
153
154     /*private void doSelectReveal() {
155         if (selection == null || createdProjects==null)
156             return;
157         String viewId = selection.getAttribute("targetViewId"); //$NON-NLS-1$
158         if (viewId == null)
159             return;
160         IWorkbenchWindow window = PlatformUI.getWorkbench()
161                 .getActiveWorkbenchWindow();
162         if (window == null)
163             return;
164         IWorkbenchPage page = window.getActivePage();
165         if (page == null)
166             return;
167         IViewPart view = page.findView(viewId);
168         if (view == null || !(view instanceof ISetSelectionTarget))
169             return;
170         ISetSelectionTarget target = (ISetSelectionTarget) view;
171         IConfigurationElement[] projects = selection.getChildren("project"); //$NON-NLS-1$
172
173         ArrayList items = new ArrayList();
174         for (int i = 0; i < projects.length; i++) {
175             String path = projects[i].getAttribute("selectReveal"); //$NON-NLS-1$
176             if (path == null)
177                 continue;
178             IResource resource = createdProjects[i].findMember(path);
179             if (resource.exists())
180                 items.add(resource);
181         }
182         if (items.size() > 0)
183             target.selectReveal(new StructuredSelection(items));
184     }
185     */

186     public void enableActivities() {
187         IConfigurationElement [] elements = selection.getChildren("activity"); //$NON-NLS-1$
188
HashSet JavaDoc activitiesToEnable=new HashSet JavaDoc();
189         IWorkbenchActivitySupport workbenchActivitySupport = PlatformUI.getWorkbench().getActivitySupport();
190         
191         for (int i=0; i<elements.length; i++) {
192             IConfigurationElement element = elements[i];
193             String JavaDoc id=element.getAttribute("id"); //$NON-NLS-1$
194
if (id==null) continue;
195             activitiesToEnable.add(id);
196         }
197         HashSet JavaDoc set = new HashSet JavaDoc(workbenchActivitySupport.getActivityManager().getEnabledActivityIds());
198         set.addAll(activitiesToEnable);
199         workbenchActivitySupport.setEnabledActivityIds(set);
200     }
201     /**
202      *
203      */

204     public void setInitializationData(IConfigurationElement config,
205             String JavaDoc propertyName, Object JavaDoc data) throws CoreException {
206         String JavaDoc variable = data != null && data instanceof String JavaDoc ? data
207                 .toString() : null;
208         if (variable != null) {
209             for (int i = 0; i < samples.length; i++) {
210                 IConfigurationElement element = samples[i];
211                 String JavaDoc id = element.getAttribute("id"); //$NON-NLS-1$
212
if (id != null && id.equals(variable)) {
213                     setSelection(element);
214                     break;
215                 }
216             }
217         }
218     }
219     public void init(IWorkbench workbench, IStructuredSelection selection) {
220     }
221     /**
222      * @return Returns the selection.
223      */

224     public IConfigurationElement getSelection() {
225         return selection;
226     }
227     /**
228      * @param selection
229      * The selection to set.
230      */

231     public void setSelection(IConfigurationElement selection) {
232         this.selection = selection;
233     }
234     /**
235      * @return Returns the sampleEditorNeeded.
236      */

237     public boolean isSampleEditorNeeded() {
238         return sampleEditorNeeded;
239     }
240     /**
241      * @param sampleEditorNeeded
242      * The sampleEditorNeeded to set.
243      */

244     public void setSampleEditorNeeded(boolean sampleEditorNeeded) {
245         this.sampleEditorNeeded = sampleEditorNeeded;
246     }
247     /**
248      * @return Returns the switchPerspective.
249      * @todo Generated comment
250      */

251     public boolean isSwitchPerspective() {
252         return switchPerspective;
253     }
254     /**
255      * @param switchPerspective The switchPerspective to set.
256      * @todo Generated comment
257      */

258     public void setSwitchPerspective(boolean switchPerspective) {
259         this.switchPerspective = switchPerspective;
260     }
261     /**
262      * @return Returns the selectRevealEnabled.
263      * @todo Generated comment
264      */

265     public boolean isSelectRevealEnabled() {
266         return selectRevealEnabled;
267     }
268     /**
269      * @param selectRevealEnabled The selectRevealEnabled to set.
270      * @todo Generated comment
271      */

272     public void setSelectRevealEnabled(boolean selectRevealEnabled) {
273         this.selectRevealEnabled = selectRevealEnabled;
274     }
275     /**
276      * @return Returns the activitiesEnabled.
277      * @todo Generated comment
278      */

279     public boolean getActivitiesEnabled() {
280         return activitiesEnabled;
281     }
282     /**
283      * @param activitiesEnabled The activitiesEnabled to set.
284      * @todo Generated comment
285      */

286     public void setActivitiesEnabled(boolean activitiesEnabled) {
287         this.activitiesEnabled = activitiesEnabled;
288     }
289 }
290
Popular Tags