Skip to content

Commit

Permalink
refactor: move code to the right panel
Browse files Browse the repository at this point in the history
  • Loading branch information
teodora-sandu committed Mar 27, 2024
1 parent 197b2e5 commit cdfa292
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@ package io.snyk.plugin.ui.toolwindow.panels

import com.intellij.ui.IdeBorderFactory
import com.intellij.ui.SideBorder
import com.intellij.ui.jcef.JBCefApp
import com.intellij.ui.jcef.JBCefBrowserBuilder
import com.intellij.uiDesigner.core.GridLayoutManager
import com.intellij.util.ui.JBUI
import com.intellij.util.ui.JBUI.Borders
import icons.SnykIcons
import io.snyk.plugin.Severity
import io.snyk.plugin.pluginSettings
import io.snyk.plugin.ui.DescriptionHeaderPanel
import io.snyk.plugin.ui.SnykBalloonNotificationHelper
import io.snyk.plugin.ui.addSpacer
import io.snyk.plugin.ui.baseGridConstraints
import io.snyk.plugin.ui.baseGridConstraintsAnchorWest
import io.snyk.plugin.ui.toolwindow.SnykToolWindowPanel
import io.snyk.plugin.ui.wrapWithScrollPane
import java.awt.BorderLayout
import java.awt.Font
Expand All @@ -28,42 +23,18 @@ private val EMPTY_PANEL = JBUI.Panels.simplePanel()

abstract class IssueDescriptionPanelBase(
private val title: String,
private val severity: Severity,
private val details: String?
private val severity: Severity
) : JPanel(BorderLayout()), IssueDescriptionPanel {
private val unexpectedErrorMessage = "Snyk encountered an issue while rendering the vulnerability description. Please try again, or contact support if the problem persists. We apologize for any inconvenience caused.";

/**
* **MUST** be invoked in derived class to actually create the UI elements.
* Can't be part of constructor due to `state` usage in underling abstract/open methods/props:
*/
protected fun createUI() {
if (pluginSettings().isGlobalIgnoresFeatureEnabled && details != null) {
if (!JBCefApp.isSupported()) {
val statePanel = StatePanel(SnykToolWindowPanel.SELECT_ISSUE_TEXT)
this.add(wrapWithScrollPane(statePanel), BorderLayout.CENTER)
SnykBalloonNotificationHelper.showError(unexpectedErrorMessage, null)
return
}

val cefClient = JBCefApp.getInstance().createClient()
val jbCefBrowser = JBCefBrowserBuilder().setClient(cefClient).setEnableOpenDevToolsMenuItem(true).build()

val panel = JPanel()
panel.add(jbCefBrowser.component, BorderLayout())
this.add(
wrapWithScrollPane(panel),
BorderLayout.CENTER
)

jbCefBrowser.loadHTML(this.details, jbCefBrowser.cefBrowser.url)

} else {
this.add(
wrapWithScrollPane(descriptionBodyPanel()),
BorderLayout.CENTER
)
}
this.add(
wrapWithScrollPane(descriptionBodyPanel()),
BorderLayout.CENTER
)
if (isBottomPanelNeeded) {
this.add(
bottomPanel(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ class SuggestionDescriptionPanel(
private val suggestionIndex: Int
) : IssueDescriptionPanelBase(
title = suggestion.title,
severity = suggestion.getSeverityAsEnum(),
details = null
severity = suggestion.getSeverityAsEnum()
) {
val project = snykCodeFile.project

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@ import com.intellij.icons.AllIcons
import com.intellij.ui.HyperlinkLabel
import com.intellij.ui.ScrollPaneFactory
import com.intellij.ui.components.JBTabbedPane
import com.intellij.ui.jcef.JBCefApp
import com.intellij.ui.jcef.JBCefBrowserBuilder
import com.intellij.uiDesigner.core.GridConstraints
import com.intellij.uiDesigner.core.GridLayoutManager
import com.intellij.util.ui.JBInsets
import com.intellij.util.ui.UIUtil
import io.snyk.plugin.getDocument
import io.snyk.plugin.navigateToSource
import io.snyk.plugin.pluginSettings
import io.snyk.plugin.snykcode.core.SnykCodeFile
import io.snyk.plugin.toVirtualFile
import io.snyk.plugin.ui.DescriptionHeaderPanel
import io.snyk.plugin.ui.SnykBalloonNotificationHelper
import io.snyk.plugin.ui.baseGridConstraintsAnchorWest
import io.snyk.plugin.ui.descriptionHeaderPanel
import io.snyk.plugin.ui.panelGridConstraints
import io.snyk.plugin.ui.toolwindow.SnykToolWindowPanel
import io.snyk.plugin.ui.wrapWithScrollPane
import snyk.common.lsp.DataFlow
import snyk.common.lsp.ScanIssue
import java.awt.BorderLayout
import java.awt.Color
import java.awt.Dimension
import java.awt.Font
Expand All @@ -35,13 +42,34 @@ class SuggestionDescriptionPanelFromLS(
private val issue: ScanIssue
) : IssueDescriptionPanelBase(
title = getIssueTitle(issue),
severity = issue.getSeverityAsEnum(),
details = issue.additionalData.details
severity = issue.getSeverityAsEnum()
) {
val project = snykCodeFile.project
private val unexpectedErrorMessage = "Snyk encountered an issue while rendering the vulnerability description. Please try again, or contact support if the problem persists. We apologize for any inconvenience caused.";

init {
createUI()
if (pluginSettings().isGlobalIgnoresFeatureEnabled && issue.additionalData.details != null) {
if (!JBCefApp.isSupported()) {
val statePanel = StatePanel(SnykToolWindowPanel.SELECT_ISSUE_TEXT)
this.add(wrapWithScrollPane(statePanel), BorderLayout.CENTER)
SnykBalloonNotificationHelper.showError(unexpectedErrorMessage, null)
} else {
val cefClient = JBCefApp.getInstance().createClient()
val jbCefBrowser = JBCefBrowserBuilder().setClient(cefClient).setEnableOpenDevToolsMenuItem(true).build()

val panel = JPanel()
panel.add(jbCefBrowser.component, BorderLayout())
this.add(
wrapWithScrollPane(panel),
BorderLayout.CENTER
)

jbCefBrowser.loadHTML(issue.additionalData.details, jbCefBrowser.cefBrowser.url)
}
} else {

createUI()
}
}

override fun secondRowTitlePanel(): DescriptionHeaderPanel = descriptionHeaderPanel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class VulnerabilityDescriptionPanel(
private val groupedVulns: Collection<Vulnerability>
) : IssueDescriptionPanelBase(
title = groupedVulns.first().title,
severity = groupedVulns.first().getSeverity(),
details = null) {
severity = groupedVulns.first().getSeverity()
) {

private val labelProvider: LabelProvider = LabelProvider()
private val vulnerability = groupedVulns.first()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ class BaseImageRemediationDetailPanel(
private val imageIssues: ContainerIssuesForImage,
) : IssueDescriptionPanelBase(
title = imageIssues.imageName,
severity = imageIssues.getSeverities().maxOrNull() ?: Severity.UNKNOWN,
details = null
severity = imageIssues.getSeverities().maxOrNull() ?: Severity.UNKNOWN
) {

private val targetImages: List<KubernetesWorkloadImage> = getKubernetesImageCache(project)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ class ContainerIssueDetailPanel(
private val groupedVulns: Collection<ContainerIssue>
) : IssueDescriptionPanelBase(
title = groupedVulns.first().title,
severity = groupedVulns.first().getSeverity(),
details = null
severity = groupedVulns.first().getSeverity()
) {

private val issue = groupedVulns.first()
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/snyk/iac/IacSuggestionDescriptionPanel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class IacSuggestionDescriptionPanel(
val issue: IacIssue,
val psiFile: PsiFile?,
val project: Project
) : IssueDescriptionPanelBase(title = issue.title, severity = issue.getSeverity(), details = null) {
) : IssueDescriptionPanelBase(title = issue.title, severity = issue.getSeverity()) {

private val labelProvider = LabelProvider()

Expand Down

0 comments on commit cdfa292

Please sign in to comment.