簡體   English   中英

Java:顯示進度監視器和子進度監視器的進度

[英]Java: Show the progress of a Progress Monitor and a Sub Progress Monitor

嗨,我實現了“ 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();
    }
}

work()方法采用整數作為工作單元數。 看起來您正在傳遞的浮點值將四舍五入為0,因此進度欄沒有任何變化。 將您的work()調用更改為傳入1,您應該看到正確的進度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM