Skip to content

Commit b775753

Browse files
Krosovoknquinquenel
authored andcommitted
SLI-2473 Move renderers to a separate classes and minor fixes.
1 parent 8bdb4b8 commit b775753

File tree

4 files changed

+235
-164
lines changed

4 files changed

+235
-164
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* SonarLint for IntelliJ IDEA
3+
* Copyright (C) 2015-2025 SonarSource Sàrl
4+
* sonarlint@sonarsource.com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public
17+
* License along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
19+
*/
20+
package org.sonarlint.intellij.config.project
21+
22+
import com.intellij.openapi.fileTypes.UnknownFileType
23+
import com.intellij.util.ui.EmptyIcon
24+
import com.intellij.util.ui.JBUI
25+
import java.awt.Component
26+
import javax.swing.JLabel
27+
import javax.swing.JTable
28+
import javax.swing.table.DefaultTableCellRenderer
29+
import org.sonarlint.intellij.ui.ruledescription.RuleLanguages
30+
31+
private val CELL_PADDING = JBUI.Borders.empty(0, 8)
32+
33+
class LanguageCellRenderer : DefaultTableCellRenderer() {
34+
override fun getTableCellRendererComponent(
35+
table: JTable, value: Any?, isSelected: Boolean, hasFocus: Boolean, row: Int, column: Int,
36+
): Component {
37+
val label = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column) as JLabel
38+
if (value is SupportedLanguageRow) {
39+
val fileType = RuleLanguages.findFileTypeByRuleLanguage(value.language)
40+
label.icon = if (fileType is UnknownFileType) EmptyIcon.ICON_16 else fileType.icon
41+
label.text = value.displayName
42+
}
43+
label.border = CELL_PADDING
44+
return label
45+
}
46+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* SonarLint for IntelliJ IDEA
3+
* Copyright (C) 2015-2025 SonarSource Sàrl
4+
* sonarlint@sonarsource.com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public
17+
* License along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
19+
*/
20+
package org.sonarlint.intellij.config.project
21+
22+
import com.intellij.ui.JBColor
23+
import com.intellij.util.ui.EmptyIcon
24+
import com.intellij.util.ui.JBUI
25+
import java.awt.Color
26+
import java.awt.Component
27+
import javax.swing.JLabel
28+
import javax.swing.JTable
29+
import javax.swing.table.DefaultTableCellRenderer
30+
import org.sonarlint.intellij.SonarLintPlugin
31+
import org.sonarlint.intellij.common.util.SonarLintUtils
32+
import org.sonarlint.intellij.ui.icons.SonarLintIcons
33+
34+
private val COLOR_BLUE = JBColor(Color(30, 100, 200), Color(100, 160, 255))
35+
private val CELL_PADDING = JBUI.Borders.empty(0, 8)
36+
37+
class SourceCellRenderer : DefaultTableCellRenderer() {
38+
private val pluginVersion: String = SonarLintUtils.getService(SonarLintPlugin::class.java).version
39+
40+
override fun getTableCellRendererComponent(table: JTable, value: Any?, isSelected: Boolean, hasFocus: Boolean, row: Int, column: Int): Component {
41+
val label = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column) as JLabel
42+
if (value is AnalyzerSource) {
43+
label.icon = when (value) {
44+
AnalyzerSource.SONARQUBE_SERVER -> SonarLintIcons.ICON_SONARQUBE_SERVER_16
45+
AnalyzerSource.SONARQUBE_CLOUD -> SonarLintIcons.ICON_SONARQUBE_CLOUD_16
46+
AnalyzerSource.LOCAL -> EmptyIcon.ICON_16
47+
}
48+
label.text = when (value) {
49+
AnalyzerSource.LOCAL -> "${value.label} $pluginVersion"
50+
else -> value.label
51+
}
52+
if (!isSelected && value != AnalyzerSource.LOCAL) {
53+
label.foreground = COLOR_BLUE
54+
}
55+
}
56+
label.horizontalAlignment = LEFT
57+
label.border = CELL_PADDING
58+
return label
59+
}
60+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* SonarLint for IntelliJ IDEA
3+
* Copyright (C) 2015-2025 SonarSource Sàrl
4+
* sonarlint@sonarsource.com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public
17+
* License along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
19+
*/
20+
package org.sonarlint.intellij.config.project
21+
22+
import com.intellij.ui.JBColor
23+
import com.intellij.util.ui.JBUI
24+
import java.awt.BorderLayout
25+
import java.awt.Color
26+
import java.awt.Component
27+
import java.awt.Dimension
28+
import java.awt.Graphics
29+
import java.awt.Graphics2D
30+
import java.awt.RenderingHints
31+
import javax.swing.Icon
32+
import javax.swing.JLabel
33+
import javax.swing.JPanel
34+
import javax.swing.JTable
35+
import javax.swing.SwingConstants
36+
import javax.swing.table.TableCellRenderer
37+
import org.sonarlint.intellij.config.project.StatusCellRenderer.GreenDotIcon
38+
39+
private val COLOR_GREEN = JBColor(Color(34, 139, 34), Color(80, 200, 80))
40+
private val COLOR_BLUE = JBColor(Color(30, 100, 200), Color(100, 160, 255))
41+
private val COLOR_RED = JBColor(Color(180, 30, 30), Color(230, 80, 80))
42+
private val CELL_PADDING = JBUI.Borders.empty(0, 8)
43+
private val DOT_AREA_WIDTH = JBUI.scale(GreenDotIcon.SIZE + 4)
44+
45+
class StatusCellRenderer : TableCellRenderer {
46+
47+
// Dot placeholder: fixed-width panel that shows the green dot for ACTIVE
48+
private val dotPlaceholder = object : JPanel() {
49+
var showDot = false
50+
init {
51+
isOpaque = false
52+
preferredSize = Dimension(DOT_AREA_WIDTH, 0)
53+
}
54+
override fun paintComponent(g: Graphics) {
55+
if (showDot) GreenDotIcon.paintIcon(
56+
this, g,
57+
(width - GreenDotIcon.SIZE) / 2,
58+
(height - GreenDotIcon.SIZE) / 2
59+
)
60+
}
61+
}
62+
63+
private val textLabel = JLabel().apply {
64+
horizontalAlignment = SwingConstants.LEFT
65+
}
66+
67+
private val cell = JPanel(BorderLayout()).apply {
68+
isOpaque = true
69+
add(dotPlaceholder, BorderLayout.WEST)
70+
add(textLabel, BorderLayout.CENTER)
71+
}
72+
73+
override fun getTableCellRendererComponent(table: JTable, value: Any?, isSelected: Boolean, hasFocus: Boolean, row: Int, column: Int): Component {
74+
val bg = if (isSelected) table.selectionBackground else table.background
75+
val fg = if (isSelected) table.selectionForeground else table.foreground
76+
77+
cell.background = bg
78+
textLabel.background = bg
79+
cell.border = CELL_PADDING
80+
textLabel.toolTipText = null
81+
dotPlaceholder.showDot = false
82+
83+
if (value is AnalyzerStatus) {
84+
textLabel.text = value.label
85+
textLabel.toolTipText = value.tooltip
86+
87+
textLabel.foreground = if (isSelected) fg else when (value) {
88+
AnalyzerStatus.ACTIVE -> COLOR_GREEN
89+
AnalyzerStatus.SYNCED -> COLOR_BLUE
90+
AnalyzerStatus.FAILED -> COLOR_RED
91+
else -> fg
92+
}
93+
94+
dotPlaceholder.showDot = value == AnalyzerStatus.ACTIVE
95+
}
96+
97+
return cell
98+
}
99+
100+
internal object GreenDotIcon : Icon {
101+
const val SIZE = 8
102+
103+
override fun getIconWidth() = SIZE
104+
override fun getIconHeight() = SIZE
105+
106+
override fun paintIcon(c: Component?, g: Graphics, x: Int, y: Int) {
107+
val g2 = g.create() as Graphics2D
108+
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
109+
g2.color = COLOR_GREEN
110+
g2.fillOval(x, y, SIZE, SIZE)
111+
g2.dispose()
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)