我爱oo,我爱java

交流blog QQ:421057986 oofrank@donews

在eclipse-plugin开发中碰到的怪问题:(eclipse 3.1.1 + wtp1.0)

一个popupMenus Extensions,
objectContribution:objectClass*: org.eclipse.core.resources.IFile

在action的Class代码中:
public void selectionChanged(IAction action, ISelection selection) {
    StructuredSelection ss = (StructuredSelection) selection;
        this.selectedFile==(IFile)ss.getFirstElement(); //此处抛出异常
}

上述代码的异常非常奇怪:
根据的的跟踪,ss.getFirstElement()返回值是File,该类实现了IFile接口,
而且我用 ss.getFirstElement().getClass().isAssignableFrom(IFile.class)返回是false;
真是奇怪!---有人知道为什么吗?

另外在实践eclipse plugin开发过程中也有几个心得:(肯定能用,但未必最佳)

1、如果开发plugin,所有的依赖库都要包含到 Plug-in Dependencies 中;而不能只是引入到工程中。
2、如何输出到console:
MessageConsole mc=new MessageConsole("****",null);
IConsole[] cs=new IConsole[1];
cs[0]=mc;
ConsolePlugin.getDefault().getConsoleManager().addConsoles(cs);
mc.activate();
PrintStream out=new PrintStream( mc.newOutputStream());
out.println("*******.");
3、如何获取依赖工程的输出路径:
selectedProject:当前工程---由用户选择
String[] ps= selectedProject.getRequiredProjectNames();                
IWorkspace w= selectedProject.getProject().getWorkspace();
for(int i=0;i<ps.length;i++){
IResource r=w.getRoot().findMember(ps[i]);
try{
    IJavaProject jp=new JavaProject((IProject)r,null);                
    File source=new File(jp.getProject().getLocation().append(jp.getOutputLocation().removeFirstSegments(1)).toOSString());
        //作你的事情.....
}catch(Exception e){
      //不是javaProject                                
    e.printStackTrace();                            
}                
4、如何使用进度Dialog:
Shell shell = new Shell();
ProgressMonitorDialog dialog = new ProgressMonitorDialog(shell);        
IRunnableWithProgress thread = new SomeRunner(shell);            
dialog.run(true, false, thread);
//=============================
private class SomeRunner implements IRunnableWithProgress {
    public void run(IProgressMonitor monitor)throws InvocationTargetException, InterruptedException {
    monitor.beginTask("一些信息", 数值-总工作量);
        for(;;){
        // 一些工作
        monitor.worked(数值-已完成工作量); //实际中,我得情况不太相符,不明白,但差不多 :(
        monitor.setTaskName("一些信息");
     // 一些工作    
       }
        monitor.done();
    }
}


posted on 2006-01-20 18:45 兼听则明 阅读(577) 评论(0)  编辑  收藏 所属分类: java


只有注册用户登录后才能发表评论。


网站导航: