1 11 package org.eclipse.jdt.internal.ui.wizards; 12 13 import java.lang.reflect.InvocationTargetException ; 14 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.IProgressMonitor; 17 import org.eclipse.core.runtime.OperationCanceledException; 18 import org.eclipse.core.runtime.jobs.ISchedulingRule; 19 import org.eclipse.core.runtime.jobs.Job; 20 21 import org.eclipse.core.resources.IFile; 22 import org.eclipse.core.resources.IResource; 23 import org.eclipse.core.resources.IWorkspaceRunnable; 24 import org.eclipse.core.resources.ResourcesPlugin; 25 26 import org.eclipse.swt.widgets.Display; 27 import org.eclipse.swt.widgets.Shell; 28 29 import org.eclipse.jface.dialogs.IDialogConstants; 30 import org.eclipse.jface.dialogs.MessageDialog; 31 import org.eclipse.jface.operation.IRunnableWithProgress; 32 import org.eclipse.jface.viewers.IStructuredSelection; 33 import org.eclipse.jface.wizard.Wizard; 34 35 import org.eclipse.jface.text.templates.persistence.TemplateStore; 36 37 import org.eclipse.ui.INewWizard; 38 import org.eclipse.ui.IWorkbench; 39 import org.eclipse.ui.IWorkbenchPage; 40 import org.eclipse.ui.PartInitException; 41 import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard; 42 43 import org.eclipse.ui.ide.IDE; 44 45 import org.eclipse.jdt.core.IJavaElement; 46 47 import org.eclipse.jdt.internal.ui.IUIConstants; 48 import org.eclipse.jdt.internal.ui.JavaPlugin; 49 import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter; 50 import org.eclipse.jdt.internal.ui.dialogs.OptionalMessageDialog; 51 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 52 53 public abstract class NewElementWizard extends Wizard implements INewWizard { 54 55 private IWorkbench fWorkbench; 56 private IStructuredSelection fSelection; 57 58 public NewElementWizard() { 59 setNeedsProgressMonitor(true); 60 } 61 62 protected void openResource(final IFile resource) { 63 final IWorkbenchPage activePage= JavaPlugin.getActivePage(); 64 if (activePage != null) { 65 final Display display= getShell().getDisplay(); 66 if (display != null) { 67 display.asyncExec(new Runnable () { 68 public void run() { 69 try { 70 IDE.openEditor(activePage, resource, true); 71 } catch (PartInitException e) { 72 JavaPlugin.log(e); 73 } 74 } 75 }); 76 } 77 } 78 } 79 80 87 protected abstract void finishPage(IProgressMonitor monitor) throws InterruptedException , CoreException; 88 89 92 protected ISchedulingRule getSchedulingRule() { 93 return ResourcesPlugin.getWorkspace().getRoot(); } 95 96 97 protected boolean canRunForked() { 98 return true; 99 } 100 101 public abstract IJavaElement getCreatedElement(); 102 103 protected void handleFinishException(Shell shell, InvocationTargetException e) { 104 String title= NewWizardMessages.NewElementWizard_op_error_title; 105 String message= NewWizardMessages.NewElementWizard_op_error_message; 106 ExceptionHandler.handle(e, shell, title, message); 107 } 108 109 112 public boolean performFinish() { 113 IWorkspaceRunnable op= new IWorkspaceRunnable() { 114 public void run(IProgressMonitor monitor) throws CoreException, OperationCanceledException { 115 try { 116 finishPage(monitor); 117 } catch (InterruptedException e) { 118 throw new OperationCanceledException(e.getMessage()); 119 } 120 } 121 }; 122 try { 123 ISchedulingRule rule= null; 124 Job job= Job.getJobManager().currentJob(); 125 if (job != null) 126 rule= job.getRule(); 127 IRunnableWithProgress runnable= null; 128 if (rule != null) 129 runnable= new WorkbenchRunnableAdapter(op, rule, true); 130 else 131 runnable= new WorkbenchRunnableAdapter(op, getSchedulingRule()); 132 getContainer().run(canRunForked(), true, runnable); 133 } catch (InvocationTargetException e) { 134 handleFinishException(getShell(), e); 135 return false; 136 } catch (InterruptedException e) { 137 return false; 138 } 139 return true; 140 } 141 142 protected void warnAboutTypeCommentDeprecation() { 143 String key= IUIConstants.DIALOGSTORE_TYPECOMMENT_DEPRECATED; 144 if (OptionalMessageDialog.isDialogEnabled(key)) { 145 TemplateStore templates= JavaPlugin.getDefault().getTemplateStore(); 146 boolean isOldWorkspace= templates.findTemplate("filecomment") != null && templates.findTemplate("typecomment") != null; if (!isOldWorkspace) { 148 OptionalMessageDialog.setDialogEnabled(key, false); 149 } 150 String title= NewWizardMessages.NewElementWizard_typecomment_deprecated_title; 151 String message= NewWizardMessages.NewElementWizard_typecomment_deprecated_message; 152 OptionalMessageDialog.open(key, getShell(), title, null, message, MessageDialog.INFORMATION, new String [] { IDialogConstants.OK_LABEL }, 0); 153 } 154 } 155 156 159 public void init(IWorkbench workbench, IStructuredSelection currentSelection) { 160 fWorkbench= workbench; 161 fSelection= currentSelection; 162 } 163 164 public IStructuredSelection getSelection() { 165 return fSelection; 166 } 167 168 public IWorkbench getWorkbench() { 169 return fWorkbench; 170 } 171 172 protected void selectAndReveal(IResource newResource) { 173 BasicNewResourceWizard.selectAndReveal(newResource, fWorkbench.getActiveWorkbenchWindow()); 174 } 175 176 } 177 | Popular Tags |