Skip to content

Commit

Permalink
fix: show success notification when pushing multi-resource editor (#710)
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Nov 7, 2024
1 parent 9ee07fa commit d769c90
Show file tree
Hide file tree
Showing 13 changed files with 239 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,12 @@ class EditorFocusListener(private val project: Project) : FileEditorManagerListe
editor: FileEditor,
project: Project
) {
ErrorNotification(editor, project).show(
val notification = ErrorNotification(editor, project)
notification.show(
e.message ?: "Undefined error",
e)
e,
{ notification.hide() }
)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
******************************************************************************/
package com.redhat.devtools.intellij.kubernetes.editor

import java.util.Objects

val FILTER_ALL = { _: EditorResource -> true }
val FILTER_TO_PUSH = { editorResource: EditorResource ->
val state = editorResource.getState()
Expand All @@ -29,37 +31,81 @@ abstract class EditorResourceState

class Error(val title: String, val message: String? = null): EditorResourceState() {
constructor(title: String, e: Throwable) : this(title, e.message)

override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
return other is Error
&& title == other.title
&& message == other.message
}

override fun hashCode(): Int {
return Objects.hash(
title,
message
)
}

override fun toString(): String {
return super.toString()
}
}

open class Identical: EditorResourceState()

abstract class Different(val exists: Boolean, val isOutdatedVersion: Boolean): EditorResourceState() {
abstract fun isPush(): Boolean
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
return other is Different
&& other.exists == exists
&& other.isOutdatedVersion == isOutdatedVersion
&& other.isPush() == isPush()
}

override fun hashCode(): Int {
return Objects.hash(
exists,
isOutdatedVersion,
isPush()
)
}
}

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

class DeletedOnCluster: Modified(false, false) {
class DeletedOnCluster(): Modified(false, false) {
override fun isPush() = true

}

class Outdated: Different(true, true) {
override fun isPush() = false

}

abstract class Pushed: Identical() {
abstract val updated: Boolean
}

class Created: Pushed() {
override val updated = false
}
class Updated: Pushed() {
override val updated = true
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
return other is Pushed
&& other.updated == updated
}

override fun hashCode(): Int {
return Objects.hashCode(updated)
}
}

class Created(override val updated: Boolean = false) : Pushed()

class Updated(override val updated: Boolean = true): Pushed()

class Pulled: EditorResourceState()
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ open class ResourceEditorFactory protected constructor(
editor.file?.putUserData(ResourceEditor.KEY_RESOURCE_EDITOR, resourceEditor)
resourceEditor
} catch (e: ResourceException) {
ErrorNotification(editor, project).show(e.message ?: "", e.cause?.message)
val notification = ErrorNotification(editor, project)
notification.show(e.message ?: "", e.cause?.message, { notification.hide() })
runAsync { telemetry.error(e).send() }
null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,41 @@
******************************************************************************/
package com.redhat.devtools.intellij.kubernetes.editor.notification

import com.intellij.icons.AllIcons
import com.intellij.openapi.fileEditor.FileEditor
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Key
import com.intellij.ui.EditorNotificationPanel
import com.redhat.devtools.intellij.kubernetes.editor.hideNotification
import com.redhat.devtools.intellij.kubernetes.editor.showNotification
import com.redhat.devtools.intellij.kubernetes.model.util.toKindAndName
import icons.Icons
import io.fabric8.kubernetes.api.model.HasMetadata
import javax.swing.JComponent

/**
* An editor (panel) notification that informs about a deleted resource on the cluster.
*/
class DeletedNotification(private val editor: FileEditor, private val project: Project) {
open class DeletedNotification(private val editor: FileEditor, private val project: Project) {

companion object {
private companion object {
private val KEY_PANEL = Key<JComponent>(DeletedNotification::class.java.canonicalName)
}

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

fun hide() {
editor.hideNotification(KEY_PANEL, project)
}

private fun createPanel(resource: HasMetadata): EditorNotificationPanel {
private fun createPanel(resource: HasMetadata, hideAction: () -> Unit): EditorNotificationPanel {
val panel = EditorNotificationPanel()
panel.text = "${toKindAndName(resource)} was deleted on cluster. Push to Cluster?"
panel.icon(Icons.upload)
addPush(false, panel)
addDismiss(panel) {
hide()
}

addHide(panel, hideAction)
return panel
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,33 @@ import javax.swing.JComponent
*/
class ErrorNotification(private val editor: FileEditor, private val project: Project) {

companion object {
private companion object {
private val KEY_PANEL = Key<JComponent>(ErrorNotification::class.java.canonicalName)
}

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

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

fun hide() {
editor.hideNotification(KEY_PANEL, project)
}

private fun createPanel(editor: FileEditor, title: String, message: String?): EditorNotificationPanel {
private fun createPanel(
editor: FileEditor,
title: String,
message: String?,
closeAction: (() -> Unit)?
): EditorNotificationPanel {
val panel = EditorNotificationPanel()
panel.icon(AllIcons.Ide.FatalError)
panel.text = title
addDetailsAction(message, panel, editor)
addDismiss(panel) {
hide()
}
addHide(panel, closeAction)
return panel
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ fun addPull(panel: EditorNotificationPanel) {
panel.createActionLabel("Pull", PullAction.ID)
}

fun addDismiss(panel: EditorNotificationPanel, consumer: () -> Unit) {
panel.createActionLabel("Dismiss", consumer)
fun addHide(panel: EditorNotificationPanel, closeAction: (() -> Unit)?) {
if (closeAction != null) {
panel.setCloseAction(closeAction)
}
}

fun addDiff(panel: EditorNotificationPanel) {
Expand Down
Loading

0 comments on commit d769c90

Please sign in to comment.