11package no.spk.fiskeoye.plugin
22
3- import com.intellij.openapi.actionSystem.AnActionEvent
43import com.intellij.openapi.actionSystem.DefaultActionGroup
54import com.intellij.openapi.project.DumbAware
65import com.intellij.openapi.project.Project
76import com.intellij.openapi.wm.ToolWindow
87import com.intellij.openapi.wm.ToolWindowFactory
98import com.intellij.openapi.wm.ex.ToolWindowEx
109import com.intellij.ui.content.ContentFactory
11- import com.intellij.ui.content.ContentManagerEvent
12- import com.intellij.ui.content.ContentManagerListener
13- import no.spk.fiskeoye.plugin.actions.FiskeoyeAction
10+ import no.spk.fiskeoye.plugin.actions.window.AddFileContentTabAction
11+ import no.spk.fiskeoye.plugin.actions.window.AddFilenameTabAction
12+ import no.spk.fiskeoye.plugin.enum.ContentType
1413import no.spk.fiskeoye.plugin.icons.FiskeoyeIcons.Add
14+ import no.spk.fiskeoye.plugin.listeners.window.TabListener
1515import no.spk.fiskeoye.plugin.ui.FileContentPanel
1616import no.spk.fiskeoye.plugin.ui.FilenamePanel
17+ import no.spk.fiskeoye.plugin.util.FiskeoyeKeys.FILENAME_COUNTER_KEY
18+ import no.spk.fiskeoye.plugin.util.FiskeoyeKeys.FILE_CONTENT_COUNTER_KEY
19+ import no.spk.fiskeoye.plugin.util.createContent
1720
1821internal class FiskeoyeToolWindowFactory : ToolWindowFactory , DumbAware {
1922
20- private var fileContentCounter = 1
21- private var filenameCounter = 1
22-
2323 override fun createToolWindowContent (project : Project , toolWindow : ToolWindow ) {
2424 val contentFactory: ContentFactory = ContentFactory .getInstance()
2525 val toolWindowEx = toolWindow as ToolWindowEx
2626
27- val fileContent = contentFactory.createContent( FileContentPanel (), " File Content " , false ). apply {
28- isCloseable = false
29- putUserData( CONTENT_TYPE_KEY , ContentType . FILE_CONTENT )
30- }
27+ project.putUserData( FILE_CONTENT_COUNTER_KEY , 1 )
28+ project.putUserData( FILENAME_COUNTER_KEY , 1 )
29+
30+ val fileContent = contentFactory.createContent( FileContentPanel (), " File Content " , ContentType . FILE_CONTENT )
3131 toolWindowEx.contentManager.addContent(fileContent)
3232
33- val filename = contentFactory.createContent(FilenamePanel (), " File Name" , false ).apply {
34- isCloseable = false
35- putUserData(CONTENT_TYPE_KEY , ContentType .FILENAME )
36- }
33+ val filename = contentFactory.createContent(FilenamePanel (), " File Name" , ContentType .FILENAME )
3734 toolWindowEx.contentManager.addContent(filename)
3835
39- toolWindowEx.contentManager.addContentManagerListener(object : ContentManagerListener {
40- override fun contentRemoved (event : ContentManagerEvent ) {
41- val content = event.content
42- when (content.getUserData(CONTENT_TYPE_KEY )) {
43- ContentType .FILE_CONTENT -> if (fileContentCounter > 1 ) fileContentCounter--
44- ContentType .FILENAME -> if (filenameCounter > 1 ) filenameCounter--
45- else -> {}
46- }
47- }
48- })
49- toolWindowEx.setTabActions(buildNewTabActionGroup(toolWindow, contentFactory))
36+ toolWindowEx.contentManager.addContentManagerListener(TabListener (project))
37+ toolWindowEx.setTabActions(buildTabActionGroup(toolWindow, contentFactory))
5038 }
5139
52- private fun buildNewTabActionGroup (toolWindow : ToolWindow , contentFactory : ContentFactory ): DefaultActionGroup {
53- val addFileContentAction = object : FiskeoyeAction (" File Content" ) {
54- override fun actionPerformed (e : AnActionEvent ) {
55- fileContentCounter++
56- val newFileContent = contentFactory.createContent(
57- FileContentPanel (),
58- " File Content ($fileContentCounter )" ,
59- false
60- ).apply {
61- isCloseable = true
62- putUserData(CONTENT_TYPE_KEY , ContentType .FILE_CONTENT )
63- }
64- toolWindow.contentManager.addContent(newFileContent)
65- toolWindow.contentManager.setSelectedContent(newFileContent, true )
66- }
67- }
68-
69- val addFilenameAction = object : FiskeoyeAction (" File Name" ) {
70- override fun actionPerformed (e : AnActionEvent ) {
71- filenameCounter++
72- val newFilename = contentFactory.createContent(
73- FilenamePanel (),
74- " File Name ($filenameCounter )" ,
75- false
76- ).apply {
77- isCloseable = true
78- putUserData(CONTENT_TYPE_KEY , ContentType .FILENAME )
79- }
80- toolWindow.contentManager.addContent(newFilename)
81- toolWindow.contentManager.setSelectedContent(newFilename, true )
82- }
83- }
84-
40+ private fun buildTabActionGroup (toolWindow : ToolWindow , contentFactory : ContentFactory ): DefaultActionGroup {
8541 return DefaultActionGroup (" New Fiskeoye Tab" , true ).apply {
8642 templatePresentation.icon = Add
87- add(addFileContentAction )
88- add(addFilenameAction )
43+ add(AddFileContentTabAction (toolWindow, contentFactory) )
44+ add(AddFilenameTabAction (toolWindow, contentFactory) )
8945 }
9046 }
9147
92- private enum class ContentType { FILE_CONTENT , FILENAME }
93- private companion object {
94- val CONTENT_TYPE_KEY = com.intellij.openapi.util.Key .create<ContentType >(" FISKEOYE_CONTENT_TYPE" )
95- }
9648}
0 commit comments