Skip to content

Commit

Permalink
Fixed #128 Add a progress monitor to the workspace synchronize Job.
Browse files Browse the repository at this point in the history
  • Loading branch information
ylussaud committed Dec 21, 2023
1 parent 6c69791 commit f145b74
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ public AcceleoProject getProject(AcceleoWorkspace workspace, URI resource) {
final AcceleoProject res;

synchronized(synchronizer) {

final IFile file = clientWorkspace.getRoot().getFileForLocation(new Path(resource.getPath()
.toString()));
final IProject eclipseProject = file.getProject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,18 +278,18 @@ protected void pushIndentation(Block block, String indentation) {
}

/**
* Peeks the last {@link #pushIndentation(Map) pushed} indentation from the stack.
* Peeks the last {@link #pushIndentation(Block, String) pushed} indentation from the stack.
*
* @return the last {@link #pushIndentation(Map) pushed} indentation from the stack
* @return the last {@link #pushIndentation(Block, String) pushed} indentation from the stack
*/
protected String peekIndentation() {
return indentationStack.peekLast();
}

/**
* Pops the last {@link #pushIndentation(Map) pushed} indentation from the stack.
* Pops the last {@link #pushIndentation(Block, String) pushed} indentation from the stack.
*
* @return the last {@link #pushIndentation(Map) pushed} indentation from the stack
* @return the last {@link #pushIndentation(Block, String) pushed} indentation from the stack
*/
protected String popIndentation() {
inlinedBlock.removeLast();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.jobs.Job;

/**
Expand All @@ -43,6 +44,35 @@
*/
public abstract class Synchronizer<P> implements IResourceVisitor, IResourceChangeListener, IResourceDeltaVisitor, IWorkspaceResolverProvider {

/**
* Counts {@link IResource} i the workspace.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
*/
private final class ResourceCounter implements IResourceVisitor {

private int count;

@Override
public boolean visit(IResource resource) throws CoreException {
final boolean res;

final IProject project = resource.getProject();
if (project == null || project.isOpen()) {
count++;
res = true;
} else {
res = resource instanceof IWorkspaceRoot;
}

return res;
}

public int getCount() {
return count;
}
}

/**
* The Eclipse {@link IWorkspace}.
*/
Expand All @@ -63,6 +93,11 @@ public abstract class Synchronizer<P> implements IResourceVisitor, IResourceChan
*/
private final Map<P, IProject> projectToEclipse = new HashMap<>();

/**
* The {@link IProgressMonitor}.
*/
private IProgressMonitor monitor;

/**
* @param queryWorkspace
*/
Expand All @@ -85,7 +120,17 @@ public void run(IProgressMonitor monitor) throws CoreException {
eclipseWorkspace.addResourceChangeListener(Synchronizer.this);

// walk the workspace for current state
eclipseWorkspace.getRoot().accept(Synchronizer.this);

final ResourceCounter resourceCounter = new ResourceCounter();
eclipseWorkspace.getRoot().accept(resourceCounter);
final SubMonitor subMonitor = SubMonitor.convert(monitor, "Validating",
resourceCounter.getCount());
Synchronizer.this.monitor = subMonitor;
try {
eclipseWorkspace.getRoot().accept(Synchronizer.this);
} finally {
Synchronizer.this.monitor = null;
}
} catch (Exception e) {
dispose();
QueryPlugin.INSTANCE.log(new Status(IStatus.ERROR, getClass(),
Expand Down Expand Up @@ -229,7 +274,17 @@ public boolean visit(IResource resource) throws CoreException {

final IProject project = resource.getProject();
if (project == null || project.isOpen()) {
add(resource);
final boolean hasMonitor = monitor != null;
if (hasMonitor) {
monitor.subTask(resource.getFullPath().toString());
}
try {
add(resource);
} finally {
if (hasMonitor) {
monitor.worked(1);
}
}
res = true;
} else {
res = resource instanceof IWorkspaceRoot;
Expand Down

0 comments on commit f145b74

Please sign in to comment.