java为什么我试图在eclipse中打开编辑器时IWorkbenchPage页面为空?
我发现了以下示例代码:
File fileToOpen = new File("externalfile.xml");
if (fileToOpen.exists() && fileToOpen.isFile()) {
IFileStore fileStore = EFS.getLocalFileSystem().getStore(fileToOpen.toURI());
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
try {
IDE.openEditorOnFileStore( page, fileStore );
} catch ( PartInitException e ) {
//Put your exception handler here if you wish to
}
} else {
//Do something if the file does not exist
}
当我运行插件时,eclipse报告IWorkbenchPage页面为空
我以这种方式实现我的代码:
public void openFile(Path filepath) throws Exception
{
File fileToOpen = new File(filepath.toString());
if (fileToOpen.exists() && fileToOpen.isFile()) {
IFileStore fileStore = EFS.getLocalFileSystem().getStore(fileToOpen.toURI());
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
try {
IDE.openEditorOnFileStore( page, fileStore );
} catch ( PartInitException e ) {
//Put your exception handler here if you wish to
}
} else {
//Do something if the file does not exist
}
}
方法openFile是从同一类中的另一个方法调用的
这是我的样本处理程序(不是我自己写的):
public class SampleHandler<IWorkspace> extends AbstractHandler {
public SampleHandler() {
}
public Object execute(ExecutionEvent event) throws ExecutionException {
String pagetitle = "";
String worspacelocation = "";
try{
//salvo in workspacelocation il path del workspace
worspacelocation =ResourcesPlugin.getWorkspace().getRoot().getLocation().toString();
}
catch (Exception e)
{
JFrame frame = new JFrame("Workspace Location Error");
JOptionPane.showMessageDialog(frame,"Impossibile determinare il workspace");
e.printStackTrace();
}
try{
IEditorPart editor = HandlerUtil.getActiveEditor(event);
if (editor.getTitle() != null)
{
pagetitle = editor.getTitleToolTip();
}
else
{
pagetitle = null;
}}
catch (Exception e){
System.out.print("errore:" + e.getMessage());
e.printStackTrace();}
NewFrame.main(null, pagetitle,worspacelocation);
return null;
}
共 (0) 个答案