gpt4 book ai didi

java - jface 向导中的 onclick 下一个按钮事件

转载 作者:行者123 更新时间:2023-12-02 07:35:43 24 4
gpt4 key购买 nike

我有一个包含两个页面的jface 向导

  • 第一页:创建一个项目文件夹。
  • 第二页:在第一步创建的文件夹内创建

但是当我单击下一步按钮时,第一页不会创建下一步要选择的文件夹,只有当我单击完成按钮时才会创建。

如何在单击下一步按钮时创建文件夹?

我的代码:

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();
}
}

最佳答案

一般来说,我会说最好在向导结束时完成 PerformFinish 方法中的所有工作,否则您必须处理第二页中的“后退”按钮的作用问题,以及用户可以取消向导,使项目只创建一半。

话虽如此,如果您确实希望在首页上创建项目,我会禁用“下一步”按钮:

setPageComplete(false);

然后在第一页添加一个“创建项目”按钮。单击该按钮后,您可以创建项目并启用“下一步”按钮。

关于java - jface 向导中的 onclick 下一个按钮事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12261104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com