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.IConfigurationElement; 17 import org.eclipse.core.runtime.IExecutableExtension; 18 import org.eclipse.core.runtime.IProgressMonitor; 19 20 import org.eclipse.swt.widgets.Display; 21 import org.eclipse.swt.widgets.Shell; 22 23 import org.eclipse.jface.viewers.IStructuredSelection; 24 25 import org.eclipse.ui.IWorkbenchPage; 26 import org.eclipse.ui.IWorkingSet; 27 import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard; 28 29 import org.eclipse.jdt.core.IJavaElement; 30 import org.eclipse.jdt.core.JavaCore; 31 32 import org.eclipse.jdt.internal.ui.JavaPlugin; 33 import org.eclipse.jdt.internal.ui.JavaPluginImages; 34 import org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart; 35 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 36 import org.eclipse.jdt.internal.ui.workingsets.JavaWorkingSetUpdater; 37 import org.eclipse.jdt.internal.ui.workingsets.ViewActionGroup; 38 import org.eclipse.jdt.internal.ui.workingsets.WorkingSetConfigurationBlock; 39 40 public class JavaProjectWizard extends NewElementWizard implements IExecutableExtension { 41 42 private JavaProjectWizardFirstPage fFirstPage; 43 private JavaProjectWizardSecondPage fSecondPage; 44 45 private IConfigurationElement fConfigElement; 46 47 public JavaProjectWizard() { 48 setDefaultPageImageDescriptor(JavaPluginImages.DESC_WIZBAN_NEWJPRJ); 49 setDialogSettings(JavaPlugin.getDefault().getDialogSettings()); 50 setWindowTitle(NewWizardMessages.JavaProjectWizard_title); 51 } 52 53 56 public void addPages() { 57 super.addPages(); 58 fFirstPage= new JavaProjectWizardFirstPage(); 59 fFirstPage.setWorkingSets(getWorkingSets(getSelection())); 60 addPage(fFirstPage); 61 fSecondPage= new JavaProjectWizardSecondPage(fFirstPage); 62 addPage(fSecondPage); 63 } 64 65 68 protected void finishPage(IProgressMonitor monitor) throws InterruptedException , CoreException { 69 fSecondPage.performFinish(monitor); } 71 72 75 public boolean performFinish() { 76 boolean res= super.performFinish(); 77 if (res) { 78 final IJavaElement newElement= getCreatedElement(); 79 80 IWorkingSet[] workingSets= fFirstPage.getWorkingSets(); 81 WorkingSetConfigurationBlock.addToWorkingSets(newElement, workingSets); 82 83 BasicNewProjectResourceWizard.updatePerspective(fConfigElement); 84 selectAndReveal(fSecondPage.getJavaProject().getProject()); 85 86 Display.getDefault().asyncExec(new Runnable () { 87 public void run() { 88 PackageExplorerPart activePackageExplorer= getActivePackageExplorer(); 89 if (activePackageExplorer != null) { 90 activePackageExplorer.tryToReveal(newElement); 91 } 92 } 93 }); 94 } 95 return res; 96 } 97 98 protected void handleFinishException(Shell shell, InvocationTargetException e) { 99 String title= NewWizardMessages.JavaProjectWizard_op_error_title; 100 String message= NewWizardMessages.JavaProjectWizard_op_error_create_message; 101 ExceptionHandler.handle(e, getShell(), title, message); 102 } 103 104 108 public void setInitializationData(IConfigurationElement cfig, String propertyName, Object data) { 109 fConfigElement= cfig; 110 } 111 112 115 public boolean performCancel() { 116 fSecondPage.performCancel(); 117 return super.performCancel(); 118 } 119 120 123 public IJavaElement getCreatedElement() { 124 return JavaCore.create(fFirstPage.getProjectHandle()); 125 } 126 127 private IWorkingSet[] getWorkingSets(IStructuredSelection selection) { 128 IWorkingSet[] selected= WorkingSetConfigurationBlock.getSelectedWorkingSet(selection); 129 if (selected != null && selected.length > 0) { 130 for (int i= 0; i < selected.length; i++) { 131 if (!isValidWorkingSet(selected[i])) 132 return null; 133 } 134 return selected; 135 } 136 137 PackageExplorerPart explorerPart= getActivePackageExplorer(); 138 if (explorerPart == null) 139 return null; 140 141 if (explorerPart.getRootMode() == ViewActionGroup.SHOW_PROJECTS) { 142 IWorkingSet filterWorkingSet= explorerPart.getFilterWorkingSet(); 144 if (filterWorkingSet == null) 145 return null; 146 147 if (!isValidWorkingSet(filterWorkingSet)) 148 return null; 149 150 return new IWorkingSet[] {filterWorkingSet}; 151 } else if (explorerPart.getRootMode() == ViewActionGroup.SHOW_WORKING_SETS) { 152 Object input= explorerPart.getViewPartInput(); 154 if (!(input instanceof IWorkingSet)) 155 return null; 156 157 IWorkingSet workingSet= (IWorkingSet)input; 158 if (!isValidWorkingSet(workingSet)) 159 return null; 160 161 return new IWorkingSet[] {workingSet}; 162 } 163 164 return null; 165 } 166 167 private PackageExplorerPart getActivePackageExplorer() { 168 PackageExplorerPart explorerPart= PackageExplorerPart.getFromActivePerspective(); 169 if (explorerPart == null) 170 return null; 171 172 IWorkbenchPage activePage= explorerPart.getViewSite().getWorkbenchWindow().getActivePage(); 173 if (activePage == null) 174 return null; 175 176 if (activePage.getActivePart() != explorerPart) 177 return null; 178 179 return explorerPart; 180 } 181 182 private boolean isValidWorkingSet(IWorkingSet workingSet) { 183 String id= workingSet.getId(); 184 if (!JavaWorkingSetUpdater.ID.equals(id) && !"org.eclipse.ui.resourceWorkingSetPage".equals(id)) return false; 186 187 if (workingSet.isAggregateWorkingSet()) 188 return false; 189 190 return true; 191 } 192 193 } 194 | Popular Tags |