我有一个有两页的jface 向导。
- 第一页:创建一个项目文件夹。
- 第二页:在第一步创建的文件夹内创建类。
但是当我点击next按钮时,第一页不会创建下一步要选择的文件夹,只有在我点击finish按钮时才会创建。
如何在单击按钮时创建文件夹next?
我的代码:
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;
}
}
第二页:
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();
}
}