KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > eclipse > enhydraWizard > NewEnhydraProjectWizard


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.IWorkspace;
20
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 /**
62  * Insert the type's description here.
63  * @see Wizard
64  */

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     /**
74      * The constructor.
75      */

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     /**
85      * Adds a new page to this wizard. The page is inserted at the
86      * end of the page list.
87      *
88      * @param page the new page
89      */

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     /**
107      * Insert the method's description here.
108      * @see Wizard#performFinish
109      */

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"); //$NON-NLS-1$
119
String message= NewEnhydraWizardMessages.getString("NewProjectCreationWizard.op_error.message"); //$NON-NLS-1$
120
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(); //Dacha add 25.02.2003
141

142         try {
143             int i;
144
145 //Dacha add
146
IPath projPath = javaPage.getJavaProject().getProject().getLocation();
147             String a = projPath.uptoSegment(projPath.segmentCount() - 1).toString();
148             
149             
150             wizard = new EclipseCodeGenWizard(prjName, a);//Dacha changed to get prj root
151
//Dacha end
152

153             IWorkspaceRoot myWorkspaceRoot = KelpPlugin.getWorkspace().getRoot();
154
155             wizard.getInnerPanel().initPreferredSize();
156             wizard.showDialog(null);
157
158             // Refresh project, so the generated files could be visible
159
//DACHA comment
160
//IProject EnhydraProject = myWorkspaceRoot.getProject(prjName);
161

162             try {
163                 //DACHA comment
164
//EnhydraProject.refreshLocal(IResource.DEPTH_INFINITE, null);
165
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 /**
174  * Creates External tool builder and make apropriate build order
175  * todo: add DODS in rebuild
176  */

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]; //default for Java Project
185

186             String location = ToolBoxInfo.getEnhydraRoot() + "/bin/ant";
187             if (System.getProperty("os.name").toLowerCase().startsWith("windows")) {
188                 location += ".bat";
189             }
190             
191             //Create XMLC External tool
192
ExternalTool antXMLCTool = new ExternalTool();
193             
194             antXMLCTool.setBlock(true);
195             antXMLCTool.setBuildTypes(new String[0]); //default: do not include in any rebuild
196
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             //Create new command for XMLC
204
ICommand antXMLCCommand = project.getDescription().newCommand();
205             Map argMap = antXMLCTool.toArgumentMap();
206             antXMLCCommand.setArguments(argMap);
207             antXMLCCommand.setBuilderName("org.eclipse.ui.externaltools.ExternalToolBuilder");
208             
209             //Create DeployerExternal tool
210
ExternalTool antDeployTool = new ExternalTool();
211             
212             antDeployTool.setBlock(true);
213             antDeployTool.setBuildTypes(new String[0]);//default: do not include in any rebuild
214
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             //Create new command for Deploy
222
ICommand antDeployCommand = project.getDescription().newCommand();
223             Map argMapDeploy = antDeployTool.toArgumentMap();
224             antDeployCommand.setArguments(argMapDeploy);
225             antDeployCommand.setBuilderName("org.eclipse.ui.externaltools.ExternalToolBuilder");
226             
227             //Create DODSExternal tool
228
ExternalTool antDODSTool = new ExternalTool();
229             
230             antDODSTool.setBlock(true);
231             antDODSTool.setBuildTypes(new String[0]);//default: do not include in any rebuild
232
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             //Create new command for DODS
240
ICommand antDODSCommand = project.getDescription().newCommand();
241             Map argMapDODS = antDODSTool.toArgumentMap();
242             antDODSCommand.setArguments(argMapDODS);
243             antDODSCommand.setBuilderName("org.eclipse.ui.externaltools.ExternalToolBuilder");
244             
245             //Set new builds order
246
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);//Set new build order
254
project.setDescription(description,null); //Save new description (file".project")
255

256         }catch(Exception e){
257                 e.printStackTrace();
258         }
259     }
260     
261     /**
262      * Selects and reveals the newly added resource in all parts
263      * of the active workbench window's active page.
264      *
265      * @see ISetSelectionTarget
266      */

267     protected void selectAndReveal(IResource newResource) {
268         selectAndReveal(newResource, workbench.getActiveWorkbenchWindow());
269     }
270
271     /**
272      * Attempts to select and reveal the specified resource in all
273      * parts within the supplied workbench window's active page.
274      * <p>
275      * Checks all parts in the active page to see if they implement <code>ISetSelectionTarget</code>,
276      * either directly or as an adapter. If so, tells the part to select and reveal the
277      * specified resource.
278      * </p>
279      *
280      * @param resource the resource to be selected and revealed
281      * @param window the workbench window to select and reveal the resource
282      *
283      * @see ISetSelectionTarget
284      */

285     public static void selectAndReveal(IResource resource, IWorkbenchWindow window) {
286         // validate the input
287
if (window == null || resource == null)
288             return;
289         IWorkbenchPage page = window.getActivePage();
290         if (page == null)
291             return;
292     
293         // get all the view and editor parts
294
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             // get the part's ISetSelectionTarget implementation
315
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                 // select and reveal resource
323
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     /**
333      * Insert the method's description here.
334      * @see Wizard#init
335      */

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     /**
346      * Returns the image descriptor with the given relative path.
347      */

348 /* private ImageDescriptor getImageDescriptor(String relativePath) {
349         String iconPath = "icons/full/";
350         try {
351             AbstractUIPlugin plugin = (AbstractUIPlugin) Platform.getPlugin(PlatformUI.PLUGIN_ID);
352             URL installURL = plugin.getDescriptor().getInstallURL();
353             URL url = new URL(installURL, iconPath + relativePath);
354             return ImageDescriptor.createFromURL(url);
355         }
356         catch (MalformedURLException e) {
357             // Should not happen
358             e.printStackTrace();
359             return null;
360         }
361     }
362 */

363     /**
364      * Utility method to create an <code>ImageDescriptor</code>
365      * from a path to a file.
366      */

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     /*
379      * Stores the configuration element for the wizard. The config element will be used
380      * in <code>performFinish</code> to set the result perspective.
381      */

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