KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > forrest > eclipse > wizards > NewProjectWizard


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation or its licensors,
3  * as applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.forrest.eclipse.wizards;
18
19 import org.apache.log4j.Logger;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25
26 import org.apache.forrest.eclipse.ForrestPlugin;
27 import org.eclipse.core.resources.IProject;
28 import org.eclipse.core.resources.IProjectDescription;
29 import org.eclipse.core.runtime.CoreException;
30 import org.eclipse.core.runtime.IPath;
31 import org.eclipse.core.runtime.IProgressMonitor;
32 import org.eclipse.core.runtime.IStatus;
33 import org.eclipse.core.runtime.NullProgressMonitor;
34 import org.eclipse.core.runtime.Status;
35 import org.eclipse.core.runtime.SubProgressMonitor;
36 import org.eclipse.jface.viewers.IStructuredSelection;
37 import org.eclipse.jface.wizard.Wizard;
38 import org.eclipse.ui.INewWizard;
39 import org.eclipse.ui.IWorkbench;
40 import org.eclipse.ui.IWorkbenchWizard;
41 import org.eclipse.ui.actions.WorkspaceModifyOperation;
42 import org.eclipse.ui.dialogs.WizardNewProjectCreationPage;
43
44 /**
45  * Create a new Content Package project.
46  */

47
48 public class NewProjectWizard extends Wizard implements INewWizard {
49     /**
50      * Logger for this class
51      */

52     private static final Logger logger = Logger
53             .getLogger(NewProjectWizard.class);
54
55     private WizardNewProjectCreationPage page;
56
57     /**
58      * Constructor for ContentPackageWizard.
59      */

60     public NewProjectWizard() {
61         super();
62         setWindowTitle("New Content Package");
63         setNeedsProgressMonitor(true);
64     }
65     
66     /**
67      * Adding the page to the wizard.
68      */

69
70     public void addPages() {
71         page = new WizardNewProjectCreationPage("NewProjectCreationWizard");
72         page.setTitle("New");
73         page.setDescription("Create a new Content Package.");
74         addPage(page);
75     }
76
77     /**
78      * This method is called when 'Finish' button is pressed in
79      * the wizard. We will create an operation and run it
80      * using wizard as execution context.
81      */

82     public boolean performFinish() {
83         WorkspaceModifyOperation op= new WorkspaceModifyOperation() {
84             protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException JavaDoc, InterruptedException JavaDoc {
85                 finishPage(monitor);
86             }
87         };
88         try {
89             getContainer().run(false, true, op);
90         } catch (InvocationTargetException JavaDoc e) {
91             return false; // TODO: should open error dialog and log
92
} catch (InterruptedException JavaDoc e) {
93             return false; // canceled
94
}
95         return true;
96     }
97     
98     private void finishPage(IProgressMonitor monitor) throws InterruptedException JavaDoc, CoreException {
99         if (monitor == null) {
100             monitor= new NullProgressMonitor();
101         }
102         
103         int exitValue = -1;
104         try {
105             String JavaDoc strName = page.getProjectName();
106             monitor.beginTask("Creating "+ strName + " Forrest Project", 3);
107
108             IProject project= page.getProjectHandle();
109             IPath locationPath= page.getLocationPath();
110         
111             // create the project
112
IProjectDescription desc= project.getWorkspace().newProjectDescription(project.getName());
113             if (!page.useDefaults()) {
114                 desc.setLocation(locationPath);
115             }
116             project.create(desc, new SubProgressMonitor(monitor, 1));
117             project.open(new SubProgressMonitor(monitor, 1));
118             
119             // seed the project
120
ForrestPlugin plugin = ForrestPlugin.getDefault();
121             
122             String JavaDoc strPath = locationPath.toOSString();
123             String JavaDoc cmdString = null;
124             
125             if (System.getProperty("os.name").toLowerCase().startsWith("linux")) {
126                 cmdString = "forrest -Dbasedir=" + strPath + "/" + strName
127                         + " seed";
128             } else if (System.getProperty("os.name").toLowerCase().startsWith("windows")) {
129                 cmdString = "cmd /c forrest -Dbasedir=" + strPath + "\\" + strName
130                         + " seed";
131             }
132             
133             try {
134               String JavaDoc lineRead = null;
135               Process JavaDoc seedProc = Runtime.getRuntime().exec(cmdString);
136               BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(seedProc.getInputStream()));
137               while((lineRead = reader.readLine()) != null) {
138                 if (logger.isDebugEnabled()) {
139                     logger.debug("finishPage(IProgressMonitor)" + lineRead);
140                 }
141               }
142               exitValue = seedProc.exitValue();
143             } catch (IOException JavaDoc e) {
144                 // TODO Auto-generated catch block
145
logger.error("finishPage(IProgressMonitor)", e);
146             }
147             
148             project.refreshLocal(IProject.DEPTH_INFINITE, monitor);
149             
150             // TODO: configure your page / nature
151

152             // TODO: change to the perspective specified in the plugin.xml
153
} finally {
154             monitor.done();
155         }
156     }
157
158     private void throwCoreException(String JavaDoc message) throws CoreException {
159         IStatus status =
160             new Status(IStatus.ERROR, "org.burrokeet.application", IStatus.OK, message, null);
161         throw new CoreException(status);
162     }
163
164     /**
165      * We will accept the selection in the workbench to see if
166      * we can initialize from it.
167      * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
168      */

169     public void init(IWorkbench workbench, IStructuredSelection selection) {
170     }
171 }
Popular Tags