Skip to content

Commit cfc24ae

Browse files
committed
fix: show success notification when pushing multi-resource editor (redhat-developer#710)
Signed-off-by: Andre Dietisheim <[email protected]>
1 parent 43b450d commit cfc24ae

13 files changed

+214
-83
lines changed

src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/EditorFocusListener.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,12 @@ class EditorFocusListener(private val project: Project) : FileEditorManagerListe
6161
editor: FileEditor,
6262
project: Project
6363
) {
64-
ErrorNotification(editor, project).show(
64+
val notification = ErrorNotification(editor, project)
65+
notification.show(
6566
e.message ?: "Undefined error",
66-
e)
67+
e,
68+
{ notification.hide() }
69+
)
6770
}
6871

6972
}

src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/EditorResourceState.kt

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
******************************************************************************/
1212
package com.redhat.devtools.intellij.kubernetes.editor
1313

14+
import java.util.Objects
15+
1416
val FILTER_ALL = { _: EditorResource -> true }
1517
val FILTER_TO_PUSH = { editorResource: EditorResource ->
1618
val state = editorResource.getState()
@@ -29,37 +31,70 @@ abstract class EditorResourceState
2931

3032
class Error(val title: String, val message: String? = null): EditorResourceState() {
3133
constructor(title: String, e: Throwable) : this(title, e.message)
34+
35+
override fun equals(other: Any?): Boolean {
36+
return other is Error
37+
&& title == other.title
38+
&& message == other.message
39+
}
40+
41+
override fun hashCode(): Int {
42+
return super.hashCode()
43+
}
44+
45+
override fun toString(): String {
46+
return super.toString()
47+
}
3248
}
3349

3450
open class Identical: EditorResourceState()
3551

3652
abstract class Different(val exists: Boolean, val isOutdatedVersion: Boolean): EditorResourceState() {
3753
abstract fun isPush(): Boolean
54+
override fun equals(other: Any?): Boolean {
55+
if (this === other) return true
56+
if (other == null || javaClass != other.javaClass) return false
57+
val otherDifferent: Different = other as Different
58+
return otherDifferent.exists == exists
59+
&& otherDifferent.isOutdatedVersion == isOutdatedVersion
60+
&& otherDifferent.isPush() == isPush()
61+
}
62+
63+
override fun hashCode(): Int {
64+
return Objects.hash(exists, isOutdatedVersion, isPush())
65+
}
3866
}
3967

4068
open class Modified(exists: Boolean, isOutdatedVersion: Boolean): Different(exists, isOutdatedVersion) {
4169
override fun isPush() = true
4270
}
4371

44-
class DeletedOnCluster: Modified(false, false) {
72+
class DeletedOnCluster(): Modified(false, false) {
4573
override fun isPush() = true
46-
4774
}
4875

4976
class Outdated: Different(true, true) {
5077
override fun isPush() = false
51-
5278
}
5379

5480
abstract class Pushed: Identical() {
5581
abstract val updated: Boolean
56-
}
5782

58-
class Created: Pushed() {
59-
override val updated = false
60-
}
61-
class Updated: Pushed() {
62-
override val updated = true
83+
override fun equals(other: Any?): Boolean {
84+
if (this === other) return true
85+
if (other == null || javaClass != other.javaClass) return false
86+
val otherPushed: Pushed = other as Pushed
87+
return otherPushed.updated == updated
88+
}
89+
90+
override fun hashCode(): Int {
91+
return Objects.hashCode(updated)
92+
}
93+
6394
}
6495

96+
class Created(override val updated: Boolean = false) : Pushed()
97+
98+
class Updated(override val updated: Boolean = true): Pushed()
99+
65100
class Pulled: EditorResourceState()

src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceEditorFactory.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ open class ResourceEditorFactory protected constructor(
127127
editor.file?.putUserData(ResourceEditor.KEY_RESOURCE_EDITOR, resourceEditor)
128128
resourceEditor
129129
} catch (e: ResourceException) {
130-
ErrorNotification(editor, project).show(e.message ?: "", e.cause?.message)
130+
val notification = ErrorNotification(editor, project)
131+
notification.show(e.message ?: "", e.cause?.message, { notification.hide() })
131132
runAsync { telemetry.error(e).send() }
132133
null
133134
}

src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/notification/DeletedNotification.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,25 @@ import javax.swing.JComponent
2323
/**
2424
* An editor (panel) notification that informs about a deleted resource on the cluster.
2525
*/
26-
class DeletedNotification(private val editor: FileEditor, private val project: Project) {
26+
open class DeletedNotification(private val editor: FileEditor, private val project: Project) {
2727

28-
companion object {
28+
private companion object {
2929
private val KEY_PANEL = Key<JComponent>(DeletedNotification::class.java.canonicalName)
3030
}
3131

32-
fun show(resource: HasMetadata) {
33-
editor.showNotification(KEY_PANEL, { createPanel(resource) }, project)
32+
fun show(resource: HasMetadata, closeAction: () -> Unit) {
33+
editor.showNotification(KEY_PANEL, { createPanel(resource, closeAction) }, project)
3434
}
3535

3636
fun hide() {
3737
editor.hideNotification(KEY_PANEL, project)
3838
}
3939

40-
private fun createPanel(resource: HasMetadata): EditorNotificationPanel {
40+
private fun createPanel(resource: HasMetadata, hideAction: () -> Unit): EditorNotificationPanel {
4141
val panel = EditorNotificationPanel()
4242
panel.text = "${toKindAndName(resource)} was deleted on cluster. Push to Cluster?"
4343
addPush(false, panel)
44-
addDismiss(panel) {
45-
hide()
46-
}
47-
44+
addHide(panel, hideAction)
4845
return panel
4946
}
5047
}

src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/notification/ErrorNotification.kt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,33 @@ import javax.swing.JComponent
2626
*/
2727
class ErrorNotification(private val editor: FileEditor, private val project: Project) {
2828

29-
companion object {
29+
private companion object {
3030
private val KEY_PANEL = Key<JComponent>(ErrorNotification::class.java.canonicalName)
3131
}
3232

33-
fun show(title: String, message: String?) {
34-
editor.showNotification(KEY_PANEL, { createPanel(editor, title, message) }, project)
33+
fun show(title: String, message: String?, closeAction: (() -> Unit)?) {
34+
editor.showNotification(KEY_PANEL, { createPanel(editor, title, message, closeAction) }, project)
3535
}
3636

37-
fun show(title: String, e: Throwable) {
38-
editor.showNotification(KEY_PANEL, { createPanel(editor, title, e.message) }, project)
37+
fun show(title: String, e: Throwable, closeAction: (() -> Unit)?) {
38+
editor.showNotification(KEY_PANEL, { createPanel(editor, title, e.message, closeAction) }, project)
3939
}
4040

4141
fun hide() {
4242
editor.hideNotification(KEY_PANEL, project)
4343
}
4444

45-
private fun createPanel(editor: FileEditor, title: String, message: String?): EditorNotificationPanel {
45+
private fun createPanel(
46+
editor: FileEditor,
47+
title: String,
48+
message: String?,
49+
closeAction: (() -> Unit)?
50+
): EditorNotificationPanel {
4651
val panel = EditorNotificationPanel()
4752
panel.icon(AllIcons.Ide.FatalError)
4853
panel.text = title
4954
addDetailsAction(message, panel, editor)
50-
addDismiss(panel) {
51-
hide()
52-
}
55+
addHide(panel, closeAction)
5356
return panel
5457
}
5558

src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/notification/NotificationActions.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ fun addPull(panel: EditorNotificationPanel) {
1818
panel.createActionLabel("Pull", PullAction.ID)
1919
}
2020

21-
fun addDismiss(panel: EditorNotificationPanel, consumer: () -> Unit) {
22-
panel.createActionLabel("Dismiss", consumer)
21+
fun addHide(panel: EditorNotificationPanel, closeAction: (() -> Unit)?) {
22+
if (closeAction != null) {
23+
panel.setCloseAction(closeAction)
24+
}
2325
}
2426

2527
fun addDiff(panel: EditorNotificationPanel) {

0 commit comments

Comments
 (0)