package tool.wizards;
import java.lang.reflect.InvocationTargetException;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import tool.ToolPlugin;
import tool.ToolProjectSupport;
public class NewSourceFolderWizard extends Wizard implements INewWizard{
protected IProject project;
private IWorkbench workbench;
private NewSourceFolderWizardPage page;
protected String newFolderName;
public NewSourceFolderWizard(){
super();
setNeedsProgressMonitor(true);
}
@Override
public void addPages() {
page = new NewSourceFolderWizardPage(project);
addPage(page);
}
@Override
public void init(IWorkbench workbench, IStructuredSelection selection) {
this.workbench = workbench;
if (selection != null && selection instanceof TreeSelection){
TreeSelection tselection = (TreeSelection)selection;
if (tselection.getFirstElement() instanceof IResource){
IResource resource = (IResource)tselection.getFirstElement();
project = resource.getProject();
}
} else {
MessageDialog.openError(Display.getDefault().getActiveShell(), "Project Error", "No project resource selected");
}
}
@Override
public boolean performFinish() {
IRunnableWithProgress op = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException {
newFolderName = page.getNewFolderName();
if (newFolderName != null){
monitor.beginTask("Creating: " + newFolderName, 1);
try {
IFolder folder = project.getFolder(newFolderName);
ToolProjectSupport.createSourceFolder(folder);
monitor.worked(1);
} catch (CoreException e) {
throw new InvocationTargetException(e);
} finally {
monitor.done();
}
}
}
};
try {
getContainer().run(true, false, op);
} catch (InterruptedException e) {
return false;
} catch (InvocationTargetException e) {
Throwable realException = e.getTargetException();
ToolPlugin.showError("Error creating Tool source folder", realException);
return false;
}
return true;
}
}