1

我正在尝试根据我的需要修改答案并使进度监视器正确反映进度。到目前为止我的方法:

import org.eclipse.core.runtime.SubProgressMonitor;

private void configureProject(IProgressMonitor monitor)
    throws CoreException, IOException
{
    try
    {
        URL templatesURL = Activator.getDefault().getBundle().getEntry(TEMPLATES);
        File templatesFolder = new File(FileLocator.toFileURL(templatesURL).getPath());
        int fileCount = getElementsCount(templatesFolder);

        monitor.beginTask("Creating file structure for new project...", fileCount + 5);

        project.getFolder(P_SRC).delete(true, new SubProgressMonitor(monitor, 1));
        project.getFolder(P_BIN).delete(true, new SubProgressMonitor(monitor, 1));

        copyFiles(templatesFolder, project, new SubProgressMonitor(monitor, fileCount));
        project.getFile(P_TOUCH).delete(true, new SubProgressMonitor(monitor, 1));

        IClasspathEntry[] newEntries = new IClasspathEntry[3];
        newEntries[0] = JavaCore.newSourceEntry(getCreatedElement().getPath().append(SRC_MAIN));
        newEntries[1] = JavaCore.newSourceEntry(getCreatedElement().getPath().append(SRC_RES),
                                                EXCLUDE_ALL);
        newEntries[2] = JavaCore.newSourceEntry(getCreatedElement().getPath().append(SRC_TEST));
        javaProject.setRawClasspath(newEntries, new SubProgressMonitor(monitor, 2));
    }
    finally
    {
        if (!monitor.isCanceled())
            monitor.done();
    }
}


private int getElementsCount(File file)
{
    // return number of files and folders in the file
}

如您所见,我有 3 个用于删除操作的滴答声和 2 个用于设置类路径的滴答声。这是源文件夹中文件的 5 加计数,如果我说:每个文件或文件夹 1 记号。现在我的方法有问题copyFiles。我修改了相关代码以使用IProgressMonitor

private void copyFiles(File srcFolder, IContainer destFolder, IProgressMonitor monitor)
    throws CoreException, IOException
{
    for (File f : srcFolder.listFiles())
    {
        if (f.isDirectory())
        {
            IFolder newFolder = destFolder.getFolder(new Path(f.getName()));
            newFolder.create(true, true, new SubProgressMonitor(monitor, 1));
            copyFiles(f, newFolder, monitor);
        }
        else
        {
            newFile.create(new FileInputStream(f), true, new SubProgressMonitor(monitor, 1));
        }
    }
}

一旦create(...)调用其中一种方法( onIFile或 on IFolder),进度条应移动 1 个刻度。但它根本不动。可能是什么原因以及如何解决问题?

upd:我将方法修改configureProject如下:

SubProgressMonitor copyFilesMonitor = new SubProgressMonitor(monitor, fileCount);
copyFilesMonitor.beginTask("Copying files...", fileCount);
copyFiles(templatesFolder, project, copyFilesMonitor);
copyFilesMonitor.done();

现在的问题是,在调用create()( onIFolder或 on IFile)之后,进度条设置为 2/3 - 2 个刻度。2/3 是为整个configureProject()方法指定的,最后 2 个刻度将由setRawClasspath(...)尚未完成。

前:

在此处输入图像描述

后:

在此处输入图像描述

4

1 回答 1

1

您需要在为该方法创建的上调用beginTask(and done) 。new SubProgressMonitor(monitor, fileCount)copyFiles

如果你不打电话beginTaskworked电话将被忽略。

于 2014-10-13T08:57:18.617 回答