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. This is 5 plus count of the files in the source folder, if I say: 1 tick per file or folder. Now I have a problem with the method copyFiles
. I modified the related code to work with 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. 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:
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. 2/3 is designated for the whole configureProject()
method and the last 2 ticks are to made by setRawClasspath(...)
yet.
Before:
After: