简体   繁体   English

将文件从文件系统复制到项目并正确管理进度监视器

[英]Copy files from filesystem to a project and correctly manage the progress monitor

I am trying to modify that answer for my needs and to make the progress monitor reflect the progress correctly. 我正在尝试根据我的需要修改答案,并使进度监视器正确反映进度。 My approach till now: 到目前为止,我的方法是:

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
}

As you see I have 3 ticks for delete operations and 2 for setting the classpath. 如您所见,我有3个用于删除操作的刻度线和2个用于设置类路径的刻度线。 This is 5 plus count of the files in the source folder, if I say: 1 tick per file or folder. 如果我说:这是源文件夹中文件的5个计数:每个文件或文件夹1个刻度。 Now I have a problem with the method copyFiles . 现在我对copyFiles方法有问题。 I modified the related code to work with IProgressMonitor : 我修改了相关代码以与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));
        }
    }
}

As soon one of the methods create(...) is called (either on IFile or on IFolder ) the progress bar should be moved by 1 tick. 一旦方法之一create(...)被调用(无论是上IFile或上IFolder )进度条应该由1点移动。 But it doesn't move at all. 但是它根本没有动。 What could be the reason and how to solve the problem? 可能是什么原因以及如何解决该问题?

upd : I modified the method configureProject as follows: upd :我修改了方法configureProject ,如下所示:

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

Now the problem is that after an invocation of create() (either on IFolder or on IFile ) the progressbar is set to 2/3 - 2 ticks. 现在的问题是,调用后create()无论是上IFolder或上IFile )的进度设置为2/3 - 2蜱。 2/3 is designated for the whole configureProject() method and the last 2 ticks are to made by setRawClasspath(...) yet. 为整个configureProject()方法指定了2/3,最后2个滴答声由setRawClasspath(...)

Before: 之前:

在此处输入图片说明

After: 后:

在此处输入图片说明

You need to call beginTask (and done ) on the new SubProgressMonitor(monitor, fileCount) that you are creating for the copyFiles method. 您需要在为copyFiles方法创建的new SubProgressMonitor(monitor, fileCount)上调用beginTask (并done )。

If you don't call beginTask the worked calls are ignored. 如果不调用beginTask则将忽略已worked调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM