[intellij sh] IDEA-267920 Fix plugin compatibility problem
[idea/community.git] / platform / diff-impl / src / com / intellij / diff / editor / DiffRequestProcessorEditor.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 com.intellij.diff.editor
3
4 import com.intellij.diff.impl.DiffRequestProcessor
5 import com.intellij.diff.util.DiffUserDataKeysEx
6 import com.intellij.diff.util.DiffUtil
7 import com.intellij.diff.util.FileEditorBase
8 import com.intellij.openapi.Disposable
9 import com.intellij.openapi.diagnostic.logger
10 import com.intellij.openapi.diff.DiffBundle
11 import com.intellij.openapi.fileEditor.FileEditor
12 import com.intellij.openapi.fileEditor.impl.EditorWindow
13 import com.intellij.openapi.util.Disposer
14 import com.intellij.openapi.vfs.VirtualFile
15 import java.awt.BorderLayout
16 import java.awt.event.ContainerAdapter
17 import java.awt.event.ContainerEvent
18 import java.awt.event.KeyEvent
19 import javax.swing.JComponent
20 import javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW
21 import javax.swing.JPanel
22 import javax.swing.KeyStroke
23
24 open class DiffRequestProcessorEditor(
25   private val file: DiffVirtualFile,
26   val processor: DiffRequestProcessor
27 ) : FileEditorBase() {
28   companion object {
29     private val LOG = logger<DiffRequestProcessorEditor>()
30   }
31
32   private var disposed = false
33
34   private val panel = MyPanel(processor.component)
35
36   init {
37     putUserData(EditorWindow.HIDE_TABS, true)
38     if (!DiffUtil.isUserDataFlagSet(DiffUserDataKeysEx.DIFF_IN_EDITOR_WITH_EXPLICIT_DISPOSABLE, processor.context)) {
39       Disposer.register(this, Disposable {
40         Disposer.dispose(processor)
41       })
42     }
43     Disposer.register(processor, Disposable {
44       propertyChangeSupport.firePropertyChange(FileEditor.PROP_VALID, true, false)
45     })
46
47     processor.component.registerKeyboardAction({ Disposer.dispose(this) },
48                                                KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), WHEN_IN_FOCUSED_WINDOW)
49   }
50
51   override fun getComponent(): JComponent = panel
52   override fun getPreferredFocusedComponent(): JComponent? = processor.preferredFocusedComponent
53
54   override fun dispose() {
55     disposed = true
56   }
57   override fun isValid(): Boolean = !disposed && !processor.isDisposed
58   override fun getFile(): VirtualFile = file
59   override fun getName(): String = DiffBundle.message("diff.file.editor.name")
60
61   override fun selectNotify() {
62     processor.updateRequest()
63   }
64
65   private inner class MyPanel(component: JComponent) : JPanel(BorderLayout()) {
66     init {
67       add(component, BorderLayout.CENTER)
68
69       addContainerListener(object : ContainerAdapter() {
70         override fun componentRemoved(e: ContainerEvent?) {
71           if (disposed) return
72           LOG.error("DiffRequestProcessor cannot be shown twice, see com.intellij.ide.actions.SplitAction.FORBID_TAB_SPLIT, file: $file")
73         }
74       })
75     }
76   }
77 }