1 11 package org.eclipse.pde.ui.internal.samples; 12 import java.lang.reflect.InvocationTargetException ; 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 queryOverwrite(String file) { 44 String [] returnCodes = {YES, NO, ALL, CANCEL}; 45 int returnVal = openDialog(file); 46 return returnVal < 0 ? CANCEL : returnCodes[returnVal]; 47 } 48 private int openDialog(final String file) { 49 final int[] result = {IDialogConstants.CANCEL_ID}; 50 getShell().getDisplay().syncExec(new Runnable () { 51 public void run() { 52 String title = PDEUIMessages.SampleWizard_title; String msg = NLS.bind(PDEUIMessages.SampleWizard_overwrite, file); String [] 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 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"); namesPage= new ProjectNamesPage(this); 76 lastPage = new ReviewPage(this); 77 setNeedsProgressMonitor(true); 78 setWindowTitle(PDEUIMessages.ShowSampleAction_title); } 80 public void dispose() { 81 PDEPlugin.getDefault().getLabelProvider().disconnect(this); 82 super.dispose(); 83 } 84 public IConfigurationElement[] getSamples() { 85 return samples; 86 } 87 90 public void addPages() { 91 if (selection == null) { 92 addPage(new SelectionPage(this)); 93 } 94 addPage(namesPage); 95 addPage(lastPage); 96 } 97 100 public boolean performFinish() { 101 try { 102 String perspId = selection.getAttribute("perspectiveId"); 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 e) { 120 PDEPlugin.logException(e); 121 return false; 122 } catch (InterruptedException e) { 123 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 142 } 143 144 176 public void enableActivities() { 177 IConfigurationElement [] elements = selection.getChildren("activity"); 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 id=element.getAttribute("id"); 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 194 public void setInitializationData(IConfigurationElement config, 195 String propertyName, Object data) throws CoreException { 196 String variable = data != null && data instanceof String ? data 197 .toString() : null; 198 if (variable != null) { 199 for (int i = 0; i < samples.length; i++) { 200 IConfigurationElement element = samples[i]; 201 String id = element.getAttribute("id"); 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 214 public IConfigurationElement getSelection() { 215 return selection; 216 } 217 221 public void setSelection(IConfigurationElement selection) { 222 this.selection = selection; 223 } 224 227 public boolean isSampleEditorNeeded() { 228 return sampleEditorNeeded; 229 } 230 234 public void setSampleEditorNeeded(boolean sampleEditorNeeded) { 235 this.sampleEditorNeeded = sampleEditorNeeded; 236 } 237 241 public boolean isSwitchPerspective() { 242 return switchPerspective; 243 } 244 248 public void setSwitchPerspective(boolean switchPerspective) { 249 this.switchPerspective = switchPerspective; 250 } 251 255 public boolean isSelectRevealEnabled() { 256 return selectRevealEnabled; 257 } 258 262 public void setSelectRevealEnabled(boolean selectRevealEnabled) { 263 this.selectRevealEnabled = selectRevealEnabled; 264 } 265 269 public boolean getActivitiesEnabled() { 270 return activitiesEnabled; 271 } 272 276 public void setActivitiesEnabled(boolean activitiesEnabled) { 277 this.activitiesEnabled = activitiesEnabled; 278 } 279 } 280 | Popular Tags |