|
| 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