Java:显示进度监视器和子进度监视器的进度

时间:2012-08-28 09:57:43

标签: java eclipse monitor progress

您好我实现了IProgressMonitor和“org.eclipse.core.runtime”的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();
    }
}

1 个答案:

答案 0 :(得分:0)

working()方法采用整数作为工作单位数。看起来你传递的浮点值被舍入为0,因此不会对进度条进行任何更改。将您的working()调用更改为传入1,您应该看到正确的进度。