Reputation: 470
I have a jface wizard with two pages.
But when I click on next button, the first page do not create the folder for to be selected on next step, only create when I click on finish button.
How can I create the folder on click on next buttom?
My code:
public class ShapesCreationWizard extends Wizard implements INewWizard,
IExecutableExtension {
private WizardNewProjectCreationPage _mainPage;
private CreationPage page2;
// cache of newly-created project
private IProject _newProject;
// switch to control write of trace data
private boolean _bTraceEnabled = true;
public void addPages() {
_mainPage = new WizardNewProjectCreationPage("Project");
_mainPage.setDescription("Create a new project .");
_mainPage.setTitle("New Project");
addPage(_mainPage);
addPage(page2);
}
public IProject getNewProject() {
return _newProject;
}
public void init(IWorkbench workbench, IStructuredSelection selection) {
page2 = new CreationPage(workbench, selection);
}
public boolean performFinish() {
createNewProject();
if (_config != null) {
BasicNewProjectResourceWizard.updatePerspective(_config);
BasicNewProjectResourceWizard.selectAndReveal(this._newProject,
PlatformUI.getWorkbench().getActiveWorkbenchWindow());
}
page2.finish();
return true;
}
public IProject createNewProject() {
if (_newProject != null) {
return _newProject;
}
// get a project handle
final IProject newProjectHandle = _mainPage.getProjectHandle();
// get a project descriptor
IPath defaultPath = Platform.getLocation();
IPath newPath = _mainPage.getLocationPath();
if (defaultPath.equals(newPath)) {
newPath = null;
}
IWorkspace workspace = ResourcesPlugin.getWorkspace();
final IProjectDescription description = workspace
.newProjectDescription(newProjectHandle.getName());
description.setLocation(newPath);
// create the new project operation
WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
protected void execute(IProgressMonitor monitor)
throws CoreException {
createProject(description, newProjectHandle, monitor);
// addOWLNature(newProjectHandle);
}
};
// run the new project creation operation
try {
getContainer().run(false, true, op);
} catch (InterruptedException e) {
return null;
} catch (InvocationTargetException e) {
return null;
}
_newProject = newProjectHandle;
return _newProject;
}
public void createProject(IProjectDescription description,IProject projectHandle, IProgressMonitor monitor)
throws CoreException {
try {
monitor.beginTask("",2000);
projectHandle.create(description, new SubProgressMonitor(monitor,1000));
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
projectHandle.open(new SubProgressMonitor(monitor,1000));
} finally {
monitor.done();
}
}
protected void resultInformation(String title, String msg) {
// Confirm Result
if (_bTraceEnabled) {
// trace only to console
System.out.println(title + msg);
} else {
// user interaction response
MessageDialog.openInformation(getShell(), title, msg);
}
}
protected void resultError(String title, String msg) {
// Indicate Error
if (_bTraceEnabled) {
// trace only to console
System.out.println(title + msg);
} else {
// user interaction response
MessageDialog.openError(getShell(), title, msg);
}
}
private IConfigurationElement _config;
public void setInitializationData(IConfigurationElement config,String propertyName, Object data) throws CoreException {
_config = config;
}
}
Second page:
public class CreationPage extends WizardNewFileCreationPage {
private static final String DEFAULT_EXTENSION = ".inte";//TODO extenção, alterara tb no shapes XML
private final IWorkbench workbench;
/**
* Create a new wizard page instance.
*
* @param workbench
* the current workbench
* @param selection
* the current object selection
* @see CopyOfShapesCreationWizard#init(IWorkbench, IStructuredSelection)
*/
CreationPage(IWorkbench workbench, IStructuredSelection selection) {
super("shapeCreationPage1", selection);
this.workbench = workbench;
setTitle("Create a new " + DEFAULT_EXTENSION + " file");
setDescription("Create a new " + DEFAULT_EXTENSION + " file");
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.ui.dialogs.WizardNewFileCreationPage#createControl(org
* .eclipse.swt.widgets.Composite)
*/
public void createControl(Composite parent) {
super.createControl(parent);
setFileName("Integrid1" + DEFAULT_EXTENSION);
setPageComplete(validatePage());
}
/** Return a new ShapesDiagram instance. */
private Object createDefaultContent() {
return new ShapesDiagram();
}
/**
* This method will be invoked, when the "Finish" button is pressed.
*
* @see CopyOfShapesCreationWizard#performFinish()
*/
boolean finish() {
// create a new file, result != null if successful
IFile newFile = createNewFile();
// open newly created file in the editor
IWorkbenchPage page = workbench.getActiveWorkbenchWindow().getActivePage();
if (newFile != null && page != null) {
try {
IDE.openEditor(page, newFile, true);
System.err.println();
} catch (PartInitException e) {
e.printStackTrace();
return false;
}
}
return true;
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.ui.dialogs.WizardNewFileCreationPage#getInitialContents()
*/
protected InputStream getInitialContents() {
ByteArrayInputStream bais = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(createDefaultContent()); // argument must be
// Serializable
oos.flush();
oos.close();
bais = new ByteArrayInputStream(baos.toByteArray());
} catch (IOException ioe) {
ioe.printStackTrace();
}
return bais;
}
/**
* Return true, if the file name entered in this page is valid.
*/
private boolean validateFilename() {
if (getFileName() != null
&& getFileName().endsWith(DEFAULT_EXTENSION)) {
return true;
}
setErrorMessage("The 'file' name must end with " + DEFAULT_EXTENSION);
return false;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.dialogs.WizardNewFileCreationPage#validatePage()
*/
protected boolean validatePage() {
return super.validatePage() && validateFilename();
}
}
Upvotes: 0
Views: 3431
Reputation: 4989
Generally I'd say it's best to do all of the work in the performFinish method at the end of the wizard, otherwise you have to deal with the issue of what the "back" button does from the second page, plus the user could cancel out of the wizard leaving the project half created.
Having said that, if you really want the project created on first page the way I would do it is to disable the "next" button:
setPageComplete(false);
then add a "Create Project" button to the first page. When the button is clicked you can create the project and enable the "next" button.
Upvotes: 2