diff-preview: move escape handler setup to file editor constructor
[idea/community.git] / plugins / github / src / org / jetbrains / plugins / github / pullrequest / GHPRDiffFileEditor.kt
1 // Copyright 2000-2020 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.diff.util.FileEditorBase
5 import com.intellij.openapi.fileEditor.FileEditor.PROP_VALID
6 import com.intellij.openapi.project.Project
7 import com.intellij.openapi.util.Disposer
8 import com.intellij.openapi.vfs.VirtualFile
9 import com.intellij.collaboration.ui.codereview.diff.MutableDiffRequestChainProcessor
10 import com.intellij.util.ui.update.MergingUpdateQueue
11 import com.intellij.util.ui.update.Update
12 import org.jetbrains.plugins.github.i18n.GithubBundle
13 import java.awt.event.KeyEvent
14 import javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW
15 import javax.swing.KeyStroke
16
17 internal class GHPRDiffFileEditor(project: Project,
18                                   private val diffRequestModel: GHPRDiffRequestModel,
19                                   private val file: GHRepoVirtualFile)
20   : FileEditorBase() {
21
22   internal val diffProcessor = MutableDiffRequestChainProcessor(project, null)
23
24   private val diffChainUpdateQueue =
25     MergingUpdateQueue("updateDiffChainQueue", 100, true, null, this).apply {
26       setRestartTimerOnAdd(true)
27     }
28
29   override fun isValid() = !Disposer.isDisposed(diffProcessor)
30
31   init {
32     Disposer.register(this, diffProcessor)
33
34     diffProcessor.component.registerKeyboardAction({
35                                                      propertyChangeSupport.firePropertyChange(PROP_VALID, true, false)
36                                                    },
37                                                    KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), WHEN_IN_FOCUSED_WINDOW)
38
39     diffRequestModel.addAndInvokeRequestChainListener(diffChainUpdateQueue) {
40       val chain = diffRequestModel.requestChain
41       diffChainUpdateQueue.run(Update.create(diffRequestModel) {
42         diffProcessor.chain = chain
43       })
44     }
45   }
46
47   override fun getName(): String = GithubBundle.message("pull.request.editor.diff")
48
49   override fun getComponent() = diffProcessor.component
50   override fun getPreferredFocusedComponent() = diffProcessor.preferredFocusedComponent
51
52   override fun selectNotify() = diffProcessor.updateRequest()
53
54   override fun getFile() = file
55 }