Skip to content

Commit

Permalink
fix: show success notification when pushing multi-resource editor (re…
Browse files Browse the repository at this point in the history
…dhat-developer#710)

Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Oct 15, 2024
1 parent 43b450d commit cfc24ae
Show file tree
Hide file tree
Showing 13 changed files with 214 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,70 @@ 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 {
return other is Error
&& title == other.title
&& message == other.message
}

override fun hashCode(): Int {
return super.hashCode()
}

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
if (other == null || javaClass != other.javaClass) return false
val otherDifferent: Different = other as Different
return otherDifferent.exists == exists
&& otherDifferent.isOutdatedVersion == isOutdatedVersion
&& otherDifferent.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
if (other == null || javaClass != other.javaClass) return false
val otherPushed: Pushed = other as Pushed
return otherPushed.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 @@ -23,28 +23,25 @@ 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?"
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 cfc24ae

Please sign in to comment.