Skip to content

Commit

Permalink
Project problems are resolved in headless environment.
Browse files Browse the repository at this point in the history
  • Loading branch information
sdedic committed Jan 22, 2021
1 parent 81bfc6d commit 8acc860
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.SwingUtilities;
Expand Down Expand Up @@ -179,13 +180,24 @@ private void fixActionImpl(@NonNull final Object value) {
final BrokenReferencesModel.ProblemReference or = (BrokenReferencesModel.ProblemReference) value;
errorList.setEnabled(false);
fix.setEnabled(false);
performProblemFix(or, (r) -> {
if (SwingUtilities.isEventDispatchThread()) {
updateAfterResolve(r);
} else {
SwingUtilities.invokeLater(() -> updateAfterResolve(r));
}
});
}

static void performProblemFix(final BrokenReferencesModel.ProblemReference or,
Consumer<ProjectProblemsProvider.Result> callbackWhenDone) {
Future<ProjectProblemsProvider.Result> becomesResult = null;
try {
becomesResult = or.problem.resolve();
assert becomesResult != null;
} finally {
if (becomesResult == null) {
updateAfterResolve(null);
callbackWhenDone.accept(null);
} else if (becomesResult.isDone()) {
ProjectProblemsProvider.Result result = null;
try {
Expand All @@ -195,7 +207,7 @@ private void fixActionImpl(@NonNull final Object value) {
} catch (ExecutionException ex) {
Exceptions.printStackTrace(ex);
} finally {
updateAfterResolve(result);
callbackWhenDone.accept(result);
}
} else {
final Future<ProjectProblemsProvider.Result> becomesResultFin = becomesResult;
Expand All @@ -210,12 +222,7 @@ public void run() {
} catch (ExecutionException ee) {
Exceptions.printStackTrace(ee);
} finally {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
updateAfterResolve(result.get());
}
});
callbackWhenDone.accept(result.get());
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
package org.netbeans.modules.project.ui.problems;

import java.awt.Dialog;
import java.awt.GraphicsEnvironment;
import java.util.Collection;
import java.util.HashSet;
import java.util.concurrent.Future;
import javax.swing.JButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
Expand All @@ -33,6 +37,8 @@
import org.openide.util.lookup.ServiceProvider;
import org.openide.windows.WindowManager;
import static org.netbeans.modules.project.ui.problems.Bundle.*;
import org.netbeans.spi.project.ui.ProjectProblemsProvider;
import org.openide.awt.StatusDisplayer;
import org.openide.util.Parameters;

/**
Expand Down Expand Up @@ -102,6 +108,10 @@ public void stateChanged(ChangeEvent e) {
resolveOption.setVisible(!ctx.isEmpty());
if (DialogDisplayer.getDefault().notify(dd) == resolveOption) {
final BrokenReferencesModel model = new BrokenReferencesModel(ctx, true);
if (GraphicsEnvironment.isHeadless()) {
fixAllProblems(model, new HashSet<>());
return;
}
final BrokenReferencesCustomizer customizer = new BrokenReferencesCustomizer(model);
JButton close = new JButton (Bundle.LBL_Broken_References_Resolve_Panel_Close());
close.getAccessibleContext ().setAccessibleDescription (Bundle.AD_Broken_References_Resolve_Panel_Close());
Expand Down Expand Up @@ -129,7 +139,11 @@ public void stateChanged(ChangeEvent e) {
rpTask = RP.create(new Runnable() {
@Override
public void run() {
WindowManager.getDefault().invokeWhenUIReady(task);
if (GraphicsEnvironment.isHeadless()) {
task.run();
} else {
WindowManager.getDefault().invokeWhenUIReady(task);
}
}
});
}
Expand All @@ -143,6 +157,29 @@ public void run() {
rpTask.schedule(BROKEN_ALERT_TIMEOUT);
}
}

private void fixAllProblems(BrokenReferencesModel model, Collection<BrokenReferencesModel.ProblemReference> seen) {
model.refresh();
for (int i = 0; i < model.getSize(); i++) {
Object value = model.getElementAt(i);
if (!(value instanceof BrokenReferencesModel.ProblemReference)) {
return;
}
final BrokenReferencesModel.ProblemReference or = (BrokenReferencesModel.ProblemReference) value;
if (or.resolved) {
continue;
}
BrokenReferencesCustomizer.performProblemFix(or, (result) -> {
seen.add(or);
int importance = result.isResolved() ? 0 : StatusDisplayer.IMPORTANCE_ERROR_HIGHLIGHT;
StatusDisplayer.getDefault().setStatusText(result.getMessage(), importance);

// next round:
fixAllProblems(model, seen);
});
break;
}
}


@NbBundle.Messages({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public final Message setStatusText(String text, int importance) {
final MessageType type;
if (importance >= IMPORTANCE_ANNOTATION) {
type = MessageType.Info;
} else if (importance >= IMPORTANCE_ERROR_HIGHLIGHT) {
type = MessageType.Error;
} else if (importance >= IMPORTANCE_FIND_OR_REPLACE) {
type = MessageType.Info;
} else if (importance >= IMPORTANCE_ERROR_HIGHLIGHT) {
type = MessageType.Error;
} else {
type = MessageType.Info;
}
Expand Down

0 comments on commit 8acc860

Please sign in to comment.