@@ -12,15 +12,14 @@ import com.intellij.util.ui.JBUI
12
12
import com.intellij.util.ui.components.BorderLayoutPanel
13
13
import java.awt.Color
14
14
import java.awt.Dimension
15
- import java.awt.Graphics
16
15
import java.awt.Insets
17
16
import java.awt.event.MouseAdapter
18
17
import java.awt.event.MouseEvent
19
18
import javax.swing.Box
20
19
import javax.swing.JComponent
21
20
22
21
class EditorPadding (private val editor : Editor , pad : Int ) :
23
- Box .Filler (Dimension (pad, 0 ), Dimension (pad, 0 ), Dimension (pad, 32767 )) {
22
+ Box .Filler (Dimension (pad, pad ), Dimension (pad, pad ), Dimension (pad, pad )) {
24
23
init {
25
24
setOpaque(true )
26
25
editor.caretModel.addCaretListener(object : CaretListener {
@@ -30,76 +29,74 @@ class EditorPadding(private val editor: Editor, pad: Int) :
30
29
})
31
30
}
32
31
33
- override fun getBackground (): Color {
34
- return editor.contentComponent.getBackground()
35
- }
36
-
37
- override fun paintComponent (g : Graphics ) {
38
- super .paintComponent(g)
39
- }
32
+ override fun getBackground (): Color = editor.contentComponent.getBackground()
40
33
}
41
34
42
35
43
36
class EditorFragment (private val editor : EditorEx , message : CompletableMessage ) {
44
37
private val editorLineThreshold = 6
45
38
private val expandCollapseTextLabel: JBLabel = JBLabel (message.getRole().roleName(), 0 ).apply {
46
- setOpaque( true )
39
+ isOpaque = true
47
40
isVisible = false
48
41
}
49
42
50
- private val content: BorderLayoutPanel
43
+ private val content: BorderLayoutPanel = createContentPanel()
51
44
private var collapsed = false
52
45
53
- init {
54
- content = object : BorderLayoutPanel () {
46
+ private fun createContentPanel (): BorderLayoutPanel {
47
+ return object : BorderLayoutPanel () {
55
48
override fun getPreferredSize (): Dimension {
56
49
val preferredSize = super .getPreferredSize()
57
- val lineCount = editor.document.lineCount
58
- val shouldCollapse = lineCount > editorLineThreshold
59
- if (shouldCollapse && getCollapsed()) {
50
+ if (editor.document.lineCount > editorLineThreshold && collapsed) {
60
51
val lineHeight = editor.lineHeight
61
- val insets = editor.scrollPane.getInsets()
62
- val height = lineHeight * editorLineThreshold + insets.height
63
-
64
- var editorMaxHeight = height + expandCollapseTextLabel.preferredSize.height + getInsets().height
65
- val header = editor.headerComponent
66
- if (header != null ) {
67
- editorMaxHeight + = header.getPreferredSize().height
68
- }
69
-
52
+ val insets = editor.scrollPane.insets
53
+ val editorMaxHeight = calculateMaxHeight(lineHeight, insets)
70
54
return Dimension (preferredSize.width, editorMaxHeight)
71
55
}
72
-
73
56
return preferredSize
74
57
}
75
- }
76
58
77
- content.setBorder(
78
- JBUI .Borders .compound(
59
+ private fun calculateMaxHeight (lineHeight : Int , insets : Insets ): Int {
60
+ val height = lineHeight * editorLineThreshold + insets.height
61
+ val headerHeight = editor.headerComponent?.preferredSize?.height ? : 0
62
+ val labelHeight = expandCollapseTextLabel.preferredSize.height
63
+ return height + headerHeight + labelHeight + insets().height
64
+ }
65
+ }.apply {
66
+ border = JBUI .Borders .compound(
79
67
JBUI .Borders .empty(10 , 0 ),
80
68
JBUI .Borders .customLine(JBColor (0xD4E1570 , 0x474071 ))
81
69
)
82
- )
83
- content.setOpaque(false )
84
- content.addToLeft((EditorPadding (editor, 5 )))
85
- content.addToCenter((editor.component))
86
- content.addToRight((EditorPadding (editor, 5 )))
87
- content.addToBottom((expandCollapseTextLabel))
70
+ isOpaque = false
71
+
72
+ addToLeft(EditorPadding (editor, 5 ))
73
+ addToCenter(editor.component)
74
+ addToRight(EditorPadding (editor, 5 ))
75
+ addToBottom(expandCollapseTextLabel)
76
+ }
77
+ }
88
78
79
+ init {
89
80
expandCollapseTextLabel.addMouseListener(object : MouseAdapter () {
90
81
override fun mouseClicked (e : MouseEvent ? ) {
91
- setCollapsed( ! getCollapsed() )
82
+ toggleCollapsedState( )
92
83
}
93
84
})
94
85
}
95
86
96
87
fun getContent (): JComponent = content
97
88
98
- fun getCollapsed (): Boolean = collapsed
89
+ fun isCollapsed (): Boolean = collapsed
99
90
100
91
fun setCollapsed (value : Boolean ) {
101
- collapsed = value
102
- updateExpandCollapseLabel()
92
+ if (collapsed != value) {
93
+ collapsed = value
94
+ updateExpandCollapseLabel()
95
+ }
96
+ }
97
+
98
+ private fun toggleCollapsedState () {
99
+ setCollapsed(! collapsed)
103
100
}
104
101
105
102
fun updateExpandCollapseLabel () {
0 commit comments