我正在尝试根据我的需要修改that答案,并使进度监控器正确反映进度。我的方法到现在为止:
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(...)
(在IFile
或IFolder
上),进度条应移动1个刻度。但它根本不动。可能是什么原因以及如何解决问题?
upd :我修改了方法configureProject
,如下所示:
SubProgressMonitor copyFilesMonitor = new SubProgressMonitor(monitor, fileCount);
copyFilesMonitor.beginTask("Copying files...", fileCount);
copyFiles(templatesFolder, project, copyFilesMonitor);
copyFilesMonitor.done();
现在的问题是,在调用create()
(在IFolder
或IFile
上)后,进度条设置为2/3 - 2个刻度。 2/3指定用于整个configureProject()
方法,最后2个刻度由setRawClasspath(...)
指定。
在:
后:
答案 0 :(得分:1)
您需要在为beginTask
方法创建的done
上致电new SubProgressMonitor(monitor, fileCount)
(和copyFiles
)。
如果您不致电beginTask
,则会忽略worked
次来电。