@@ -14,6 +14,7 @@ import com.intellij.util.messages.MessageBusConnection
1414import com.intellij.util.ui.JBUI
1515import com.jetbrains.rd.util.lifetime.LifetimeDefinition
1616import org.digma.intellij.plugin.analytics.AnalyticsService
17+ import org.digma.intellij.plugin.analytics.BackendConnectionMonitor
1718import org.digma.intellij.plugin.analytics.EnvironmentChanged
1819import org.digma.intellij.plugin.common.EDT
1920import org.digma.intellij.plugin.common.usageStatusChange.UsageStatusChangeListener
@@ -37,6 +38,8 @@ import javax.swing.event.PopupMenuEvent
3738import javax.swing.event.PopupMenuListener
3839import javax.swing.plaf.basic.BasicComboPopup
3940
41+ const val NO_ENVIRONMENTS_MESSAGE : String = " No Environments"
42+
4043class 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
0 commit comments