diff-preview: extract Esc handler setup to common editor customizer
[idea/community.git] / plugins / github / src / org / jetbrains / plugins / github / pullrequest / GHPRDiffFileEditor.kt
1 // Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2 package org.jetbrains.plugins.github.pullrequest
3
4 import com.intellij.collaboration.ui.codereview.diff.MutableDiffRequestChainProcessor
5 import com.intellij.diff.util.FileEditorBase
6 import com.intellij.openapi.project.Project
7 import com.intellij.openapi.util.Disposer
8 import com.intellij.util.ui.update.MergingUpdateQueue
9 import com.intellij.util.ui.update.Update
10 import org.jetbrains.plugins.github.i18n.GithubBundle
11
12 internal class GHPRDiffFileEditor(project: Project,
13                                   private val diffRequestModel: GHPRDiffRequestModel,
14                                   private val file: GHRepoVirtualFile)
15   : FileEditorBase() {
16
17   internal val diffProcessor = MutableDiffRequestChainProcessor(project, null)
18
19   private val diffChainUpdateQueue =
20     MergingUpdateQueue("updateDiffChainQueue", 100, true, null, this).apply {
21       setRestartTimerOnAdd(true)
22     }
23
24   override fun isValid() = !Disposer.isDisposed(diffProcessor)
25
26   init {
27     diffRequestModel.addAndInvokeRequestChainListener(diffChainUpdateQueue) {
28       val chain = diffRequestModel.requestChain
29       diffChainUpdateQueue.run(Update.create(diffRequestModel) {
30         diffProcessor.chain = chain
31       })
32     }
33   }
34
35   override fun getName(): String = GithubBundle.message("pull.request.editor.diff")
36
37   override fun getComponent() = diffProcessor.component
38   override fun getPreferredFocusedComponent() = diffProcessor.preferredFocusedComponent
39
40   override fun selectNotify() = diffProcessor.updateRequest()
41
42   override fun getFile() = file
43 }