1 11 package org.eclipse.pde.internal.ui.samples; 12 import java.lang.reflect.InvocationTargetException ; 13 import java.util.HashSet ; 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 queryOverwrite(String file) { 54 String [] returnCodes = {YES, NO, ALL, CANCEL}; 55 int returnVal = openDialog(file); 56 return returnVal < 0 ? CANCEL : returnCodes[returnVal]; 57 } 58 private int openDialog(final String file) { 59 final int[] result = {IDialogConstants.CANCEL_ID}; 60 getShell().getDisplay().syncExec(new Runnable () { 61 public void run() { 62 String title = PDEUIMessages.SampleWizard_title; 63 String msg = NLS.bind(PDEUIMessages.SampleWizard_overwrite, file); 64 String [] 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 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"); 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 100 public void addPages() { 101 if (selection == null) { 102 addPage(new SelectionPage(this)); 103 } 104 addPage(namesPage); 105 addPage(lastPage); 106 } 107 110 public boolean performFinish() { 111 try { 112 String perspId = selection.getAttribute("perspectiveId"); 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 e) { 130 PDEPlugin.logException(e); 131 return false; 132 } catch (InterruptedException e) { 133 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 152 } 153 154 186 public void enableActivities() { 187 IConfigurationElement [] elements = selection.getChildren("activity"); HashSet activitiesToEnable=new HashSet (); 189 IWorkbenchActivitySupport workbenchActivitySupport = PlatformUI.getWorkbench().getActivitySupport(); 190 191 for (int i=0; i<elements.length; i++) { 192 IConfigurationElement element = elements[i]; 193 String id=element.getAttribute("id"); if (id==null) continue; 195 activitiesToEnable.add(id); 196 } 197 HashSet set = new HashSet (workbenchActivitySupport.getActivityManager().getEnabledActivityIds()); 198 set.addAll(activitiesToEnable); 199 workbenchActivitySupport.setEnabledActivityIds(set); 200 } 201 204 public void setInitializationData(IConfigurationElement config, 205 String propertyName, Object data) throws CoreException { 206 String variable = data != null && data instanceof String ? data 207 .toString() : null; 208 if (variable != null) { 209 for (int i = 0; i < samples.length; i++) { 210 IConfigurationElement element = samples[i]; 211 String id = element.getAttribute("id"); 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 224 public IConfigurationElement getSelection() { 225 return selection; 226 } 227 231 public void setSelection(IConfigurationElement selection) { 232 this.selection = selection; 233 } 234 237 public boolean isSampleEditorNeeded() { 238 return sampleEditorNeeded; 239 } 240 244 public void setSampleEditorNeeded(boolean sampleEditorNeeded) { 245 this.sampleEditorNeeded = sampleEditorNeeded; 246 } 247 251 public boolean isSwitchPerspective() { 252 return switchPerspective; 253 } 254 258 public void setSwitchPerspective(boolean switchPerspective) { 259 this.switchPerspective = switchPerspective; 260 } 261 265 public boolean isSelectRevealEnabled() { 266 return selectRevealEnabled; 267 } 268 272 public void setSelectRevealEnabled(boolean selectRevealEnabled) { 273 this.selectRevealEnabled = selectRevealEnabled; 274 } 275 279 public boolean getActivitiesEnabled() { 280 return activitiesEnabled; 281 } 282 286 public void setActivitiesEnabled(boolean activitiesEnabled) { 287 this.activitiesEnabled = activitiesEnabled; 288 } 289 } 290 | Popular Tags |