KickJava   Java API By Example, From Geeks To Geeks.

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


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

70     public SampleWizard() {
71         PDEPlugin.getDefault().getLabelProvider().connect(this);
72         setDefaultPageImageDescriptor(PDEPluginImages.DESC_NEWEXP_WIZ);
73         samples = Platform.getExtensionRegistry().getConfigurationElementsFor(
74                 "org.eclipse.pde.ui.samples"); //$NON-NLS-1$
75
namesPage= new ProjectNamesPage(this);
76         lastPage = new ReviewPage(this);
77         setNeedsProgressMonitor(true);
78         setWindowTitle(PDEUIMessages.ShowSampleAction_title); //$NON-NLS-1$
79
}
80     public void dispose() {
81         PDEPlugin.getDefault().getLabelProvider().disconnect(this);
82         super.dispose();
83     }
84     public IConfigurationElement[] getSamples() {
85         return samples;
86     }
87     /**
88      *
89      */

90     public void addPages() {
91         if (selection == null) {
92             addPage(new SelectionPage(this));
93         }
94         addPage(namesPage);
95         addPage(lastPage);
96     }
97     /**
98      *
99      */

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

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

176     public void enableActivities() {
177         IConfigurationElement [] elements = selection.getChildren("activity"); //$NON-NLS-1$
178
HashSet activitiesToEnable=new HashSet();
179         IWorkbenchActivitySupport workbenchActivitySupport = PlatformUI.getWorkbench().getActivitySupport();
180         
181         for (int i=0; i<elements.length; i++) {
182             IConfigurationElement element = elements[i];
183             String JavaDoc id=element.getAttribute("id"); //$NON-NLS-1$
184
if (id==null) continue;
185             activitiesToEnable.add(id);
186         }
187         HashSet set = new HashSet(workbenchActivitySupport.getActivityManager().getEnabledActivityIds());
188         set.addAll(activitiesToEnable);
189         workbenchActivitySupport.setEnabledActivityIds(set);
190     }
191     /**
192      *
193      */

194     public void setInitializationData(IConfigurationElement config,
195             String JavaDoc propertyName, Object JavaDoc data) throws CoreException {
196         String JavaDoc variable = data != null && data instanceof String JavaDoc ? data
197                 .toString() : null;
198         if (variable != null) {
199             for (int i = 0; i < samples.length; i++) {
200                 IConfigurationElement element = samples[i];
201                 String JavaDoc id = element.getAttribute("id"); //$NON-NLS-1$
202
if (id != null && id.equals(variable)) {
203                     setSelection(element);
204                     break;
205                 }
206             }
207         }
208     }
209     public void init(IWorkbench workbench, IStructuredSelection selection) {
210     }
211     /**
212      * @return Returns the selection.
213      */

214     public IConfigurationElement getSelection() {
215         return selection;
216     }
217     /**
218      * @param selection
219      * The selection to set.
220      */

221     public void setSelection(IConfigurationElement selection) {
222         this.selection = selection;
223     }
224     /**
225      * @return Returns the sampleEditorNeeded.
226      */

227     public boolean isSampleEditorNeeded() {
228         return sampleEditorNeeded;
229     }
230     /**
231      * @param sampleEditorNeeded
232      * The sampleEditorNeeded to set.
233      */

234     public void setSampleEditorNeeded(boolean sampleEditorNeeded) {
235         this.sampleEditorNeeded = sampleEditorNeeded;
236     }
237     /**
238      * @return Returns the switchPerspective.
239      * @todo Generated comment
240      */

241     public boolean isSwitchPerspective() {
242         return switchPerspective;
243     }
244     /**
245      * @param switchPerspective The switchPerspective to set.
246      * @todo Generated comment
247      */

248     public void setSwitchPerspective(boolean switchPerspective) {
249         this.switchPerspective = switchPerspective;
250     }
251     /**
252      * @return Returns the selectRevealEnabled.
253      * @todo Generated comment
254      */

255     public boolean isSelectRevealEnabled() {
256         return selectRevealEnabled;
257     }
258     /**
259      * @param selectRevealEnabled The selectRevealEnabled to set.
260      * @todo Generated comment
261      */

262     public void setSelectRevealEnabled(boolean selectRevealEnabled) {
263         this.selectRevealEnabled = selectRevealEnabled;
264     }
265     /**
266      * @return Returns the activitiesEnabled.
267      * @todo Generated comment
268      */

269     public boolean getActivitiesEnabled() {
270         return activitiesEnabled;
271     }
272     /**
273      * @param activitiesEnabled The activitiesEnabled to set.
274      * @todo Generated comment
275      */

276     public void setActivitiesEnabled(boolean activitiesEnabled) {
277         this.activitiesEnabled = activitiesEnabled;
278     }
279 }
280
Popular Tags