Skip to content

Commit 3305848

Browse files
#262 disable refreshAll on ui once btn was clicked and action is in progress to avoid creation of tasks queue
1 parent 4c531ca commit 3305848

File tree

9 files changed

+209
-49
lines changed

9 files changed

+209
-49
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.digma.intellij.plugin.refreshInsightsTask;
2+
3+
import com.intellij.util.messages.Topic;
4+
5+
public interface RefreshInsightsTaskScheduled {
6+
7+
@Topic.ProjectLevel
8+
Topic<RefreshInsightsTaskScheduled> REFRESH_INSIGHTS_TASK_TOPIC = Topic.create("REFRESH_INSIGHTS_TASK_TOPIC", RefreshInsightsTaskScheduled.class);
9+
10+
void refreshInsightsTaskStarted();
11+
12+
void refreshInsightsTaskFinished();
13+
14+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package org.digma.intellij.plugin.refreshInsightsTask
2+
3+
import com.intellij.openapi.diagnostic.Logger
4+
import com.intellij.openapi.editor.Editor
5+
import com.intellij.openapi.fileEditor.FileEditorManager
6+
import com.intellij.openapi.project.Project
7+
import org.digma.intellij.plugin.common.Backgroundable
8+
import org.digma.intellij.plugin.document.DocumentInfoContainer
9+
import org.digma.intellij.plugin.document.DocumentInfoService
10+
import org.digma.intellij.plugin.log.Log
11+
import org.digma.intellij.plugin.ui.model.MethodScope
12+
import org.digma.intellij.plugin.ui.service.ErrorsViewService
13+
import org.digma.intellij.plugin.ui.service.InsightsViewService
14+
import java.util.concurrent.atomic.AtomicBoolean
15+
import java.util.concurrent.locks.ReentrantLock
16+
17+
class RefreshService(private val project: Project) {
18+
private val logger: Logger = Logger.getInstance(RefreshService::class.java)
19+
20+
private val errorsViewService: ErrorsViewService = project.getService(ErrorsViewService::class.java)
21+
private val insightsViewService: InsightsViewService = project.getService(InsightsViewService::class.java)
22+
private val refreshInsightsTaskScheduledLock: ReentrantLock = ReentrantLock()
23+
private val isGeneralRefreshButtonEnabled = AtomicBoolean(true)
24+
25+
companion object {
26+
fun getInstance(project: Project): RefreshService {
27+
return project.getService(RefreshService::class.java)
28+
}
29+
}
30+
31+
fun refreshAll() {
32+
if (isGeneralRefreshButtonEnabled.getAndSet(false)) {
33+
val scope = insightsViewService.model.scope
34+
val selectedTextEditor = FileEditorManager.getInstance(project).selectedTextEditor
35+
if (scope is MethodScope) {
36+
val documentInfoContainer = DocumentInfoService.getInstance(project).getDocumentInfoByMethodInfo(scope.getMethodInfo())
37+
38+
39+
//updateInsightsCache must run in the background, the use of refreshInsightsTaskScheduledLock makes sure no two threads
40+
//update InsightsCache at the same time.
41+
val task = Runnable {
42+
refreshInsightsTaskScheduledLock.lock()
43+
Log.log(logger::debug, "Lock acquired for refreshAll to {}. ", documentInfoContainer?.documentInfo?.fileUri)
44+
try {
45+
notifyRefreshInsightsTaskStarted(documentInfoContainer?.documentInfo?.fileUri)
46+
updateInsightsCache(selectedTextEditor, documentInfoContainer, scope)
47+
} finally {
48+
refreshInsightsTaskScheduledLock.unlock()
49+
Log.log(logger::debug, "Lock released for refreshAll to {}. ", documentInfoContainer?.documentInfo?.fileUri)
50+
notifyRefreshInsightsTaskFinished(documentInfoContainer?.documentInfo?.fileUri)
51+
isGeneralRefreshButtonEnabled.set(true)
52+
}
53+
}
54+
Backgroundable.ensureBackground(project, "Refreshing insights", task)
55+
}
56+
}
57+
}
58+
59+
private fun updateInsightsCache(selectedTextEditor: Editor?, documentInfoContainer: DocumentInfoContainer?, scope: MethodScope) {
60+
val selectedDocument = selectedTextEditor?.document
61+
62+
// firstly update actual document that is opened
63+
if (selectedDocument != null) {
64+
documentInfoContainer?.updateCache()
65+
}
66+
insightsViewService.updateInsightsModel(scope.getMethodInfo())
67+
errorsViewService.updateErrorsModel(scope.getMethodInfo())
68+
69+
// update all our local cache in the background
70+
if (selectedDocument != null) {
71+
DocumentInfoService.getInstance(project).updateCacheForOtherOpenedDocuments(documentInfoContainer?.documentInfo?.fileUri)
72+
} else {
73+
DocumentInfoService.getInstance(project).updateCacheForAllOpenedDocuments()
74+
}
75+
}
76+
77+
private fun notifyRefreshInsightsTaskStarted(fileUri: String?) {
78+
Log.log(logger::debug, "Notifying RefreshInsightsTaskStarted for {}. ", fileUri)
79+
if (project.isDisposed) {
80+
return
81+
}
82+
project.messageBus.syncPublisher(RefreshInsightsTaskScheduled.REFRESH_INSIGHTS_TASK_TOPIC).refreshInsightsTaskStarted()
83+
}
84+
85+
private fun notifyRefreshInsightsTaskFinished(fileUri: String?) {
86+
Log.log(logger::debug, "Notifying RefreshInsightsTaskFinished for {}. ", fileUri)
87+
if (project.isDisposed) {
88+
return
89+
}
90+
project.messageBus.syncPublisher(RefreshInsightsTaskScheduled.REFRESH_INSIGHTS_TASK_TOPIC).refreshInsightsTaskFinished()
91+
}
92+
93+
}

ide-common/src/main/kotlin/org/digma/intellij/plugin/ui/service/RefreshService.kt

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.digma.intellij.plugin.rider.refreshInsightsTask;
2+
3+
import org.digma.intellij.plugin.refreshInsightsTask.RefreshInsightsTaskScheduled;
4+
5+
public class RefreshInsightsTaskScheduledHandler implements RefreshInsightsTaskScheduled {
6+
7+
@Override
8+
public void refreshInsightsTaskStarted() {
9+
//nothing to do here
10+
}
11+
12+
@Override
13+
public void refreshInsightsTaskFinished() {
14+
//nothing to do here
15+
}
16+
}

src/main/kotlin/org/digma/intellij/plugin/ui/common/Panels.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import com.intellij.ui.dsl.builder.panel
1010
import com.intellij.ui.dsl.gridLayout.HorizontalAlign
1111
import com.intellij.util.ui.JBUI
1212
import org.digma.intellij.plugin.analytics.AnalyticsService
13-
import org.digma.intellij.plugin.ui.errors.IconButton
13+
import org.digma.intellij.plugin.ui.errors.GeneralRefreshIconButton
1414
import org.digma.intellij.plugin.ui.model.NOT_SUPPORTED_OBJECT_MSG
1515
import org.digma.intellij.plugin.ui.model.PanelModel
1616
import org.digma.intellij.plugin.ui.model.errors.ErrorsModel
1717
import org.digma.intellij.plugin.ui.model.insights.InsightsModel
1818
import org.digma.intellij.plugin.ui.panels.DigmaResettablePanel
1919
import org.digma.intellij.plugin.ui.panels.DigmaTabPanel
20-
import org.digma.intellij.plugin.ui.service.RefreshService
20+
import org.digma.intellij.plugin.refreshInsightsTask.RefreshService
2121
import java.awt.BorderLayout
2222
import java.awt.Cursor
2323
import java.awt.Dimension
@@ -106,7 +106,7 @@ private fun getGeneralRefreshButton(project: Project): JButton {
106106

107107
val size = Laf.scalePanels(Laf.Sizes.BUTTON_SIZE_26)
108108
val buttonsSize = Dimension(size, size)
109-
val generalRefreshIconButton = IconButton(Laf.Icons.Insight.REFRESH)
109+
val generalRefreshIconButton = GeneralRefreshIconButton(project, Laf.Icons.Insight.REFRESH)
110110
generalRefreshIconButton.preferredSize = buttonsSize
111111
generalRefreshIconButton.maximumSize = buttonsSize
112112
generalRefreshIconButton.toolTipText = asHtml(REFRESH_ALL_INSIGHTS_AND_ERRORS)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package org.digma.intellij.plugin.ui.errors
2+
3+
import com.intellij.openapi.diagnostic.Logger
4+
import com.intellij.openapi.project.Project
5+
import com.intellij.util.messages.MessageBusConnection
6+
import org.digma.intellij.plugin.refreshInsightsTask.RefreshInsightsTaskScheduled
7+
import org.digma.intellij.plugin.ui.common.Laf
8+
import java.awt.Graphics
9+
import java.awt.event.MouseAdapter
10+
import java.awt.event.MouseEvent
11+
import java.util.concurrent.atomic.AtomicBoolean
12+
import javax.swing.Icon
13+
import javax.swing.JButton
14+
15+
16+
internal class GeneralRefreshIconButton(project: Project, icon: Icon) : JButton(icon) {
17+
18+
private val analyticsConnectionEventsConnection: MessageBusConnection = project.messageBus.connect()
19+
20+
init {
21+
isOpaque = false
22+
isContentAreaFilled = false
23+
isBorderPainted = false
24+
isEnabled = true
25+
background = Laf.Colors.TRANSPARENT
26+
val hoverBackground = Laf.Colors.LIST_ITEM_BACKGROUND
27+
val isGeneralRefreshButtonEnabled = AtomicBoolean(true)
28+
29+
analyticsConnectionEventsConnection.subscribe(
30+
RefreshInsightsTaskScheduled.REFRESH_INSIGHTS_TASK_TOPIC,
31+
handler = object : RefreshInsightsTaskScheduled {
32+
override fun refreshInsightsTaskStarted() {
33+
isGeneralRefreshButtonEnabled.set(false)
34+
}
35+
36+
override fun refreshInsightsTaskFinished() {
37+
isGeneralRefreshButtonEnabled.set(true)
38+
}
39+
}
40+
)
41+
42+
addMouseListener(object : MouseAdapter() {
43+
override fun mouseEntered(e: MouseEvent?) {
44+
isEnabled = isGeneralRefreshButtonEnabled.get()
45+
if (isGeneralRefreshButtonEnabled.get()) {
46+
background = hoverBackground
47+
}
48+
}
49+
50+
override fun mouseExited(e: MouseEvent?) {
51+
background = Laf.Colors.TRANSPARENT
52+
}
53+
54+
override fun mousePressed(e: MouseEvent?) {
55+
isEnabled = isGeneralRefreshButtonEnabled.get()
56+
background = Laf.Colors.TRANSPARENT
57+
}
58+
59+
override fun mouseReleased(e: MouseEvent?) {
60+
if (isGeneralRefreshButtonEnabled.get()) {
61+
background = hoverBackground
62+
}
63+
isEnabled = isGeneralRefreshButtonEnabled.get()
64+
}
65+
66+
override fun mouseClicked(e: MouseEvent?) {
67+
isEnabled = isGeneralRefreshButtonEnabled.get()
68+
background = Laf.Colors.TRANSPARENT
69+
}
70+
})
71+
}
72+
73+
override fun paintComponent(g: Graphics) {
74+
g.color = background
75+
g.fillRect(0, 0, width, height)
76+
super.paintComponent(g)
77+
}
78+
}

src/main/kotlin/org/digma/intellij/plugin/ui/list/insights/InsightsCommon.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import org.digma.intellij.plugin.ui.common.buildBoldTitleGrayedComment
1616
import org.digma.intellij.plugin.ui.list.PanelsLayoutHelper
1717
import org.digma.intellij.plugin.ui.list.commonListItemPanel
1818
import org.digma.intellij.plugin.ui.panels.DigmaResettablePanel
19-
import org.digma.intellij.plugin.ui.service.RefreshService
19+
import org.digma.intellij.plugin.refreshInsightsTask.RefreshService
2020
import java.awt.BorderLayout
2121
import java.awt.Cursor
2222
import java.awt.Dimension

src/main/resources/META-INF/org.digma.intellij-with-rider.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
<listener
3333
class="org.digma.intellij.plugin.rider.document.RiderDocumentInfoConsumer"
3434
topic="org.digma.intellij.plugin.document.DocumentInfoChanged"/>
35+
<listener
36+
class="org.digma.intellij.plugin.rider.refreshInsightsTask.RefreshInsightsTaskScheduledHandler"
37+
topic="org.digma.intellij.plugin.refreshInsightsTask.RefreshInsightsTaskScheduled"/>
3538
<listener
3639
class="org.digma.intellij.plugin.rider.editor.RiderFileClosedListener"
3740
topic="com.intellij.openapi.fileEditor.FileEditorManagerListener"/>

src/main/resources/META-INF/plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
<projectService serviceImplementation="org.digma.intellij.plugin.ui.service.InsightsViewService"/>
4040

41-
<projectService serviceImplementation="org.digma.intellij.plugin.ui.service.RefreshService"/>
41+
<projectService serviceImplementation="org.digma.intellij.plugin.refreshInsightsTask.RefreshService"/>
4242

4343
<projectService serviceImplementation="org.digma.intellij.plugin.ui.service.ErrorsViewService"/>
4444

0 commit comments

Comments
 (0)