Skip to content

Commit

Permalink
Reload Project action reimplemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
sdedic committed Aug 26, 2024
1 parent fd5ec2e commit 9bda082
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@

import java.awt.event.ActionEvent;
import java.util.Collection;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import javax.swing.AbstractAction;
import javax.swing.Action;
import org.netbeans.modules.maven.NbMavenProjectImpl;
import static org.netbeans.modules.maven.actions.Bundle.*;
import org.netbeans.modules.maven.api.NbMavenProject;
import org.netbeans.modules.project.dependency.ProjectReload;
import org.netbeans.modules.project.dependency.ProjectReload.ProjectState;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionRegistration;
import org.openide.awt.StatusDisplayer;
import org.openide.util.ContextAwareAction;
import org.openide.util.Lookup;
import org.openide.util.NbBundle.Messages;
Expand All @@ -37,7 +41,7 @@
@ActionID(id = "org.netbeans.modules.maven.refresh", category = "Project")
@ActionRegistration(displayName = "#ACT_Reload_Project", lazy=false)
@ActionReference(position = 1700, path = "Projects/org-netbeans-modules-maven/Actions")
@Messages("ACT_Reload_Project=Reload POM")
@Messages("ACT_Reload_Project=Reload Project")
public class RefreshAction extends AbstractAction implements ContextAwareAction {

private final Lookup context;
Expand All @@ -59,11 +63,18 @@ private RefreshAction(Lookup lkp) {
@Override public void actionPerformed(ActionEvent event) {
// #166919 - need to run in RP to prevent RPing later in fireProjectReload()
//since #227101 fireMavenProjectReload() always posts to the RP...
//#211217 in 3.x should not be necessary..
//EmbedderFactory.resetCachedEmbedders();
for (NbMavenProjectImpl prj : context.lookupAll(NbMavenProjectImpl.class)) {
NbMavenProject.fireMavenProjectReload(prj);
for (NbMavenProjectImpl prj : context.lookupAll(NbMavenProjectImpl.class)) {
CompletableFuture<ProjectState> f = ProjectReload.withProjectState(prj,
ProjectReload.StateRequest.reload().saveModifications().toQuality(ProjectReload.Quality.SIMPLE));
f.whenComplete((s, err) -> {
if (err instanceof CompletionException) {
err = err.getCause();
}
if (err != null) {
StatusDisplayer.getDefault().setStatusText(err.getMessage());
}
});
}
}

@Override public Action createContextAwareInstance(Lookup actionContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.netbeans.modules.maven.embedder.EmbedderFactory;
import org.netbeans.modules.maven.modelcache.MavenProjectCache;
import org.netbeans.modules.project.dependency.ProjectReload;
import org.netbeans.modules.project.dependency.ProjectReload.Quality;
import org.netbeans.modules.project.dependency.ProjectReload.StateRequest;
import org.netbeans.modules.project.dependency.spi.ProjectReloadImplementation;
import org.netbeans.spi.project.LookupProvider;
Expand Down Expand Up @@ -152,23 +153,30 @@ Set<FileObject> findProjectFiles0(MavenProject mp) {
private static class CF extends CompletableFuture<ProjectStateData<MavenProject>> {}

static ProjectReload.Quality getProjectQuality(MavenProject mp) {
if (mp == null) {
return ProjectReload.Quality.NONE;
}
if (MavenProjectCache.isFallbackproject(mp)) {
// could not load at all.
MavenProject fallback = MavenProjectCache.getPartialProject(mp);
return fallback != null ?
ProjectReload.Quality.BROKEN:
ProjectReload.Quality.NONE;
ProjectReload.Quality.FALLBACK;
}
if (!MavenProjectCache.getPlaceholderArtifacts(mp).isEmpty()) {
return ProjectReload.Quality.LOADED;
}
return ProjectReload.Quality.RESOLVED;
}

private ProjectStateData createStateData(MavenProject mp) {
private ProjectStateData createStateData(MavenProject mp, Quality q) {
ProjectStateData d;

ProjectStateBuilder builder = ProjectStateData.builder(getProjectQuality(mp)).
if (q == null) {
q = getProjectQuality(mp);
}

ProjectStateBuilder builder = ProjectStateData.builder(q).
files(findProjectFiles()).
timestamp(MavenProjectCache.getLoadTimestamp(mp));
builder.data(mp);
Expand Down Expand Up @@ -222,7 +230,7 @@ private void loadMavenProject2(MavenProject p, StateRequest request, CF future)
ProjectReload.Quality q = getProjectQuality(p);
if (q.isAtLeast(request.getMinQuality())) {
if (!request.isConsistent()) {
future.complete(createStateData(p));
future.complete(createStateData(p, null));
return;
}

Expand All @@ -236,7 +244,7 @@ private void loadMavenProject2(MavenProject p, StateRequest request, CF future)
}
}
if (!obsolete) {
future.complete(createStateData(p));
future.complete(createStateData(p, null));
return;
}
}
Expand All @@ -246,14 +254,25 @@ private void loadMavenProject2(MavenProject p, StateRequest request, CF future)

private void loadMavenProject3(CF future) {
NbMavenProjectImpl nbImpl = project.getLookup().lookup(NbMavenProjectImpl.class);
MavenProject current = nbImpl.getOriginalMavenProjectOrNull();
RequestProcessor.Task t = nbImpl.fireProjectReload(true);
t.addTaskListener(new TaskListener() {
@Override
public void taskFinished(Task task) {
task.removeTaskListener(this);
// retain initial = true for the first project load.
nbImpl.getFreshOriginalMavenProject().thenAccept((mp) -> {
future.complete(createStateData(mp));
future.complete(createStateData(mp, null));
}).exceptionally(ex -> {
MavenProject renewed = nbImpl.getOriginalMavenProjectOrNull();
if (renewed == current) {
ProjectStateData sd = createStateData(current, Quality.BROKEN);
sd.fireChanged(false, true);
future.complete(sd);
} else {
future.complete(createStateData(renewed, Quality.BROKEN));
}
return null;
});
}
});
Expand Down

0 comments on commit 9bda082

Please sign in to comment.