use of ru.fix.completable.reactor.graph.viewer.GraphViewer in project completable-reactor by ru-fix.
the class EditorPanelFactory method create.
public static JFXPanel create(Project project, String graphModel) {
JFXPanel swingFxPanel = new JFXPanel();
GraphViewer graphViewer = new GraphViewer();
graphViewer.setShortcut(ShortcutType.GOTO_SERIALIZATION_POINT, new Shortcut(true, KeyCode.B));
graphViewer.registerListener(new GraphViewer.ActionListener() {
@Override
public void goToSource(@NotNull ReactorGraphModel.Source source) {
ApplicationManager.getApplication().invokeLater(() -> ApplicationManager.getApplication().runReadAction(() -> {
PsiFile foundFile = null;
if (source.fileName != null) {
foundFile = Arrays.stream(PsiShortNamesCache.getInstance(project).getFilesByName(source.fileName)).findAny().orElse(null);
} else if (source.className != null) {
ProjectAndLibrariesScope searchScope = new ProjectAndLibrariesScope(project);
PsiClass payloadPsiClass = JavaPsiFacade.getInstance(project).findClass(source.className, searchScope);
if (payloadPsiClass != null) {
foundFile = payloadPsiClass.getContainingFile();
}
}
if (foundFile == null) {
log.warn("Can not find file for source: " + source);
return;
}
OpenFileDescriptor descriptor = new OpenFileDescriptor(project, foundFile.getVirtualFile(), source.fileNameLine != null ? source.fileNameLine - 1 : 0, 0);
descriptor.navigate(true);
}));
}
@Override
public void goToSubgraph(String subgraphPayloadClass) {
ApplicationManager.getApplication().invokeLater(() -> ApplicationManager.getApplication().runReadAction(() -> {
PsiFile foundFile = null;
if (subgraphPayloadClass != null) {
foundFile = Arrays.stream(PsiShortNamesCache.getInstance(project).getFilesByName(subgraphPayloadClass + ".rg")).findAny().orElse(null);
}
if (foundFile == null) {
log.warn("Can not find file for subgraph: " + subgraphPayloadClass);
return;
}
OpenFileDescriptor descriptor = new OpenFileDescriptor(project, foundFile.getVirtualFile());
descriptor.navigate(true);
}));
}
@Override
public void coordinatesChanged(List<GraphViewer.CoordinateItem> coordinateItems) {
ReactorGraphModel graphModel = graphViewer.getGraphModel();
final ReactorGraphModel.Source codeBlockFrom = graphModel.coordinatesSource;
if (codeBlockFrom == null) {
log.warn("Coordinates source start location not specified in model.");
return;
}
ApplicationManager.getApplication().invokeLater(() -> {
ApplicationManager.getApplication().runReadAction(() -> {
PsiFile[] foundFiles = PsiShortNamesCache.getInstance(project).getFilesByName(codeBlockFrom.fileName);
if (foundFiles.length == 0) {
log.warn("No file with name " + codeBlockFrom.fileName + " found");
return;
}
if (foundFiles.length > 1) {
log.warn("Found more than one file with name " + codeBlockFrom.fileName);
}
PsiFile foundFile = foundFiles[0];
Document document = PsiDocumentManager.getInstance(project).getDocument(foundFile);
TextRange textRange = new TextRange(document.getLineStartOffset(codeBlockFrom.fileNameLine - 1), document.getLineEndOffset(document.getLineCount() - 1));
String codeBlock = document.getText(textRange);
String newCodeBlock = codeUpdater.updateCoordinates(codeBlock, coordinateItems);
WriteCommandAction.runWriteCommandAction(project, () -> document.replaceString(textRange.getStartOffset(), textRange.getEndOffset(), newCodeBlock));
});
});
}
});
// Because of https://bugs.openjdk.java.net/browse/JDK-8090517, it is important to disable implicit exit.
Platform.setImplicitExit(false);
Platform.runLater(() -> {
try {
graphViewer.openGraph(graphModel);
swingFxPanel.setScene(graphViewer.getScene());
} catch (Exception exc) {
log.error("Failed to open graph.", exc);
}
});
return swingFxPanel;
}
Aggregations