@@ -23,14 +23,10 @@ import com.intellij.openapi.util.Disposer
23
23
import com.intellij.openapi.util.Key
24
24
import com.intellij.openapi.vfs.VirtualFile
25
25
import com.intellij.psi.PsiDocumentManager
26
+ import com.intellij.util.messages.MessageBusConnection
26
27
import com.redhat.devtools.intellij.common.utils.MetadataClutter
27
28
import com.redhat.devtools.intellij.common.validation.KubernetesResourceInfo
28
- import com.redhat.devtools.intellij.kubernetes.editor.notification.DeletedNotification
29
- import com.redhat.devtools.intellij.kubernetes.editor.notification.ErrorNotification
30
- import com.redhat.devtools.intellij.kubernetes.editor.notification.PullNotification
31
- import com.redhat.devtools.intellij.kubernetes.editor.notification.PulledNotification
32
- import com.redhat.devtools.intellij.kubernetes.editor.notification.PushNotification
33
- import com.redhat.devtools.intellij.kubernetes.editor.notification.PushedNotification
29
+ import com.redhat.devtools.intellij.kubernetes.editor.notification.Notifications
34
30
import com.redhat.devtools.intellij.kubernetes.editor.util.getDocument
35
31
import com.redhat.devtools.intellij.kubernetes.editor.util.isKubernetesResource
36
32
import com.redhat.devtools.intellij.kubernetes.model.IResourceModel
@@ -39,6 +35,8 @@ import com.redhat.devtools.intellij.kubernetes.model.context.IActiveContext
39
35
import com.redhat.devtools.intellij.kubernetes.model.util.ResourceException
40
36
import com.redhat.devtools.intellij.kubernetes.model.util.toMessage
41
37
import com.redhat.devtools.intellij.kubernetes.model.util.toTitle
38
+ import com.redhat.devtools.intellij.kubernetes.settings.SettingsChangeListener
39
+ import com.redhat.devtools.intellij.kubernetes.settings.SettingsConfigurable
42
40
import io.fabric8.kubernetes.api.model.HasMetadata
43
41
import java.util.concurrent.CompletableFuture
44
42
@@ -57,18 +55,7 @@ open class ResourceEditor(
57
55
// for mocking purposes
58
56
private val createResourceFileForVirtual : (file: VirtualFile ? ) -> ResourceFile ? =
59
57
ResourceFile .Factory : :create,
60
- // for mocking purposes
61
- private val pushNotification : PushNotification = PushNotification (editor, project),
62
- // for mocking purposes
63
- private val pushedNotification : PushedNotification = PushedNotification (editor, project),
64
- // for mocking purposes
65
- private val pullNotification : PullNotification = PullNotification (editor, project),
66
- // for mocking purposes
67
- private val pulledNotification : PulledNotification = PulledNotification (editor, project),
68
- // for mocking purposes
69
- private val deletedNotification : DeletedNotification = DeletedNotification (editor, project),
70
- // for mocking purposes
71
- private val errorNotification : ErrorNotification = ErrorNotification (editor, project),
58
+ private val notifications : Notifications = Notifications (editor, project),
72
59
// for mocking purposes
73
60
private val getDocument : (FileEditor ) -> Document ? = ::getDocument,
74
61
// for mocking purposes
@@ -81,14 +68,18 @@ open class ResourceEditor(
81
68
// for mocking purposes
82
69
private val diff : ResourceDiff = ResourceDiff (project),
83
70
// for mocking purposes
84
- protected val editorResources : EditorResources = EditorResources (resourceModel)
71
+ protected val editorResources : EditorResources = EditorResources (resourceModel),
72
+ private val connection : MessageBusConnection = ApplicationManager .getApplication().messageBus.connect()
85
73
) : Disposable {
86
74
87
75
init {
88
76
Disposer .register(editor, this )
89
77
editorResources.resourceChangedListener = onResourceChanged()
90
78
resourceModel.addListener(onNamespaceOrContextChanged())
91
- runAsync { enableEditingNonProjectFile() }
79
+ runAsync {
80
+ enableEditingNonProjectFile()
81
+ connection.subscribe(SettingsChangeListener .CHANGED , onSettingsChanged())
82
+ }
92
83
}
93
84
94
85
companion object {
@@ -117,15 +108,15 @@ open class ResourceEditor(
117
108
if (editorResources.size == 1 ) {
118
109
// show notification for 1 resource
119
110
val editorResource = editorResources.firstOrNull() ? : return @runAsync
120
- showNotification (editorResource)
111
+ notifications.show (editorResource)
121
112
} else if (editorResources.size > 1 ) {
122
113
// show notification for multiple resources
123
- showNotification (editorResources)
114
+ notifications.show (editorResources)
124
115
}
125
116
} catch (e: Exception ) {
126
117
runInUI {
127
- hideNotifications ()
128
- errorNotification.show (
118
+ notifications.hideAll ()
119
+ notifications.showError (
129
120
toTitle(e),
130
121
toMessage(e.cause)
131
122
)
@@ -134,108 +125,14 @@ open class ResourceEditor(
134
125
}
135
126
}
136
127
137
- open protected fun updateDeleted (deleted : HasMetadata ? ) {
128
+ protected open fun updateDeleted (deleted : HasMetadata ? ) {
138
129
if (deleted == null ) {
139
130
return
140
131
}
141
132
editorResources.setDeleted(deleted)
142
133
update()
143
134
}
144
135
145
- private fun showNotification (editorResource : EditorResource ) {
146
- val state = editorResource.getState()
147
- val resource = editorResource.getResource()
148
- when (state) {
149
- is Error ->
150
- showErrorNotification(state.title, state.message)
151
- is Pushed ->
152
- showPushedNotification(listOf (editorResource))
153
- is DeletedOnCluster ->
154
- showDeletedNotification(resource)
155
- /* *
156
- * avoid too many notifications, don't notify outdated
157
- is Outdated ->
158
- showPullNotification(resource)
159
- */
160
- is Modified ->
161
- showPushNotification(true , listOf (editorResource))
162
-
163
- else ->
164
- runInUI {
165
- hideNotifications()
166
- }
167
- }
168
- }
169
-
170
- private fun showNotification (editorResources : Collection <EditorResource >) {
171
- val toPush = editorResources.filter(FILTER_TO_PUSH )
172
- if (toPush.isNotEmpty()) {
173
- showPushNotification(false , toPush)
174
- return
175
- }
176
- val inError = editorResources.filter(FILTER_ERROR )
177
- if (inError.isNotEmpty()) {
178
- showErrorNotification(inError)
179
- } else {
180
- runInUI {
181
- hideNotifications()
182
- }
183
- }
184
- }
185
-
186
- private fun showErrorNotification (title : String , message : String? ) {
187
- runInUI {
188
- hideNotifications()
189
- errorNotification.show(title, message)
190
- }
191
- }
192
-
193
- private fun showErrorNotification (editorResources : Collection <EditorResource >) {
194
- val inError = editorResources.filter(FILTER_ERROR )
195
- val toDisplay = inError.firstOrNull()?.getState() as ? Error ? : return
196
- showErrorNotification(toDisplay.title, toDisplay.message)
197
- }
198
-
199
- private fun showPushNotification (showPull : Boolean , editorResources : Collection <EditorResource >) {
200
- runInUI {
201
- // hide & show in the same UI thread runnable avoid flickering
202
- hideNotifications()
203
- pushNotification.show(showPull, editorResources)
204
- }
205
- }
206
-
207
- private fun showPushedNotification (editorResources : Collection <EditorResource >) {
208
- runInUI {
209
- // hide & show in the same UI thread runnable avoid flickering
210
- hideNotifications()
211
- pushedNotification.show(editorResources)
212
- }
213
- }
214
-
215
- private fun showPullNotification (resource : HasMetadata ) {
216
- runInUI {
217
- hideNotifications()
218
- pullNotification.show(resource)
219
- }
220
- }
221
-
222
- private fun showDeletedNotification (resource : HasMetadata ) {
223
- runInUI {
224
- // hide & show in the same UI thread runnable avoid flickering
225
- hideNotifications()
226
- deletedNotification.show(resource)
227
- }
228
- }
229
-
230
- private fun hideNotifications () {
231
- errorNotification.hide()
232
- pullNotification.hide()
233
- deletedNotification.hide()
234
- pushNotification.hide()
235
- pushedNotification.hide()
236
- pulledNotification.hide()
237
- }
238
-
239
136
/* *
240
137
* Pulls the resource from the cluster and replaces the content of this editor.
241
138
* Does nothing if it doesn't exist.
@@ -248,7 +145,7 @@ open class ResourceEditor(
248
145
}
249
146
runInUI {
250
147
// hide before running pull. Pull may take quite some time on remote cluster
251
- hideNotifications ()
148
+ notifications.hideAll ()
252
149
}
253
150
val resource = resources.firstOrNull() ? : return
254
151
@@ -293,7 +190,7 @@ open class ResourceEditor(
293
190
fun push (all : Boolean ) {
294
191
runInUI {
295
192
// hide before running push. Push may take quite some time on remote cluster
296
- hideNotifications ()
193
+ notifications.hideAll ()
297
194
}
298
195
runAsync {
299
196
if (all) {
@@ -370,7 +267,7 @@ open class ResourceEditor(
370
267
}
371
268
runInUI {
372
269
replaceDocument(resources)
373
- hideNotifications ()
270
+ notifications.hideAll ()
374
271
}
375
272
}
376
273
@@ -431,6 +328,21 @@ open class ResourceEditor(
431
328
}
432
329
}
433
330
331
+ protected open fun onSettingsChanged (): SettingsChangeListener {
332
+ return object : SettingsChangeListener {
333
+ override fun changed (property : String , value : String? ) {
334
+ if (value != null
335
+ && SettingsConfigurable .EDITOR_NOTIFICATIONS_DISABLED == property) {
336
+ if (true == value.toBoolean()) {
337
+ notifications.hideAll()
338
+ } else {
339
+ update()
340
+ }
341
+ }
342
+ }
343
+ }
344
+ }
345
+
434
346
/* *
435
347
* Closes this instance and cleans up references to it.
436
348
* - Removes the resource model listener,
@@ -496,5 +408,6 @@ open class ResourceEditor(
496
408
override fun dispose () {
497
409
resourceModel.removeListener(onNamespaceContextChanged)
498
410
editorResources.dispose()
411
+ connection.dispose()
499
412
}
500
413
}
0 commit comments