From 07b389fd38b7b1c36b0dca0ada1284fc4764b499 Mon Sep 17 00:00:00 2001 From: Tal Arian Date: Tue, 15 Aug 2023 11:35:26 +0300 Subject: [PATCH] Show floating toolbar when results doesn't match source code --- .../idea/actions/StartLocalScanAction.java | 10 ++++ .../ide/idea/events/AnnotationEvents.java | 13 ++++++ .../inspections/JFrogSecurityAnnotator.java | 8 ++++ .../ide/idea/ui/JFrogFloatingToolbar.java | 46 ++++++++++++++++++- 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/jfrog/ide/idea/events/AnnotationEvents.java diff --git a/src/main/java/com/jfrog/ide/idea/actions/StartLocalScanAction.java b/src/main/java/com/jfrog/ide/idea/actions/StartLocalScanAction.java index 0d486813..9d6794f8 100644 --- a/src/main/java/com/jfrog/ide/idea/actions/StartLocalScanAction.java +++ b/src/main/java/com/jfrog/ide/idea/actions/StartLocalScanAction.java @@ -1,8 +1,12 @@ package com.jfrog.ide.idea.actions; +import com.intellij.openapi.actionSystem.ActionUpdateThread; import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.project.Project; +import com.intellij.util.messages.MessageBus; +import com.jfrog.ide.idea.events.ApplicationEvents; import com.jfrog.ide.idea.scan.ScanManager; import org.jetbrains.annotations.NotNull; @@ -10,6 +14,10 @@ * Created by romang on 3/6/17. */ public class StartLocalScanAction extends AnAction { + @Override + public @NotNull ActionUpdateThread getActionUpdateThread() { + return ActionUpdateThread.BGT; + } @Override public void actionPerformed(AnActionEvent e) { @@ -17,6 +25,8 @@ public void actionPerformed(AnActionEvent e) { if (project == null) { return; } + MessageBus messageBus = ApplicationManager.getApplication().getMessageBus(); + messageBus.syncPublisher(ApplicationEvents.ON_SCAN_LOCAL_STARTED).update(); ScanManager.getInstance(project).startScan(); } diff --git a/src/main/java/com/jfrog/ide/idea/events/AnnotationEvents.java b/src/main/java/com/jfrog/ide/idea/events/AnnotationEvents.java new file mode 100644 index 00000000..dacc4c8f --- /dev/null +++ b/src/main/java/com/jfrog/ide/idea/events/AnnotationEvents.java @@ -0,0 +1,13 @@ +package com.jfrog.ide.idea.events; + +import com.intellij.util.messages.Topic; + +public interface AnnotationEvents { + // Results expiry + Topic ON_IRRELEVANT_RESULT = Topic.create("Source code changed", AnnotationEvents.class); + + /** + * Called when the selected file is modified. + */ + void update(String filePath); +} diff --git a/src/main/java/com/jfrog/ide/idea/inspections/JFrogSecurityAnnotator.java b/src/main/java/com/jfrog/ide/idea/inspections/JFrogSecurityAnnotator.java index 82775004..ff272cbd 100644 --- a/src/main/java/com/jfrog/ide/idea/inspections/JFrogSecurityAnnotator.java +++ b/src/main/java/com/jfrog/ide/idea/inspections/JFrogSecurityAnnotator.java @@ -3,15 +3,18 @@ import com.intellij.lang.annotation.AnnotationHolder; import com.intellij.lang.annotation.ExternalAnnotator; import com.intellij.lang.annotation.HighlightSeverity; +import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.util.TextRange; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiFile; +import com.intellij.util.messages.MessageBus; import com.jfrog.ide.common.nodes.FileIssueNode; import com.jfrog.ide.common.nodes.FileTreeNode; import com.jfrog.ide.common.nodes.SortableChildrenTreeNode; +import com.jfrog.ide.idea.events.AnnotationEvents; import com.jfrog.ide.idea.ui.ComponentsTree; import com.jfrog.ide.idea.ui.LocalComponentsTree; import org.jetbrains.annotations.NotNull; @@ -85,6 +88,11 @@ public void apply(@NotNull PsiFile file, List warnings, @NotNull .gutterIconRenderer(iconRenderer) .create(); } + // Notify outdated scan result + else { + MessageBus messageBus = ApplicationManager.getApplication().getMessageBus(); + messageBus.syncPublisher(AnnotationEvents.ON_IRRELEVANT_RESULT).update(warning.getFilePath()); + } }); } } diff --git a/src/main/java/com/jfrog/ide/idea/ui/JFrogFloatingToolbar.java b/src/main/java/com/jfrog/ide/idea/ui/JFrogFloatingToolbar.java index cdd31504..8e63794e 100644 --- a/src/main/java/com/jfrog/ide/idea/ui/JFrogFloatingToolbar.java +++ b/src/main/java/com/jfrog/ide/idea/ui/JFrogFloatingToolbar.java @@ -3,20 +3,35 @@ import com.intellij.openapi.Disposable; import com.intellij.openapi.actionSystem.DataContext; import com.intellij.openapi.actionSystem.PlatformDataKeys; +import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.editor.toolbar.floating.AbstractFloatingToolbarProvider; import com.intellij.openapi.editor.toolbar.floating.FloatingToolbarComponent; import com.intellij.openapi.fileEditor.FileEditor; import com.intellij.openapi.project.Project; +import com.intellij.util.messages.MessageBusConnection; +import com.jfrog.ide.idea.events.AnnotationEvents; +import com.jfrog.ide.idea.events.ApplicationEvents; import com.jfrog.ide.idea.utils.Descriptor; import org.jetbrains.annotations.NotNull; +import java.util.HashSet; +import java.util.Set; + /** * @author yahavi **/ -public class JFrogFloatingToolbar extends AbstractFloatingToolbarProvider { +public class JFrogFloatingToolbar extends AbstractFloatingToolbarProvider implements Disposable { + private final MessageBusConnection appBusConnection; + private Set changedFiles; + private Set components; + public JFrogFloatingToolbar() { super("JFrog.Floating"); + changedFiles = new HashSet<>(); + components = new HashSet<>(); + this.appBusConnection = ApplicationManager.getApplication().getMessageBus().connect(this); + registerOnChangeHandlers(); } @Override @@ -31,6 +46,13 @@ public void register(@NotNull DataContext dataContext, @NotNull FloatingToolbarC if (fileEditor == null || fileEditor.getFile() == null) { return; } + if (changedFiles.contains(fileEditor.getFile().getPath())) { + component.scheduleShow(); + components.add(component); + return; + } else { + component.scheduleHide(); + } Descriptor descriptor = Descriptor.fromFileName(fileEditor.getFile().getName()); if (descriptor == null) { return; @@ -43,6 +65,28 @@ public void register(@NotNull DataContext dataContext, @NotNull FloatingToolbarC LocalComponentsTree localComponentsTree = LocalComponentsTree.getInstance(project); if (localComponentsTree.isCacheEmpty() || localComponentsTree.isCacheExpired()) { component.scheduleShow(); + components.add(component); } } + + private void registerOnChangeHandlers() { + appBusConnection.subscribe(AnnotationEvents.ON_IRRELEVANT_RESULT, (AnnotationEvents) this::updateChangedFiles); + appBusConnection.subscribe(ApplicationEvents.ON_SCAN_LOCAL_STARTED, (ApplicationEvents) this::clear); + } + + private void clear() { + this.changedFiles = new HashSet<>(); + components.forEach(FloatingToolbarComponent::scheduleHide); + components = new HashSet<>(); + } + + private void updateChangedFiles(String s) { + this.changedFiles.add(s); + } + + @Override + public void dispose() { + // Disconnect and release resources from the application bus connection + appBusConnection.disconnect(); + } }