package tool.wizards;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.IWizardContainer;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWizard;
import org.eclipse.ui.dialogs.WizardNewProjectCreationPage;
import tool.ToolPlugin;
import tool.properties.ProjectPropertyPage;
public class NewProjectWizard extends Wizard implements INewWizard {
private WizardNewProjectCreationPage _pageOne;
private ProjectPropertyPage _projectPropertyPage;
private ISelection selection;
private static final String WIZARD_NAME = "New Tool Project";
private IConfigurationElement _configurationElement;
/**
* Constructor for NewPlanWizard.
*/
public NewProjectWizard() {
super();
setWindowTitle(WIZARD_NAME);
}
/**
* Adding the page to the wizard.
*/
@Override
public void addPages() {
super.addPages();
_pageOne = new WizardNewProjectCreationPage("New Tool Project");
addPage(_pageOne);
_projectPropertyPage = new ProjectPropertyPage();
_projectPropertyPage.setTitle("Tool Properties");
_projectPropertyPage.setDescription("Forte root and Tool Repository.");
_projectPropertyPage.performValueDefaults();
addPage(_projectPropertyPage);
}
/**
* This method is called when 'Finish' button is pressed in
* the wizard. We will create an operation and run it
* using wizard as execution context.
*/
public boolean performFinish() {
String name = _pageOne.getProjectName();
URI location = null;
if (!_pageOne.useDefaults()) {
location = _pageOne.getLocationURI();
}
String forteLogger = _projectPropertyPage.getForteLogger();
String repository = _projectPropertyPage.getForteRepository();
String workspace = _projectPropertyPage.getForteWorkspace();
String workspacePassword = _projectPropertyPage.getForteWorkspacePassword();
IWizardContainer container = getContainer();
try {
ProjectCreationJob creator = new ProjectCreationJob(_configurationElement,
name,
location,
forteLogger,
repository,
workspace,
workspacePassword);
container.run(true, true, creator);
return true;
} catch (InvocationTargetException e) {
ToolPlugin.showError("Error Creating Tool project",e);
} catch (InterruptedException e) {
ToolPlugin.showError("Error Creating Tool project",e);
}
// try {
// container.run(false, true, new IRunnableWithProgress() {
//
// @Override
// public void run(IProgressMonitor monitor) throws InvocationTargetException,
// InterruptedException {
// String name = _pageOne.getProjectName();
// URI location = null;
// if (!_pageOne.useDefaults()) {
// location = _pageOne.getLocationURI();
// } // else location == null
//
// try {
// IProject project = ToolProjectSupport.createProject(name, location, forteRoot, forteLogger, repository, workspace, workspacePassword, false);
// ToolBuilder.getRepository(project);
// BasicNewProjectResourceWizard.updatePerspective(_configurationElement);
// } catch (CoreException e) {
// } catch (ToolRepositoryException e) {
// ToolPlugin.showError("Error creating project",e);
// } catch (IOException e) {
// ToolPlugin.showError("Error creating project",e);
// }
// }
// });
// } catch (InvocationTargetException e) {
// ToolPlugin.showError("Error creating project",e);
// } catch (InterruptedException e) {
// ToolPlugin.showError("Error creating project",e);
// }
return true;
}
/**
* We will accept the selection in the workbench to see if
* we can initialize from it.
* @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
*/
public void init(IWorkbench workbench, IStructuredSelection selection) {
this.selection = selection;
}
private void addBuilder(IProject project, String id) {
try {
IProjectDescription desc = project.getDescription();
ICommand[] commands = desc.getBuildSpec();
for (int i = 0; i < commands.length; ++i)
if (commands[i].getBuilderName().equals(id))
return;
//add builder to project
ICommand command = desc.newCommand();
command.setBuilderName(id);
ICommand[] nc = new ICommand[commands.length + 1];
// Add it before other builders.
System.arraycopy(commands, 0, nc, 1, commands.length);
nc[0] = command;
desc.setBuildSpec(nc);
project.setDescription(desc, null);
} catch (CoreException e) {
ToolPlugin.log(IStatus.ERROR,"Error creating a new project.", e);
}
}
public void setInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException {
_configurationElement = config;
}
}