@@ -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,9 @@ 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.Settings
39
+ import com.redhat.devtools.intellij.kubernetes.settings.Settings.Companion.PROP_EDITOR_SYNC_ENABLED
40
+ import com.redhat.devtools.intellij.kubernetes.settings.SettingsChangeListener
42
41
import io.fabric8.kubernetes.api.model.HasMetadata
43
42
import java.util.concurrent.CompletableFuture
44
43
@@ -57,18 +56,7 @@ open class ResourceEditor(
57
56
// for mocking purposes
58
57
private val createResourceFileForVirtual : (file: VirtualFile ? ) -> ResourceFile ? =
59
58
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),
59
+ private val notifications : Notifications = Notifications (editor, project),
72
60
// for mocking purposes
73
61
private val getDocument : (FileEditor ) -> Document ? = ::getDocument,
74
62
// for mocking purposes
@@ -81,14 +69,19 @@ open class ResourceEditor(
81
69
// for mocking purposes
82
70
private val diff : ResourceDiff = ResourceDiff (project),
83
71
// for mocking purposes
84
- protected val editorResources : EditorResources = EditorResources (resourceModel)
72
+ protected val editorResources : EditorResources = EditorResources (resourceModel),
73
+ private val settings : Settings = Settings .getInstance(),
74
+ private val connection : MessageBusConnection = ApplicationManager .getApplication().messageBus.connect()
85
75
) : Disposable {
86
76
87
77
init {
88
78
Disposer .register(editor, this )
89
79
editorResources.resourceChangedListener = onResourceChanged()
90
80
resourceModel.addListener(onNamespaceOrContextChanged())
91
- runAsync { enableEditingNonProjectFile() }
81
+ runAsync {
82
+ enableEditingNonProjectFile()
83
+ connection.subscribe(SettingsChangeListener .CHANGED , onSettingsChanged())
84
+ }
92
85
}
93
86
94
87
companion object {
@@ -113,19 +106,11 @@ open class ResourceEditor(
113
106
resourceModel.getCurrentNamespace()
114
107
)
115
108
val editorResources = editorResources.setResources(resources)
116
-
117
- if (editorResources.size == 1 ) {
118
- // show notification for 1 resource
119
- val editorResource = editorResources.firstOrNull() ? : return @runAsync
120
- showNotification(editorResource)
121
- } else if (editorResources.size > 1 ) {
122
- // show notification for multiple resources
123
- showNotification(editorResources)
124
- }
109
+ showNotifications(editorResources)
125
110
} catch (e: Exception ) {
126
111
runInUI {
127
- hideNotifications ()
128
- errorNotification.show (
112
+ notifications.hideAll ()
113
+ notifications.showError (
129
114
toTitle(e),
130
115
toMessage(e.cause)
131
116
)
@@ -134,106 +119,24 @@ open class ResourceEditor(
134
119
}
135
120
}
136
121
137
- open protected fun updateDeleted (deleted : HasMetadata ? ) {
138
- if (deleted == null ) {
139
- return
122
+ private fun showNotifications (editorResources : Collection <EditorResource >) {
123
+ val syncEnabled = Settings .getInstance().isEditorSyncEnabled()
124
+ if (editorResources.size == 1 ) {
125
+ // show notification for 1 resource
126
+ val editorResource = editorResources.firstOrNull() ? : return
127
+ notifications.show(editorResource, syncEnabled)
128
+ } else if (editorResources.size > 1 ) {
129
+ // show notification for multiple resources
130
+ notifications.show(editorResources, syncEnabled)
140
131
}
141
- editorResources.setDeleted(deleted)
142
- update()
143
132
}
144
133
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)
134
+ protected open fun updateDeleted (deleted : HasMetadata ? ) {
135
+ if (deleted == null ) {
174
136
return
175
137
}
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()
138
+ editorResources.setDeleted(deleted)
139
+ update()
237
140
}
238
141
239
142
/* *
@@ -248,7 +151,7 @@ open class ResourceEditor(
248
151
}
249
152
runInUI {
250
153
// hide before running pull. Pull may take quite some time on remote cluster
251
- hideNotifications ()
154
+ notifications.hideAll ()
252
155
}
253
156
val resource = resources.firstOrNull() ? : return
254
157
@@ -293,7 +196,7 @@ open class ResourceEditor(
293
196
fun push (all : Boolean ) {
294
197
runInUI {
295
198
// hide before running push. Push may take quite some time on remote cluster
296
- hideNotifications ()
199
+ notifications.hideAll ()
297
200
}
298
201
runAsync {
299
202
if (all) {
@@ -355,7 +258,9 @@ open class ResourceEditor(
355
258
}
356
259
357
260
fun startWatch (): ResourceEditor {
358
- editorResources.watchAll()
261
+ if (settings.isEditorSyncEnabled()) {
262
+ editorResources.watchAll()
263
+ }
359
264
return this
360
265
}
361
266
@@ -370,7 +275,7 @@ open class ResourceEditor(
370
275
}
371
276
runInUI {
372
277
replaceDocument(resources)
373
- hideNotifications ()
278
+ notifications.hideAll ()
374
279
}
375
280
}
376
281
@@ -404,11 +309,15 @@ open class ResourceEditor(
404
309
}
405
310
406
311
override fun removed (removed : Any ) {
407
- updateDeleted(removed as ? HasMetadata )
312
+ if (settings.isEditorSyncEnabled()) {
313
+ updateDeleted(removed as ? HasMetadata )
314
+ }
408
315
}
409
316
410
317
override fun modified (modified : Any ) {
411
- update()
318
+ if (settings.isEditorSyncEnabled()) {
319
+ update()
320
+ }
412
321
}
413
322
}
414
323
}
@@ -431,6 +340,27 @@ open class ResourceEditor(
431
340
}
432
341
}
433
342
343
+ protected open fun onSettingsChanged (): SettingsChangeListener {
344
+ return object : SettingsChangeListener {
345
+ override fun changed (property : String , value : String? ) {
346
+ if (value == null ) {
347
+ return
348
+ }
349
+
350
+ if (PROP_EDITOR_SYNC_ENABLED == property) {
351
+ val enabled = value.toBoolean()
352
+ if (enabled) {
353
+ editorResources.watchAll()
354
+ update()
355
+ } else {
356
+ editorResources.stopWatchAll()
357
+ notifications.hideSyncNotifications()
358
+ }
359
+ }
360
+ }
361
+ }
362
+ }
363
+
434
364
/* *
435
365
* Closes this instance and cleans up references to it.
436
366
* - Removes the resource model listener,
@@ -496,5 +426,6 @@ open class ResourceEditor(
496
426
override fun dispose () {
497
427
resourceModel.removeListener(onNamespaceContextChanged)
498
428
editorResources.dispose()
429
+ connection.dispose()
499
430
}
500
431
}
0 commit comments