1 2 package org.enhydra.kelp.eclipse.enhydraWizard; 3 4 import java.lang.reflect.InvocationTargetException; 5 import java.net.MalformedURLException; 6 import java.net.URL; 7 import java.util.ArrayList; 8 import java.util.Arrays; 9 import java.util.Iterator; 10 import java.util.List; 11 import java.util.Map; 12 13 import org.eclipse.core.internal.events.BuildManager; 14 import org.eclipse.core.internal.resources.Workspace; 15 import org.eclipse.core.resources.ICommand; 16 import org.eclipse.core.resources.IProject; 17 import org.eclipse.core.resources.IProjectDescription; 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.resources.IWorkspaceRoot; 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IConfigurationElement; 23 import org.eclipse.core.runtime.IPath; 24 import org.eclipse.core.runtime.Path; 25 26 import org.eclipse.jdt.internal.ui.preferences.NewJavaProjectPreferencePage; 27 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 28 import org.eclipse.jface.operation.IRunnableWithProgress; 29 import org.eclipse.jface.resource.ImageDescriptor; 30 import org.eclipse.jface.viewers.ISelection; 31 import org.eclipse.jface.viewers.IStructuredSelection; 32 import org.eclipse.jface.viewers.StructuredSelection; 33 import org.eclipse.jface.wizard.Wizard; 34 import org.eclipse.ui.IEditorReference; 35 import org.eclipse.ui.INewWizard; 36 import org.eclipse.ui.IViewPart; 37 import org.eclipse.ui.IViewReference; 38 import org.eclipse.ui.IWorkbench; 39 import org.eclipse.ui.IWorkbenchPage; 40 import org.eclipse.ui.IWorkbenchPart; 41 import org.eclipse.ui.IWorkbenchWindow; 42 import org.eclipse.ui.actions.WorkspaceModifyDelegatingOperation; 43 import org.eclipse.ui.externaltools.internal.core.DefaultRunnerContext; 44 import org.eclipse.ui.externaltools.internal.core.ExternalTool; 45 import org.eclipse.ui.externaltools.internal.core.ExternalToolsPlugin; 46 import org.eclipse.ui.externaltools.internal.core.ExternalToolsRegistry; 47 import org.eclipse.ui.part.ISetSelectionTarget; 48 import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard; 49 import org.enhydra.kelp.eclipse.KelpPlugin; 50 import org.enhydra.kelp.eclipse.codegen.wizard.EclipseCodeGenWizard; 51 import org.enhydra.tool.ToolBoxInfo; 52 import org.enhydra.tool.codegen.Generator; 53 import org.enhydra.tool.codegen.GeneratorException; 54 import org.enhydra.tool.codegen.GeneratorOption; 55 import org.enhydra.tool.codegen.wizard.CodeGenWizard; 56 import org.enhydra.tool.common.SwingUtil; 57 import org.eclipse.jdt.internal.ui.JavaPlugin; 58 59 60 61 65 public class NewEnhydraProjectWizard extends Wizard implements INewWizard { 66 67 private NewEnhydraWizardPage mainPage; 68 private NewEnhydraWizardSecondPage javaPage; 69 private IConfigurationElement fConfigElement; 70 private IWorkbench workbench; 71 private IStructuredSelection selection; 72 73 76 public NewEnhydraProjectWizard() { 77 super(); 78 79 setDefaultPageImageDescriptor(createImageDescriptor("EnhydraBig.gif")); 80 setDialogSettings(KelpPlugin.getDefault().getDialogSettings()); 81 setWindowTitle(NewEnhydraWizardMessages.getString("NewEnhydraProjectWizard.title")); 82 } 83 84 90 public void addPages() { 91 super.addPages(); 92 93 mainPage = new NewEnhydraWizardPage("NewEnhydraProjectWizard"); 94 mainPage.setDescription(NewEnhydraWizardMessages.getString("NewEnhydraProjectWizard.MainPage.description")); 95 mainPage.setTitle(NewEnhydraWizardMessages.getString("NewEnhydraProjectWizard.MainPage.title")); 96 addPage(mainPage); 97 98 javaPage = new NewEnhydraWizardSecondPage(mainPage); 99 addPage(javaPage); 100 } 101 102 public NewEnhydraWizardSecondPage getJavaPage() { 103 return javaPage; 104 } 105 106 110 public boolean performFinish() { 111 112 113 114 IRunnableWithProgress op= new WorkspaceModifyDelegatingOperation(javaPage.getRunnable()); 115 try { 116 getContainer().run(false, true, op); 117 } catch (InvocationTargetException e) { 118 String title= NewEnhydraWizardMessages.getString("NewProjectCreationWizard.op_error.title"); String message= NewEnhydraWizardMessages.getString("NewProjectCreationWizard.op_error.message"); ExceptionHandler.handle(e, getShell(), title, message); 121 return false; 122 } catch (InterruptedException e) { 123 return false; 124 } 125 BasicNewProjectResourceWizard.updatePerspective(fConfigElement); 126 selectAndReveal(javaPage.getJavaProject().getProject()); 127 createExternalBuilders(); 128 129 runEnhydraWizard(); 130 131 132 return true; 133 } 134 135 private void runEnhydraWizard() { 136 EclipseCodeGenWizard wizard; 137 IProject project = javaPage.getJavaProject().getProject(); 138 139 String prjName = javaPage.getJavaProject().getProject().getName(); 140 SwingUtil.setLookAndFeelToSystem(); 142 try { 143 int i; 144 145 IPath projPath = javaPage.getJavaProject().getProject().getLocation(); 147 String a = projPath.uptoSegment(projPath.segmentCount() - 1).toString(); 148 149 150 wizard = new EclipseCodeGenWizard(prjName, a); 153 IWorkspaceRoot myWorkspaceRoot = KelpPlugin.getWorkspace().getRoot(); 154 155 wizard.getInnerPanel().initPreferredSize(); 156 wizard.showDialog(null); 157 158 162 try { 163 project.refreshLocal(IResource.DEPTH_INFINITE, null); 166 } catch (CoreException e) { 167 System.err.println(prjName); 168 } 169 } catch (GeneratorException e) { 170 System.err.println(e.getMessage()); 171 } 172 } 173 177 private void createExternalBuilders(){ 178 IProject project = javaPage.getJavaProject().getProject(); 179 180 ICommand[] commands = new ICommand[0]; 181 try{ 182 commands = project.getDescription().getBuildSpec(); 183 184 ICommand javaBuildCommand = commands[0]; 186 String location = ToolBoxInfo.getEnhydraRoot() + "/bin/ant"; 187 if (System.getProperty("os.name").toLowerCase().startsWith("windows")) { 188 location += ".bat"; 189 } 190 191 ExternalTool antXMLCTool = new ExternalTool(); 193 194 antXMLCTool.setBlock(true); 195 antXMLCTool.setBuildTypes(new String[0]); antXMLCTool.setLocation(location); 197 antXMLCTool.setArguments("xmlc"); 198 antXMLCTool.setName("ant xmlc"); 199 antXMLCTool.setRefreshScope("${project}"); 200 antXMLCTool.setShowLog(true); 201 antXMLCTool.setType(ExternalTool.TOOL_TYPE_PROGRAM); 202 antXMLCTool.setWorkingDirectory("${workspace_loc:/"+project.getName()+"}"); 203 ICommand antXMLCCommand = project.getDescription().newCommand(); 205 Map argMap = antXMLCTool.toArgumentMap(); 206 antXMLCCommand.setArguments(argMap); 207 antXMLCCommand.setBuilderName("org.eclipse.ui.externaltools.ExternalToolBuilder"); 208 209 ExternalTool antDeployTool = new ExternalTool(); 211 212 antDeployTool.setBlock(true); 213 antDeployTool.setBuildTypes(new String[0]); antDeployTool.setLocation(location); 215 antDeployTool.setArguments("deploy"); 216 antDeployTool.setName("ant deploy"); 217 antDeployTool.setRefreshScope("${project}"); 218 antDeployTool.setShowLog(true); 219 antDeployTool.setType(ExternalTool.TOOL_TYPE_PROGRAM); 220 antDeployTool.setWorkingDirectory("${workspace_loc:/"+project.getName()+"}"); 221 ICommand antDeployCommand = project.getDescription().newCommand(); 223 Map argMapDeploy = antDeployTool.toArgumentMap(); 224 antDeployCommand.setArguments(argMapDeploy); 225 antDeployCommand.setBuilderName("org.eclipse.ui.externaltools.ExternalToolBuilder"); 226 227 ExternalTool antDODSTool = new ExternalTool(); 229 230 antDODSTool.setBlock(true); 231 antDODSTool.setBuildTypes(new String[0]); antDODSTool.setLocation(location); 233 antDODSTool.setArguments("dods"); 234 antDODSTool.setName("ant dods"); 235 antDODSTool.setRefreshScope("${project}"); 236 antDODSTool.setShowLog(true); 237 antDODSTool.setType(ExternalTool.TOOL_TYPE_PROGRAM); 238 antDODSTool.setWorkingDirectory("${workspace_loc:/"+project.getName()+"}"); 239 ICommand antDODSCommand = project.getDescription().newCommand(); 241 Map argMapDODS = antDODSTool.toArgumentMap(); 242 antDODSCommand.setArguments(argMapDODS); 243 antDODSCommand.setBuilderName("org.eclipse.ui.externaltools.ExternalToolBuilder"); 244 245 commands = new ICommand[4]; 247 commands[0] = antXMLCCommand; 248 commands[1] = antDODSCommand; 249 commands[2] = javaBuildCommand; 250 commands[3] = antDeployCommand; 251 252 IProjectDescription description = project.getDescription(); 253 description.setBuildSpec(commands); project.setDescription(description,null); 256 }catch(Exception e){ 257 e.printStackTrace(); 258 } 259 } 260 261 267 protected void selectAndReveal(IResource newResource) { 268 selectAndReveal(newResource, workbench.getActiveWorkbenchWindow()); 269 } 270 271 285 public static void selectAndReveal(IResource resource, IWorkbenchWindow window) { 286 if (window == null || resource == null) 288 return; 289 IWorkbenchPage page = window.getActivePage(); 290 if (page == null) 291 return; 292 293 List parts = new ArrayList(); 295 296 IViewReference[] viewRefs = page.getViewReferences(); 297 IViewPart[] viewParts = new IViewPart[viewRefs.length]; 298 for(int i = 0; i < viewRefs.length; i++) { 299 viewParts[i] = viewRefs[i].getView(true); 300 } 301 302 parts.addAll(Arrays.asList(viewParts)); 303 IEditorReference refs[] = page.getEditorReferences(); 304 for (int i = 0; i < refs.length; i++) { 305 if(refs[i].getPart(false) != null) 306 parts.add(refs[i].getPart(false)); 307 } 308 309 final ISelection selection = new StructuredSelection(resource); 310 Iterator enum = parts.iterator(); 311 while (enum.hasNext()) { 312 IWorkbenchPart part = (IWorkbenchPart) enum.next(); 313 314 ISetSelectionTarget target = null; 316 if (part instanceof ISetSelectionTarget) 317 target = (ISetSelectionTarget) part; 318 else 319 target = (ISetSelectionTarget) part.getAdapter(ISetSelectionTarget.class); 320 321 if (target != null) { 322 final ISetSelectionTarget finalTarget = target; 324 window.getShell().getDisplay().asyncExec(new Runnable() { 325 public void run() { 326 finalTarget.selectReveal(selection); 327 } 328 }); 329 } 330 } 331 } 332 336 public void init(IWorkbench workbench, IStructuredSelection selection) { 337 this.workbench = workbench; 338 this.selection = selection; 339 340 setWindowTitle(NewEnhydraWizardMessages.getString("NewEnhydraProjectWizard.MainPage.title")); 341 setDefaultPageImageDescriptor(createImageDescriptor("EnhydraBig.gif")); 342 setNeedsProgressMonitor(true); 343 } 344 345 348 363 367 private ImageDescriptor createImageDescriptor(String path) { 368 URL baseURL = KelpPlugin.getDefault().getDescriptor().getInstallURL(); 369 String iconPath = "icons/"; 370 try { 371 URL url = new URL(baseURL, iconPath + path); 372 return ImageDescriptor.createFromURL(url); 373 } catch (MalformedURLException e) { 374 } 375 return ImageDescriptor.getMissingImageDescriptor(); 376 } 377 378 382 public void setInitializationData(IConfigurationElement cfig, String propertyName, Object data) { 383 fConfigElement= cfig; 384 } 385 386 public boolean canFinish() { 387 if (javaPage.getJavaProject() == null) { 388 return false; 389 } 390 return true; 391 } 392 393 394 } 395 | Popular Tags |