Skip to content

Commit ae40acd

Browse files
committed
feat: show/allow setting decoded base64 values in Secrets, ConfigMaps (redhat-developer#663)
Signed-off-by: Andre Dietisheim <[email protected]>
1 parent 0a05c6a commit ae40acd

File tree

8 files changed

+596
-3
lines changed

8 files changed

+596
-3
lines changed

Diff for: .github/workflows/IJ.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
runs-on: ubuntu-latest
1515
strategy:
1616
matrix:
17-
IJ: [IC-2021.1, IC-2021.2, IC-2021.3, IC-2022.1, IC-2022.2, IC-2022.3, IC-2023.1, IC-2023.2, IC-2023.3]
17+
IJ: [IC-2021.3, IC-2022.1, IC-2022.2, IC-2022.3, IC-2023.1, IC-2023.2, IC-2023.3]
1818

1919
steps:
2020
- uses: actions/checkout@v2

Diff for: gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ideaVersion=IC-2022.1
1+
ideaVersion=IC-2021.3
22
# build number ranges
33
# https://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html
44
sinceIdeaBuild=221
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 Red Hat, Inc.
3+
* Distributed under license by Red Hat, Inc. All rights reserved.
4+
* This program is made available under the terms of the
5+
* Eclipse Public License v2.0 which accompanies this distribution,
6+
* and is available at http://www.eclipse.org/legal/epl-v20.html
7+
*
8+
* Contributors:
9+
* Red Hat, Inc. - initial API and implementation
10+
******************************************************************************/
11+
package com.redhat.devtools.intellij.kubernetes.balloon
12+
13+
import com.intellij.openapi.editor.Editor
14+
import com.intellij.openapi.ui.ComponentValidator
15+
import com.intellij.openapi.ui.ValidationInfo
16+
import com.intellij.openapi.ui.popup.Balloon
17+
import com.intellij.openapi.ui.popup.JBPopupFactory
18+
import com.intellij.openapi.ui.popup.JBPopupListener
19+
import com.intellij.openapi.ui.popup.LightweightWindowEvent
20+
import com.intellij.openapi.util.Disposer
21+
import com.intellij.openapi.util.ExpirableRunnable
22+
import com.intellij.openapi.util.text.StringUtil
23+
import com.intellij.openapi.wm.IdeFocusManager
24+
import com.intellij.ui.ScrollPaneFactory
25+
import com.intellij.ui.awt.RelativePoint
26+
import com.intellij.ui.components.JBLabel
27+
import com.intellij.ui.components.JBTextArea
28+
import com.intellij.ui.components.JBTextField
29+
import com.intellij.util.ui.JBUI
30+
import java.awt.BorderLayout
31+
import java.awt.Dimension
32+
import java.awt.FlowLayout
33+
import java.awt.event.KeyAdapter
34+
import java.awt.event.KeyEvent
35+
import java.awt.event.MouseEvent
36+
import java.util.function.Supplier
37+
import javax.swing.BoxLayout
38+
import javax.swing.JComponent
39+
import javax.swing.JPanel
40+
import javax.swing.JTextArea
41+
import javax.swing.text.JTextComponent
42+
import kotlin.math.max
43+
44+
class StringInputBalloon(private val value: String, private val onValidValue: (String) -> Unit, private val editor: Editor) {
45+
46+
companion object {
47+
private const val MAX_WIDTH = 220.0
48+
private const val MAX_CHARACTERS = 64
49+
}
50+
51+
private var isValid = false
52+
53+
fun show(event: MouseEvent) {
54+
val (field, balloon) = create()
55+
balloon.show(RelativePoint(event), Balloon.Position.above)
56+
val focusManager = IdeFocusManager.getInstance(editor.project)
57+
focusManager.doWhenFocusSettlesDown(onFocused(focusManager, field))
58+
}
59+
60+
private fun create(): Pair<JTextComponent, Balloon> {
61+
val panel = JPanel(BorderLayout())
62+
val textComponent = if (value.contains('\n')) {
63+
createTextArea(panel)
64+
} else {
65+
createTextField(panel)
66+
}
67+
val balloon = createBalloon(panel)
68+
val disposable = Disposer.newDisposable()
69+
Disposer.register(balloon, disposable)
70+
ComponentValidator(disposable)
71+
.withValidator(ValueValidator(textComponent))
72+
.installOn(textComponent)
73+
.andRegisterOnDocumentListener(textComponent)
74+
.revalidate()
75+
val keyListener = onKeyPressed(textComponent, balloon)
76+
textComponent.addKeyListener(keyListener)
77+
balloon.addListener(onClosed(textComponent, keyListener))
78+
return Pair(textComponent, balloon)
79+
}
80+
81+
private fun createTextField(panel: JPanel): JBTextField {
82+
val label = JBLabel("Value:")
83+
label.border = JBUI.Borders.empty(0, 3, 0, 1)
84+
panel.add(label, BorderLayout.WEST)
85+
val field = JBTextField(value)
86+
field.preferredSize = Dimension(
87+
max(MAX_WIDTH, field.preferredSize.width.toDouble()).toInt(),
88+
field.preferredSize.height
89+
)
90+
panel.add(field, BorderLayout.CENTER)
91+
return field
92+
}
93+
94+
private fun createTextArea(panel: JPanel): JTextArea {
95+
val label = JBLabel("Value:")
96+
label.border = JBUI.Borders.empty(0, 3, 4, 0)
97+
panel.add(label, BorderLayout.NORTH)
98+
val textArea = JBTextArea(value,
99+
value.length.floorDiv(MAX_CHARACTERS - 1) + 2, // textarea has text lines + 1
100+
MAX_CHARACTERS - 1)
101+
val scrolled = ScrollPaneFactory.createScrollPane(textArea, true)
102+
panel.add(scrolled, BorderLayout.CENTER)
103+
return textArea
104+
}
105+
106+
private fun createBalloon(panel: JPanel): Balloon {
107+
return JBPopupFactory.getInstance()
108+
.createBalloonBuilder(panel)
109+
.setCloseButtonEnabled(true)
110+
.setBlockClicksThroughBalloon(true)
111+
.setAnimationCycle(0)
112+
.setHideOnKeyOutside(true)
113+
.setHideOnClickOutside(true)
114+
.setFillColor(panel.background)
115+
.setHideOnAction(false) // allow user to Ctrl+A & Ctrl+C
116+
.createBalloon()
117+
}
118+
119+
private fun onClosed(field: JTextComponent, keyListener: KeyAdapter): JBPopupListener {
120+
return object : JBPopupListener {
121+
override fun beforeShown(event: LightweightWindowEvent) {}
122+
override fun onClosed(event: LightweightWindowEvent) {
123+
field.removeKeyListener(keyListener)
124+
}
125+
}
126+
}
127+
128+
private fun onKeyPressed(textComponent: JTextComponent, balloon: Balloon) = object : KeyAdapter() {
129+
override fun keyPressed(e: KeyEvent) {
130+
when (e.keyCode) {
131+
KeyEvent.VK_ESCAPE ->
132+
balloon.hide()
133+
KeyEvent.VK_ENTER ->
134+
if (isValid) {
135+
balloon.hide()
136+
onValidValue.invoke(textComponent.text)
137+
}
138+
}
139+
}
140+
}
141+
142+
private fun onFocused(focusManager: IdeFocusManager, field: JTextComponent): ExpirableRunnable {
143+
return object : ExpirableRunnable {
144+
145+
override fun run() {
146+
focusManager.requestFocus(field, true)
147+
field.selectAll()
148+
}
149+
150+
override fun isExpired(): Boolean {
151+
return false
152+
}
153+
}
154+
}
155+
156+
private inner class ValueValidator(private val textComponent: JTextComponent) : Supplier<ValidationInfo?> {
157+
158+
override fun get(): ValidationInfo? {
159+
if (!textComponent.isEnabled
160+
|| !textComponent.isVisible
161+
) {
162+
return null
163+
}
164+
return validate(textComponent.text)
165+
}
166+
167+
private fun validate(newValue: String): ValidationInfo? {
168+
val validation = when {
169+
StringUtil.isEmptyOrSpaces(newValue) ->
170+
ValidationInfo("Provide a value", textComponent).asWarning()
171+
value == newValue ->
172+
ValidationInfo("Provide new value", textComponent).asWarning()
173+
else ->
174+
null
175+
}
176+
this@StringInputBalloon.isValid = (validation == null)
177+
return validation
178+
}
179+
}
180+
181+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 Red Hat, Inc.
3+
* Distributed under license by Red Hat, Inc. All rights reserved.
4+
* This program is made available under the terms of the
5+
* Eclipse Public License v2.0 which accompanies this distribution,
6+
* and is available at http://www.eclipse.org/legal/epl-v20.html
7+
*
8+
* Contributors:
9+
* Red Hat, Inc. - initial API and implementation
10+
******************************************************************************/
11+
@file:Suppress("UnstableApiUsage")
12+
package com.redhat.devtools.intellij.kubernetes.editor.inlay
13+
14+
import com.intellij.codeInsight.hints.InlayHintsSink
15+
import com.intellij.codeInsight.hints.presentation.InlayPresentation
16+
import com.intellij.codeInsight.hints.presentation.PresentationFactory
17+
import com.intellij.openapi.command.WriteCommandAction
18+
import com.intellij.openapi.editor.Editor
19+
import com.intellij.openapi.editor.impl.EditorImpl
20+
import com.intellij.openapi.project.Project
21+
import com.intellij.psi.PsiElement
22+
import com.redhat.devtools.intellij.common.validation.KubernetesResourceInfo
23+
import com.redhat.devtools.intellij.kubernetes.balloon.StringInputBalloon
24+
import com.redhat.devtools.intellij.kubernetes.editor.util.getBinaryData
25+
import com.redhat.devtools.intellij.kubernetes.editor.util.getData
26+
import com.redhat.devtools.intellij.kubernetes.editor.util.isKubernetesResource
27+
import com.redhat.devtools.intellij.kubernetes.model.util.trimWithEllipsis
28+
import org.jetbrains.concurrency.runAsync
29+
import java.awt.event.MouseEvent
30+
31+
class Base64PresentationsFactory {
32+
33+
companion object {
34+
private const val SECRET_RESOURCE_KIND = "Secret"
35+
private const val CONFIGMAP_RESOURCE_KIND = "ConfigMap"
36+
}
37+
38+
fun create(content: PsiElement, info: KubernetesResourceInfo, editor: Editor, sink: InlayHintsSink): InlayPresentationFactory? {
39+
return when {
40+
isKubernetesResource(SECRET_RESOURCE_KIND, info) ->
41+
StringPresentationsFactory(content, editor, sink)
42+
43+
isKubernetesResource(CONFIGMAP_RESOURCE_KIND, info) ->
44+
BinaryPresentationsFactory(content, editor, sink)
45+
46+
else -> null
47+
}
48+
49+
}
50+
51+
abstract class InlayPresentationFactory(
52+
protected val element: PsiElement,
53+
protected val editor: Editor,
54+
protected val sink: InlayHintsSink
55+
) {
56+
57+
companion object {
58+
const val INLAY_HINT_MAX_WIDTH = 50
59+
}
60+
61+
fun create(): Collection<InlayPresentation>? {
62+
return getChildren(element)?.children?.mapNotNull { child ->
63+
create(Base64ValueAdapter(child), editor, sink)
64+
}
65+
}
66+
67+
protected abstract fun getChildren(element: PsiElement): PsiElement?
68+
69+
protected abstract fun create(adapter: Base64ValueAdapter, editor: Editor, sink: InlayHintsSink): InlayPresentation?
70+
71+
}
72+
73+
class StringPresentationsFactory(element: PsiElement, editor: Editor, sink: InlayHintsSink) :
74+
InlayPresentationFactory(element, editor, sink) {
75+
76+
override fun getChildren(element: PsiElement): PsiElement? {
77+
return getData(element)
78+
}
79+
80+
override fun create(adapter: Base64ValueAdapter, editor: Editor, sink: InlayHintsSink): InlayPresentation? {
81+
val decoded = adapter.getDecoded() ?: return null
82+
val offset = adapter.getStartOffset() ?: return null
83+
val onClick = StringInputBalloon(
84+
decoded,
85+
onValidValue(adapter::set, editor.project),
86+
editor
87+
)::show
88+
val presentation = create(decoded, onClick, editor) ?: return null
89+
sink.addInlineElement(offset, false, presentation, false)
90+
return presentation
91+
}
92+
93+
private fun create(text: String, onClick: (event: MouseEvent) -> Unit, editor: Editor): InlayPresentation? {
94+
val factory = PresentationFactory(editor as EditorImpl)
95+
val trimmed = trimWithEllipsis(text, INLAY_HINT_MAX_WIDTH) ?: return null
96+
val textPresentation = factory.smallText(trimmed)
97+
val hoverPresentation = factory.referenceOnHover(textPresentation) { event, translated ->
98+
onClick.invoke(event)
99+
}
100+
val tooltipPresentation = factory.withTooltip("Click to change value", hoverPresentation)
101+
val roundPresentation = factory.roundWithBackground(tooltipPresentation)
102+
return roundPresentation
103+
}
104+
105+
fun onValidValue(
106+
setter: (value: String, project: Project?) -> Unit,
107+
project: Project?
108+
): (value: String) -> Unit {
109+
return { value ->
110+
runAsync {
111+
WriteCommandAction.runWriteCommandAction(project) {
112+
setter.invoke(value, project)
113+
}
114+
}
115+
}
116+
}
117+
118+
}
119+
120+
class BinaryPresentationsFactory(element: PsiElement, editor: Editor, sink: InlayHintsSink) :
121+
InlayPresentationFactory(element, editor, sink) {
122+
123+
override fun getChildren(element: PsiElement): PsiElement? {
124+
return getBinaryData(element)
125+
}
126+
127+
override fun create(adapter: Base64ValueAdapter, editor: Editor, sink: InlayHintsSink): InlayPresentation? {
128+
val decoded = adapter.getDecodedBytes() ?: return null
129+
val offset = adapter.getStartOffset() ?: return null
130+
val presentation = create(decoded, editor) ?: return null
131+
sink.addInlineElement(offset, false, presentation, false)
132+
return presentation
133+
}
134+
135+
private fun create(bytes: ByteArray, editor: Editor): InlayPresentation? {
136+
val factory = PresentationFactory(editor as EditorImpl)
137+
val hex = toHexString(bytes) ?: return null
138+
val trimmed = trimWithEllipsis(hex, INLAY_HINT_MAX_WIDTH) ?: return null
139+
return factory.roundWithBackground(factory.smallText(trimmed))
140+
}
141+
142+
private fun toHexString(bytes: ByteArray): String? {
143+
return try {
144+
bytes.joinToString(separator = " ") { byte ->
145+
Integer.toHexString(byte.toInt())
146+
}
147+
} catch (e: Exception) {
148+
null
149+
}
150+
}
151+
}
152+
}

0 commit comments

Comments
 (0)