@@ -16,16 +16,18 @@ import org.digma.intellij.plugin.ui.common.Laf.Icons.Environment.Companion.ENVIR
1616import org.digma.intellij.plugin.ui.model.PanelModel
1717import org.digma.intellij.plugin.ui.model.environment.EnvironmentsSupplier
1818import org.digma.intellij.plugin.ui.panels.DigmaResettablePanel
19+ import org.digma.intellij.plugin.ui.panels.DigmaTabPanel
1920import java.awt.Dimension
2021import java.awt.FlowLayout
22+ import java.lang.Integer.max
2123import java.util.*
2224import java.util.function.Function
2325import javax.swing.Icon
2426import javax.swing.JComponent
2527import javax.swing.SwingUtilities
2628
27- // need to remember we have two instances of this panel , one for the insights tab and one for the errors tab.
28- // both instances need to be in sync with the selected button and the environments list.
29+ // need to remember we have few instances of this panel , one on every main tab.
30+ // all instances need to be in sync with the selected button and the environments list.
2931class EnvironmentsPanel (
3032 project : Project ,
3133 private val model : PanelModel ,
@@ -70,30 +72,49 @@ class EnvironmentsPanel(
7072
7173 /*
7274 usually this panel works fine.
73- there is one issue: sometimes when the plugin window opens on startup this panel takes too much
74- vertical space and the insights list is almost not shown. any hover over this panel with the mouse or any
75- other action related to the plugin will fix it and the tab will re-layout. from there on the panel functions ok.
76- It seems that super.getPreferredSize() sometimes returns a size with negative width, when that happens the flow
77- layout computes a much too large vertical space and when the panel is first visible it's too large.
78- I could not find any way to cause this panel to layout correctly on startup.
79- this code in getPreferredSize will keep track on the last positive size and return that one if super returns
80- a negative width. it seems to do the job.
81- */
82- private var lastPositivePs: Dimension = Dimension (100 , 300 )
75+ there is one issue: sometimes when the plugin window opens on startup the WrapLayout computes a large
76+ vertical height and the panel takes too much vertical space.
77+ Its noticed that when the computed width is a negative number it means the calculation is not good. in that
78+ case this method will calculate a preferred size based on the buttons size. its usually only on first
79+ initialization of the tool window.
80+ */
8381 override fun getPreferredSize (): Dimension {
8482 val ps = super .getPreferredSize()
8583 if (ps != null ) {
86- return if (ps.width >= 0 && ps.height >= 0 ) {
87- this .lastPositivePs = ps
84+ return if (ps.width > 0 && ps.height > 0 ) {
8885 ps
8986 } else {
90- this .lastPositivePs
87+ computePreferredSize()
9188 }
9289 }
9390
9491 return super .getPreferredSize()
9592 }
9693
94+ private fun computePreferredSize (): Dimension {
95+
96+ var tabPanel = parent
97+ while ((tabPanel != null ) && (tabPanel !is DigmaTabPanel )) {
98+ tabPanel = tabPanel.parent
99+ }
100+
101+ if ((tabPanel != null ) && (tabPanel.size.width > 0 ) && components.isNotEmpty()) {
102+ val width = tabPanel.size.width - (this .insets.left + this .insets.right)
103+ var componentsWidth = 0
104+ var componentsHeight = 0
105+ components.forEach {
106+ componentsWidth + = it.preferredSize.width + (layout as WrapLayout ).hgap
107+ componentsHeight = max(componentsHeight, it.preferredSize.height)
108+ }
109+
110+ val lines = (componentsWidth / width) + 1
111+ val height = lines * (componentsHeight + ((layout as WrapLayout ).vgap) * 2 )
112+ return Dimension (width, height)
113+ }
114+
115+ return super .getPreferredSize()
116+ }
117+
97118
98119 private fun select (newSelectedEnv : String? ) {
99120 val currentSelected: EnvLink ? = getSelected()
@@ -122,7 +143,6 @@ class EnvironmentsPanel(
122143 this .components.forEach {
123144 this .remove(it)
124145 }
125- revalidate()
126146 }
127147
128148 val usageStatusResult = model.getUsageStatus()
0 commit comments