2

嗨,我实现了“org.eclipse.core.runtime”的 IProgressMonitor 和 SubProgressMonitor。进度监视器正在工作,但我的问题是它没有像我想要的那样显示进度。在完成之前它没有显示任何进展。我会给你代码示例。

    try {
            PlatformUI.getWorkbench().getProgressService().
            busyCursorWhile(new IRunnableWithProgress() {

                @Override
                public void run(IProgressMonitor monitor) throws InvocationTargetException,
                        InterruptedException {
                    // TODO Auto-generated method stub
                    try{

                        monitor.beginTask("Operation in Progress...", ticks);

                        for(int i = 0;  i < getCompilationUnit().size(); i++){

                            try {
                                IType type = getCompilationUnit().get(i).findPrimaryType();

                                IType[] types = getCompilationUnit().get(i).getTypes();

                                if(monitor.isCanceled())
                                    throw new OperationCanceledException();

                                updateListPrimaryType(type, totalNumberOfCode, types);
                                monitor.worked(1/ticks);

                                IMethod[] method = type.getMethods();
                                int work = method.length;
                                updateListIMethod(method);
                                monitor.worked(1/ticks);

                                updateListMethodAttributeMember(type,new SubProgressMonitor(monitor, (1/ticks)));           
                                updateListMember(type,types);
                                monitor.worked(1/ticks);

                            } catch (JavaModelException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }

                    }finally{
                        monitor.done();
                    }
                }
            });

这是方法 updateListMethodAttributeMember 的代码:

  private void updateListMethodAttributeMember(IType type,SubProgressMonitor subProgressMonitor) {
    // TODO Auto-generated method stub      
    ClassList tmp;
    try{

        IMethod[] method = type.getMethods();
        IField[] field = type.getFields();

        subProgressMonitor.beginTask("Operation in Progress...", method.length);
        for(int m = 0; m < method.length; m++){
            String methodName = getSubstringName(method[m].toString());

            IMethod met = method[m];
            tmp = new ClassList(className, methodName, path);

            if(!met.isConstructor() || !met.isMainMethod()){
                try {
                    int number = performIMethodSearch(met);
                    tmp.setNumberOfCalls(number);
                } catch (CoreException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            subProgressMonitor.worked(1/method.length);
            getMethodClassList().add(tmp);
        }

        for(int f = 0; f < field.length; f++){
            String attributeName = getSubstringName(field[f].toString());
            tmp = new ClassList(className, attributeName, path);
            String parent = field[f].getParent().getElementName().toString();
            String description = "Parent Class: " + parent;
            tmp.setDataDescription(description);
            getAttributeClassList().add(tmp);
        }

    } catch (JavaModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
        subProgressMonitor.done();
    }
}
4

1 回答 1

0

working() 方法采用一个整数作为工作单元的数量。看起来您正在传递一个四舍五入为 0 的浮点值,因此对进度条没有任何更改。更改您的 working() 调用以传入 1,您应该会看到正确的进度。

于 2012-08-28T11:38:17.110 回答