Skip to content

Commit bb35bc1

Browse files
Set default value for ComboBox when connection was lost
and when there are no environments
1 parent 8e7dff2 commit bb35bc1

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

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

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import com.intellij.util.messages.MessageBusConnection
1414
import com.intellij.util.ui.JBUI
1515
import com.jetbrains.rd.util.lifetime.LifetimeDefinition
1616
import org.digma.intellij.plugin.analytics.AnalyticsService
17+
import org.digma.intellij.plugin.analytics.BackendConnectionMonitor
1718
import org.digma.intellij.plugin.analytics.EnvironmentChanged
1819
import org.digma.intellij.plugin.common.EDT
1920
import org.digma.intellij.plugin.common.usageStatusChange.UsageStatusChangeListener
@@ -37,6 +38,8 @@ import javax.swing.event.PopupMenuEvent
3738
import javax.swing.event.PopupMenuListener
3839
import javax.swing.plaf.basic.BasicComboPopup
3940

41+
const val NO_ENVIRONMENTS_MESSAGE: String = "No Environments"
42+
4043
class EnvironmentsDropdownPanel(
4144
project: Project,
4245
usageStatusResult: UsageStatusResult,
@@ -46,6 +49,7 @@ class EnvironmentsDropdownPanel(
4649
private val logger: Logger = Logger.getInstance(EnvironmentsDropdownPanel::class.java)
4750

4851
private val usageStatusChangeConnection: MessageBusConnection = project.messageBus.connect()
52+
private val backendConnectionMonitor: BackendConnectionMonitor
4953
private var usageStatusResult: UsageStatusResult
5054
private val project: Project
5155
private val rebuildPanelLock = ReentrantLock()
@@ -84,6 +88,7 @@ class EnvironmentsDropdownPanel(
8488
EDT.ensureEDT{rebuildInBackground(usageStatusResult)}
8589
}
8690
})
91+
backendConnectionMonitor = project.getService(BackendConnectionMonitor::class.java)
8792
}
8893

8994
override fun reset() {
@@ -94,18 +99,20 @@ class EnvironmentsDropdownPanel(
9499
usageStatusChangeConnection.dispose()
95100
}
96101

97-
private fun rebuildInBackground(usageStatus: UsageStatusResult) {
102+
private fun rebuildInBackground(newUsageStatus: UsageStatusResult) {
98103
if (!popupMenuOpened.get()) {
99-
if (usageStatus.environmentStatuses.size != usageStatusResult.environmentStatuses.size
100-
|| usageStatusResult.codeObjectStatuses.isEmpty() || wasNotInitializedYet.get()) {
101-
usageStatusResult = usageStatus
104+
if (newUsageStatus.environmentStatuses.size != usageStatusResult.environmentStatuses.size
105+
|| (usageStatusResult.codeObjectStatuses.isEmpty() && newUsageStatus.environmentStatuses.isNotEmpty())
106+
|| wasNotInitializedYet.get()
107+
|| backendConnectionMonitor.isConnectionError()) {
108+
usageStatusResult = newUsageStatus
102109

103110
val lifetimeDefinition = LifetimeDefinition()
104111
lifetimeDefinition.lifetime.launchBackground {
105112
rebuildPanelLock.lock()
106113
Log.log(logger::debug, "Lock acquired for rebuild EnvironmentsDropdownPanel process.")
107114
try {
108-
rebuild(usageStatus)
115+
rebuild(newUsageStatus)
109116
} finally {
110117
rebuildPanelLock.unlock()
111118
Log.log(logger::debug, "Lock released for rebuild EnvironmentsDropdownPanel process.")
@@ -193,7 +200,6 @@ class EnvironmentsDropdownPanel(
193200
) {
194201
val items = mutableListOf<String>()
195202
val icons = mutableListOf<Icon>()
196-
val environmentsInfo = environmentsInfo
197203

198204
for (envInfo in environmentsInfo) {
199205
val buttonData = envInfo.value
@@ -204,17 +210,18 @@ class EnvironmentsDropdownPanel(
204210
icons.add(icon)
205211
}
206212

213+
val comboBox = ComboBox(items.toTypedArray())
207214
if (items.size > 0) {
208215
// this flag fixes initial load issue
209216
wasNotInitializedYet.set(false)
210-
}
211-
val comboBox = ComboBox(items.toTypedArray())
212-
comboBox.renderer = object : SimpleListCellRenderer<String>() {
213-
override fun customize(list: JList<out String>, value: String, index: Int, selected: Boolean, hasFocus: Boolean) {
214-
text = value
215-
icon = icons.getOrElse(index) { null }
216-
foreground = if (selected) JBColor.WHITE else JBColor.BLACK
217-
background = if (selected) JBColor.BLUE else JBColor.WHITE
217+
218+
comboBox.renderer = object : SimpleListCellRenderer<String>() {
219+
override fun customize(list: JList<out String>, value: String, index: Int, selected: Boolean, hasFocus: Boolean) {
220+
text = value
221+
icon = icons.getOrElse(index) { null }
222+
foreground = if (selected) JBColor.WHITE else JBColor.BLACK
223+
background = if (selected) JBColor.BLUE else JBColor.WHITE
224+
}
218225
}
219226
}
220227

@@ -263,6 +270,16 @@ class EnvironmentsDropdownPanel(
263270
// Set a fixed width for the closed ComboBox
264271
comboBox.preferredSize = Dimension(30, comboBox.preferredSize.height)
265272
comboBox.selectedItem = getSelected()
273+
comboBox.isEditable = false
274+
275+
if (comboBox.itemCount == 0) {
276+
// display default value
277+
comboBox.addItem(NO_ENVIRONMENTS_MESSAGE)
278+
}
279+
if (backendConnectionMonitor.isConnectionError()) {
280+
comboBox.removeAllItems()
281+
comboBox.addItem(NO_ENVIRONMENTS_MESSAGE)
282+
}
266283

267284
this.add(comboBox)
268285

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ import com.intellij.openapi.rd.util.launchBackground
88
import com.intellij.ui.awt.RelativePoint
99
import com.intellij.util.Alarm
1010
import com.intellij.util.AlarmFactory
11+
import com.intellij.util.messages.MessageBusConnection
1112
import com.intellij.util.ui.JBUI
1213
import com.jetbrains.rd.util.lifetime.LifetimeDefinition
14+
import org.digma.intellij.plugin.analytics.AnalyticsService
15+
import org.digma.intellij.plugin.analytics.AnalyticsServiceConnectionEvent
1316
import org.digma.intellij.plugin.common.CommonUtils
1417
import org.digma.intellij.plugin.common.IDEUtilsService
1518
import org.digma.intellij.plugin.log.Log
@@ -32,10 +35,12 @@ class NavigationPanel(
3235
) : DigmaResettablePanel() {
3336
private val logger: Logger = Logger.getInstance(NavigationPanel::class.java)
3437

38+
private val analyticsServiceStatusConnection: MessageBusConnection = project.messageBus.connect()
3539
private val project: Project
3640
private val insightsModel: InsightsModel
3741
private val changeEnvAlarm: Alarm
3842
private val localHostname: String
43+
private var analyticsService: AnalyticsService? = null
3944
private val rebuildPanelLock = ReentrantLock()
4045

4146
init {
@@ -46,8 +51,19 @@ class NavigationPanel(
4651
isOpaque = false
4752
layout = GridLayout(2, 1)
4853
border = JBUI.Borders.empty()
54+
analyticsService = project.getService(AnalyticsService::class.java)
4955

5056
rebuildInBackground()
57+
58+
analyticsServiceStatusConnection.subscribe(AnalyticsServiceConnectionEvent.ANALYTICS_SERVICE_CONNECTION_EVENT_TOPIC, object : AnalyticsServiceConnectionEvent {
59+
override fun connectionLost() {
60+
rebuildInBackground()
61+
}
62+
63+
override fun connectionGained() {
64+
rebuildInBackground()
65+
}
66+
})
5167
}
5268

5369

0 commit comments

Comments
 (0)